6

I have a map with 'Layer A' (polygons representing cities) and 'Layer B' (lines representing roads).

I would like to add a virtual field on my 'Layer B' table with the name of every city crossed by each road.

enter image description here

In this example, I would like my Road 1 attribute to tell me it crosses 'City A', 'City B', 'City C', and 'City D'.

Is this possible with a QGIS expression, within a Virtual Field?

0

2 Answers 2

12

Try this expression:

array_to_string(overlay_intersects('Layer_A', name))

(assuming the City names are stored in name field in the 'Layer A').

1
  • Thank you, this works as expected !
    – Arthur
    Oct 1, 2021 at 7:25
5

Currently a bit "old-fashioned" approach using the aggregate() function:

aggregate(layer:='Layer_A',
          aggregate:='concatenate',
          concatenator:=',',
          expression:="name",
          filter:=intersects($geometry, geometry(@parent))
          )
2
  • What is the length of the road segments through these areas of cities?
    – Frodo
    Oct 7, 2021 at 11:55
  • 1
    A good question and I think it deserves a new question (perhaps it was already asked before). However, this solution may help you: expression:=to_string("id") || ':' || to_string(length(intersection($geometry, geometry(@parent)))) results in length for each feature separately, if sum required then aggregate:='sum' and expression:=length(intersection($geometry, geometry(@parent))).
    – Taras
    Oct 7, 2021 at 12:55

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.