Labelling with geometry expressions in Context

“Geometry expressions offer a powerful way to manipulate attribute value, geometry and variables in order to dynamically change features within a layer”

The geometry functions are a special group of functions that are used to manipulate the geometry of layers. The geometry functions can be used in multiple places like labelling and symbology. When the geometry expressions are used in labelling they act like virtual fields/virtual layers where they evaluate the expressions on the fly and render them independent of the attribute table.
In this module, we will look at how we can use the geometry functions to generate labels.

You try:

Goal: To learn how to use the geometry functions to mask labels

  • Load the exercise data
  • Add a background layer i.e web map or any XYZ tile layers and ensure the rendering order has the raster layer at the bottom.
  • Label the two layers dams and rivers using the columns name.
  • Zoom in to the extent of the dam Vaaldam.
  • Inspect the labels. Do they look right?

NB In cartographic standards rivers flow into dams and as such a rivers label should not be visible on top of the dam.

  • Go to the label settings of the river and adjust the label column to use the geometric expression.
  • Inspect the labels of the rivers. Are the river labels still visible on top of the dams?
  • Add a second rule for dams that uses the expression Area label. Adjust the label settings so that the label fits within the polygon.
  • Adjust the placement for the second rule so that it labels along the perimeter of the polygon.

NB To prevent overriding the first rule switch to Rule Based labelling which allows you to define multiple label rules for the layer.

Name Expectation

Area label

round( area(transform( $geometry ,'EPSG:4326','EPSG:3857')) / 10000,3)

geometric

if (aggregate(layer:='dams', aggregate:='max', expression:="name", filter:=intersects( $geometry, geometry(@parent) ) ), '', "name")

More about labelling using geometry expressions

Geometry functions are very useful as they allow on-the-fly computation of values. This reduces the need to have to create multiple layers or attributes to hold certain values. When using the geometry functions in labelling you have to take into consideration the number of features that are in the layer as this tends to make it slow.

Example: x(line_interpolate_point($geometry,0.7*$length))
The function above places a label at 70 % of the length of the line.

Check your knowledge:

  1. What is a geometry function:

    1. A type of vector data.
    2. A type of symbology mechanism that can be used for rasters and vector layers.
    3. A type of function that allows new values to be computed based on existing geometry of the layer.
  2. When using the geometry functions to calculate area does the data need to be in a projected co ordinate reference system:

    1. Yes, you can only get accurate area interpretation when the data is projected.
    2. No, A GIS should be able to handle this.
    3. I do not know.
  3. The units of area can only be metres.:

    1. True
    2. False