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

Scrchd
  • 155
  • 6

3 Answers3

7

This expression appears to be working just fine:

area(
    intersection(
        $geometry,
         aggregate(
             'Layer_1',
             'collect',
             $geometry,
              "Value" = 10
            )
        )
    )
Taras
  • 32,823
  • 4
  • 66
  • 137
Scrchd
  • 155
  • 6
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

Babel
  • 71,072
  • 14
  • 78
  • 208
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
rickD
  • 577
  • 3
  • 13
  • 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 Jan 05 '23 at 13:09