[Qgis-user] Calculate intersecting area of two layers in a virtual field. Context of @geometry in overlay_intersects()

Klaus Affeldt ka at ansperger.de
Thu Apr 4 05:01:21 PDT 2024


Hello Bruno,

try it the other way around:

coalesce(
  area(
    intersection(
      @geometry, 
      collect_geometries(
        overlay_intersects(
          'forest',
          @geometry
        )
      )
    )
  ),
  0
)

In addition you can use the inner part in the geometry generator.
So you have a visual help to compare your results:

    intersection(
      @geometry, 
      collect_geometries(
        overlay_intersects(
          'forest',
          @geometry
        )
      )
    )

Mit freundlichen Grüßen

Ihre PV ANSPERGER mbH
Klaus Affeldt

Zentrale Kamp-Lintfort
Südstraße 25
47475 Kamp-Lintfort
Tel. +49 2842 9635-14
https://www.ansperger.de

A==============================================================================================
>> 
>> Dear list
>> 
>> Great, how powerful expressions are in QGIS!
>> I have two polygon layers, one with parcels and one with forests. In a virtual field of the layer parcels I'm trying to calculate the area covered with forest. I can sum the area of intersecting forests in a virtual field with the follwing expression:
>> 
>> coalesce( 
>>   array_sum( 
>>     overlay_intersects(
>>       layer:= 'forest', 
>>       expression:= area(@geometry)
>>     )
>>   )
>> , 0)
>> 
>> But @geometry doesen't return the intersecting area. It returns the whole polygons, that partly intersect the parcel. So I could add an additional intersection():
>> 
>> coalesce( 
>>   array_sum( 
>>     overlay_intersects(
>>       layer:= 'forest', 
>>       expression:= area(
>>         intersection(
>>           @geometry, geometry(get_feature_by_id()
>>         )
>>       )
>>     )
>>   )
>> , 0)
>> 
>> @geometry is calculated in the context of the layer forests. How could I access the geometry of the parcel inside of overlay_intersects()? get_feature_by_id() doesn't seem to help, since I don't see a way to get the actual ID of the parcel. 
>> It is not possible to return @geometry as expression in overlay_intersects and do the intersection outside.
>> 
>> Does any one have an idea, how I could get the intersected area?
>> Thank you. Bruno
>>



More information about the QGIS-User mailing list