10

I have a map of Canada and the United States that's broken down by electoral districts. At near zoom levels, I have labels for the electoral districts. Closer zoom reveals more information (the names of MPs or Reps and party controlling that district).

For that, label scale functionality works great and the functionality is good enough to keep from getting too cluttered.

Electoral districts

Electoral districts with fine detail

However, if I want to label higher levels, the issue is that, of course, all those labels get repeated every electoral district. So Michigan and Ontario will have that label on every district, and similarly further zoomed out will do so for America and Canada.

State or province labels over electoral district

I know about the dissolve function, but that's not much use as I need to keep the subdivisions.

Essentially, I'm looking for any solution that is the opposite of "Label every feature of a multipart feature" -- i.e. to label only one of multiple parts that have a common attribute, like MI or ON.

3 Answers 3

12

You can use the following expression in "Layer Styling > Label > Placement > Geometry Generator".

collect( $geometry, group_by:="common_attribute")

enter image description here

Before:

enter image description here

After:

enter image description here

0
11

You can use an expression in the Geometetry Generator of the label postion tab.

enter image description here

This expression uses subdivision centroids at scale < 100000, for scales greater, it uses the centroid of subdivisions grouped by an attribute.

CASE
    WHEN
        @map_scale < 100000
    THEN
        $geometry
    ELSE        
        centroid(
            collect(
                expression:= $geometry,
                group_by:=state
            )
        )
END

enter image description here

Notes:

You may need an additional CASE statement in the label value expression to display different information.

The centroid of the collected geometries may fall outside the boundary of an individual polygon. In which case you can try point_on_surface in place of centroid.

9

To label only the largest feature of the same category (same electoral district, here based on the field named "NAME"), use Label Placement > Geometry Generator with this expression:

coalesce(
    order_parts(
        collect($geometry, "NAME"), 
        area($geometry),
        False
        )
    )

Before: mainland + all islands labeled: enter image description here

After: only mainland is labelled: enter image description here

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.