Update to: Adding node to Neo4j spatial index index

I hope someone can provide some updated clarifications on adding nodes to Spatial. The best instructions I can find are:

Neo4j Spatial 'WithinDistance' Cypher request returns empty while REST call returns data

However, it is almost 2 years old and has some conflicting information with a recognized error ( https://github.com/neo4j-contrib/spatial/issues/106 ), which is still open.

I also found this tutorial:

http://mattbanderson.com/setting-up-the-neo4j-spatial-extension/

That says, we should add node to the layer AND insert Neo4j ID # in node as a property, but DO NOT insert node in the geometry index.

My main priority here is that I can request through Cypher (in the browser), but in the end we want to request REST as well. Therefore, ideally, I would like to insert nodes in such a way that we can do both.

So my questions are:

1) What are the right steps here to allow requests through REST and Cypher?

2) If I call / addSimplePointLayer and then / index / node to add a Spatial index (both via cURL or REST), can I use LOAD CSV to insert nodes and be able to query Spatial Plugin through both REST and Cypher?

3) If I use REST to insert my nodes, what calls (and in what order) will I need to make so that I can request via REST and Cypher (web browser)?

Thank you, I look forward to all of this coming out!

+6
source share
1 answer

question 1

First you need to initialize the layer and create a spatial index using REST calls:

POST /db/data/ext/SpatialPlugin/graphdb/addSimplePointLayer HTTP/1.1 Host: localhost:7474 Accept: application/json Content-Type: application/json Cache-Control: no-cache { "layer" : "geom", "lat" : "lat", "lon" : "lon" } 

and

 POST /db/data/index/node/ HTTP/1.1 Host: localhost:7474 Accept: application/json Content-Type: application/json Cache-Control: no-cache { "name" : "geom", "config" : { "provider" : "spatial", "geometry_type" : "point", "lat" : "lat", "lon" : "lon" } } 

Create a node with lon / lat properties using Cypher via a transactional endpoint:

 POST /db/data/transaction/commit HTTP/1.1 Host: localhost:7474 Accept: application/json Content-Type: application/json Cache-Control: no-cache { "statements" : [ { "statement" : "create (city:City {data}) return id(city)", "parameters" : { "data" : { "name" : "MyTown", "lon": 15.2, "lat": 60.1 } } } ] } 

and add it to the spatial index - be sure to accept the node identifier with the node identifier returned from the previous query:

 POST /db/data/ext/SpatialPlugin/graphdb/addNodeToLayer HTTP/1.1 Host: localhost:7474 Accept: application/json Content-Type: application/json Cache-Control: no-cache { "layer": "geom", "node": "http://localhost:7474/db/data/node/<my_nodeid_goes_here>" } 

To enable Spatial play with Cypher, you need to hack: each geo-indexed node must have a property with the name id with the value id node. This can be done using a simple cypher statement (the example below does this for all nodes in the city):

 POST /db/data/transaction/commit HTTP/1.1 Host: localhost:7474 Accept: application/json Content-Type: application/json Cache-Control: no-cache { "statements" : [ { "statement" : "match (n:City) set n.id=id(n)" } ] } 

Using this place, you can use Cypher for geoinformation, for example:

 POST /db/data/transaction/commit HTTP/1.1 Host: localhost:7474 Accept: application/json Content-Type: application/json Cache-Control: no-cache { "statements" : [ { "statement" : "start city = node:geom('withinDistance:[60.1,15.2, 100.0]') return city", "parameters" : {} } ] } 

NB: for withinDistance you need to specify lat , lon , distanceInKm .

question 2

As described above, you need to have a separate REST call to add the node to the spatial index. This is currently not possible for Cypher. As a workaround, use LOAD CSV to create nodes with lon / lat properties. At the preprocessing stage, execute a statement of the type MATCH (n:City) where not has(n.id) set n.id = id(n) return id(n) as id . This sets the id properties (the hack described above) and returns a list of identifiers for the new nodes. For each of them, we emit a REST call to add node to the geo-index.

question 3

The answer was already given above :-)

+12
source

Source: https://habr.com/ru/post/984922/


All Articles