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:
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:
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:
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!
Posted: September 18th, 2014 under GIS.