In QGIS 3.18, I have two point layers: layer_1 with attribute id_1 and layer_2 with id_2. I want to use the overlay_nearest function: overlay_nearest(layer[,expression][,filter][,limit=1][,max_distance][,cache=false])
Applied on layer_1, I want to get for each point on layer_1 all points on layer_2 where id_2 has a different value from id_1 (to connect them by a line). However, I can't find out how the syntax of the filter-argument works, how to refer to attribute values of two different layers for comparison.
The basic expression is: overlay_nearest( 'layer_2', $geometry, [filter])
How is the syntax of the [filter] part? filter:="id1" <> "id2" obviously doesn't work. $id <> "id2" and $id <> "id1" don't work either.
Edit: see this example here to make the idea clearer.
- Red dots =
layer1, labeled withid1 - White dots =
layer2, labeled withid2
Each red point should be connected to it's nearest white point, except when their id is the same. However, on the upper left (red arrows), you see that 12 is connected to 12, 17 to 17:
The expression used in geometry generator on layer1 for this is as follows (the filter condition is on lines 6 to 18):
collect_geometries (
array_foreach (
overlay_nearest(
'layer2',
$geometry,
filter:=attribute (
get_feature_by_id (
'layer1',
$id
),
'id1'
) <> attribute (
get_feature_by_id (
'layer2',
$id
),
'id2'
) ,
limit:=1
),
make_line (
$geometry,
@element
)
)
)
*Edit
See more here in the responses to the feature request, especially:
The purpose of the filter parameter is to reduce the dataset (layer2) to use for research, so it's applied before it proceeds to any overlay test


filter:=eval('"id2" <>' || "id1"')(just an idea, not tested) – J. Monticolo Mar 29 '21 at 13:36filter:=eval("id_2" || '<>' || "id_1")nor if both terms in the brackets are in single quotes'does work. At least the syntax doesn't produce an error, but returns an empty result, even if there are corresponding features. I guess the problem is somehow to tell QGIS which attribute comes from which layer. – Babel Mar 29 '21 at 14:01filter:=attribute (get_feature_by_id ('layer_1', $id), 'id_1') <> "id_2"seems to work - have to check it, will come back later. – Babel Mar 29 '21 at 14:13