QGIS – Solve Problem with QGIS Expression Using overlay_nearest and Filter Condition

expressionqgisqgis-expression

There seems to be a strange behaviour using QGIS expressions overlay_nearest() function with filter condition. The problem can be reduced to this minimal reproducible example (Geopackage with random point layer; see the comment to this answer for the larger context):

enter image description here

The idea is the find all features with the same name, using overlay_nearest(). To get the values in the attribute field result, I used this expression:

array_to_string(
    with_variable (
        'nname',
        name,
    eval('
        overlay_nearest(
            @layer,
            name,
            limit:=11,
            filter:=name='''  || @nname  || ''')
    '))
)

It returns all features with the same name, including the current feature – so for id=4, it returns three times D as we have 3 feature with that value: 4, 5 and 7. This is the expected behaviour.

However, in the case of features with id= 0 or 1, it returns just one result. Playing around with the expression, it becomes clear that feature with id=0 only finds feature with id=1 and the other way round. So the first feature seems to be treated differently.

Why is that respectively: how to avoid that?

Best Answer

This seems to be a bug, I reported it accordingly.

A workaround mentioned there is to duplicate the layer and use this duplicated layer in the overlay_nearest() function as the bug seems to be limted to the case when refering to the very same layer.