Site menu:

Site search

Categories

Label Only Features Intersecting with Current Atlas Feature in QGIS

If you’ve ever used the excellent Atlas functionality feature in QGIS, you know that you can make some very nice map books (‘atlases’ in QGIS parlance) with feature-driven layouts, titles, styling, etc.. This functionality is becoming more and more advanced in recent releases. Yesterday, I was working on an atlas, and wanted to label only roads and lakes that were within the currently ‘active’ atlas feature, rather than labeling all roads and lakes. With a bit of searching, I was able to find a solution that worked beautifully. I present it below in the hope that others find it useful!

In my example, I had an atlast with a ‘fade out’ so that features outside of my current atlas feature got ‘blurred’ until they became white. However, when I labelled my roads, all labels were showing, even in the otherwise white areas. This was not desirable, as you can see below:

Labels On White Background

Notice how the road labels show on the otherwise whited-out background

To fix this issue, I set a rule for the labels that looks as follows, where LABEL_EXPRESSION_FIELD should be replaced with the appropriate field name or expression that you want to use for your labels:

CASE WHEN intersects($atlasgeometry, $geometry ) THEN “LABEL_EXPRESSION_FIELD”  END

All you need to do is go into the label setting, and use the above expression in the “Label this layer with” expression field:

Label Expression

Label Settings Window showing how I set up the expression.

QGIS will then only label features that intersect (i.e. touch at some point) the current atlas feature. In my case, this gives the much more desirable results shown below. Note how the white areas no longer have labelled roads shown:

A much nicer looking map, with only roads labelled that touch the current map feature.

A much nicer looking map, with roads labelled only where they touch the current map feature.

 

Of course you can have a lot of fun with this doing other neat things as well. Hopefully this inspires you to create your own awesome atlases!

Write a comment





Read previous post:
Create Midpoints Along Lines with OGR

Today I found myself trying to create a 'midpoint' on a large shapefile line layer. QGIS doesn't have a tool...

Close