Neo4j – H3 – Intersecting Polygons

Geospatial use cases with Neo4j are interesting. A colleague recently asked if there was a way to identify which pieces of property a power line might intersect. Thinking back to previous work on H3 and Neo4j, I knew we could model the power line as a set of hex addresses and model the property geometry as a set of hex addresses.

First we needed to find some data. Fortunately, there is a dataset of US Electric Power Transmission Lines from ESRI. I also was able to use a dataset of US County data that I had previously accessed.

The Power Transmission Line data was in JSON format but the GeoJson information was in a MultiLineString. A couple of months ago, I thought I had the conversion from MultiLineString to H3 done in my Neo4j H3 plugin, but alas, I had messed it up royally. However, a great StackOverflow post with insight from Nick Rabinowitz got me headed in the right direction.

Basically, I took each pair of lat/lon and found their midpoint. Then I found the list of H3 addresses at resolution 12 along that line using the H3 GridPathCells feature. Each resulting H3 address was linked to the Power Transmission Line. Our Neo4j model. looks like this:

We have 93,000 Powerline nodes and 50.7M HexAddress12 nodes.

Our State -> County -> Hex Address 10 Knowledge Graph looks like this:


So now that we have the data in Neo4j, let’s ask some questions:

In order to answer that question, we run the following query:

CALL {
    USE geodemo.powerlines
    match (p:Powerline)-[:CONTAINS_HEXADDRESS12]->(h12) 
    where p.gid = '{D9CEA096-794A-498B-BDED-BD54CE9EAF24}'
    WITH distinct (com.neo4jh3.toparent(h12.hexAddress12,10)) as h10
    return collect(h10) as cha10
}
CALL {
    USE geodemo.geokb
    with cha10
    MATCH (h10:HexAddress10)<-[:CONTAINS_HEXADDRESS10]-(c:County)
    where h10.hexAddress10 IN cha10
    return c
}
return c;

Our query returns the 3 counties that the Power line crosses.

Let’s ask one more question. This time let’s look at NYC and see if we can figure out which neighborhoods are impacted by a Power line.

Again, let’s write a Cypher query to figure this out.

CALL {
    USE geodemo.powerlines
    match (p:Powerline)-[:CONTAINS_HEXADDRESS12]->(h12) 
    where p.gid = '{4492FD07-9B29-4ED7-95CC-31DE4262B9C1}'
    WITH distinct (com.neo4jh3.toparent(h12.hexAddress12,10)) as h10
    return collect(h10) as cha10
}
CALL {
    USE geodemo.geokb
    with cha10
    MATCH (h10:HexAddress10)<-[:CONTAINS_HEXADDRESS10]-(n:Neighborhood)
    where h10.hexAddress10 IN cha10
    return distinct n
}
return n;

In 31ms, we get the 12 neighborhoods that the Power Transmission Line crosses.

These are just a couple of ways that we can use Neo4j, H3 and geospatial queries to answer questions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.