5

With the following code I can generate a listing. However, the entire list is displayed and not the data of the attribute of the respective geometry.

enter image description here

'Schäden: '|| 
array_to_string(
  array_agg(
    "schadennr", 
    group_by, 
    "ursache_detail" = 'Hochwasser/Überschwemmung'
  ),
    ', '
)

How is it possible to display only the data of the attribute of the geometry as a list? For the example shown, two strings should be displayed, separated by a comma.

enter image description here

This is the picture of my previous comment.


7
  • 2
    group_by what exactly? Commented Dec 21, 2022 at 10:50
  • The grouping may be superfluous. The two attributes of the cumulation should simply be displayed as a list.
    – Alex
    Commented Dec 21, 2022 at 11:13
  • 1
    Could you add an example (text or image) showing exactly what you would like to be displayed as a result? What do you mean by "two strings"? Commented Dec 21, 2022 at 11:19
  • 3
    Please provide an example of how the attribute table looks.
    – Erik
    Commented Dec 21, 2022 at 11:50
  • 1
    My understanding is that here, you aggregate data from all features in the layer, and use it as a label for each feature. Where what you want to do is just aggregate feature value from 'field1' & 'field2' for each feature. In that case, the answer is here: gis.stackexchange.com/questions/12062/…
    – Kasper
    Commented Dec 21, 2022 at 11:55

2 Answers 2

5

Based on the updates to your question I'm guessing you want your label to show a single label with concatenated attributes for any number of features that have identical geometry.

Your approach was grouping the schadennr values by another attribute ursache_detail which helps to narrow it down, but I'm guessing for your dataset that encompasses a group that is much larger than an incident at the same location. So, by this logic you need to group your schadennr values by the actual geometry of the feature.


Step 1: make sure you only show one label per geometry (so you don't end up with two labels for identical geometry, regardless of label overlap settings).

Refer to the method in this answer, but use the following expression instead for the data defined override under 'Show Label':

$id = array_first(array_agg($id,geom_to_wkt($geometry)))

Step 2: for the label itself, use the following expression:

'Schäden: '||array_to_string(array_agg("schadennr",geom_to_wkt($geometry)),', ')

Original:

enter image description here

Step 1 - show only one label per feature with identical geometry regardless of label overlap settings:

enter image description here

Step 2 - set label to show concatenated attributes with identical geometry:

enter image description here


Note, if you want to change the order in which your attributes appear in the concatenated label, you can use the order_by parameter for array_agg() to specify the order. E.g.

'Schäden: '||array_to_string(array_agg("schadennr",geom_to_wkt($geometry),order_by:="ORDER_FIELD"),', ')`

I also don't know why these aggregate expressions only work with geom_to_wkt() and not $geometry

3

Wrap array_agg function to array_slice as follows:

'Schäden: '|| 
array_to_string(
    array_slice(   -- here
        array_agg(
            "schadennr",   
            -- add parameter name 'filter'   
            filter:="ursache_detail"='Hochwasser/Überschwemmung'
        ),
        0, 1),     -- here
    ', '
)
1
  • Thank you for your help. The display is correct for the example, but the same data is displayed everywhere. I am looking for an expression that takes the location (geometry) into account and then shows me the attribute values of the different data sets. The following graphic shows an example. There are two data sets with the same geometry (but there can be more). These are displayed accumulated (see first graphic) and are to be labelled. The labelling should be site-specific in each case.
    – Alex
    Commented Dec 21, 2022 at 12:46

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.