3

I would like to create a set of rule based label styles for a subset of points inside a particular polygon, with the ability to still quickly show/hide the labels using the button enter image description here

The situation: I have a polygon layer made up of several protected areas. Inside each protected area I have points (for more labels than I want to show) for things like mountains and islands. The project is going for review by committee and I don't know which features the committee will want so I have to guess when I make the draft and then I will need to go back over the maps to make corrections.

I would like to only show point labels inside 'Protected Area A', but not all the labels because there are too many, and style the point labels for mountains in different colours (aka: only label some mountains within Protected Area A).

  • The 'within' code below gets me part of the way, but I can't figure out how to add in the type of point e.g.: mountain, and I can't figure out how to show labels in only one of the polygons instead of all of them. (within($geometry, aggregate('ProtectedArea', 'collect', $geometry)) = 1)) So far this seems like my best option, I'd just need to make separate polygon layers for each protected area.
  • I tried using the 'within' polygon code in the label renderer, but it stops the show/hide button from working
  • Someone suggested maybe the overlay_intersects() might be a direction to go but I am not familiar with the expression.
  • I can create a new field for show/hide in the attribute table, but it's so slow doing the manual data entry
  • I can split the points up into different category layers but that gets cumbersome for the editing because there are too many categories, polygons, and scales that I am working at.

enter image description here enter image description here

1 Answer 1

2

To show only labels of features (points) that are within polygon A, on the point layer's label settings go to the Rendering tab and select a data driven override for Show label using this expression ('area' is the name of the polygon layer, "name" is the fieldname):

overlay_within('area', filter:=name='A')

After you set this automatic rule which labels to show and hide, you can still use the show/hide icon for manually overriding this automatic setting and to fine tune visibility (turn on/off) of individual labels.

If you want to delete manual override, go to Layer properties / Auxiliary Storage and delete the corresponding entry. You might need to define the data driven override for the Show label again, pasting the expression.

enter image description here

Edit

If you want to combine like: label only points within polygon A that contain in their "name" attribute the string 'mountain', use this expression:

overlay_within('polygon', filter:=polygonname='A') and regexp_match(name, 'mountain')

Or, if you have a field type, containing for each category the same entry (mountain, island), simply:

overlay_within('polygon', filter:=polygonname='A') and type='mountain'

enter image description here

3
  • This is closer, but because your solution uses the 'show label' control I can't use the show/hide button and the associated auxilliary_storage_labeling_show (which is vital to me being able to quickly turn labels off and on). But if I could use it as the polygon exclusion AND the label category selector it would work. Do you know how to combine the overlay_within ('area', filter:=name='A') and something like "category" = 'mountain' into a single expression?
    – CDEdwards
    Sep 13, 2021 at 17:44
  • Based on what you want to do, you could also consider duplicating the point layer, set symbology to no symbols and only define a label: so you have one layer for the symbols (without labels) and one layer with the labels (without symbols) so that you can turn labels on and off indepenedently
    – Babel
    Sep 13, 2021 at 19:50
  • I'm not quite sure what you want to achieve: I can still use show/hide button with auxilliary_storage_labeling_show after I set the expression like described. The expression creates a selection based on the rules defined, then clicking on the features with the show/hide button adds finetunig to manually override these automatic setting. I added to my solution a way to only show labels inside polygon A that contain the string mountain in their attribute-field called name. By the way: wouldn't it easiert to create two layers - one for each category mountain, island)?
    – Babel
    Sep 13, 2021 at 19:55

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.