13

I would like to have an example on how to use the Intersects function in the Field Calculator of QGIS.

Basically, I want to know which feature from a polylines layer intersects a polygons layer and store the result in an attribute. (0 = not intersecting, 1 = Intersecting).

I'm aware of the geoprocessing tools "Clip" or "Intersect", but is it possible in the Field calculator ?

2 Answers 2

8

This is an older question and the accepted answer was probably correct at the time, however I believe that with new versions of QGIS, (specifically 3.16) an introduction of the overlay_intersection (and overlay_contains/crosses/within/ etc) functions in the expression editor, can allow for spatial queries to be developed within the Field Calculator.

Another thread that talks about this is here:

Use field calculator to check for intersection

As an example, i developed the following which takes the intersecting geometry of the current record, with the target layer, and returns the attribute value from the 'field_site_no' field.

array_to_string(overlay_intersects( '<target_layer_name>', field_site_no))
7

This is not possible in field calculator. The intersect function in the field calculator does not work with two layers. What you can do is calculate intersections with specific geometries, e.g.

intersects( $geometry, geomFromWKT( 'LINESTRING(765145.88 6274561.22, 776031.67 6284189.52)' ) )

For more on these spatial operators check http://docs.qgis.org/latest/en/docs/user_manual/working_with_vector/expression.html#geometry-functions

Instead, use the Spatial query plugin to first select all features that intersect the polygon layer. Then use field calculator and only update selected features to set the intersection attribute to 1.

3
  • Dead link unfortunately... really looking for more documentation on how these operators work... Commented Nov 3, 2017 at 15:05
  • @DPSSpatial Field calculator has built-in documentation. What are you missing? I've also added the relevant user manual link
    – underdark
    Commented Nov 3, 2017 at 17:47
  • Thanks, Anita. I've never seen this, and likely because I don't use the field calculator, always looking in the expression, rules, atlas settings, etc... when you access these as Variables, they're hard to figure out what they expect in terms of inputs and outputs... I'll go through that doc, so thanks for that! Commented Nov 3, 2017 at 19:16

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.