Skip to content

Working with Rules, Scale, and Labels

This module will teach you styling and symbology techniques using rules, scale, and labels.

As an intermediate level workbook, you are expected to already know basic GIS concepts such as spatial data models and formats and QGIS functions such as loading and styling layers, running processing algorithms, saving QGIS projects, etc.

Rule-based symbology in QGIS allows you to apply different styles to features within a layer based on specified rules. Instead of using a single symbology for the entire layer, you can define multiple rules that determine how each feature should be displayed based on its attributes.

This approach is useful for applying complex and combined symbology on layers or when applying styles to things like vector layers. For example, you may want to style a road network based on different parameters.

Output for Exercise 3.1. Styling road maps

In this exercise, we’ll create a road network similar in style to the ones we see in web maps by applying rule-based symbology on OpenStreetMap road data.

Create a cool-looking basemap

  1. Load the adm_albay, landuse, and osm_roads_legazpi layers.
  2. Duplicate the adm_albay layer and name it sea. Create a group named base and put the adm_albay, sea, landuse layers there.

Land Use, Albay admin boundary, and OSM roads in Legazpi

Figure 3.1.1. Land Use, Albay admin boundary, and OSM roads in Legazpi

  1. Style adm_albay using Single Symbol with a slightly-dark color.
  2. Style sea using Inverted Polygons and apply two fills (a Simple Fill and a Gradient Fill).

The adm_albay and sea layers styled

Figure 3.1.2. The adm_albay and sea layers styled

  1. For landuse, let’s apply a simple rule-based symbology. In this case, we want to style only the “green” areas.
    1. Select the landuse layer.
    2. Select Rule-based for the symbology type.
    3. Add a new rule using the green arrow.
    4. Use the following rule:
      1. Label: green
      2. Filter:
"landuse" in ('farmland' , 'forest' , 'grass' , 'greenfield' , 'meadow' , 'orchard')

Simple rule-based symbology for "green" landuse

Figure 3.1.3. Simple rule-based symbology for “green” landuse

Landuse styled using rule-based symbology

Figure 3.1.4. Landuse styled using rule-based symbology

Apply rule-based symbology to the road network data

  1. For the road network data, you will be applying a more complex rule-based symbology. You will create 3 rules—one for primary roads, another for secondary roads, and a catch-all (for everything else). The rules will be:
    • Primary roads:
      • Label: primary
      • Filter:
        "fclass" in ('primary')
      • Symbol:
        • Simple Line (1)

          • Color: #fff495
          • Stroke width: 40 meters at scale
          • Stroke style, Join style, Cap style: Solid line, Round, Flat
        • Simple Line (2)

          • Color: #ecec32
          • Stroke width: 50 meters at scale
          • Stroke style, Join style, Cap style: Solid line, Round, Flat

Rules for styling the primary roads (1)

Figure 3.1.5. Rules for styling the primary roads (1)

Rules for styling the primary roads (2)

Figure 3.1.6. Rules for styling the primary roads (2)

  • Secondary and tertiary roads:

    • Label: secondary and tertiary
    • Filter:
      fclass in ('secondary', 'tertiary')
    • Symbol:
      • Simple Line (1)
        • Color: #fff495
        • Stroke width: 30 meters at scale
        • Stroke style, Join style, Cap style: Solid line, Round, Flat
  • Other roads:

    • Label: Others
    • Filter:
      Else
    • Symbol:
      • Simple Line (1)
        • Color: #ffffff
        • Stroke width: 8 meters at scale
        • Stroke style, Join style, Cap style: Solid line, Round, Flat
      • Simple Line (2)
        • Color: #d3d3d3
        • Stroke width: 12 meters at scale
        • Stroke style, Join style, Cap style: Solid line, Round, Flat

Rules for primary, secondary + tertiary, and other roads

Figure 3.1.7. Rules for primary, secondary + tertiary, and other roads

  1. After applying these rules, your road layer should look like below.

Road layer styled using rule-based symbology

Figure 3.1.8. Road layer styled using rule-based symbology

However, if you zoom in to the map, you will notice a bit of an issue.

Zoomed in road layer. Notice the poor overlapping of the different features and symbols.

Figure 3.1.9. Zoomed in road layer. Notice the poor overlapping of the different features and symbols.

If you look real close, you will notice that the different road feature overlap in a poor way. What you want to do is control the rendering order so that the primary roads are rendered over the secondary and other roads while the secondary roads are rendered over the other roads. To do this, you can use Symbol Levels.

Use symbol levels to feature rendering order

Symbol Levels allow you to define the order in which the symbols are rendered. The numbers assigned to symbol rules define when they will be rendered, that means a lower number is rendered first and a higher number will be rendered on top of it. Using this, you can ensure that the other roads are rendered at the bottom and the primary roads are rendered on top.

  1. Click Symbol Levels on the Layer Styling panel or the layer Properties of the osm_roads_legazpi layer.

Opening the Symbol Levels dialog

Figure 3.1.10. Opening the Symbol Levels dialog

  1. Update the Symbol Levels for each of the rules. Make sure that the values for primary are the highest while the values for Others are the lowest.

Symbol level values for the road symbology rules

Figure 3.1.11. Symbol level values for the road symbology rules

  1. With these symbol level rules applied, your road layer should look like below when zoomed in.

Properly configured symbol levels for roads

Figure 3.1.12. Properly configured symbol levels for roads

Road network data styled using rule-based symbology with symbol levels applied

Figure 3.1.13. Road network data styled using rule-based symbology with symbol levels applied

Using scale-dependent rendering, where layers or specific features adjust their visibility based on the scale (or zoom level) is a simple yet powerful way to enhance your map-making abilities in QGIS.

The terms “small scale” and “large scale” are very context-dependent but—by convention—we consider a small scale map as one that shows less detail while a large scale map is one that shows more detail. When representing scales as ratios (i.e. 1:XX,XXX), the one with the smaller ratio (or larger denominator) is considered smaller. Therefore, 1:100,000 is a smaller scale than 1:1,000.

Exercise 3.2. Showing different features at different zoom levels

Section titled “Exercise 3.2. Showing different features at different zoom levels”

Output for Exercise 3.2. Showing different features at different zoom levels

Output for Exercise 3.2. Showing different features at different zoom levels

For this exercise, your goal is to change which features and layers are shown as you zoom in or out of the map. In a way, you will be changing the level of detail in the map depending on the zoom level. For example:

  • When zoomed out (e.g. scale < 1:50,000): you only want to see the primary, secondary, and tertiary roads as well as the green areas.
  • At middle zoom level (e.g. scale between 1:25,000 and 1:50,000): you want to add the other roads as well as the flood hazard layer.
  • When zoomed in (e.g. scale > 1:25,000): you want to show all layers including the osm_buildings.

How will you do this?

The trick here is to use Scale Dependent Visibility. In QGIS, you can fine-tune and control the scale range at which layers and features become visible.

  • For layers: you can find this in the Rendering tab of the Layer Properties -** For features styled using rule-based symbology**: you can specify the minimum scale and maximum scale in the style settings.
  1. Add the flood-hazard and osm_buildings layer to the map you created in the previous section and style them accordingly.

Roads, buildings, and flood hazard layers loaded in QGIS

Figure 3.2.1. Roads, buildings, and flood hazard layers loaded in QGIS

  1. For the road layer, edit the Min. Scale and Max. Scale parameters for the Others rule to 1:50000 and 1:100, respectively. Don’t forget to apply your changes.

Adding the minimum scale and maximum scale parameters to the rule based symbology for roads

Figure 3.2.2. Adding the minimum scale and maximum scale parameters to the rule based symbology for roads

  1. For the flood hazard layer, open its Layer Properties and go to the Rendering tab. Check the Scale Dependent Visibility and put 1:50,000 as the Minimum.

Scale dependent visibility parameters of flood hazard layer

Figure 3.2.3. Scale dependent visibility parameters of flood hazard layer

  1. Look at your map canvas. You should notice that when you are zoomed out (or when the scale is less than 1:50,000), the other roads and the flood hazard are not visible and when you zoom in (or once the scale is greater than 1:50,000) they appear.

At 1:100,000 scale, the other roads and flood hazard do not appear on the map canvas even if they are visible based on the Layers panel

Figure 3.2.4. At 1:100,000 scale, the other roads and flood hazard do not appear on the map canvas even if they are visible based on the Layers panel

At 1:40,000 scale, the other roads and flood hazard both appear on the map canvas

Figure 3.2.5. At 1:40,000 scale, the other roads and flood hazard both appear on the map canvas

  1. For the osm_buildings layer, do the same thing that you did for the flood hazard layer but put the Minimum to 1:25,000. Take a look at you map canvas again, this time the building data should not appear when the scale is less than 1:25,000.

 At 1:40,000 scale, notice that the osm_buildings layer does not appear on the map canvas even if it should be visible

Figure 3.2.6. At 1:40,000 scale, notice that the osm_buildings layer does not appear on the map canvas even if it should be visible

At 1:20,000 scale, the osm_buildings layer is now visible on the map canvas

Figure 3.2.7. At 1:20,000 scale, the osm_buildings layer is now visible on the map canvas

QGIS has a very mature and customizable mechanism for adding labels. Data-driven overrides, QGIS expressions, and even geometry generators can be used when adding labels to vector maps. Another awesome feature of labels in QGIS is that they can be moved manually anywhere on the map canvas and configured comprehensively to take on different shapes, curvatures, or direction.

Exercise 3.3.1. Labeling cities based on population

Section titled “Exercise 3.3.1. Labeling cities based on population”

Output for Exercise 3.3.1. Labeling cities based on population

In this exercise, you will add labels to a map showing highly-populated cities in the Philippines—from basic labels to ones using callouts and manual placement.

  1. Load the gadm_phl_provinces and ne_10m_populated_places_philippines layers in QGIS.
  2. Duplicate the gadm_phl_provinces and name it sea. Create a group named base and add both layers to it. Style the provinces and sea layers accordingly.
    1. gadm_phl_provinces: Single Symbol, Simple Fill
    2. sea: Inverted Polygon, Single Symbol, Simple Fill (Fill Style: Dense 2)

The provinces and sea layers styled

Figure 3.3.1.1. The provinces and sea layers styled

  1. For ne_10m_populated_places_philippines, apply a symbology that will use size to show the population of the location. You can use several techniques such as a Single Symbol with data-defined overrides for the size parameter or a Graduated symbology with Size method. For the latter, you can use the following parameters.
    1. Value: POP_MAX
    2. Method: Size
    3. Size from: 2.0 to 12.0 Millimeters
    4. Value Ranges:
      1. 1 – 500,000 | 500,000 – 1,000,000 | 1,000,000 – 2,500,000 | 2,500,000 – 5,000,000 | 5,000,000 – 99,000,000

The populated places layer styled

Figure 3.3.1.2. The populated places layer styled

  1. To add a label, go the the Labels tab in the layer Properties or in the Layer Styling Panel.
  2. For ne_10m_populated_places_philippines, select Single Labels and use NAME for the value. You can also enable other customizations such as text buffers.

Single Labels using NAME of the populated places layer

Figure 3.3.1.3. Single Labels using NAME of the populated places layer

  1. You now have labels but they look cluttered. What if you only want to label the places with population greater than 500,000? To do that, you need only to edit the VALUE parameter using this expression:
IF(POP_MAX > 500000, "NAME", NULL)
  1. Your map should now look like the one below. Notice that only the locations whose population is greater than 500,000 is now labeled.

Using expressions to filter labels

Figure 3.3.1.4. Using expressions to filter labels

Adding callouts to your labels

Another thing that you can do with labels is to add callouts. A callout refers to a** line or connector that connects a label to the feature it’s labeling on a map**. The purpose of a callout is to prevent label overlap and ensure that labels are clearly associated with their corresponding features, even if there’s limited space or a cluttered layout on the map. Callouts help improve map readability and comprehension by indicating which feature a label pertains to especially when labels are positioned near or inside complex geometries. To add callouts to your labels, check Draw callouts in Callouts of the Labels Tab.

Editing Callouts in the Labels tab

Figure 3.3.1.5. Editing Callouts in the Labels tab

There are several ways to configure callouts and several built-in callout styles in QGIS. For this exercise, you can use a Manhattan line callout.

The map labels using Manhattan lines callout

Figure 3.3.1.6. The map labels using Manhattan lines callout

Manual placement of labels

What if you’re not content with the default placement of labels? Well, don’t fret. QGIS has several functionalities that allow you to fine-tune the placement, shape, and even orientation of labels. For example, you can edit the placement of your labels in the Placement tab of Labels.

Placement tab where you can edit the placement of labels

Figure 3.3.1.7. Placement tab where you can edit the placement of labels

Similarly, QGIS has the Labels Toolbar that allows you to edit and modify your labels—such as manually placing them anywhere on the map.

The Labels Toolbar

Figure 3.3.1.8. The Labels Toolbar

Use the Move Label button (Move Label icon) to move your labels anywhere you want on the map. Notice that the callouts will automatically move with the labels.

Labels manually placed on the map

Figure 3.3.1.9. Labels manually placed on the map

Exercise 3.3.2. Multi-attribute and data-defined labels

Section titled “Exercise 3.3.2. Multi-attribute and data-defined labels”

Output for Exercise 3.3.2. Multi-attribute and data-defined labels

In this exercise, we have a volcanoes layer that has fields for the names and elevations of certain volcanoes/mountains in Bicol. What you will do is style and apply a label to these volcanoes/mountains but with a twist—the size of the marker and label is relative to the elevation of the volcanoes/mountains they pertain to.

To do this, you will use Expressions and Data-defined overrides in your labels.

  1. Load the volcanoes layer and apply a Single Symbol symbology with a Simple Marker whose Size parameter is data-defined (uses the elevation field) value.

The volcanoes layer styled with a data-defined size parameter

Figure 3.3.2.1. The volcanoes layer styled with a data-defined size parameter

  1. Apply a simple Single Label to the volcanoes layer and style it accordingly. For the Value parameter, use the following expression:
concat(name, '\n[', elevation, ' m]')
  1. By default, your map should look similar to the one below:

Volcanoes layer with default label

Figure 3.3.2.2. Volcanoes layer with default label

Making the text size be relative to the actual elevation of the volcano/mountain

Here, what you will do is make QGIS have the text size of the label be based on the actual elevation of the mountain/volcano—this means that if Mount Mayon is taller than Masaraga then the label of the former should be larger than the latter.

  1. To do this, apply a data-defined override to the size parameter of the label’s text using the expression below:
6 + coalesce(scale_exp("elevation", 1000, 2500, 2, 5, 0.57), 0)

Label texts are based on the elevation of the volcanoes/mountains

Figure 3.3.2.3. Label texts are based on the elevation of the volcanoes/mountains

Label placement

Next, you might want to put the labels below the markers. You can do this by editing some of the parameters in the Placement tab of Labels.

  1. To put the labels at the bottom of the marker, update the Position priority parameter with the following expression.
'B,BSR,BSL,BR,BL,R,L'
  1. To manage the distance between the marker and the label, edit the Distance parameter with the following expression.
coalesce(scale_exp("elevation", 1000, 2500, 3, 5, 0.57), 0) - 2
  1. Your map should now look similar to the one below:

Volcano labels with proper (data-defined) placement

Figure 3.3.2.4. Volcano labels with proper (data-defined) placement

Contact us or sign-up to our courses if you are interested in having this as an instructor-led or self-paced course.