3

I am looking for a QGIS expression in Field Calculator that delivers the values "good", "medium" and "bad", depending on the percentage that the geometry of features in Layer 1 (red polygons) intersect with the geometry of feature-class A (green polygon) or feature-class B (yellow polygon) of Layer 2.

If the geometry of Layer 1 features intersects more than 50% with feature-class A geometry of Layer 2, it should be "good", if more then 50% with feature class B geometry of Layer 2 it should be "medium", else "bad".

enter image description here

I tried the following expression but it delivered "bad" for all features":

CASE 
WHEN area(intersection(geometry(get_feature('layer 2', 'feature-class', '1')),$geometry)) / $area > 0.5 
THEN  'good' 
WHEN area(intersection(geometry(get_feature('layer 2', 'feature-class', '2')),$geometry)) / $area > 0.5 
THEN  'medium'  
ELSE 'bad' 
END

Do you know what the right expression would be?

1 Answer 1

4

Solution below works only for QGIS 3.28 and later.


overlay_intersects() is the expression you want to use.

It allows you to filter for values of the intersecting layer, e.g. your feature classes. Furthermore, there's the option to set a minimum overlap for the expression to return true for the intersection.

I used the following expression to label the smaller polygons in the screenshot at the bottom:

CASE
WHEN overlay_intersects('other layer',filter:=("class" = 1),min_overlap:=($area/2))
THEN 'nice'
WHEN overlay_intersects('other layer',filter:=("class" = 2),min_overlap:=($area/2))
THEN 'mediocre'
ELSE 'not so good'
END

enter image description here

As said, the filter allows us to restrict the true values to features which have the attribute of interest. min_overlap provides an additional, geometry based filter, in this case half of the area of the current feature we're checking for intersection with the other layer.

8
  • Thanks! I used the expression: CASE WHEN overlay_intersects( 'Differenz_742e2289_6bb1_475a_b74b_2485539ba484', filter:=("distance" = 500),min_overlap:=(§area/2)) THEN 'Nahversorgung v.a. weniger als 500 m' WHEN overlay_intersects( 'Differenz_742e2289_6bb1_475a_b74b_2485539ba484', filter:=("distance" = 1000),min_overlap:=(§area/2)) THEN 'Nahversorgung v.a. innerhalb 500-1000 m' ELSE 'Nahversorgung v.a. weiter als 1000 m' END
    – Mat Thias
    Jun 1 at 12:23
  • But received following errors: Interpretationsfehler: Invalid QgsExpressionFunction::Parameter name 'min_overlap' for overlay_intersects syntax error, unexpected WHEN, expecting $end Invalid QgsExpressionFunction::Parameter name 'min_overlap' for overlay_intersects syntax error, unexpected ELSE, expecting $end syntax error, unexpected END, expecting $end (And I cannot find "min_overlap" in the expression list.)
    – Mat Thias
    Jun 1 at 12:41
  • 1
    It is $area, not §area @MatThias. Does the "distance" field exist, and does it contain the values you're looking for?
    – Erik
    Jun 1 at 12:53
  • oh, so sorry for the mistake, I put $area now. yes "distance" is a column in "other layer", it has the values '500,000000' and '100,000000'. (is there a way to chose fields and values of another layer in the expression bulider?) Still get following error: Interpretationsfehler: Invalid QgsExpressionFunction::Parameter name 'min_overlap' for overlay_intersects syntax error, unexpected WHEN, expecting $end Invalid QgsExpressionFunction::Parameter name 'min_overlap' for overlay_intersects syntax error, unexpected ELSE, expecting $end syntax error, unexpected END, expecting $end
    – Mat Thias
    Jun 1 at 13:19
  • 1
    Which QGIS are you working with?
    – Erik
    Jun 1 at 13:32

Your Answer

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

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