I have some existing data in my neo4j database. I want to calculate distance from one node to another nodes based on longitude and latitude which are present with node. For that I want to create spatial Index on my existing data but I don't know how to create that index.
Asked
Active
Viewed 177 times
1 Answers
2
Are you using the spatial extension to create an OSMLayer? If not, and you just want the geodesic between to points, you can use the haversine
function.
http://neo4j.com/docs/stable/query-functions-mathematical.html#functions-haversin
CREATE (ber:City { lat: 52.5, lon: 13.4 }),(sm:City { lat: 37.5, lon: -122.3 })
RETURN 2 * 6371 * asin(sqrt(haversin(radians(sm.lat - ber.lat))+ cos(radians(sm.lat))*
cos(radians(ber.lat))* haversin(radians(sm.lon - ber.lon)))) AS dist

Brent Barbata
- 3,631
- 3
- 24
- 23
-
Thanks Brent !! your approch works for me but is it possible to implement this same thing via spatial index? – niraj darji Nov 19 '15 at 10:48
-
1Sure. You'll need to download the Spatial plugin, create a spatial index, and then add your nodes (the nodes with lat/lng properties) to the spatial index. This article should help get you started: http://www.lyonwj.com/mapping-the-worlds-airports-with-neo4j-spatial-and-openflights-part-1/ – Brent Barbata Nov 19 '15 at 12:29
-
1If you're having trouble installing the spatial extension, I recommend the Neo4j Cookbook. You can view what you need to get started here: https://books.google.com/books?id=WSmzCQAAQBAJ&pg=PA114&lpg=PA114&dq=install+neo4j+spatial+apt-get+install+maven&source=bl&ots=ZSjdLUUmzb&sig=pBXzUa_lbLE_kbQusJVKH8O3f5o&hl=en&sa=X&ved=0CCIQ6AEwAWoVChMIg6qPwb-cyQIVS-5jCh3psAkW#v=onepage&q=install%20neo4j%20spatial%20apt-get%20install%20maven&f=false – Brent Barbata Nov 19 '15 at 12:36
-
I have installed spatial plugin in my local neo4j. Now the thing is i'm not able to create index. I refer some documents as they suggest Some REST API to create spatial index.can you please give me some idea about that?Thanks in advanced !! – niraj darji Nov 19 '15 at 13:09
-
Did you have a chance to check out the first link I posted in the comments (http://www.lyonwj.com/mapping-the-worlds-airports-with-neo4j-spatial-and-openflights-part-1/)? If you scroll down to the section "Neo4j Spatial REST API", you'll see he wrote a python script that interacts with the REST API. The python script is on that page, but here's a raw version as well: https://gist.githubusercontent.com/johnymontana/d208de69249dbd947655/raw/51422c4df2fe796075c2cd4dad2e9c69600d1d84/openflightsneo.py – Brent Barbata Nov 19 '15 at 13:23
-
Yes I have checked [link](http://www.lyonwj.com/mapping-the-worlds-airports-with-neo4j-spatial-and-openflights-part-1/) And follow the **#add node to Spatial index** as i have some nodes with latitude and longitude properties. URL: http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addNodeToLayer Payload : { "layer": "geom", "node": "http://localhost:7474/db/data/node" } but at last end with getting error { message: "For input string: "node"" exception: "BadInputException" fullname: "org.neo4j.server.rest.repr.BadInputException" stackTrace: [16]..... } Any suggestion please – niraj darji Nov 20 '15 at 06:37
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95654/discussion-between-brent-barbata-and-niraj-darji). – Brent Barbata Nov 20 '15 at 06:59