1

In QGIS 3.16, I have a layer of polygons corresponding to particular ecological habitat zones. I'd like to perform a series of intersections in order to add information to each polygon as to where it is located, for example: what town it is in, what parcel, what catchment/drainage basin, what protected area (and several more).

The issue is that of course, many of my habitat polygons cross administrative and hydrological borders. So if I intersect my layer1 with a layer containing town borders, the resulting intersection layer will duplicate each cross-border polygon, creating a separate entry for each town that the habitat polygon overlaps.

I'd like to avoid this duplication, or at least come up with a post-intersection trick to collapse the duplicates. What I would want, ideally, would be a way to concatenate multiple towns (or basins, or protected areas) in the same field. For example:

  • [Habitat name]-[Town]
  • Pond1 - Arlington, Lexington
  • Bog1 - Lincoln
  • Pond2 - Lincoln, Lexington

I've not found this trick yet.

1
  • here is a similar question with a solution using a virtual layer
    – JGH
    Commented May 5, 2021 at 17:46

1 Answer 1

2

You can use QGIS expressions with the function overlay_intersects( ), available since QGIS 3.16. Set the field type to string as the output will be formatted as text:

array_to_string (
    overlay_intersects( 
        'polygon2', 
        $id
    )
)

Screenshot: on the blue polygon1, create a new attribute, refering to the intersecting orange polygon2 features. As you see in the preview of the expression editor at the bottom, the output will be 1,2,3, thus the id of the three orange polygons the blue one intersects: enter image description here

5
  • Thank you Babel. This is working to give me the IDs of the relevant polygons -- but I'm afraid I'm not sure how to specify the field I want to visualize (Town Name, etc). When I attempt to specify a field, I am only given the options for the fields in the current layer, and not another layer. I am sure there is an SQL way to specify layer-->field... do you know how this is accomplished?
    – pete
    Commented May 6, 2021 at 12:47
  • Replace $id with the name of the field, like "town_name" - or even without quotes: town_name. This will return a list of the names contained in that field. Be sure to make the new field you create this way long enough so that names are not cut off.
    – Babel
    Commented May 6, 2021 at 13:17
  • Excellent! I had tried it before but after the $ sign, naturally with no luck... Is there a way to add a space after the comma between names? Right now it shows up as (for example) "Lincoln,Lexington" and I would prefer "Lincoln, Lexington".
    – pete
    Commented May 6, 2021 at 15:18
  • Just replace the comma by a comma+space by adding to the expression a replace function: replace( [array], ',', ', ' ) - instead of [array], insert the whole expression from above. You could also change the field you already created: replace( fieldname, ',', ', ' ) and change fieldname to the name of your field.
    – Babel
    Commented May 6, 2021 at 15:35
  • Apologies for the late reply -- this was exactly what I needed, Babel. Thanks very much for the helpful and clear explanation.
    – pete
    Commented May 11, 2021 at 11:18

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.