I simply want to get a specific attribute (f.e. as label or default value in form) of a feature within another feature. Aggregate could do the job but it don't.

Hard for me to write it down, so let me explain on the screenshot below: All polygons are in the same layer. The green polygon is labeled with the expression:

aggregate('SameLayer','concatenate', to_string("Number"), intersects($geometry, geometry(@parent)))

That works fine so it tells me, I'm on the right way! All intersection features (101, 102, 103) are written to the label (or later to the attribute form). Since I just want the number of the the polygon, where the green one complete fits within, I changed 'intersects' to 'within' (or 'contains') and expect as result: 102 - but it don't!

aggregate('SameLayer','concatenate', to_string("Number"), within($geometry, geometry(@parent)))

The reason is, the green polygon touches with 3 of its sides, the parent polygon. If I draw the polygon complete within the parent, the expression works.

I wonder what could be the right geometry function in my case, and if Aggregate is the right way to solve that trivial task?

enter image description here


2 Answers 2


I don't know if something is off with how aggregate() handles geometric predicates (I am using QGIS 3.12.2)

I tried using the DE-9!M representation - according to relate() the relationship between the green polygon and 102 was '2FF11F212', so I used that to test the relationship instead of within(), but it kept returning an empty string (or NULL since the green polygon itself has a null value for "Number").

Eventually I got it to work with: aggregate('SameLayer','concatenate', to_string("Number"), relate($geometry,geometry(@parent),'212F11FF2'))

But, perhaps a crucial difference is that I also managed to get the expression to work with contains(), so maybe our geometries are not the same?? See if the above works for you...

enter image description here


You can also use this expression that uses the overlay_within() function introduced in QGIS 3.16: array_to_string(overlay_within(@layer, "number"))

*Screenshot: I have 3 polygons (101,102,103) and a fourth on the same layer that lies within polygon 102 and has a NULL value for the attribute "number": I use this empty value for a rule based label with the expression from above:

enter image description here

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.