Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TomMRiddleJr opened this issue Aug 24, 2021 · 4 comments
Open

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

TomMRiddleJr opened this issue Aug 24, 2021 · 4 comments

Comments

@TomMRiddleJr
Copy link

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

@craigtaverner
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants