spatial.withinDistance returning geoms further away than stated distance


(Shaun Brown) #1

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

(Craig Taverner) #2

I traced the code, and found the following line at

// 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 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

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.

(Craig Taverner) #3

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 and let us know if this works for your use case.

(Shaun Brown) #4

I will get on this today, thank you!