Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top