qgis – Expression to Fetch Polygon Value Intersecting End of Line

intersectionlinepolygonqgisqgsexpression

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:

enter image description here

Best Answer

You can use aggregate() with an intersects() filter:

aggregate(
'ok_ak_riks',
'concatenate',
"kommunkod",
filter:=intersects($geometry,end_point(geometry(@parent))),
concatenator:=','
)

Previous / alternative answer:

array_to_string(
    array_filter( 
        array_foreach( -- 2. iterate over ALL intersecting features
            overlay_intersects('ok_ak_riks',$currentfeature), -- 1. get ALL intersecting features
            if(
                intersects(geometry(@element),end_point($geometry)), -- 3. check if the current one intersects with the lines endpoint
                attribute(@element,'kommunkod'), -- 4a. if it does, return the attribute
                '' -- 4b. otherwise return an empty string
            )
        ),
    @element <> '') -- 5. filter out the empty strings from the array
)

Explanation: you can first get all intersecting features as array by using overlay_intersects('ok_ak_riks',$currentfeature), then iterate over this array by using array_foreach() and filter to the intersecting ones with your endpoint by testing if the current one intersects with your endpoint if(intersects(geometry(@element),end_point($geometry)). This will return the feature of ok_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 use array_filter(array,@element <> '') to remove all empty values.

Related Question