I'm trying to make a filter inside an overlay_nearest
function where I must relate an attribute from the current layer to the overlay layer.
I'm trying to use with_variable
to store the outer layer attribute, but for a weird reason it's not working. Can someone shed some light on how to make it work?
This is the expression I'm using (where "ID"
is an integer):
with_variable(
'myId',
"ID",
overlay_nearest(
'Lines',
$geometry,
filter:="ID"=@myId
)
)
The expression above returns an array with 0 elements, but any of the expressions below work ok:
This shows that with_variable
seems to be working correctly as it returns the expected ID -> 80000.
with_variable('myId', "ID", @myId)
This shows that using the filter with a correct ID also works, as it returns 1 element in the array
with_variable(
'myId',
"ID",
overlay_nearest(
'Lines',
$geometry,
filter:="ID"=80000
)
)
So, if @myId
has the correct value (80000), and filtering with "ID"=80000
works as expected, why does filtering with "ID"=@myId
not work?
Best Answer
See my answer at: https://gis.stackexchange.com/a/477802/129409
Basically, it's a limitation in QGIS. There is a feature request open (https://github.com/qgis/QGIS/issues/43146), so it may become available at some point in the future.
In the meantime, the workaround is to wrap the
overlay_nearest
function in aneval
call.So rewriting your expression, you get: