I have a line layer and a polygon layer. I'm trying to create an expression to fetch a polygon attribute and add it to the line, based on in which polygon the line ends.
array_last(overlay_intersects('ok_ak_riks', kommunkod))
This isn't always working. How can I modify it? I've been thinking of if I can use end_point($geometry)
somehow?
It is working for line 1 and 2, but not 3 (3 is one line, although the symbology is two arrows) end_polygon
should be 2505:
Best Answer
You can use
aggregate()
with anintersects()
filter:Previous / alternative answer:
Explanation: you can first get all intersecting features as array by using
overlay_intersects('ok_ak_riks',$currentfeature)
, then iterate over this array by usingarray_foreach()
and filter to the intersecting ones with your endpoint by testing if the current one intersects with your endpointif(intersects(geometry(@element),end_point($geometry))
. This will return the feature ofok_ak_riks
intersecting with the lines end. At this point you will get an array like['','your attribute','','']
where you never know at which position your value is. So you can usearray_filter(array,@element <> '')
to remove all empty values.