4

I have a point layer on a world map with several attributes. Each point marks the position of a berth at a terminal in a port in a certain country. When I zoom out, it's getting too crowed so I managed to show only the port names and not the terminal and berth names any more by using an expression for the label. This gives me less text so it's already much better. But as a result, I'm now seeing a lot of points all showing the same port name in a certain area which is also kind of suboptimal. I would like QGIS to cluster these points but only if the attribute port name is the same. The individual labels need to be hidden in case of clustering. However, if there is only a single port (unique port name) the clustering is obviously not working but I still need the port name to appear.

I was playing around now for a long time with the Point Cluster function under Symbology but cannot get it to work. Especially the dependency of individual point labels and cluster labels is problematic.

1

Here's a completely different solution that doesn't bother with the Cluster Renderer.

  1. Duplicate the layer by right-clicking on the layer name in the layer panel > duplicate. This creates a second layer that links to the same source data. We're going to define different symbology for each copy of the layer, and set up scale-based visibility so that at each scale you only see one of the two layers. Make sure the minimum zoom level of one layer is the same as the maximum zoom level for the other layer, so there's no gap or overlap as you cross the threshold.

  2. For one of the duplicate layers, set scale-based visibility so it layer only appears when you're zoomed in. Use simple points and full-detail labels.

    enter image description here

  3. For the other layer, set scale-based visibility so it only appears when you're zoomed out. Use a geometry generator symbol with this expression to display a single point for each port:

    centroid(convex_hull( collect($geometry, "Port")))
    

    Instead of using the standard labels, use a font marker to create labels. This way, we can nest the font marker inside the geometry generator, so the label only appears at the Port point.

    Add a font marker symbol level inside the geometry generator. Use data-defined override and choose the port field for the character selector.

    enter image description here

    enter image description here

    The labels and symbols appear darker for ports with more than one point, because QGIS is actually re-drawing the same symbol in the same place multiple times. If that becomes a problem you could fix it with a conditional statement in the geometry generator expression. You would need to add a field to the layer, where for all the points in each port, one of them has a different value than all the other points in that port. Eg, use the Field Calculator to create a numerical field called "display," with the expression @row_number = minimum( @row_number, port). Then in the geometry generator, use this expression: if("display"=1,centroid(convex_hull( collect($geometry, "Port"))),null).

If you would rather see all the points at all scales, and have only the labels scale-based, you can achieve that with a small modification.

  • Make the first layer visible at all scales. Make the labels for that layer only visible at zoomed-in scales.
  • Keep the second layer visible at zoomed-out scales. Remove the point marker symbol layer, but keep the font marker.
  • Wow, thank you very much for this comprehensive answer and this smart solution. Much aprreaciated. I really learned a lot and will play around with it now and hopefully get it to work. Thanks again! Great help... :-) – Trostlooser Sep 10 at 9:50
  • Hello again, is there a way to avoid that duplication of the layer? Would it be possible to use an expression with the @map_scale parameter which can toggle visibility? – Trostlooser Sep 12 at 6:56
  • I tried doing it in a single layer with multiple symbol levels at first, but the scale-based visibility settings apply to the entire layer so it was much easier to duplicate the layer. If having a duplicated layer is a problem for you, you can probably use the map scale parameter in the data defined settings for the "enable layer" settings, which you can see at the bottom of the second image. – csk Sep 12 at 18:54

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.