1

I'm attempting to label population centers in a map of a state which has a vast disparity between the population density in the west versus the east:

map of Oregon State USA

I'd like to reduce the clutter in the western part of the state. I see a lot of options in the Layer Styling pane for controlling label placement, but nothing that seems to limit label placement based on proximity to another feature (an orange city-limits polygon) on the same layer.

1 Answer 1

1

You can use a setting that only shows labels of there are no other features nearby: nearest neighbor must have a minimal distance and only if that condition is fulfilles, a label will be placed.

You can make the settings in the tab placement: set Priority to low, Obstacle Settings (bottom right) to high and use the Symbol ε for a data defined override with the following expression. Change 250 at the bottom to a value that fits your data: it is the distance each point has to its closest neighbour. If this value is smaller than the value you set, placing labels is avoided. The overlay_nearest function is available in QGIS 3.16 +:

length (
    make_line (
        $geometry, 
        array_first (
            overlay_nearest( 
                @layer, 
                $geometry
            )
        )
    )
) < 250

The expression measures the distance of each point to it's next neighbour. If it is lower than the threshold defined (250 meters in my case), then these features are set to true and in this way generate a data driven value for the setting that says:

When activated, features acting as obstacles discourage labels and diagrams from covering them.

T:\Daniel\Uni\Lehrveranstaltungen\Vorlesung_Zey\Dynastien.jpg

You have further options to reduce labels based on data driven override: in the tab Rendering set a data driven override for Show Labels: I used the same expression as above, just changed the last line to ) > 400 (remark the changed symbol > as well!):

enter image description here

There are a lot of other options how to control how and where labels appear (see e.g. here). You could use Rule based labels and define different rules for areas with low and high density of features: using similar filters as here or creating a new attribute for several areas of different label density etc.

It depends very much what exactly you want to achieve. You should define a list of criteria to be fulfilled. Based on that, a more precise answer for your very use case is possible.

2
  • Thanks for the answer! It only worked for me when I look for the two nearest points. The "nearest" point seems to be the geometry itself. The second nearest seems to be the closest point I am looking for: overlay_nearest(@layer, $geometry, limit:=2)
    – Andreas
    Commented Jun 17, 2022 at 10:47
  • OK, then use [1] to get the second element (0 is the first): overlay_nearest(@layer, $geometry, limit:=2)[1]
    – Babel
    Commented Jun 17, 2022 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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