3

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:

enter image description here

3

Use the following expression in the Field Calculator of layer 1:

$area - array_sum(overlay_contains('layer_2', "area"))

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, use array_sum(), then subtract this from the area of the current feature.

1
  • Thanks a lot!!!
    – Felippe M.
    Dec 2 '21 at 21:16

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.