Using polygon areas in Bounding Box Query

Hi all,

Currently I have a lot of positional data (include lat and long) representing millions of nodes.

Original plan was to use bounding box query to put those nodes from above into clusters/region.

Spatial searches

I realized there are only 2 options, a)spatial bounding box search and b)spatial distance searches.

There is no option for polygons which I have available from postgreSQL.

Is there a way to use polygon shapes/areas/length to do spatial grouping of nodes into regions? Any suggestions will be appreciated.

You can get the same effect of an index-backed within-polygon search by first converting the polygon to a bounding box, searching and then filtering by the original polygon. This requires two additional functions:

  • Convert polygon to bounding box
  • Filter results by polygon

I discussed this approach in the GraphConnect 2018 presentation. I covered it in a little more detail in a followup meeting, which you can see in the video at All About Neo4j Spatial (Neo4j Online Meetup #44) - YouTube.

In that video I had written the two functions in the namespace amanzi.*, however, since then we have created a newer library called spatial-algorithms that has two functions:

  • spatial.boundingBox
  • spatial.algo.withinPolygon

You can find this library at GitHub - neo4j-contrib/spatial-algorithms: Spatial algorithms for both cartesian and geographic data

1 Like

Hi @craig.taverner,

Thanks for pointing me in the right direction.

Now, I am creating relationships between nodes: Position and nodes:Region using the bounding box method.

match (r:region {region_name: '12 Ulu'}
match (p:position {deviceid: 2284} where point.withinBBox(p.positions_point, r.first_coor, r.last_coor)
call apoc.create.relationship(p, "IN")

r.first_coor, r.last_coor and p.positions_point all have the format point({srid:4326,x:104,y:2.97})

The code snippet has provided some errors, so do correct me if I am wrong.

My question is how to use the spatial.boundingBox or spatial.algo.withinPolygon to incorporate into the above query.

Any suggestions are welcomed.

Are r.first_coor and r.last_coor really the bottom left and top right of the polygon? The names make me think they are just the first and last coordinates of the polygon.

Do you perhaps have a list of coordinates for the entire polygon, because that is what you really need. For example, I could imagine you have a r.coordinates which is an array of point instances. Then you could do the following:

MATCH (r:region {region_name: '12 Ulu'}
WITH spatial.boundingBox(r.coordinates) as bbox
MATCH (p:position {deviceid: 2284} where point.withinBBox(p.positions_point, bbox.min, bbox.max)
CREATE (p)-[:IN]->(r)

I do not know why you wanted to use APOC to create relationships, so I replaced that with normal Cypher.

Yes r.first_coor and r.last_coor represent the min and max points of the polygon.

Currently I have polygon data type, which is long list of coordinates.

I have also looked into the github link and checked all the latest versions, using the link below.

There is no version for neo4j v4.4 which is what I am currently using.

I have installed the latest plugin spatial-algorithms-algo-0.2.5-neo4j-4.2.11.jar but when I try to find the spatial.algo.withinPolygon within the neo4j browser, it is unable to find it.

call dbms.procedures()
yield name where name starts with 'spatial.algo.withinPolygon'
return name

Is there a version of spatial algorithms meant for neo4j 4.4?

I took a look at this over the weekend, and there is no spatial-algorithms release for Neo4j 4.3 or Neo4j 4.4. So I did a test port to 4.3, but there are some changes in the kernel relating to cursor closing that has prevented completion of this port. I'll see if I can get some support to fix that, and then port to 4.4 as well later.

The other option you have is to try extracting just the withinPolygon function or try use the older version from the spatial3d library.

1 Like

Hi @craig.taverner ,

I appreciate that you looked into my issue regarding the spatial-algorithms.

Personally, I think I will wait for more news on the fix. I honestly have no idea how to extract the withinPolygon and since my organization still plans on using the latest Neo4j 4.4, I do not think trying the older version of spatial3d library would be of any use either.

Lastly, I was hoping if there any additional resources on how to use withinPolygon and other use cases for it, utilizing Python drivers to display onto Neomap or into OpenStreetMap. Other than the sources listed below:

Neo4j Docs

Any suggestions are welcome,

I have managed to port this library to Neo4j 4.4. See the release at Release Spatial Algorithms 0.2.5-neo4j-4.4.3 · neo4j-contrib/spatial-algorithms · GitHub

The main challenge was the preceding port to Neo4j 4.3, which involved some bigger algorithm changes to cope with a change in behaviour in the Neo4j kernel. But once that was done, porting to Neo4j 4.4 was trivial.

Regarding documentation of withinPolyon, there is only the test code at spatial-algorithms/ at master · neo4j-contrib/spatial-algorithms · GitHub