5

I'm working with QGIS 3.16 Hannover under MacOS 10.13 environment. I've got two polygon layers A and B with multiple features in each. I want to calculate, for each feature in layer A, the percentage of its area that is actually covered by any feature of layer B. I have built in the field calculator of layer A the following code, that seems to work in some cases, but definitely doesn't in others

(area(intersection($geometry, collect_geometries(overlay_intersects('B', $geometry)))))*100/area($geometry)

Any idea of what's wrong?

0
8

I see three cases when this does not work:

  1. If a feature is not covered at all (result = NULL) (which is actually correct)
  2. If covering features of B do overlap each other
  3. If the intersection of both layers is of type GeometryCollection

To cover both cases, you can use this expression:

coalesce((area(buffer(intersection($geometry, buffer(collect_geometries(overlay_intersects('b', $geometry)),0)),0)))*100/area($geometry),0)

Explanation:

  • for case 1) coalesce() will cover the NULL case and insert 0 if no result is returned by the actual expression.
  • for case 2) buffer(...,0) will dissolve the overlapping parts of the multipolygon by a buffer of 0 (workaround since there is no actual dissolve() function) so will prevent from causing an error in the calculation
  • for case 3) just add another buffer to turn the GeometryCollection (result of intersection()) into a MultiPolygon

Example:

enter image description here

6
  • Beautifully solved and explained. – Jesús Pinilla Mar 15 at 20:45
  • Unfortunately, @MrXsquared, there are 30 cases out of 402 in which it doesn't work. For example, this feature (in red) do overlap with 3 features of the B layer [link] (cloud.mail.ru/public/yKQS/Xe9aPNUgZ). See the attributes table [link] (cloud.mail.ru/public/EKR1/k5AF2xe2g) where the 3 features are listed using this code array_to_string(overlay_intersects('B', SITE_CODE)), but the calculated overlapping area is 0. – Jesús Pinilla Mar 17 at 16:01
  • 1
    Huhm, cant really tell.. are the geometries valid? Is it possible for you to share that specific data so we can try and test? You could also try to do the calculation steps without expressions but step by step using processing tools, maybe you will then find the cause. – MrXsquared Mar 17 at 20:36
  • I've prepared two sample layers where the issue arises (cloud.mail.ru/public/BXw6/eqeoHhsnT). It seems to have to do with multipolygons and geometries with holes. Perhaps other "peculiar" geometries act that way too. – Jesús Pinilla Mar 19 at 16:28
  • Was the same issue as I covered with case 2: The resulting geometry of intersection() is of type GeometryCollection, you can turn it into a MultiPolygon by just adding another buffer and it should work: coalesce((area(buffer(intersection($geometry, buffer(collect_geometries(overlay_intersects('b', $geometry)),0)),0)))*100/area($geometry),0) – MrXsquared Mar 19 at 17:17

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.