8

When using overlay_nearest function in the QGIS expression builder, sometimes a feature considers itself as the nearest geometry, and other times not.

To demonstrate, I made a label expression:

CASE
    WHEN
        letter='A'
    THEN
        'fid: ' || "fid" || 
        '\nletter: ' || "letter" || 
        '\n\nnearest fid\'s: ' || array_to_string(
                                      overlay_nearest(layer:=@layer, 
                                          expression:=fid, 
                                          filter:=letter='A', 
                                          limit:=2
                                      )
                                  )
    ELSE
        NULL
END

I set the feature limit at 2 to show the 2 nearest fids for each feature to show that the true nearest feature is being found second.

The points outlined in red consider themselves their nearest neighbour, the others do not. It happens also with a projected coordinate system, and with various permutations of the WHEN clause.

Can anyone explain why this is?

enter image description here

Example dataset (CRS is WGS84) -- GeoPackage download link (with label expression pre-loaded)

WKT fid letter
POINT (27.7981358844261 -24.2696491141906) 1 A
POINT (27.7988424066944 -24.2678611535372) 2 A
POINT (27.7989643301811 -24.2688320385901) 3 B
POINT (27.8007927306901 -24.2685965478542) 4 B
POINT (27.7979157991226 -24.2678537822582) 5 B
POINT (27.7995823651664 -24.2670624651733) 6 A
POINT (27.7999025692956 -24.2692146208195) 7 B
POINT (27.8002376476489 -24.2696982965109) 9 A
POINT (27.7984109576877 -24.2694592018388) 10 A
POINT (27.8001260102486 -24.2667596896059) 11 B
2
  • 3
    the overlay_nearest function and others, respond badly if used with the same layer, here bug: github.com/qgis/QGIS/issues/47201
    – pigreco
    Commented Feb 17, 2022 at 14:46
  • 1
    Ah, thank you. I thought it was caused by the way I was filtering. If you want to make an answer, I can accept it. I was thinking a workaround could be to check the fid and if it matches, take the second array element instead of the first. Seems horribly hacky though.
    – Matt
    Commented Feb 17, 2022 at 14:52

2 Answers 2

6

The overlay_nearest function and others, respond badly if used with the same layer.

Here is a bug report: https://github.com/qgis/QGIS/issues/47201

1
  • Would it help to duplicate the layer and use the duplicated layer (in fact, the same data) for overlay_nearest or does that not improve performance?
    – Babel
    Commented May 23, 2023 at 19:22
3

So far I noticed that the overlay_nearest will result in an array, where the same feature will always be at the 0-index position.

Therefore, like in this thread Drawing 10 nearest lines from each point connecting to the centroid of the 5 points nearest to each point with QGIS expression, I am simply suggesting to delete it by its index i.e. 0.

However, in such a case, the number in limit should be incremented in one more feature.

array_remove_at(
    overlay_nearest(
        layer:=@layer,
        expression:=fid, 
        filter:=letter='A', 
        limit:=3 -- notice this change
        )
    0)
2
  • The problem was, at the time of posting the question, that the nearest feature was only sometimes itself, and sometimes not. I haven't tried to reproduce the problem recently, but if you're getting consistent results then perhaps there has been a bug fix.
    – Matt
    Commented Dec 20, 2023 at 19:32
  • I got you. Currently, my results are consistent. Perhaps, there was some bug or issue with data etc., it needs investigation
    – Taras
    Commented Dec 20, 2023 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.