2

I have two separate polygon layers, A and B, of which I want to calculate the intersection to union ratio and store it as an attribute to polygon A. To do this, one needs to divide the area of the intersection over the area of the union, like this (in pseudo-code): area(intersection(PolyA, PolyB)) / area(union(PolyA, PolyB)).

I am aware that the standard routine would be to first produce the intermediate products (the polygon of intersection, and the polygon of the union), then join their respective areas to Polygon A, and finally calculate the ratio. However, given the possibilities in the Field Calculator, like demonstrated over here, I thought it might be neat to do it all inside the Field Calculator.

I tried the following in the field calculator: area( intersection( @PolyA , @PolyB )) / area( union( @PolyA , @PolyB )), but that resulted in the error: Evaluation error in expression. Cannot convert to geometry.

How do you calculate the intersection to union ratio between two polygons in the Field Calculator in QGIS?

I'm new to QGIS. I use version 3.32 Lima.

0

1 Answer 1

1

Try something like this:

with_variable(
    'B',
    overlay_intersects(
        'PolyB layer name / id', -- set inside quotes PolyB layer name or id
        $geometry
    )[0],
    area(intersection($geometry, @B)) / area(union($geometry, @B))
)
3
  • try it this way, i already put the name of your variables so you don't need to replace any thing. I'm texting from my phone so sorry if there are still some errors with_variable( 'B', overlay_intersects( @Simplify_output, $geometry )[0], area(intersection($geometry, @B)) / area(union($geometry, @B)) )
    – Mayo
    Sep 18, 2023 at 14:01
  • It worked! In the end, only the first instance should be 'B', and the rest is @B. Now, I was wondering: what is the function of the index [0]?
    – saQuist
    Sep 19, 2023 at 6:51
  • overlay_intersects returns an array of the features of the B layer that intersects the current feature that's being evaluated on the A layer. So in this case, where there's only one feature of the B layer that intersect one feature of the A layer, you only need to take the unique feature returned by the overlay, that will be the first element of that array, so [0] works fine here.
    – Mayo
    Sep 19, 2023 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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