neo4j spatial wgs84 distance

321 views Asked by At

I'm using this code to find nearest nabours to an given point in the wgs84 format(the variable co)

for(Node n : geomPoints){
    ExecutionResult result = engine.execute("START a=node(" + n.getId() + ") "+ 
                                                    "MATCH a-[:FIRST_NODE]->()-[:NEXT*0..]->()-[:NODE]->b "+
                                                    "RETURN b");

    Iterator<Node> nodes = result.columnAs("b");
    while(nodes.hasNext()){
        Node nodesInGeom = nodes.next();
        Float lon = Float.parseFloat(nodesInGeom.getProperty("lon").toString());
        Float lat = Float.parseFloat(nodesInGeom.getProperty("lat").toString());
        Coordinate coo = new Coordinate(lon,lat);
        if(co.distance(coo)<distance) {
            distance = co.distance(coo);
            nearestNabour = nodesInGeom;
        }
    }

So the important part of this code is this part

co.distance(coo)

because I was told that this function uses the euclidian distance and not the distance, that I need fpr the wgs84 format

So my question is: Is there a function in neo4j satial, that returns the right distance??

Thanks alot

1

There are 1 answers

2
Craig Taverner On BEST ANSWER

I would suggest using the org.geotools.referencing.datum.DefaultEllipsoid.WGS84.orthodromicDistance() method. See, for example, it's use in the OSMImporter at https://github.com/neo4j/spatial/blob/master/src/main/java/org/neo4j/gis/spatial/osm/OSMImporter.java#L2158

And in response to the secondary question about finding the closest Geometry to the point, take a look at the utility method findClosestEdges at https://github.com/neo4j/spatial/blob/master/src/main/java/org/neo4j/gis/spatial/SpatialTopologyUtils.java#L102. This will return a collection of PointResult, containing the closest Point and the Geometry it belongs to. If you review the code you'll see that it uses JTS class LocationIndexedLine to calculate the closest part of an edge.