11

I have a database with of accidents in an area. I want to show a lable with the type of accident for every occurence. In the database there are multiple entries for the ID in the first column (because the database is populated according to the involvement in the accident).

I want to show only ONE lable for an accident. For every ID number I want to show one label (per accident). Without rule-based labeling the labels are of course multiple.

I don't know how to write a rule to show me what I want.

Can someone help me?

Data

Wrong labeling

2 Answers 2

13

Under label settings > rendering (paintbrush icon) > Data Defined - Show label, use the following expression:

$id = array_first(array_agg($id,"ACCIDENT_COLUMN_NAME"))

It will only show a label for the first feature in a group of features that has the same ACCIDENT_COLUMN_NAME.


Example data:

enter image description here

Normal label display vs. label display with expression applied:

enter image description here enter image description here

Where to apply the expression - in this example I used

$id = array_first(array_agg($id,"label"))

enter image description here

1

Something tricky but since you are using QGIS :

  • create a virtual layer based at least on your layer with a distinct geometry and label, to have unique labels. Something like :

    select distinct geometry, id, fieldYouWantAsLabel from yourLayer

  • set no symology to this virtual layer

  • use the label field you want in the virtual layer.

When you will add a feature in the layer, the layer label will be automatically updated, so will the labels.

1
  • 1
    You might try an expression (e.g. Count(1,"ID")>1) to filter the records in your labeling rule. Alternately you could use the Delete Duplicates by Attribute to create a temporary layer and label just it.
    – John
    Commented Nov 21, 2022 at 11:14

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.