0

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

user3042984
  • 61
  • 1
  • 8
  • Could you share the point classes and indexes been used in the node class? as well which packages been used in the pom file? thanks! – USer22999299 Nov 21 '18 at 10:00

1 Answers1

1

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.

Craig Taverner
  • 759
  • 4
  • 5
  • Thank you for the answer. I wonder if there is a way to find the shortest wgs84 distance between a point and a polygon. greetings – user3042984 Mar 29 '14 at 15:54
  • The JTS class LocationIndexedLine can help here. We also implemented a utility method doing what you asked for at https://github.com/neo4j/spatial/blob/master/src/main/java/org/neo4j/gis/spatial/SpatialTopologyUtils.java#L102. I'll update the answer above for that too. – Craig Taverner Apr 07 '14 at 12:31