neo4j-contrib / spatial

Neo4j Spatial is a library of utilities for Neo4j that faciliates the enabling of spatial operations on data. In particular you can add spatial indexes to already located data, and perform spatial operations on the data like searching for data within specified regions or within a specified distance of a point of interest. In addition classes are provided to expose the data to geotools and thereby to geotools enabled applications like geoserver and uDig.
http://neo4j-contrib.github.io/spatial
Other
781 stars 192 forks source link

Find area of intersection of two polygons using Cypher query #386

Open TomMRiddleJr opened 3 years ago

TomMRiddleJr commented 3 years ago

I have this use case where: -

Polygon here is list of Points, of course.

For instance, in the image shown below: -

IMG-0033

I could not find an example to calculate area of intersection, let alone iterating and finding the maximum. Could you please help me with this problem statement? @craigtaverner

craigtaverner commented 3 years ago

This kind of capability certainly exists inside this library, but is not exposed in the procedures, and therefor you would have to write a procedure for this. On the other hand, you mention that you are dealing with arrays of Neo4j point objects, which would make this quite suitable to the newer library at https://github.com/neo4j-contrib/spatial-algorithms. That library is more focused on just the basic algorithms, including graph intersection. However, I do remember it does have some bugs in graph intersection.

There are already two user-defined functions in that library that sound appropriate:

I've not tried those functions myself. Hopefully they work for you.

If you specifically want to use the spatial library, then you will need to write a procedure that does this. There already exists one that is close:

This currently returns a list of geometries that intersect the geometry provided. You could, for example, copy it and enhance the copy to return a list ordered by overlap area. The underlying library uses a concept called a GeoPipeline which allows chaining of operators, so you would need one operator to convert geometries to pairs of geometry and overlap area, and then another to sort that.

craigtaverner commented 3 years ago

Actually, you would not need to write the sorting, as you could do this in Cypher afterwards:

  MATCH (n:Geometry) WHERE n.id = $selected
  CALL spatial.intersects.area('mylayer',n.geom) YIELD area, node, geometry
  RETURN area, geometry ORDER BY area DESC LIMIT 1

Of course, you still need to write the procedure spatial.interests.area, but that is probably quite similar to the existing spatial.interests function.

TomMRiddleJr commented 3 years ago

Thanks for your reply. I was wondering if I could deploy our user defined procedure in a managed Neo4j service like Aura? Please let us know! @craigtaverner

craigtaverner commented 3 years ago

Aura currently does not support custom plugins, but I presume that might be a feature sometime in the future. I believe some of our enterprise customers have custom plugins, but that was always done on a case-by-case basis, and is not part of the standard service.

In fact, none of the spatial plugins are currently available for Aura at all. APOC, on the other hand, is available on all Aura instances. I once considered adding the spatial-algorithms library to APOC, but it requires more work for that.