2

Screenshot added

I have two polygon layers:

A) Contains around 500 river catchment areas

B) Contains 100 river catchment areas

I want to create a new data field in Catchment A showing which catchment (Catchment B) the catchment (Catchment A) belongs to.

I could do this manually by "Selection by location", but since there are a lot of rows, it would be nice to do this in the Field Calculator.

QGIS-version is 3.24.3 and I use the following expression:

overlay_intersects('Catchment B',"id",sort_by_intersection_size:='des')[0]

As you can see on the screendump, catchment 200 (Layer B) overlaps with catchment 3110,7330, 7320 and 7310 from Layer A. I want "200" added to a new field in the table of these fore catchments.

1

1 Answer 1

2

You can use overlay_intersects() function. The basic expression is:

overlay_intersects('LayerB', "id")[0]

Explanation: for each feature of LayerA, get the value of field "id" from LayerB of those features that intersect with the current feature. The result is an array of id values. With [0] you get the first element (array index, starting with 0 for first element).

Variant: If some of the features of LayerA intersect more than one feature of LayerB, you can get the largest overlapping area with this expression (argument sort_by_intersection_size only available in newer QGIS versions):

overlay_intersects('LayerB',"id",sort_by_intersection_size:='des')[0]

enter image description here

5
  • Thanks for answering. I tried to run the suggested line, replacing layer name and id, but unfortunately it only gives me a column of null values. Commented Nov 30, 2022 at 8:40
  • What QGIS version do you use? Can you post one or more screenshots showing your layers panel and the expression used? You can edit the initial question, post the screenshots there, copy the links to the images and insert the linkes here in the comments, deleting the images from the edited questions (or simply don't save the changes).
    – Babel
    Commented Nov 30, 2022 at 8:45
  • I've updated my original post and added a screenshot and a bit of explanation. Thank you again. Commented Nov 30, 2022 at 14:32
  • Do you have an attribute field called id on Catchment B layer? If not, it can't work. If so, it should work. Otherwise: can you share your project + data (layers) for testing?
    – Babel
    Commented Nov 30, 2022 at 14:42
  • Just tried again and it worked! Think I might messed up something in the expression, but now it works. Thank you again - and thank you too @Taras. Commented Nov 30, 2022 at 15:00

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.