QGIS: label only if more than 1 polygon visible in map extent

atlaslabelingqgisqgis-3

I am working on a series of maps in QGIS. I am creating an atlas, not generated on a grid of regular size, but instead based on polygon features of variable size. Everything working well so far, sometimes using answers provided in this community. The only questions I did not find an existing answer about, concerns fine-tuned labelling of overlaying layers. I will be taking here a simplified example using country polygon layers, and examples labels highlighted in yellow (country names).

Due to the varying size of polygons defining atlas pages extents, both of the following case will happen:

In case A, some page extent intersects the border of 2 countries. In case B, page extent is entirely contained within one country.

I would like the country layer to be labelled in case B only, to avoid a lone country label getting placed at the center of the canvas.

Instinctively, I would be looking to advanced labelling rules and try to build a custom label, trying to state the following rule: label layer only if more than 1 polygon is visible in canvas extent.

  • Would that logic be valid and how should I do it?
  • Can I combine this logic with Atlas generation in Layout, or would it work only with individual map creation?

enter image description here

I am currently trying along the lines proposed at Selecting features visible in map extent using QGIS

Best Answer

There is different solution:

The simplest way but may be not applicable. You set the label to be only shown over a specific scale. Choose "rule-based labelling" and adjust the maximum zoom to fit your use case:

enter image description here

Other solution, you can transform your country layer used for label to a line and the name of the country will only be shown when you see the border of a country. Use the tool polygons to lines to do so and adjust the labelling.

The last solution using expression. I think it could be quite hard to do well and more time consuming. I will not tell you the complete formula you have to find your own because it could depend on your project.

What you should know:

  • The intersects formula or geometrical process work only if the layer are all in the same projection.
  • You should find your solution using aggregate formula, counting the number of feature that cross the map extent. Then, if it is above one feature, you should have the label visible.

Here is a base of formula:

aggregate (
   'YOURLAYER' , 
   'count', 1, 
   filter:=intersects( @map_extent , geometry(@parent))
) > 1

I don't know if you should use geometry(@parent) or $geometry.

Related Question