6

I have two layers, a point layer and a polygon layer ('Sektorplan'). The polygon layer attribute table has a "Sektor" column ('1','2', ...).

The Goal

I want to add a new column ("Sector") to the attribute table of the point layer to indicate in which sector the point is located.

Tried so far

I used overlay_within('Sektorplan', Sektor) in the field calculator of the point layer.

The Problem

The resulting column only shows NULL. Using a Virtual Field or not does not make any difference. The preview of the field calculator shows up correct (['1']).

Pictures say more than a thousand words

The table of the polygon layer

field calculator

resulting point layer table

Anyone knows what causes this? My search-fu failed me.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Oct 6, 2021 at 11:37
2
  • 3
    If you read the QGIS docs, where it says: "Returns whether the current feature is spatially within at least one feature from a target layer, or an array of expression-based results for the features in the target layer that contain the current feature.", so using of the array_to_string() function additionally is required i.e. array_to_string(overlay_within('Sektorplan', Sektor)) Commented Oct 6, 2021 at 11:49
  • 1
    Yes, I overlooked the array part. Thought as there's only one other layer .... It's always those details. Commented Oct 6, 2021 at 12:05

1 Answer 1

11

overlay_within returns Sektor value of the related polygon feature(s) as a list/array (even if it returns one polygon). You should convert to string.

Set Output field type to Text and use this expression:

array_to_string(overlay_within('Sektorplan', Sektor))

enter image description here

answered Oct 6, 2021 at 11:48
2
  • 1
    Awesome! Thanks so much! Clearly did not understand that it ALWAYS returns an array. Commented Oct 6, 2021 at 12:03
  • 2
    It always returns an array since a point may be within multiple polygons., unlike your case. Commented Oct 6, 2021 at 12:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.