8

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

6

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.

4
  • 1
    Perfect thanks! Was trying with aggregate formula already, but remained blocked as was not using using geometry(@parent) correctly, as well as forgot to align layer+CRS projections. Adjusted formula according to your instructions, worked like a charm. Also thanks for alternatives, scale-based not meeting my needs but lines a good option! Nov 30 '21 at 12:07
  • 1
    Fine if it worked well ! The formula work with geometry(@parent) or $geometry ?? (to correct the answer). Nov 30 '21 at 15:51
  • By means of logic it should be $geometry, since this one referes to the geometry of the layer YOURLAYER, while geometry(@parent) always depends on where you insert the expression, so it could be YOURLAYER, but also something else.
    – MrXsquared
    Nov 30 '21 at 22:16
  • I was looking for what @JGeographer_GSU have used to solve his problem. The filter argument and the expression argument in the aggregate formula are really interpreted differently. In the expression argument the context is the layer target ("id" mean "id" in your target layer), in the filter argument it is not the case. I found no documentation about parent so it has still some mystery to me. Dec 1 '21 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.