4

Layer 1 consists of 3 features with different values assigned to them (10 or 20). Feature from Layer 2 overlaps all 3 of them. I'm looking to calculate intersection area with polygons but only with ones that have value 10 assigned to them.

Here is a catch, I want it done only using expression calculator without processing tools. Here is a thread that I found that is somewhat close to what I'm trying to do: Using the field calculator to calculate intersections.

I'm using QGIS 3.20.1

Update: I've managed to calculate the area of polygon intersection using:
area(intersection($geometry,aggregate('Layer_1','collect',$geometry)))
but it still lacks value = 10 condition

Visualization

2
  • start by selecting polygons with the required value, then do the intersection
    – Ian Turton
    Commented Jan 5, 2023 at 10:59
  • Thank you for the answer, but as I stated in the question I don't want to use processing tools.
    – Scrchd
    Commented Jan 5, 2023 at 11:12

3 Answers 3

7

This expression appears to be working just fine:

area(
    intersection(
        $geometry,
         aggregate(
             'Layer_1',
             'collect',
             $geometry,
              "Value" = 10
            )
        )
    )
6

You can use an overlay_intersect() function for that, as it contains an (optional) filter argument:

area(
    intersection (
        $geometry, 
        collect_geometries(
            overlay_intersects ('layer1',$geometry, filter:=value=10)
        )
    )
)

Using the expression from above without the area() function in Geometry Generator, you can create the intersection (red) of features from layer 1 (blue) and value=10 with the polygon from layer 2 (yellow) for visual control of what you are calculating the area:

enter image description here

0

I got weird results from the one above, this isolates the return value for "value"=20 out and the return values are more accurate.

case
when "Value"=10 --this can be changed, but for this case it works
then
area(
    intersection(
        $geometry,aggregate(
            layer:='Layer_1',
            aggregate:='collect',
            expression:=$geometry,
            filter:="Value"=20 AND --could also use not equal to 10
                intersects(
                    $geometry,
                    geometry(@parent)
                )
            )
    )
)
else 0
end
1
  • 1
    What do you mean by weird results ? Can you please elaborate, ? Also I can't get your expression to work, I think it might be because in the part when "Value"=10 you are trying to refer to attribute from Layer 1 while doing calculations on Layer 2.
    – Scrchd
    Commented Jan 5, 2023 at 13: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.