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 fid
s 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?
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 |
fid
and if it matches, take the second array element instead of the first. Seems horribly hacky though.