Search code examples
neo4jcypheropenstreetmapspatial

Neo4j OSM Intersections and Spatial Graph Setup


I've been trying to setup Neo4j for use as a routing engine, and this has lead to two questions (amongst others).

Main Question

When following the instructions from the README, after importing the OSM data, the next step is to identify intersections, as shown below:

MATCH (n:OSMNode)
  WHERE size((n)<-[:NODE]-(:OSMWayNode)-[:NEXT]-(:OSMWayNode)) > 2
  AND NOT (n:Intersection)
WITH n LIMIT 100
MATCH (n)<-[:NODE]-(wn:OSMWayNode), (wn)<-[:NEXT*0..100]-(wx),
      (wx)<-[:FIRST_NODE]-(w:OSMWay)-[:TAGS]->(wt:OSMTags)
  WHERE exists(wt.highway)
SET n:Intersection
RETURN count(*);

I haven't figured out how the >2 works in the second line, WHERE size((n)<-[:NODE]-(:OSMWayNode)-[:NEXT]-(:OSMWayNode)) > 2.
Can anyone explain how this works?
I think it filters for connections to a node, where an intersection should be more than 2 connections (because it has to have multiple roads there to "intersect"). However, if there's only 2 roads at an intersection (i.e., the streets keep the same name when passing through the intersection), there seems to be only 1 connection from each street to the node, thus giving exactly 2 connections and failing the check. I want to believe I am missing something trivial here...

Secondary Question

I've been combining instructions from the README with the demo to build a routing graph. I'm certain these instructions are complete...in some fashion.
Are there more clear or updated instructions for building a routing graph in Neo4j?

References:


Solution

  • The function size() returns the number of elements in a list. https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-size

    What you mean would be the case if we only consider the following query: size((n)<-[:NODE]-(:OSMWayNode)) > 2

    This would only count the connected OSMWayNode and 'Street-Links' would be classified as intersections.

    But because the next OSMWayNode appears in the list there can be multiple appereances for one [NODE] relation:

    1. (n:OSMNode)<-[nr1:NODE]-(own1:OSMWayNode)-[wr1:NEXT]->(own2:OSMWayNode)
    2. (n:OSMNode)<-[nr2:NODE]-(own3:OSMWayNode)-[wr2:NEXT]->(own4:OSMWayNode)
    3. (n:OSMNode)<-[nr2:NODE]-(own3:OSMWayNode)<-[wr3:NEXT]-(own5:OSMWayNode)

    (note that the [:NEXT]-relations are bidirectional in the query)

    example of an intersection in munich

    This shows two crossing roads leading to an intersection rather than a link