Mapping relationships between Neo4j spatial nodes with GeoPandas

neub9
By neub9
1 Min Read

“`html

Building upon our previous work on mapping neo4j spatial nodes, we are now moving forward to map the relationships between these nodes.

An excellent example of this approach is analyzing the relationships between GTFS StopTime and Trip nodes. For instance, here is the Cypher query to retrieve all StopTime nodes of Trip 17:

        MATCH 
            (t:Trip {id: "17"})
            <-[:BELONGS_TO]-
            (st:StopTime) 
        RETURN st
        

Furthermore, in order to obtain the stop locations, we also need to retrieve the stop nodes:

        MATCH 
            (t:Trip {id: "17"})
            <-[:BELONGS_TO]-
            (st:StopTime)
            -[:STOPS_AT]->
            (s:Stop)
        RETURN st ,s
        

Adapting the code from the previous post, we can plot the stops:

        
        

Ordering by stop sequence is optional, but for a different approach, we can use the NEXT_STOP relationships to generate a DataFrame of the start and end stops for each segment:

        
        

Finally, we can use Cypher to calculate the travel time between two stops:

        MATCH (t:Trip {id: "17"})
           <-[:BELONGS_TO]-
           (st1:StopTime)
           -[:NEXT_STOP]->
           (st2:StopTime)
        MATCH (st1)-[:STOPS_AT]->(s1:Stop)
        MATCH (st2)-[:STOPS_AT]->(s2:Stop)
        RETURN st1.departureTime AS time1, 
           st2.arrivalTime AS time2, 
           s1.location AS geom1, 
           s2.location AS geom2, 
           duration.inSeconds(
              time(st1.departureTime), 
              time(st2.arrivalTime)
           ).seconds AS traveltime
        

Find the complete notebook here.

“`

Share This Article
Leave a comment

Leave a Reply

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