def service_calendar_from_timezone(gtfsdb, timezone_name): timezone = pytz.timezone(timezone_name) # grab date, day service bounds start_date, end_date = gtfsdb.date_range() # init empty calendar cal = ServiceCalendar() # for each day in service range, inclusive for currdate in iter_dates(start_date, end_date): # get and encode in utf-8 the service_ids of all service periods running thos date service_ids = [ x.encode('utf8') for x in gtfsdb.service_periods(currdate) ] # figure datetime.datetime bounds of this service day currdate_start = datetime.combine(currdate, time(0)) currdate_local_start = timezone.localize(currdate_start) service_period_begins = timezone.normalize(currdate_local_start) service_period_ends = timezone.normalize(currdate_local_start + timedelta(hours=24)) # enter as entry in service calendar cal.add_period(TimeHelpers.datetime_to_unix(service_period_begins), TimeHelpers.datetime_to_unix(service_period_ends), service_ids) return cal
def service_calendar_from_timezone(gtfsdb, timezone_name): MAX_CALENDAR_SIZE = 1024 sc_count = list(gtfsdb.execute( "SELECT DISTINCT count(num) FROM (SELECT service_id FROM gtfs_calendar_dates UNION SELECT service_id FROM gtfs_calendar ) AS num" ))[0][0] if sc_count > MAX_CALENDAR_SIZE: raise Exception( "Service period count %d is greater than the maximum of %d"%(sc_count, MAX_CALENDAR_SIZE) ) timezone = pytz.timezone( timezone_name ) # grab date, day service bounds start_date, end_date = gtfsdb.date_range() # init empty calendar cal = ServiceCalendar() # for each day in service range, inclusive for currdate in iter_dates(start_date, end_date): # get and encode in utf-8 the service_ids of all service periods running thos date service_ids = [x.encode('utf8') for x in gtfsdb.service_periods( currdate )] # figure datetime.datetime bounds of this service day currdate_start = datetime.combine(currdate, time(0)) currdate_local_start = timezone.localize(currdate_start) service_period_begins = timezone.normalize( currdate_local_start ) service_period_ends = timezone.normalize( currdate_local_start + timedelta(hours=24) ) # enter as entry in service calendar cal.add_period( TimeHelpers.datetime_to_unix(service_period_begins), TimeHelpers.datetime_to_unix(service_period_ends), service_ids ) return cal
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()