QGIS Labeling – How to Avoid Duplicate Attributes and Condition by Two Fields

expressionlabelingqgis

In a layer with point geometry, I have the house number label. Each geometry has the HOUSENUM, PARCEL and STREET fields. In some cases the geometry is duplicated (equal geometry), in some cases the house number attribute could be also duplicated.

I am using the following expression to get the labels avoiding duplicates. But I need to be more precise and take into consideration the groupings by PARCEL and STREET. At the same time this function is not working well as it seems to duplicate labels in some cases.

array_to_string(string_to_array(aggregate(
  layer:='HOUSE',
  aggregate:='concatenate_unique',
  expression:="HOUSENUM",
  concatenator:='\n',
  filter:=intersects($geometry, geometry(@parent))
)),',')

I am sending a screenshot of the result I am getting at the moment with the display of the attribute table for three different PARCELS, so that you can see the structure of the table and the duplicate values:

PARCEL 00001 and STREET A and B:
enter image description here

PARCEL 00002 and STREET A:
enter image description here

PARCEL 00004 and STREET A and B and C:
enter image description here

And I send screenshot of the expected result. I am interested in solving the conflict of duplicated geometries and filtering without duplicates the HOUSENUM texts according to the PARCEL and the STREET to which they belong. Regarding the Placement expected, just show the position to understand the problem.

enter image description here

Best Answer

To prevent the duplicated labels, you can try this to render the label only once per PARCEL|STREET group.

case
    -- render the label for only the first feature of the group
    when $id = array_min(array_agg($id, group_by:="PARCEL"||'|'||"STREET"))
    then
        array_to_string(string_to_array(aggregate(
            layer:='HOUSE',
            aggregate:='concatenate_unique',
            expression:="HOUSENUM",
            concatenator:='\n',
            filter:=intersects($geometry, geometry(@parent))
        )),',')
end
Related Question