Exemplo n.º 1
0
    def load_gtfs_dag(self, sched_or_datadir, stops_timezone_name=None, prefix="gtfs"):

        if type(sched_or_datadir)==str:
            sched = transitfeed.Loader(sched_or_datadir).Load()
        else:
            sched = sched_or_datadir

        

        tzs = dict( [(agency.agency_id, Timezone.generate(agency.agency_timezone)) for agency in sched.GetAgencyList()] )
        scs = dict( [(agency.agency_id, schedule_to_service_calendar(sched, agency.agency_id)) for agency in sched.GetAgencyList()] )
        agints = dict( zip([x.agency_id for x in sched.GetAgencyList()], range(len(sched.GetAgencyList())) ))
        
        if stops_timezone_name is None:
            default_agency = sched.GetDefaultAgency() or sched.GetAgencyList()[0]
            stz = scs[default_agency.agency_id]
        else:
            stz = Timezone.generate( stops_timezone_name )

        stop_times = dict( [(x.stop_id, set()) for x in sched.GetStopList()] )

        for route in sched.GetRouteList():
            print "adding all triphops for route %s"%route.route_id

            for trip in route.trips:
                agency_id = route_for_trip(trip).agency_id
                stops = trip.GetTimeInterpolatedStops()

                for (fromst_time,fromst_st,fromst_timed), (tost_time,tost_st,tost_timed) in cons(stops):
                    fromst_name = "%s@%s"%(fromst_st.stop.stop_id,fromst_time)
                    tost_name = "%s@%s"%(tost_st.stop.stop_id,tost_time)

                    self.add_vertex(fromst_name)
                    self.add_vertex(tost_name)

                    sc = scs[agency_id]
                    th = TripHop(fromst_time, tost_time, trip.trip_id, sc, tzs[agency_id], agints[agency_id], trip.service_id.encode("ascii"))

                    self.add_edge(fromst_name, tost_name, th)

                    stop_times[fromst_st.stop.stop_id].add(fromst_time)
                    stop_times[tost_st.stop.stop_id].add(tost_time)

        for stop_id, times in stop_times.iteritems():
            print "laying down beanstock for %s"%stop_id
            times = list(times)
            times.sort()

            self.add_vertex(stop_id)

            for time in times:
                self.add_edge( stop_id, "%s@%s"%(stop_id,time), Wait(time, stz) )
                self.add_edge( "%s@%s"%(stop_id,time), stop_id, Wait(time, stz) )

            for fromtime, totime in cons(times):
                self.add_edge( "%s@%s"%(stop_id,fromtime), "%s@%s"%(stop_id,totime), Wait(totime, stz) )
Exemplo n.º 2
0
    def test_wait(self):
        waitend = 100
        tz = Timezone()
        tz.add_period(TimezonePeriod(0, 100000, 0))
        w = Wait(waitend, tz)
        assert w.end == waitend
        assert w.timezone.soul == tz.soul
        assert w.to_xml() == "<Wait end='100' />"

        s = State(1, 0)
        sprime = w.walk(s, WalkOptions())
        assert sprime.time == 100
        assert sprime.weight == 100

        s = State(1, 150)
        sprime = w.walk_back(s, WalkOptions())
        assert sprime.time == 100
        assert sprime.weight == 50

        s = State(1, 86400)
        sprime = w.walk(s, WalkOptions())
        assert sprime.time == 86500
        assert sprime.weight == 100

        w.destroy()

        tz = Timezone()
        tz.add_period(TimezonePeriod(0, 100000, -20))
        w = Wait(100, tz)
        assert w.end == 100
        assert w.timezone.soul == tz.soul
        s = State(1, 86400)
        sprime = w.walk(s, WalkOptions())
        assert sprime.weight == 120
Exemplo n.º 3
0
    def _load_agency(self, sched, agency, agency_int, prefix):
        cal = schedule_to_service_calendar(sched, agency.agency_id)

        gs_tz = Timezone.generate(agency.agency_timezone)

        for stop in sched.GetStopList():
            print "loading stop %s for agency %s"%(stop.stop_id, agency.agency_id)
            rawtriphopschedules = self._raw_triphopschedules_from_stop(sched, stop, agency)

            for rawtriphopschedule in rawtriphopschedules:
                #hops = [(fromv.departure_secs, tov.arrival_secs, trip.trip_id.encode("ascii")) for trip,fromv,tov in rawtriphopschedule]
                hops =[]
                service_id = rawtriphopschedule[0][0].service_id.encode("ascii")
                for trip,fromv,tov in rawtriphopschedule:
                  trip_id = trip.trip_id.encode("ascii")
                  if len(trip.GetHeadwayPeriodTuples())!=0:
                    for start_time, end_time, headway_secs in trip.GetHeadwayPeriodTuples():
                      hw = Headway( start_time, end_time, headway_secs, (tov.arrival_secs - fromv.departure_secs), trip_id, cal, gs_tz, agency_int, service_id )
                      e = self.add_edge( prefix+fromv.stop_id, prefix+tov.stop_id, hw )
                  else:
                    hops.append( (fromv.departure_secs, tov.arrival_secs, trip_id) )

                if hops!=[]:
                  ths = TripHopSchedule( hops, service_id, cal, gs_tz, agency_int )
                  e = self.add_edge( prefix+fromv.stop_id, prefix+tov.stop_id, ths )
Exemplo n.º 4
0
def gdb_load_gtfsdb_to_boardalight(gdb, agency_namespace, gtfsdb, agency_id, cursor, agency_id_int, maxtrips=None, reporter=sys.stdout):
    
    # get graphserver.core.Timezone and graphserver.core.ServiceCalendars from gtfsdb for agency with given agency_id
    tz = Timezone.generate(gtfsdb.agency_timezone_name( agency_id ))
    sc = gtfsdb_to_service_calendar(gtfsdb, agency_id )

    # enter station vertices
    for stop_id, stop_name, stop_lat, stop_lon in gtfsdb.stops():
        station_vertex_label = "sta-%s"%stop_id
        reporter.write("adding station vertex '%s'\n"%station_vertex_label)
        gdb.add_vertex( station_vertex_label, stop_lat, stop_lon )
    
    # compile trip bundles from gtfsdb
    if reporter: reporter.write( "Compiling trip bundles...\n" )
    bundles = gtfsdb.compile_trip_bundles(maxtrips=maxtrips, reporter=reporter)

    # load bundles to graph
    if reporter: reporter.write( "Loading trip bundles into graph...\n" )
    n_bundles = len(bundles)
    for i, bundle in enumerate(bundles):
        if reporter: reporter.write( "%d/%d loading %s\n"%(i, n_bundles, bundle) )
        
        for service_id in [x.encode("ascii") for x in gtfsdb.service_ids()]:
            gdb_boardalight_load_bundle(gdb, agency_namespace, gtfsdb, bundle, service_id, sc, tz, cursor, agency_id_int)
            
    # load headways
    if reporter: reporter.write( "Loading headways trips to graph...\n" )
    for trip_id, start_time, end_time, headway_secs in gtfsdb.execute( "SELECT * FROM frequencies" ):
        service_id = list(gtfsdb.execute( "SELECT service_id FROM trips WHERE trip_id=?", (trip_id,) ))[0][0]
        service_id = service_id.encode('utf-8')
        
        route_type = gtfsdb.route_type_for_trip_id(trip_id)
        stoptimes = list(gtfsdb.execute( "SELECT * FROM stop_times WHERE trip_id=? ORDER BY stop_sequence", (trip_id,)) )
        
        #add board edges
        for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled, stop_headsign in stoptimes[:-1]:
            board_stop_id, board_stop_name, board_stop_lat, board_stop_lon = gtfsdb.stop(stop_id)
            wheelchair_boarding = gtfsdb.wheelchair_boarding_for_stop_id(stop_id)
            gdb.add_vertex( "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id), board_stop_lat, board_stop_lon )
            gdb.add_edge( "sta-%s"%stop_id, "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id), HeadwayBoard( service_id, sc, tz, agency_id_int, route_type, wheelchair_boarding, trip_id.encode('utf-8'), start_time, end_time, headway_secs ) )
            
        #add alight edges
        for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled, stop_headsign in stoptimes[1:]:
            alight_stop_id, alight_stop_name, alight_stop_lat, alight_stop_lon = gtfsdb.stop(stop_id)
            wheelchair_boarding = gtfsdb.wheelchair_boarding_for_stop_id(stop_id)
            gdb.add_vertex( "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id), alight_stop_lat, alight_stop_lon )
            gdb.add_edge( "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id), "sta-%s"%stop_id, HeadwayAlight( service_id, sc, tz, agency_id_int, route_type, wheelchair_boarding, trip_id.encode('utf-8'), start_time, end_time, headway_secs ) )
            
        #add crossing edges
        for (trip_id1, arrival_time1, departure_time1, stop_id1, stop_sequence1, stop_dist_traveled1, stop_headsign1), (trip_id2, arrival_time2, departure_time2, stop_id2, stop_sequence2, stop_dist_traveled2, stop_headsign2) in cons(stoptimes):
            gdb.add_edge( "hwv-%s-%s-%s"%(agency_namespace,stop_id1, trip_id1), "hwv-%s-%s-%s"%(agency_namespace,stop_id2, trip_id2), Crossing(arrival_time2-departure_time1) )
            
    # load connections
    if reporter: reporter.write( "Loading connections to graph...\n" )
    for stop_id1, stop_id2, conn_type, distance in gtfsdb.execute( "SELECT * FROM connections" ):
        gdb.add_edge( "sta-%s"%stop_id1, "sta-%s"%stop_id2, Street( conn_type, distance ) )
        gdb.add_edge( "sta-%s"%stop_id2, "sta-%s"%stop_id1, Street( conn_type, distance ) )
Exemplo n.º 5
0
    def __init__(self, gtfsdb, agency_namespace, agency_id=None, reporter=None):
        self.gtfsdb = gtfsdb
        self.agency_namespace = agency_namespace
        self.reporter = reporter

        # get graphserver.core.Timezone and graphserver.core.ServiceCalendars from gtfsdb for agency with given agency_id
        timezone_name = gtfsdb.agency_timezone_name(agency_id)
        self.tz = Timezone.generate( timezone_name )
        if reporter: reporter.write( "constructing service calendar for timezone '%s'\n"%timezone_name )
        self.sc = service_calendar_from_timezone(gtfsdb, timezone_name )
Exemplo n.º 6
0
def load_gtfsdb_to_boardalight_graph(g, agency_namespace, gtfsdb, agency_id, service_ids, reporter=sys.stdout):
    
    # get graphserver.core.Timezone and graphserver.core.ServiceCalendars from gtfsdb for agency with given agency_id
    tz = Timezone.generate(gtfsdb.agency_timezone_name( agency_id ))
    sc = gtfsdb_to_service_calendar(gtfsdb, agency_id )

    # enter station vertices
    for stop_id, stop_name, stop_lat, stop_lon in gtfsdb.stops():
        g.add_vertex( "sta-%s"%stop_id )
    
    # compile trip bundles from gtfsdb
    if reporter: reporter.write( "Compiling trip bundles...\n" )
    bundles = gtfsdb.compile_trip_bundles(reporter=reporter)

    # load bundles to graph
    if reporter: reporter.write( "Loading trip bundles into graph...\n" )
    n_bundles = len(bundles)
    for i, bundle in enumerate(bundles):
        if reporter and i%((n_bundles//100)+1)==0: reporter.write( "%d/%d trip bundles loaded\n"%(i, n_bundles) )
        
        for service_id in service_ids:
            load_bundle_to_boardalight_graph(g, agency_namespace, bundle, service_id, sc, tz)
            
    # load headways
    if reporter: reporter.write( "Loading headways trips to graph...\n" )
    for trip_id, start_time, end_time, headway_secs in gtfsdb.execute( "SELECT * FROM frequencies" ):
        service_id = list(gtfsdb.execute( "SELECT service_id FROM trips WHERE trip_id=?", (trip_id,) ))[0][0]
        service_id = service_id.encode('utf-8')
        
        hb = HeadwayBoard( service_id, sc, tz, 0, trip_id.encode('utf-8'), start_time, end_time, headway_secs )
        ha = HeadwayAlight( service_id, sc, tz, 0, trip_id.encode('utf-8'), start_time, end_time, headway_secs )
        
        stoptimes = list(gtfsdb.execute( "SELECT trip_id, arrival_time, departure_time, stop_id, stop_sequence, shape_dist_traveled FROM stop_times WHERE trip_id=? ORDER BY stop_sequence", (trip_id,)) )
        
        #add board edges
        for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stoptimes[:-1]:
            g.add_vertex( "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id) )
            g.add_edge( "sta-%s"%stop_id, "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id), hb )
            
        #add alight edges
        for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stoptimes[1:]:
            g.add_vertex( "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id) )
            g.add_edge( "hwv-%s-%s-%s"%(agency_namespace,stop_id, trip_id), "sta-%s"%stop_id, ha )
            print ha
        
        #add crossing edges
        for (trip_id1, arrival_time1, departure_time1, stop_id1, stop_sequence1, stop_dist_traveled1), (trip_id2, arrival_time2, departure_time2, stop_id2, stop_sequence2,stop_dist_traveled2) in cons(stoptimes):
            g.add_edge( "hwv-%s-%s-%s"%(agency_namespace,stop_id1, trip_id1), "hwv-%s-%s-%s"%(agency_namespace,stop_id2, trip_id2), Crossing(arrival_time2-departure_time1) )
            
    # load connections
    if reporter: reporter.write( "Loading connections to graph...\n" )
    for stop_id1, stop_id2, conn_type, distance in gtfsdb.execute( "SELECT * FROM connections" ):
        g.add_edge( "sta-%s"%stop_id1, "sta-%s"%stop_id2, Street( conn_type, distance ) )
        g.add_edge( "sta-%s"%stop_id2, "sta-%s"%stop_id1, Street( conn_type, distance ) )
Exemplo n.º 7
0
    def test_wait(self):
        waitend = 100
        tz = Timezone()
        tz.add_period(TimezonePeriod(0,100000,0))
        w = Wait(waitend, tz)
        assert w.end == waitend
        assert w.timezone.soul == tz.soul
        assert w.to_xml() == "<Wait end='100' />"

        s = State(1,0)
        sprime = w.walk(s, WalkOptions())
        assert sprime.time == 100
        assert sprime.weight == 100

        s = State(1, 150)
        sprime = w.walk_back(s, WalkOptions())
        assert sprime.time == 100
        assert sprime.weight == 50
        
        s = State(1, 86400)
        sprime = w.walk(s, WalkOptions())
        assert sprime.time == 86500
        assert sprime.weight == 100

        w.destroy()
        
        tz = Timezone()
        tz.add_period(TimezonePeriod(0,100000,-20))
        w = Wait(100, tz)
        assert w.end == 100
        assert w.timezone.soul == tz.soul
        s = State(1, 86400)
        sprime = w.walk(s, WalkOptions())
        assert sprime.weight == 120
Exemplo n.º 8
0
 def test_august(self):
     # noon, -7 hours off UTC, as America/Los_Angeles in summer
     tz = Timezone.generate("America/Los_Angeles")
     w = Wait(43200, tz)
     
     # one calendar, noon august 27, America/Los_Angeles
     s = State(1, 1219863600)
     
     assert w.walk(s, WalkOptions()).time == 1219863600
     
     # one calendar, 11:55 AM August 27 2008, America/Los_Angeles
     s = State(1, 1219863300)
     assert w.walk(s, WalkOptions()).time == 1219863600
     assert w.walk(s, WalkOptions()).weight == 300
Exemplo n.º 9
0
    def test_august(self):
        # noon, -7 hours off UTC, as America/Los_Angeles in summer
        tz = Timezone.generate("America/Los_Angeles")
        w = Wait(43200, tz)

        # one calendar, noon august 27, America/Los_Angeles
        s = State(1, 1219863600)

        assert w.walk(s, WalkOptions()).time == 1219863600

        # one calendar, 11:55 AM August 27 2008, America/Los_Angeles
        s = State(1, 1219863300)
        assert w.walk(s, WalkOptions()).time == 1219863600
        assert w.walk(s, WalkOptions()).weight == 300
Exemplo n.º 10
0
 def test_getstate(self):
     # noon, -7 hours off UTC, as America/Los_Angeles in summer
     tz = Timezone.generate("America/Los_Angeles")
     w = Wait(43200, tz)
     
     assert w.__getstate__() == (43200, tz.soul)
Exemplo n.º 11
0
    def test_getstate(self):
        # noon, -7 hours off UTC, as America/Los_Angeles in summer
        tz = Timezone.generate("America/Los_Angeles")
        w = Wait(43200, tz)

        assert w.__getstate__() == (43200, tz.soul)
Exemplo n.º 12
0
 def test_hello_world(self):
     g = Graph()
     
     g.add_vertex( "Seattle" )
     g.add_vertex( "Portland" )
     
     g.add_edge( "Seattle", "Portland", Street("I-5 south", 5000) )
     g.add_edge( "Portland", "Seattle", Street("I-5 north", 5500) )
     
     spt = g.shortest_path_tree( "Seattle", "Portland", State(g.numagencies,0), WalkOptions() )
     
     assert spt.get_vertex("Seattle").outgoing[0].payload.name == "I-5 south"
     
     g.add_vertex( "Portland-busstop" )
     g.add_vertex( "Seattle-busstop" )
     
     g.add_edge( "Seattle", "Seattle-busstop", Link() )
     g.add_edge( "Seattle-busstop", "Seattle", Link() )
     g.add_edge( "Portland", "Portland-busstop", Link() )
     g.add_edge( "Portland-busstop", "Portland", Link() )
     
     spt = g.shortest_path_tree( "Seattle", "Seattle-busstop", State(g.numagencies,0), WalkOptions() )
     assert spt.get_vertex("Seattle-busstop").incoming[0].payload.__class__ == Link
     spt.destroy()
     
     spt = g.shortest_path_tree( "Seattle-busstop", "Portland", State(g.numagencies,0), WalkOptions() )
     assert spt.get_vertex("Portland").incoming[0].payload.__class__ == Street
     spt.destroy()
     
     sc = ServiceCalendar()
     sc.add_period( 0, 86400, ["WKDY","SAT"] )
     tz = Timezone()
     tz.add_period( TimezonePeriod( 0, 86400, 0 ) )
     
     g.add_vertex( "Portland-busstop-onbus" )
     g.add_vertex( "Seattle-busstop-onbus" )
     
     tb = TripBoard("WKDY", sc, tz, 0)
     tb.add_boarding( "A", 10, 0 )
     tb.add_boarding( "B", 15, 0 )
     tb.add_boarding( "C", 400, 0 )
     
     cr = Crossing()
     
     al = TripAlight("WKDY", sc, tz, 0)
     al.add_alighting( "A", 10+20, 0 )
     al.add_alighting( "B", 15+20, 0 )
     al.add_alighting( "C", 400+20, 0 )
     
     g.add_edge( "Seattle-busstop", "Seattle-busstop-onbus", tb )
     g.add_edge( "Seattle-busstop-onbus", "Portland-busstop-onbus", cr )
     g.add_edge( "Portland-busstop-onbus", "Portland-busstop", al )
     
     spt = g.shortest_path_tree( "Seattle", "Portland", State(g.numagencies,0), WalkOptions() )
     
     assert spt.get_vertex( "Portland" ).incoming[0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[0].from_v.label == "Seattle"
     
     spt = g.shortest_path_tree( "Seattle", "Portland", State(g.numagencies,0), WalkOptions() )
     vertices, edges = spt.path( "Portland" )
     
     assert [v.label for v in vertices] == ['Seattle', 'Seattle-busstop', "Seattle-busstop-onbus", "Portland-busstop-onbus", 'Portland-busstop', 'Portland']
     assert [e.payload.__class__ for e in edges] == [Link, TripBoard, Crossing, TripAlight, Link]
     
     spt.destroy()
     g.destroy()
Exemplo n.º 13
0
    def test_hello_world(self):
        g = Graph()

        g.add_vertex("Seattle")
        g.add_vertex("Portland")

        g.add_edge("Seattle", "Portland", Street("I-5 south", 5000))
        g.add_edge("Portland", "Seattle", Street("I-5 north", 5500))

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())

        assert spt.get_vertex(
            "Seattle").outgoing[0].payload.name == "I-5 south"

        g.add_vertex("Portland-busstop")
        g.add_vertex("Seattle-busstop")

        g.add_edge("Seattle", "Seattle-busstop", Link())
        g.add_edge("Seattle-busstop", "Seattle", Link())
        g.add_edge("Portland", "Portland-busstop", Link())
        g.add_edge("Portland-busstop", "Portland", Link())

        spt = g.shortest_path_tree("Seattle", "Seattle-busstop",
                                   State(g.numagencies, 0), WalkOptions())
        assert spt.get_vertex(
            "Seattle-busstop").incoming[0].payload.__class__ == Link
        spt.destroy()

        spt = g.shortest_path_tree("Seattle-busstop", "Portland",
                                   State(g.numagencies, 0), WalkOptions())
        assert spt.get_vertex(
            "Portland").incoming[0].payload.__class__ == Street
        spt.destroy()

        sc = ServiceCalendar()
        sc.add_period(0, 86400, ["WKDY", "SAT"])
        tz = Timezone()
        tz.add_period(TimezonePeriod(0, 86400, 0))

        g.add_vertex("Portland-busstop-onbus")
        g.add_vertex("Seattle-busstop-onbus")

        tb = TripBoard("WKDY", sc, tz, 0)
        tb.add_boarding("A", 10, 0)
        tb.add_boarding("B", 15, 0)
        tb.add_boarding("C", 400, 0)

        cr = Crossing()

        al = TripAlight("WKDY", sc, tz, 0)
        al.add_alighting("A", 10 + 20, 0)
        al.add_alighting("B", 15 + 20, 0)
        al.add_alighting("C", 400 + 20, 0)

        g.add_edge("Seattle-busstop", "Seattle-busstop-onbus", tb)
        g.add_edge("Seattle-busstop-onbus", "Portland-busstop-onbus", cr)
        g.add_edge("Portland-busstop-onbus", "Portland-busstop", al)

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())

        assert spt.get_vertex("Portland").incoming[0].from_v.incoming[
            0].from_v.incoming[0].from_v.incoming[0].from_v.incoming[
                0].from_v.label == "Seattle"

        spt = g.shortest_path_tree("Seattle", "Portland",
                                   State(g.numagencies, 0), WalkOptions())
        vertices, edges = spt.path("Portland")

        assert [v.label for v in vertices] == [
            'Seattle', 'Seattle-busstop', "Seattle-busstop-onbus",
            "Portland-busstop-onbus", 'Portland-busstop', 'Portland'
        ]
        assert [e.payload.__class__ for e in edges
                ] == [Link, TripBoard, Crossing, TripAlight, Link]

        spt.destroy()
        g.destroy()