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.