Data driven labelling in Context

“Data driven labelling gives you the ability to control label placement using data”

In this module, we will look at how you can use data in your attribute table to specify where labels should be placed, how they should be rotated, what font and font size should be used and so on.

You try:

Goal: To be able to place labels with pixel perfect precision on your map.

  • Add the airports layer to the map
  • Set the map scale to 1:1 mil
  • Edit the airports layer and add the attributes as listed in 'New attributes' on the table, right
  • Enable labelling and link these attributes to their equivalents in the 'placement' and 'text' areas of the label properties dialogue
  • Ensure your layer is in edit mode
  • Use the label toolbar to shift, rotate labels into their ideal positions
  • Be sure to save your edits regularly

Check your results:

  • What happens when you zoom in close to your labels? Why?
  • How do you revert a label back to automatic placement?
  • Look in the attribute table to see what happened to the attributes when you manually placed labels.

Name Expectation

New attributes

label_x_1mil

Decimal: 15, 5

label_y_1mil

Decimal: 15, 5

rotation

Whole number: 3

font

Text: 255

font_size_1mil

Whole number: 3

Labelling options

Formatting: Wrap on character

Space (space bar)

Formatting: Alignment

center

More about

When you use a data field rather than an expression, there is a subtle behaviour difference: When using a data field, QGIS will default to the label property set in the user interface if the attribute is null. This means you can explicitly define placement etc. options in the attribute table where they are needed, and fall back to user interface defaults in all other cases.

It should be noted that there is when you use data driven properties it implies that the cartography has been defined for a specific scale. You may have wondered at the naming convention above. It is useful to know what scale the label placements are optimal for. If you are working in a shared database with multiple projects, it is likely you would create several label_x etc. positions - one for every standard map product you produce.

You can 'bootstrap' data driven labelling properties by using the field calculator. For example, you can pre-fill in the font size property to 12. Similarly, you can pre-pin the label_x and label_y positions based on a simple expression.

While data driven labelling has some advantages, if you have pinned your labels you lose a lot in terms of accommodating new layers being added to the map and automatically ensuring that your labels do not cover important information.

Use data driven label positioning sparingly if you can, the other data driven label properties are probably more valuable.

Check your knowledge:

  1. Choose all that apply: data driven labelling:

    1. lets you create data from your labels for insertion into a spreadsheet
    2. lets you override label properties on a per-feature basis
    3. is a convenient way to ensure that your labels work at all scales
  2. Which of these would pre-pin your label x position to the feature position:

    1. IF (label_x_1mil = 0, x( $geometry), label_x_1mil)
    2. IF (label_x_1mil IS NULL, $geometry.x, label_x_1mil)
    3. IF (label_x_1mil IS NULL, x( $geometry), label_x_1mil)

Further reading:

Download the sample data for the lesson.