In QGIS how can I get the difference between the areas of two layers through field calculator? And is there a way to automatize it so every time a new feature is added the difference updates?
- Layer 1 = Big polygon
- Layer 2 = small polygons inside polygon in layer 1
The "difference" field in this case would be an attribute of the Layer 1.
The area attribute in both layers are virtual fields ($area
)
I thought putting a code in the Default Value of the field in Attribute Form and check 'Apply default Value on update' but I just can't write the code, maybe something with aggregate?
The image bellow shows a schematic representation of what I'm trying to achieve:
Best Answer
Use the following expression in the Field Calculator of layer 1:
Explanation:
With
overlay_contains()
, you get all the polygons from layer 2 that are inside the current feature of layer 1. With"area"
, you refer to the attribute with this name. The result is an array, thus to add all the areas of all polygons inside the current feature, usearray_sum()
, then subtract this from the area of the current feature.