What is the difference between the 'Spatial' built into Neo4j and the community spatial plugin?


(Craig Taverner) #1

I do not see people asking this exact question, and yet I see a lot of questions that this would answer. Specifically, people with point data and location-based search use cases trying to use the spatial plugin, or trying to use a combination of the plugin and the built-in features. So I wanted to try to clarify the differences between the two:

Neo4j 3.0 introduced the point and distance functions to Cypher, and Neo4j 3.4 enhanced this dramatically with the ability to store points as properties, and do index-backed searches using both range (and bounding box) as well as distance searches. If your use case involves point data (latitude and longitude, or just x and y), and you want to find data close to other points, or within specific regions, then it is best to use Neo4j 3.4. The support is built into the database through the entire stack. From the storage and indexing, through Cypher and even into the drivers and clients (browser and cypher-shell).

If you instead have complex GIS use cases, need to store complex geometries like Polygons and multi-polygons and need to import and perform GIS queries against the OpenStreetMap model, then the spatial plugin is the better choice. It has a much richer spatial feature set than Neo4j itself. However, keep in mind that this is a community maintained project, written back when Neo4j was primarily an embedded database. It was not designed for the high volume and high concurrency production environments that Neo4j is used for today. By all means, give it a try for your complex GIS case, but make sure you test if it is up to the challenge of your production use cases before relying on it too heavily.

So the short answer is, try Neo4j itself first. Only use the spatial plugin if you really need its richer capabilities.


#2

Hi Craig,

thank you very much for that explanation. It indeed helps. I have a very small example importing the cantons of Switzerland into a neo4j database with nothing inside. This already makes some of the Neo4J browser functionality and the of Bloom unusable. It seems to me that my polygons are stored as cleartext into the nodes and that this makes the Neo4j browser crash.

Could you please explain a bit more what you mean with:

[quote="craig.taverner, post:1, topic:159"]
, or within specific regions,
[/quote]?

If I have the Swiss cantons and want to identify power plants inside these cantons along with other smaller areas (e.g. municipalities, owners of electricity distribution grids) and other points (e.g. CO2 sources, consumers, clusters of buildings) for clustering, aggregation or other analyses. How would you implement such geospatial information in Neo4j? My goal is to build up a knowledge graph which later on should be the input of different machine learning algorithms.

Thanks in advance for your input.


Sinan Teske - Team Lead & Researcher at the Research Institute Empa in Switzerland
(Craig Taverner) #3

Hi Sinan,

Regarding your question about what I meant about within specific regions, in Neo4j 3.4 there are only two relevant search queries: distance and bounding box (MBR). Search within polygon is not (yet) supported, so you would need to calculate the bounding box of the rectangle and then call the range search with that, followed by filtering out anything outside your rectangle. For more information on spatial range queries, see the docs at https://neo4j.com/docs/developer-manual/current/cypher/schema/index/#schema-index-spatial-bounding-box-searches-single-property-index

I have also done some simple work on procedures for creating and filtering using polygons in a separate community library. This is very alpha work, but might make your life easier if you want to search for points in polygons. See https://github.com/craigtaverner/spatial-3d. The goal of that library is to provide point in 3D polygon mesh searches, but as preparatory work I developed point in 2D polygon already. See the example on this line: https://github.com/craigtaverner/spatial-3d/blob/master/neo4j/src/test/java/org/amanzi/spatial/neo4j/UserDefinedFunctionsTest.java#L108

Since that is alpha code we have not made a release yet and you would need to build it yourself.

Back to your original comment about importing cantons. Did you import them as plain text into Neo4j, or did you use the 'Neo4j Spatial' plugin to import them? I suspect the browser is struggling with returning large data and storing it in local JSON memory. I'm not sure if it will treat binary more carefully than strings, and if so you could try storing as WKB instead of WKT.

Regards, Craig