def load_bundle_to_boardalight_graph(g, agency_namespace, bundle, service_id, sc, tz): stop_time_bundles = list(bundle.stop_time_bundles(service_id)) # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places if len(stop_time_bundles) < 2: return # If there are no stop_times in a bundle on this service day, there is nothing to load if len(stop_time_bundles[0]) == 0: return # add board edges for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]): if len(stop_time_bundle) == 0: continue trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] patternstop_vx_name = "psv-%s-%03d-%03d" % (agency_namespace, bundle.pattern.pattern_id, i) g.add_vertex(patternstop_vx_name) b = TripBoard(service_id, sc, tz, 0) for trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: b.add_boarding(trip_id, departure_time) g.add_edge("sta-%s" % stop_id, patternstop_vx_name, b) # add alight edges for i, stop_time_bundle in enumerate(stop_time_bundles[1:]): if len(stop_time_bundle) == 0: continue trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] patternstop_vx_name = "psv-%s-%03d-%03d" % (agency_namespace, bundle.pattern.pattern_id, i + 1) g.add_vertex(patternstop_vx_name) al = Alight(service_id, sc, tz, 0) for trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: al.add_alighting(trip_id.encode("ascii"), arrival_time) g.add_edge(patternstop_vx_name, "sta-%s" % stop_id, al) # add crossing edges for j, crossing_time in enumerate(bundle.pattern.crossings): c = Crossing(crossing_time) g.add_edge( "psv-%s-%03d-%03d" % (agency_namespace, bundle.pattern.pattern_id, j), "psv-%s-%03d-%03d" % (agency_namespace, bundle.pattern.pattern_id, j + 1), c, )
def gdb_boardalight_load_bundle(gdb, agency_namespace, bundle, service_id, sc, tz, cursor): stop_time_bundles = bundle.stop_time_bundles(service_id) n_trips = len(bundle.trip_ids) # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places if len(stop_time_bundles)<2: return # If there are no stop_times in a bundle on this service day, there is nothing to load if n_trips==0: return print "inserting %d trips with %d stop_time bundles on service_id '%s'"%(len(stop_time_bundles[0]),len(stop_time_bundles),service_id) #print bundle.pattern.stop_ids #add board edges for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]): trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] patternstop_vx_name = "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,i) gdb.add_vertex( patternstop_vx_name, cursor ) b = TripBoard(service_id, sc, tz, 0) for trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: b.add_boarding( trip_id, departure_time ) gdb.add_edge( "sta-%s"%stop_id, patternstop_vx_name, b, cursor ) #add alight edges for i, stop_time_bundle in enumerate(stop_time_bundles[1:]): trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] patternstop_vx_name = "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,i+1) gdb.add_vertex( patternstop_vx_name, cursor ) al = Alight(service_id, sc, tz, 0) for trip_id, departure_time, arrival_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: al.add_alighting( trip_id.encode('ascii'), arrival_time ) gdb.add_edge( patternstop_vx_name, "sta-%s"%stop_id, al, cursor ) # add crossing edges for j, crossing_time in enumerate(bundle.pattern.crossings): c = Crossing( crossing_time ) gdb.add_edge( "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,j), "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,j+1), c, cursor ) gdb.commit()
def bundle_to_boardalight_edges(self, bundle, service_id): """takes a bundle and yields a bunch of edges""" stop_time_bundles = bundle.stop_time_bundles(service_id) n_trips = len(bundle.trip_ids) # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places if len(stop_time_bundles) < 2: return # If there are no stop_times in a bundle on this service day, there is nothing to load if n_trips == 0: return if self.reporter: self.reporter.write( "inserting %d trips with %d stop_time bundles on service_id '%s'\n" % (len( stop_time_bundles[0]), len(stop_time_bundles), service_id)) #add board edges for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]): trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[ 0] if arrival_time != departure_time: patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart" % ( self.agency_namespace, bundle.pattern.pattern_id, i, service_id) # construct the board/alight/dwell triangle for this patternstop patternstop_arrival_vx_name = "psv-%s-%03d-%03d-%s-arrive" % ( self.agency_namespace, bundle.pattern.pattern_id, i, service_id) dwell_crossing = Crossing() for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: dwell_crossing.add_crossing_time( trip_id, departure_time - arrival_time) yield (patternstop_arrival_vx_name, patternstop_vx_name, dwell_crossing) else: patternstop_vx_name = "psv-%s-%03d-%03d-%s" % ( self.agency_namespace, bundle.pattern.pattern_id, i, service_id) b = TripBoard(service_id, self.sc, self.tz, 0) for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: b.add_boarding(trip_id, departure_time, stop_sequence) yield ("sta-%s" % stop_id, patternstop_vx_name, b) #add alight edges for i, stop_time_bundle in enumerate(stop_time_bundles[1:]): trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[ 0] if arrival_time != departure_time: patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive" % ( self.agency_namespace, bundle.pattern.pattern_id, i + 1, service_id) else: patternstop_vx_name = "psv-%s-%03d-%03d-%s" % ( self.agency_namespace, bundle.pattern.pattern_id, i + 1, service_id) al = TripAlight(service_id, self.sc, self.tz, 0) for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: al.add_alighting(trip_id.encode('ascii'), arrival_time, stop_sequence) yield (patternstop_vx_name, "sta-%s" % stop_id, al) # add crossing edges for i, (from_stop_time_bundle, to_stop_time_bundle) in enumerate(cons(stop_time_bundles)): trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[ 0] trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[ 0] if from_arrival_time != from_departure_time: from_patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart" % ( self.agency_namespace, bundle.pattern.pattern_id, i, service_id) else: from_patternstop_vx_name = "psv-%s-%03d-%03d-%s" % ( self.agency_namespace, bundle.pattern.pattern_id, i, service_id) if to_arrival_time != to_departure_time: to_patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive" % ( self.agency_namespace, bundle.pattern.pattern_id, i + 1, service_id) else: to_patternstop_vx_name = "psv-%s-%03d-%03d-%s" % ( self.agency_namespace, bundle.pattern.pattern_id, i + 1, service_id) crossing = Crossing() for i in range(len(from_stop_time_bundle)): trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[ i] trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[ i] crossing.add_crossing_time( trip_id, (to_arrival_time - from_departure_time)) yield (from_patternstop_vx_name, to_patternstop_vx_name, crossing)
def bundle_to_boardalight_edges(self, bundle, service_id): """takes a bundle and yields a bunch of edges""" stop_time_bundles = bundle.stop_time_bundles(service_id) n_trips = len(bundle.trip_ids) # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places if len(stop_time_bundles)<2: return # If there are no stop_times in a bundle on this service day, there is nothing to load if n_trips==0: return if self.reporter: self.reporter.write( "inserting %d trips with %d stop_time bundles on service_id '%s'\n"%(len(stop_time_bundles[0]),len(stop_time_bundles),service_id) ) #add board edges for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]): trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] if arrival_time != departure_time: patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id) # construct the board/alight/dwell triangle for this patternstop patternstop_arrival_vx_name = "psv-%s-%03d-%03d-%s-arrive"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id) dwell_crossing = Crossing() for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: dwell_crossing.add_crossing_time( trip_id, departure_time-arrival_time ) yield (patternstop_arrival_vx_name, patternstop_vx_name, dwell_crossing) else: patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id) b = TripBoard(service_id, self.sc, self.tz, 0) for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: b.add_boarding( trip_id, departure_time, stop_sequence ) yield ( "sta-%s"%stop_id, patternstop_vx_name, b ) #add alight edges for i, stop_time_bundle in enumerate(stop_time_bundles[1:]): trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] if arrival_time != departure_time: patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id) else: patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id) al = TripAlight(service_id, self.sc, self.tz, 0) for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: al.add_alighting( trip_id.encode('ascii'), arrival_time, stop_sequence ) yield ( patternstop_vx_name, "sta-%s"%stop_id, al ) # add crossing edges for i, (from_stop_time_bundle, to_stop_time_bundle) in enumerate(cons(stop_time_bundles)): trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[0] trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[0] if from_arrival_time!=from_departure_time: from_patternstop_vx_name = "psv-%s-%03d-%03d-%s-depart"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id) else: from_patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i,service_id) if to_arrival_time!=to_departure_time: to_patternstop_vx_name = "psv-%s-%03d-%03d-%s-arrive"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id) else: to_patternstop_vx_name = "psv-%s-%03d-%03d-%s"%(self.agency_namespace,bundle.pattern.pattern_id,i+1,service_id) crossing = Crossing() for i in range( len( from_stop_time_bundle ) ): trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[i] trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[i] crossing.add_crossing_time( trip_id, (to_arrival_time-from_departure_time) ) yield ( from_patternstop_vx_name, to_patternstop_vx_name, crossing )
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()
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()
def gdb_boardalight_load_bundle(gdb, agency_namespace, gtfsdb, bundle, service_id, sc, tz, cursor, agency_id_int): stop_time_bundles = bundle.stop_time_bundles(service_id) n_trips = len(bundle.trip_ids) # If there's less than two stations on this trip bundle, the trip bundle doesn't actually span two places if len(stop_time_bundles)<2: return # If there are no stop_times in a bundle on this service day, there is nothing to load if n_trips==0: return print "inserting %d trips with %d stop_time bundles on service_id '%s'"%(len(stop_time_bundles[0]),len(stop_time_bundles),service_id) #print bundle.pattern.stop_ids #add board edges for i, stop_time_bundle in enumerate(stop_time_bundles[:-1]): trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] board_stop_id, board_stop_name, board_stop_lat, board_stop_lon = gtfsdb.stop(stop_id) route_type = gtfsdb.route_type_for_trip_id(trip_id) wheelchair_boarding = gtfsdb.wheelchair_boarding_for_stop_id(stop_id) if arrival_time != departure_time: patternstop_vx_name = "psv-%s-%03d-%03d-depart"%(agency_namespace,bundle.pattern.pattern_id,i) # construct the board/alight/dwell triangle for this patternstop patternstop_arrival_vx_name = "psv-%s-%03d-%03d-arrive"%(agency_namespace,bundle.pattern.pattern_id,i) gdb.add_vertex( patternstop_arrival_vx_name, board_stop_lat, board_stop_lon, cursor ) gdb.add_edge( patternstop_arrival_vx_name, patternstop_vx_name, Crossing( (departure_time-arrival_time) ), cursor ) else: patternstop_vx_name = "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,i) gdb.add_vertex( patternstop_vx_name, board_stop_lat, board_stop_lon, cursor ) b = TripBoard(service_id, sc, tz, agency_id_int, route_type, wheelchair_boarding) for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: b.add_boarding( trip_id, departure_time ) gdb.add_edge( "sta-%s"%stop_id, patternstop_vx_name, b, cursor ) #add alight edges for i, stop_time_bundle in enumerate(stop_time_bundles[1:]): trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled = stop_time_bundle[0] alight_stop_id, alight_stop_name, alight_stop_lat, alight_stop_lon = gtfsdb.stop(stop_id) route_type = gtfsdb.route_type_for_trip_id(trip_id) wheelchair_boarding = gtfsdb.wheelchair_boarding_for_stop_id(stop_id) if arrival_time != departure_time: patternstop_vx_name = "psv-%s-%03d-%03d-arrive"%(agency_namespace,bundle.pattern.pattern_id,i+1) else: patternstop_vx_name = "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,i+1) gdb.add_vertex( patternstop_vx_name, alight_stop_lat, alight_stop_lon, cursor ) al = Alight(service_id, sc, tz, agency_id_int, route_type, wheelchair_boarding) for trip_id, arrival_time, departure_time, stop_id, stop_sequence, stop_dist_traveled in stop_time_bundle: al.add_alighting( trip_id.encode('ascii'), arrival_time ) gdb.add_edge( patternstop_vx_name, "sta-%s"%stop_id, al, cursor ) # add crossing edges for i, (from_stop_time_bundle, to_stop_time_bundle) in enumerate(cons(stop_time_bundles)): trip_id, from_arrival_time, from_departure_time, stop_id, stop_sequence, stop_dist_traveled = from_stop_time_bundle[0] trip_id, to_arrival_time, to_departure_time, stop_id, stop_sequence, stop_dist_traveled = to_stop_time_bundle[0] if from_arrival_time!=from_departure_time: from_patternstop_vx_name = "psv-%s-%03d-%03d-depart"%(agency_namespace,bundle.pattern.pattern_id,i) else: from_patternstop_vx_name = "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,i) if to_arrival_time!=to_departure_time: to_patternstop_vx_name = "psv-%s-%03d-%03d-arrive"%(agency_namespace,bundle.pattern.pattern_id,i+1) else: to_patternstop_vx_name = "psv-%s-%03d-%03d"%(agency_namespace,bundle.pattern.pattern_id,i+1) gdb.add_edge( from_patternstop_vx_name, to_patternstop_vx_name, Crossing( (to_arrival_time-from_departure_time) ), cursor ) gdb.commit()