TransWikia.com

Percentage of feature area covered by features of other layer in QGIS

Geographic Information Systems Asked on March 19, 2021

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?

One Answer

I see two 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

To cover both cases, you can use this expression:

 coalesce((area(intersection($geometry, buffer(collect_geometries(overlay_intersects('B', $geometry)),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

Example:

enter image description here

Correct answer by MrXsquared on March 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP