spatial icon indicating copy to clipboard operation
spatial copied to clipboard

Find area of intersection of two polygons using Cypher query

Open TomMRiddleJr opened this issue 4 years ago • 4 comments

I have this use case where: -

  • I have a list of destination polygons and a source polygon (destination and source are just used to denote polygons).
  • What I want to do is find area of intersection of the source polygon with each of the destination polygons
  • Then select the one with the maximum area of intersection.

Polygon here is list of Points, of course.

For instance, in the image shown below: -

  • I have destination polygons D1 to D4
  • Source polygon -> S
  • Return D4 since it has max area of intersection with S

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

TomMRiddleJr avatar Aug 24 '21 14:08 TomMRiddleJr

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:

  • spatial.algo.intersection as defined at https://github.com/neo4j-contrib/spatial-algorithms/blob/master/neo4j/src/main/java/org/neo4j/spatial/neo4j/UserDefinedFunctions.java#L403
  • spatial.algo.intersection.sweepline as defined at https://github.com/neo4j-contrib/spatial-algorithms/blob/master/neo4j/src/main/java/org/neo4j/spatial/neo4j/UserDefinedFunctions.java#L419

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:

  • spatial.intersects at https://github.com/neo4j-contrib/spatial/blob/master/src/main/java/org/neo4j/gis/spatial/procedures/SpatialProcedures.java#L856

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 avatar Aug 24 '21 15:08 craigtaverner

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.

craigtaverner avatar Aug 24 '21 15:08 craigtaverner

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

TomMRiddleJr avatar Aug 25 '21 08:08 TomMRiddleJr

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.

craigtaverner avatar Aug 25 '21 15:08 craigtaverner