spatial.withinDistance returning geoms further away than stated distance

The following query is returning many results much further than 0.01km from the point... Is there any reason why? I initially though it was returning the centroid of the geometry and some of a polygon may still be within 0.01km but when plotting the returned data that is not the case

CALL spatial.withinDistance('layer', {latitude: 54.904099672863794, longitude: -1.3702582511780292}, 0.01) yield node, distance return id(node), distance

I traced the code, and found the following line at spatial/ at master · neo4j-contrib/spatial · GitHub

// TODO check Geometry is a point? use Centroid?
Coordinate point = flow.getGeometry().getCoordinate();

It looks like this code will work only with Point geometries. The method getCoordinate() is defined in the JTS library at Geometry (org.locationtech.jts:jts-core 1.19.0 API) and claims the following:

Returns a vertex of this Geometry (usually, but not necessarily, the first one)

I would recommend you open this as a bug report in the issues of the github project at Issues · neo4j-contrib/spatial · GitHub

If the bug-fix is urgent, you could also submit a PR to fix it, replacing the call to getCoordinate() with something more sensible, like perhaps getCentroid().getCoordinate(). To get the PR accepted, you need to write tests and sign the CLA.

I thought more about this and noticed that you saw the option of using the centroid as a problem too. So I've made a new bug-fix and released version 0.25.7 of the Neo4j Spatial library built against Neo4j 3.4.9 (but should work on all Neo4j 3.4.x servers). This version makes two key changes to the withinDistance function relevant to your use case:

  • When looking at distances to non-Point geometries, it will use the JTS DistanceOps functions to find the nearest point of the complex geometry and calculate the distance to that instead
  • Previously a filter was only applied to remove results if the layer was a point-only layer, and this has been changed to filter out distances further than the threshold for all geometry types

Please can you try the new release at Release Spatial 0.25.7-neo4j-3.4.9 · neo4j-contrib/spatial · GitHub and let us know if this works for your use case.

1 Like

I will get on this today, thank you!

Iam also having problems.
The query is:

CALL spatial.withinDistance('hikes',{lat:11.515159369999996, lon:64.4836848995831}, 1000000)
yield node, distance return id(node), distance

and the results are weird.

id(node)	distance
190	7142.979887242287
125	7143.036407397392
42	7143.45712742689
140	7143.498292525642
63	7143.518485761018
79	7143.518485761018
161	7143.518485761018
142	7143.5205202181405
146	7143.5439201608415
58	7143.551335359225
65	7143.574107399573
98	7143.618875394959
144	7143.618875394961

No matter the distance all of them are in this weird range.

A am using neo 3.4.9 and the version of spatial plugin you mentioned.