3

I have a polygon layer with hierarchical polygons. The main polygon has a capital letter like 'A' or 'B'. All polygons inside the main polygon got a number and all inside them get a second, third, fourth... number. You can see it in the graphic below.

I want to create new polygons inside a other, they should get the letter and numbers of there parent features. And also if I change a parent, the child's (an maybe the "older" parents) should automatically update. For example: in the second graphic, I changed the letter A to B. All related polygons should now update. enter image description here

I did use an aggregate function to get the attributes of the parent feature. This works for adding new polygons but won't help if I change a parent. I think aggregate is not the perfect function since I'm working in just one layer:

aggregate('layer','concatenate_unique',to_string("parent"),within(centroid(geometry(@parent)),$geometry))

At the moment I use only one Geopackage-Layer for the polygons and would like to keep it that simple. But If there is a nice solution by using more layers or a "real" database, I'm okay with it! I'm running QGIS 3.10.

1 Answer 1

4
+200

You can do this by adding a VirtualField to your layer (which will be calculated upon feature update/creation), using the following expression:

aggregate(
  layer:= 'Polygons',
  aggregate:= 'concatenate',
  expression:= Id,
  concatenator:= '-',
  filter:= contains($geometry, geometry(@parent)),
  order_by:= -area($geometry) 
)

In this case Polygons is the layer of interest, and Id is the field containing the base attribute to be rolled up. The result is an aggregate of the Id values a given polygon is contained by.

Initial Initial

After Insert/Update After Insert/Update

Tested on QGIS 3.8.3

1
  • It is simple and it works! But the aggregate function is consuming too much resources for 300 polygons. I thought to go the same way with a SQL virtual Layer - but I don't know how - any idea?
    – MartinMap
    Jul 13, 2020 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.