On Fri, 25 Jun 2021 08:32:56 -0700 (PDT), Totò Fiandaca wrote:
> result:
> KNN2 returns 1911 rows
> spatialIndex 1937
>
> what did I not understand about the new KNN2?
>
Hi Toto',
there is a subtle problem of undestanding well the intended
scope of KNN/KNN2
as the name itself says, any KNN algorithm is intended to
identify the "nearest neighbors" to a given point.
the old KNN searched all the brances of the tree until
the requested number of nearest points was found.
KNN2 has introduced a "distance radius" in order to speed
up the execution time, but this is just a dirty trick for
passing to R*Tree queries a bounding box (aka MBR) where
the nearest points maust be searched.
this doesn't implies at all that points exceeding the
distance radius will be discarded; if they fall within
the given BBOX they'll be included into the returned
resultset.
if your specific logic requires to filter a maximum distance
this is not a KNN problem, it's a problem of the SQL query
that you are writing around the KNN core.
countercheck:
SELECT a.fid as fid, a.distance_crs as distance,
zz.chiave_uni as chiave_uni,zz.geometry
FROM scuole as zz
JOIN knn2 as a
WHERE f_table_name = 'route'
AND f_geometry_column = 'geometry'
AND ref_geometry = zz.geometry
AND radius = 100.0 and max_items = 1
ORDER BY a.distance_crs DESC;
this is exactly your initial query (the one returning
1911 rows), but this time we've requested to order
the rows by decrearing distance.
as you can easily check there ara many rows
exceeding the 100.0 metres radius.
SELECT a.fid as fid, a.distance_crs as distance,
zz.chiave_uni as chiave_uni,zz.geometry
FROM scuole as zz
JOIN knn2 as a
WHERE f_table_name = 'route'
AND f_geometry_column = 'geometry'
AND ref_geometry = zz.geometry
AND radius = 100.0 and max_items = 1
GROUP by zz.chiave_uni
HAVING distance <= 100.0;
this further rewrite of your initial query
finally returns 1,737 rows.
exactly as the one you've written by directly+
accessing the Spatial Index.
quick conclusione: there is nothing wrong in KNN2,
that exactely does what is expected to do.
your assumption that the KNN2 module will automatically
discard any match exceeding the distance radius was
not justified.
bye Sandro