1

I am working on a print composer layout, and want to dynamically define a text field with an expression. In my QGIS project, I have a vector layer with polygons ("Polygons") with no overlaps, no gaps, and shared borders and another polygon layer containing a single polygon ("Single_Polygon"). I calculate the centroid of this latter polygon layer with the expression "centroid(aggregate('Single_Polygon', 'collect', $geometry))". I now want to get a certain attribute (let's call it "my_attribute") from exactly the polygon of the first polygon layer, that intersects the calculated centroid point (the centroid always exactly intersects one polygon). The value of this attribute should be displayed in the text field.

This is easy when I manually calculate the centroid of the single polygon, and then use following expression in the field calculator: overlay_intersects(layer:='Polygons', expression:= "my_attribute", limit:=1)[0]

The goal is, that I can define a layout variable with the name of the "Single_Polygon" layer, and that the text field then automatically shows the desired attribute value of the polygon from the "Polygons" layer, that the centroid of the "Single_Polygon" intersects.

How can I do this?

Below I'll append a picture of the situation. The pink polygons correspond to the "Polygons" layer, the yellow polygon corresponds to the "Single_Polygon", and the red point symbolizes the centroid.

1 Answer 1

1

It seems like I found the solution. I got it to work by using the aggregate() function:

with_variable(
    'centroid',
    transform(centroid(aggregate('Single_Polygon', 'collect', $geometry)), layer_property('Single_Polygon', 'crs'), 'EPSG:25832'),
    aggregate(
        'Polygons', -- this layer is in the CRS 'EPSG:25832'
        aggregate:= 'array_agg',
        expression:= "my_attribute",
        filter:= intersects(@centroid, @geometry)
    )[0]
)

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.