5

Ultimate goal

I plan to retrieve attributes from layers using the field calculator where they intersect with a geometry.

Current step

Checking whether the current $geometry intersects with the layer of interest.

What I tried

intersects($geometry,collect_geometries('Layer')) - and variations of map_get. Since I just learned that intersects has to be handed known geometries, while I want to check IF there is a geometry on the layer, I actually am at a loss. I simply don't know how to check for intersections using the field calculator.

If this is not possible using the field calculator, then maybe Python?

0

1 Answer 1

6

Let's presume you have a layer with polygons that contains an attribute "name": in the screenshot below, this name field has the values A, B, C, D, E and F (polygons are labeled).

We further have a lines layer and we want to get the names of the polygons the line intersects. Since QGIS 3.16 (see visual changelog; for older QGIS versions maybe the RefFunctions Plugin referenced there could help), there are new overlay-expressions. We can use one of them to label the line with the names of the polygons it intersects. In the same way, the expression can be used in the field calculator to generate new fields (even virtual fields for dynamic calculation):

array_to_string ( 
   overlay_intersects ( 
      'Polygons', 
      name
    ) 
)

enter image description here

6
  • Nice one! I guess this is not possible using 3.10?
    – Erik
    Commented Dec 3, 2020 at 16:06
  • No, new to 3.16 - definitely a reason to change to 3.16, at least for this one and continue work back in 3.10
    – Babel
    Commented Dec 3, 2020 at 16:07
  • Since I plan to use this in my everyday workflow, I'll request IT to update my software.
    – Erik
    Commented Dec 3, 2020 at 16:08
  • 1
    at least a nice reason to convince them to update! Added a link to the visual changelog explaining the new expressions in the answer
    – Babel
    Commented Dec 3, 2020 at 16:09
  • See however the refFunctions Plugin for older version (referenced in the visual changelog): plugins.qgis.org/plugins/refFunctions
    – Babel
    Commented Dec 3, 2020 at 16:12

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.