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
def _points(self, vertex_label, starttime, cutoff, speed): starttime = starttime or time.time() #=== find shortest path tree === print "Finding shortest path tree" t0 = time.time() wo = WalkOptions() wo.walking_speed = speed spt = self.graph.shortest_path_tree( vertex_label, None, State(1,starttime), wo, maxtime=starttime+int(cutoff*1.25) ) wo.destroy() t1 = time.time() print "took %s s"%(t1-t0) #=== cobble together ETA surface === print "Creating ETA surface from OSM points..." points = [] t0 = time.time() for vertex in spt.vertices: if "osm" in vertex.label: x, y = self.node_positions[vertex.label[3:]] points.append( (x, y, vertex.payload.time-starttime) ) t1 = time.time() print "Took %s s"%(t1-t0) spt.destroy() return points
def path(self, origin, dest, currtime=None, time_offset=None, transfer_penalty=0, walking_speed=1.0, hill_reluctance=1.5, turn_penalty=None, walking_reluctance=None, max_walk=None, jsoncallback=None): performance = {} if currtime is None: currtime = int(time.time()) if time_offset is not None: currtime += time_offset # time path query t0 = time.time() wo = WalkOptions() wo.transfer_penalty = transfer_penalty wo.walking_speed = walking_speed wo.hill_reluctance = hill_reluctance if turn_penalty is not None: wo.turn_penalty = turn_penalty if walking_reluctance is not None: wo.walking_reluctance = walking_reluctance if max_walk is not None: wo.max_walk = max_walk spt = self.graph.shortest_path_tree(origin, dest, State(1, currtime), wo) vertices, edges = spt.path(dest) performance['path_query_time'] = time.time() - t0 t0 = time.time() narrative = list( postprocess_path(vertices, edges, self.vertex_events, self.edge_events)) performance['narrative_postprocess_time'] = time.time() - t0 t0 = time.time() wo.destroy() spt.destroy() performance['cleanup_time'] = time.time() - t0 ret = {'narrative': narrative, 'performance': performance} if jsoncallback is None: return json.dumps(ret, indent=2, cls=SelfEncoderHelper) else: return "%s(%s)" % ( jsoncallback, json.dumps(ret, indent=2, cls=SelfEncoderHelper))
def vertex(self, label, currtime=None, hill_reluctance=1.5, walking_speed=0.85): currtime = currtime or int(time.time()) ret = [] ret.append( "<h1>%s</h1>"%label ) wo = WalkOptions() ret.append( "<h3>walk options</h3>" ) ret.append( "<li>transfer_penalty: %s</li>"%wo.transfer_penalty ) ret.append( "<li>turn_penalty: %s</li>"%wo.turn_penalty ) ret.append( "<li>walking_speed: %s</li>"%wo.walking_speed ) ret.append( "<li>walking_reluctance: %s</li>"%wo.walking_reluctance ) ret.append( "<li>uphill_slowness: %s</li>"%wo.uphill_slowness ) ret.append( "<li>downhill_fastness: %s</li>"%wo.downhill_fastness ) ret.append( "<li>hill_reluctance: %s</li>"%wo.hill_reluctance ) ret.append( "<li>max_walk: %s</li>"%wo.max_walk ) ret.append( "<li>walking_overage: %s</li>"%wo.walking_overage ) ret.append( "<h3>incoming from:</h3>" ) for i, (vertex1, vertex2, edgetype) in enumerate( self.graphdb.all_incoming( label ) ): s1 = State(1,int(currtime)) wo = WalkOptions() wo.hill_reluctance=hill_reluctance wo.walking_speed=walking_speed s0 = edgetype.walk_back( s1, wo ) if s0: toterm = "<a href=\"/vertex?label="%s"&currtime=%d\">%s@%d</a>"%(vertex1, s0.time, vertex1, s1.time) else: toterm = "<a href=\"/vertex?label="%s"\">%s</a>"%(vertex1, vertex1) ret.append( "%s<br><pre> via %s (<a href=\"/incoming?label="%s"&edgenum=%d\">details</a>)</pre>"%(toterm, cgi.escape(repr(edgetype)), vertex2, i) ) if s0: ret.append( "<pre> %s</pre>"%cgi.escape(str(s0)) ) ret.append( "<h3>outgoing to:</h3>" ) for i, (vertex1, vertex2, edgetype) in enumerate( self.graphdb.all_outgoing( label ) ): s0 = State(1,int(currtime)) wo = WalkOptions() wo.hill_reluctance=hill_reluctance wo.walking_speed=walking_speed s1 = edgetype.walk( s0, wo ) if s1: toterm = "<a href=\"/vertex?label="%s"&currtime=%d\">%s@%d</a>"%(vertex2, s1.time, vertex2, s1.time) else: toterm = "<a href=\"/vertex?label="%s"\">%s</a>"%(vertex2, vertex2) ret.append( "%s<br><pre> via %s (<a href=\"/outgoing?label="%s"&edgenum=%d\">details</a>)</pre>"%(toterm, cgi.escape(repr(edgetype)), vertex1, i) ) if s1: ret.append( "<pre> %s</pre>"%cgi.escape(str(s1)) ) wo.destroy() return "".join(ret)
def path_raw_retro(self, origin, dest, currtime): wo = WalkOptions() spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo ) wo.destroy() vertices, edges = spt.path_retro( origin ) ret = "\n".join([str(x) for x in vertices]) + "\n\n" + "\n".join([str(x) for x in edges]) spt.destroy() return ret
def make_native_ch(basename): gdb = GraphDatabase(basename + ".gdb") gg = gdb.incarnate() wo = WalkOptions() wo.hill_reluctance = 20 ch = gg.get_contraction_hierarchies(wo) chdowndb = GraphDatabase(basename + ".down.gdb", overwrite=True) chdowndb.populate(ch.downgraph, reporter=sys.stdout) chupdb = GraphDatabase(basename + ".up.gdb", overwrite=True) chupdb.populate(ch.upgraph, reporter=sys.stdout)
def path_raw_retro(self, origin, dest, currtime): wo = WalkOptions() spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo ) wo.destroy() vertices, edges = spt.path_retro( origin ) ret = postprocess_path_raw(vertices, edges) spt.destroy() return ret
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
def make_native_ch(basename): gdb = GraphDatabase( basename+".gdb" ) gg = gdb.incarnate() wo = WalkOptions() wo.hill_reluctance=20 ch = gg.get_contraction_hierarchies( wo ) chdowndb = GraphDatabase( basename+".down.gdb", overwrite=True ) chdowndb.populate( ch.downgraph, reporter=sys.stdout ) chupdb = GraphDatabase( basename+".up.gdb", overwrite=True ) chupdb.populate( ch.upgraph, reporter=sys.stdout )
def path(self, origin, dest, currtime=None, time_offset=None, transfer_penalty=0, walking_speed=1.0, hill_reluctance=1.5, turn_penalty=None, walking_reluctance=None, max_walk=None, jsoncallback=None): performance = {} if currtime is None: currtime = int(time.time()) if time_offset is not None: currtime += time_offset # time path query t0 = time.time() wo = WalkOptions() wo.transfer_penalty=transfer_penalty wo.walking_speed=walking_speed wo.hill_reluctance=hill_reluctance if turn_penalty is not None: wo.turn_penalty = turn_penalty if walking_reluctance is not None: wo.walking_reluctance = walking_reluctance if max_walk is not None: wo.max_walk = max_walk spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo ) vertices, edges = spt.path( dest ) performance['path_query_time'] = time.time()-t0 t0 = time.time() narrative = list(postprocess_path(vertices, edges, self.vertex_events, self.edge_events)) performance['narrative_postprocess_time'] = time.time()-t0 t0 = time.time() wo.destroy() spt.destroy() performance['cleanup_time'] = time.time()-t0 ret = {'narrative':narrative, 'performance':performance} if jsoncallback is None: return json.dumps(ret, indent=2, cls=SelfEncoderHelper) else: return "%s(%s)"%(jsoncallback,json.dumps(ret, indent=2, cls=SelfEncoderHelper))
def path_raw(self, origin, dest, currtime=None): if currtime is None: currtime = int(time.time()) wo = WalkOptions() spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo ) wo.destroy() vertices, edges = spt.path( dest ) ret = postprocess_path_raw(vertices, edges) spt.destroy() return ret
def test_bart(self): wo = WalkOptions() g = TestGTFS() g.load_gtfs(find_resource("google_transit.zip")) # just a basic sanity test s1 = State(g.numagencies, 1219863720) s2 = g.get_vertex("gtfsMONT").outgoing[1].walk(s1, wo) assert s2.time == 1219864320
def path_retro(self, origin, dest, currtime, transfer_penalty=0, walking_speed=1.0): wo = WalkOptions() wo.transfer_penalty = transfer_penalty wo.walking_speed = walking_speed spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo ) wo.destroy() vertices, edges = spt.path_retro( origin ) ret = [] for i in range(len(edges)): edgetype = edges[i].payload.__class__ if edgetype in self.event_dispatch: ret.append( self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1] ) ) spt.destroy() return json.dumps(ret)
def path(self, lat1, lng1, lat2, lng2, transfer_penalty=0, walking_speed=1.0, hill_reluctance=20, narrative=True, jsoncallback=None): t0 = time.time() origin = "osm-%s"%self.osmdb.nearest_node( lat1, lng1 )[0] dest = "osm-%s"%self.osmdb.nearest_node( lat2, lng2 )[0] endpoint_find_time = time.time()-t0 print origin, dest t0 = time.time() wo = WalkOptions() #wo.transfer_penalty=transfer_penalty #wo.walking_speed=walking_speed wo.walking_speed=4 wo.walking_overage = 0 wo.hill_reluctance = 20 wo.turn_penalty = 15 edgepayloads = self.ch.shortest_path( origin, dest, State(1,0), wo ) wo.destroy() route_find_time = time.time()-t0 t0 = time.time() names = [] geoms = [] profile = Profile() total_dist = 0 total_elev = 0 if narrative: names, total_dist = get_full_route_narrative( self.osmdb, edgepayloads ) for edgepayload in edgepayloads: geom, profile_seg = self.shortcut_cache.get( edgepayload.external_id ) #geom = get_ep_geom( self.osmdb, edgepayload ) #profile_seg = get_ep_profile( self.profiledb, edgepayload ) geoms.extend( geom ) profile.add( profile_seg ) route_desc_time = time.time()-t0 ret = json.dumps( (names, encode_pairs( [(lat, lon) for lon, lat in geoms] ), profile.concat(300), { 'route_find_time':route_find_time, 'route_desc_time':route_desc_time, 'endpoint_find_time':endpoint_find_time,}, { 'total_dist':total_dist, 'total_elev':total_elev}) ) if jsoncallback: return "%s(%s)"%(jsoncallback,ret) else: return ret
def path_retro(self, origin, dest, currtime=None, time_offset=None, transfer_penalty=0, walking_speed=1.0): if currtime is None: currtime = int(time.time()) if time_offset is not None: currtime += time_offset wo = WalkOptions() wo.transfer_penalty = transfer_penalty wo.walking_speed = walking_speed spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo ) wo.destroy() vertices, edges = spt.path_retro( origin ) ret = list(postprocess_path(vertices, edges, self.vertex_events, self.edge_events)) spt.destroy() return json.dumps(ret, indent=2, cls=SelfEncoderHelper)
def test_walk_back(self): l = Link() before = l.walk_back(State(1, 0), WalkOptions()) assert before.time == 0 assert before.weight == 0 assert before.dist_walked == 0.0 assert before.prev_edge.type == 3 assert before.prev_edge.name == "LINK" assert before.num_agencies == 1
def test_walk(self): l = Link() after = l.walk(State(1, 0), WalkOptions()) assert after.time == 0 assert after.weight == 0 assert after.dist_walked == 0 assert after.prev_edge.type == 3 assert after.prev_edge.name == "LINK" assert after.num_agencies == 1
def path_retro(self, origin, dest, currtime=None, time_offset=None, transfer_penalty=0, walking_speed=1.0): if currtime is None: currtime = int(time.time()) if time_offset is not None: currtime += time_offset wo = WalkOptions() wo.transfer_penalty = transfer_penalty wo.walking_speed = walking_speed spt = self.graph.shortest_path_tree_retro(origin, dest, State(1, currtime), wo) wo.destroy() vertices, edges = spt.path_retro(origin) ret = list( postprocess_path(vertices, edges, self.vertex_events, self.edge_events)) spt.destroy() return json.dumps(ret, indent=2, cls=SelfEncoderHelper)
def test_load_sample(self): g = TestGTFS() wo = WalkOptions() g.load_gtfs(find_resource("sample-feed.zip")) def leads_to(x, y): vs = [edge.to_v.label for edge in g.get_vertex(x).outgoing] assert vs == list(y), "%s vs %s" % (vs, list(y)) # check that the graph is layed out like we'd expect leads_to("gtfsEMSI", ('gtfsDADAN', )) leads_to("gtfsBEATTY_AIRPORT", ('gtfsAMV', 'gtfsBULLFROG')) leads_to("gtfsNADAV", ('gtfsDADAN', 'gtfsNANAA')) leads_to('gtfsBULLFROG', ('gtfsBEATTY_AIRPORT', 'gtfsFUR_CREEK_RES')) leads_to('gtfsAMV', ('gtfsBEATTY_AIRPORT', )) leads_to('gtfsNANAA', ('gtfsNADAV', 'gtfsSTAGECOACH')) leads_to('gtfsDADAN', ('gtfsNADAV', 'gtfsEMSI')) leads_to('gtfsSTAGECOACH', ('gtfsBEATTY_AIRPORT', 'gtfsNANAA')) leads_to('gtfsFUR_CREEK_RES', ('gtfsBULLFROG', )) s = State(1, 1219842000) #6 am august 27, 2008, America/Los_Angeles # walk one edge edge_to_airport = g.get_vertex("gtfsSTAGECOACH").outgoing[0] sprime = edge_to_airport.walk(s, wo) assert sprime.time == 1219843200 assert sprime.weight == 1200 # find a sample route spt = g.shortest_path_tree("gtfsSTAGECOACH", None, s) vertices, edges = spt.path("gtfsBULLFROG") assert spt.get_vertex( "gtfsBULLFROG" ).payload.time == 1219849800 #8:10 am wed august 27, 2008, America/Los_Angeles assert [v.label for v in vertices ] == ['gtfsSTAGECOACH', 'gtfsBEATTY_AIRPORT', 'gtfsBULLFROG'] s = State(1, 1202911200) #6am feb 13, 2008, America/Los_Angeles edge_to_airport = g.get_vertex("gtfsSTAGECOACH").outgoing[0] sprime = edge_to_airport.walk(s, wo) assert sprime.time == 1202912400 assert sprime.weight == 1200 spt = g.shortest_path_tree("gtfsSTAGECOACH", None, s) vertices, edges = spt.path("gtfsBULLFROG") assert spt.get_vertex( "gtfsBULLFROG" ).payload.time == 1202919000 #8:10 am feb 13, 2008, America/Los_Angeles assert [v.label for v in vertices ] == ['gtfsSTAGECOACH', 'gtfsBEATTY_AIRPORT', 'gtfsBULLFROG']
def _walk_edges_general(self, forward_dir, label, time): vertex = self.gg.get_vertex( label ) init_state = self._parse_init_state(self.gg.numagencies, time) dest_states = [] for edge in vertex.outgoing: if forward_dir: if hasattr( edge.payload, 'collapse' ): collapsed = edge.payload.collapse( init_state ) else: collapsed = edge.payload if collapsed: wo = WalkOptions() dest_states.append( (edge, collapsed, collapsed.walk( init_state,wo )) ) wo.destroy() else: if hasattr( edge.payload, 'collapse_back' ): collapsed = edge.payload.collapse_back( init_state ) else: collapsed = edge.payload if collapsed: wo = WalkOptions() dest_states.append( (edge, collapsed, collapsed.walk_back( init_state,wo )) ) wo.destroy() def sort_states(x,y): if x[2] is None: return 1 if y[2] is None: return -1 else: return cmp(x[2].weight, y[2].weight) dest_states.sort(cmp=sort_states) #sort by weight of final state #==================== ret = ["<?xml version='1.0'?>"] ret.append("<vertex>") ret.append(init_state.to_xml()) ret.append("<outgoing_edges>") for edge, collapsed, sprime in dest_states: ret.append("<edge>") ret.append("<destination label='%s'>" % edge.to_v.label) if sprime: ret.append(sprime.to_xml()) else: ret.append("<state/>") ret.append("</destination>") if collapsed: ret.append("<payload>%s</payload>" % collapsed.to_xml()) else: ret.append("<payload/>") ret.append("</edge>") ret.append("</outgoing_edges>") ret.append("</vertex>") return "".join(ret)
def __init__(self, graph, db_connection_string, time_step=240, walking_speed=1.2, max_walk=1080, walking_reluctance=2, trip_prefix='', logfile = None): self.trip_prefix = trip_prefix self.time_step = time_step self.walk_ops = WalkOptions() self.walk_ops.walking_speed = walking_speed self.walk_ops.max_walk = max_walk self.walk_ops.walking_reluctance = walking_reluctance self.graph = graph self.conn = psycopg2.connect(db_connection_string) self.cursor = self.conn.cursor() self.trip_id = 0 self.trips_calculated = 0 self.logfile = logfile self.run()
def get_cell_weights(self, cellIndex=0, dep_time=0): sys.stderr.write("get_cell_weights cell index: " + str(cellIndex) + "\n") # if the departure time is not specified, set it if (dep_time == 0): dep_time = int(time.time()) wo = WalkOptions() wo.transfer_penalty=60 wo.walking_speed=1.0 wo.walking_reluctance=1.0 wo.max_walk=10000 wo.walking_overage=0.1 core.shortestPathForKDTree(self.graph, State(self.graph.num_agencies,dep_time), wo, cellIndex) yield "Finished!"
def path(self, origin, dest, currtime=None, time_offset=None, transfer_penalty=0, walking_speed=1.0, hill_reluctance=1.5, turn_penalty=None, walking_reluctance=None, max_walk=None, jsoncallback=None): performance = {} if currtime is None: currtime = int(time.time()) if time_offset is not None: currtime += time_offset # time path query t0 = time.time() wo = WalkOptions() wo.transfer_penalty=transfer_penalty wo.walking_speed=walking_speed wo.hill_reluctance=hill_reluctance if turn_penalty is not None: wo.turn_penalty = turn_penalty if walking_reluctance is not None: wo.walking_reluctance = walking_reluctance if max_walk is not None: wo.max_walk = max_walk spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo ) try: vertices, edges = spt.path( dest ) except Exception, e: return json.dumps( {'error':str(e)} )
def path(self, origin, dest, currtime=None, time_offset=None, transfer_penalty=0, walking_speed=1.0, hill_reluctance=1.5, turn_penalty=None, walking_reluctance=None, max_walk=None, jsoncallback=None): performance = {} if currtime is None: currtime = int(time.time()) if time_offset is not None: currtime += time_offset # time path query t0 = time.time() wo = WalkOptions() wo.transfer_penalty=transfer_penalty wo.walking_speed=walking_speed wo.hill_reluctance=hill_reluctance if turn_penalty is not None: wo.turn_penalty = turn_penalty if walking_reluctance is not None: wo.walking_reluctance = walking_reluctance if max_walk is not None: wo.max_walk = max_walk spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo, maxtime=4000000000, hoplimit=2000000, weightlimit=4000000000 ) try: vertices, edges = spt.path( dest ) except Exception, e: return json.dumps( {'error':str(e)} )
def test_ch(self): g = Graph() g.add_vertex("A") g.add_vertex("B") g.add_vertex("C") g.add_edge("A", "B", Street("foo", 10)) g.add_edge("B", "C", Street("bar", 10)) g.add_edge("C", "A", Street("baz", 10)) wo = WalkOptions() ch = g.get_contraction_hierarchies(wo) gdb_file = os.path.dirname(__file__) + "unit_test.db" gdb = GraphDatabase(gdb_file) gdb.populate(ch.upgraph) laz = gdb.incarnate() combo = laz.edges[1] self.assertEqual(combo.payload.get(0).name, "baz") self.assertEqual(combo.payload.get(1).name, "foo") os.remove(gdb_file)
def vertex(self, label, currtime=None): currtime = currtime or int(time.time()) ret = [] ret.append( "<h1>%s</h1>"%label ) ret.append( "<h3>incoming from:</h3>" ) for i, (vertex1, vertex2, edgetype) in enumerate( self.graphdb.all_incoming( label ) ): s1 = State(1,int(currtime)) wo = WalkOptions() s0 = edgetype.walk_back( s1, wo ) wo.destroy() if s0: toterm = "<a href=\"/vertex?label="%s"&currtime=%d\">%s@%d</a>"%(vertex1, s0.time, vertex1, s1.time) else: toterm = "<a href=\"/vertex?label="%s"\">%s</a>"%(vertex1, vertex1) ret.append( "%s<br><pre> via %s (<a href=\"/incoming?label="%s"&edgenum=%d\">details</a>)</pre>"%(toterm, cgi.escape(repr(edgetype)), vertex2, i) ) if s0: ret.append( "<pre> %s</pre>"%cgi.escape(str(s0)) ) ret.append( "<h3>outgoing to:</h3>" ) for i, (vertex1, vertex2, edgetype) in enumerate( self.graphdb.all_outgoing( label ) ): s0 = State(1,int(currtime)) wo = WalkOptions() s1 = edgetype.walk( s0, wo ) wo.destroy() if s1: toterm = "<a href=\"/vertex?label="%s"&currtime=%d\">%s@%d</a>"%(vertex2, s1.time, vertex2, s1.time) else: toterm = "<a href=\"/vertex?label="%s"\">%s</a>"%(vertex2, vertex2) ret.append( "%s<br><pre> via %s (<a href=\"/outgoing?label="%s"&edgenum=%d\">details</a>)</pre>"%(toterm, cgi.escape(repr(edgetype)), vertex1, i) ) if s1: ret.append( "<pre> %s</pre>"%cgi.escape(str(s1)) ) return "".join(ret)
def main(count): print 'Loading boston.osmdb' nodedb = osmdb.OSMDB(DATA_DIR + 'boston.osmdb') print 'Importing Boston street network...' gdb = GraphDatabase(DATA_DIR + 'boston.gdb') graph = gdb.incarnate() print 'Importing trip network...' tripdb = osmdb.OSMDB(DATA_DIR + 'trip_data.db') stime = time() wo = WalkOptions() cursor = tripdb.get_cursor() tripcount = 0 # For each station for tnode in tripdb.nodes(): lat1 = float(tnode[2]) lng1 = float(tnode[3]) # find origin node on the street network orig = nodedb.nearest_node(lat1, lng1) # get all trips departing this station tedges = cursor.execute("select * from edges where start_nd = ?", [tnode[0]]) # For each trip for tedge in tedges: dnode = tripdb.node(tedge[3]) lat2 = float(dnode[2]) lng2 = float(dnode[3]) if lat2 == lat1 and lng2 == lng1: # Do not route something that ends where it begins print 'Begin and end node are the same.' else: # find the destination node on the street network dest = nodedb.nearest_node(lat2, lng2) # route! spt = graph.shortest_path_tree('osm-' + orig[0], 'osm-' + dest[0], State(1, stime), wo) # get the path vertices and edges pvert, pedges = spt.path('osm-' + dest[0]) # convert the results to geometries allgeom = [] for e in pedges: dbedge = nodedb.edge(e.payload.name) if e.payload.reverse_of_source: allgeom.extend(reversed(dbedge[5])) else: allgeom.extend(dbedge[5]) print allgeom tripcount += 1 if tripcount >= count: break if tripcount >= count: break
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 vertex(self, label, currtime=None, hill_reluctance=1.5, walking_speed=0.85): currtime = currtime or int(time.time()) ret = [] ret.append("<h1>%s</h1>" % label) wo = WalkOptions() ret.append("<h3>walk options</h3>") ret.append("<li>transfer_penalty: %s</li>" % wo.transfer_penalty) ret.append("<li>turn_penalty: %s</li>" % wo.turn_penalty) ret.append("<li>walking_speed: %s</li>" % wo.walking_speed) ret.append("<li>walking_reluctance: %s</li>" % wo.walking_reluctance) ret.append("<li>uphill_slowness: %s</li>" % wo.uphill_slowness) ret.append("<li>downhill_fastness: %s</li>" % wo.downhill_fastness) ret.append("<li>hill_reluctance: %s</li>" % wo.hill_reluctance) ret.append("<li>max_walk: %s</li>" % wo.max_walk) ret.append("<li>walking_overage: %s</li>" % wo.walking_overage) ret.append("<h3>incoming from:</h3>") for i, (vertex1, vertex2, edgetype) in enumerate(self.graphdb.all_incoming(label)): s1 = State(1, int(currtime)) wo = WalkOptions() wo.hill_reluctance = hill_reluctance wo.walking_speed = walking_speed s0 = edgetype.walk_back(s1, wo) if s0: toterm = "<a href=\"/vertex?label="%s"&currtime=%d\">%s@%d</a>" % ( vertex1, s0.time, vertex1, s1.time) else: toterm = "<a href=\"/vertex?label="%s"\">%s</a>" % ( vertex1, vertex1) ret.append( "%s<br><pre> via %s (<a href=\"/incoming?label="%s"&edgenum=%d\">details</a>)</pre>" % (toterm, cgi.escape(repr(edgetype)), vertex2, i)) if s0: ret.append("<pre> %s</pre>" % cgi.escape(str(s0))) ret.append("<h3>outgoing to:</h3>") for i, (vertex1, vertex2, edgetype) in enumerate(self.graphdb.all_outgoing(label)): s0 = State(1, int(currtime)) wo = WalkOptions() wo.hill_reluctance = hill_reluctance wo.walking_speed = walking_speed s1 = edgetype.walk(s0, wo) if s1: toterm = "<a href=\"/vertex?label="%s"&currtime=%d\">%s@%d</a>" % ( vertex2, s1.time, vertex2, s1.time) else: toterm = "<a href=\"/vertex?label="%s"\">%s</a>" % ( vertex2, vertex2) ret.append( "%s<br><pre> via %s (<a href=\"/outgoing?label="%s"&edgenum=%d\">details</a>)</pre>" % (toterm, cgi.escape(repr(edgetype)), vertex1, i)) if s1: ret.append("<pre> %s</pre>" % cgi.escape(str(s1))) wo.destroy() return "".join(ret)
print "search date: ", d0s print "search time: ", time.ctime(t0), t0 gtfsdb = GTFSDatabase("../data/trimet-20100117.gtfsdb") gdb = GraphDatabase("../data/trimet-linked-20100117.gsdb") g = gdb.incarnate() station_labels = [s[0] for s in gtfsdb.stops()] origins = station_labels[:] destinations = station_labels[:] random.shuffle(origins) random.shuffle(destinations) pairs = zip(origins, destinations)[:SAMPLE_SIZE] wo = WalkOptions() wo.max_walk = 1000 # about 1/2 mile wo.walking_overage = 0.2 wo.walking_speed = 0.8 # trimet uses 0.03 miles / 1 minute wo.transfer_penalty = 0 wo.walking_reluctance = 1 residuals = [] magnitudes = [] normalize = 0 for o, d in pairs: og = gtfsdb.stop(o) dg = gtfsdb.stop(d) # print 'Origin (mds, gtfs)' # print o, og # print 'Destination (mds, gtfs)'
def path_xml(self, origlon, origlat, destlon, destlat, dep_time=0, arr_time=0, max_results=1, timezone="", transfer_penalty=60, walking_speed=1.0, walking_reluctance=1.0, max_walk=10000, walking_overage=0.1, seqno=0, street_mode="walk", transit_mode="Both", less_walking="False", udid="", version="2.0", two_way_routing="True"): if (two_way_routing == "False"): self.two_way_routing = False # set hard bounds on max_results [0,25] if (max_results < 0): max_results = 0 elif (max_results > 25): max_results = 25 sys.stderr.write("[xml_path_entry_point," + str(time.time()) + "] \"origlon=" + str(origlon) + "&origlat=" + str(origlat) + "&destlon=" + str(destlon) + "&destlat=" + str(destlat) + "&dep_time=" + str(dep_time) + "&arr_time=" + str(arr_time) + "&timezone=" + str(timezone) + "&transfer_penalty=" + str(transfer_penalty) + "&walking_speed=" + str(walking_speed) + "&walking_reluctance=" + str(walking_reluctance) + "&max_walk=" + str(max_walk) + "&walking_overage=" + str(walking_overage) + "&seqno=" + str(seqno) + "&street_mode=\"" + str(street_mode) + "\"&less_walking=\"" + str(less_walking) + "\"&udid=\"" + str(udid) + "\"&version=" + str(version) + "\"\n") if (origlon <= ChicagoMap.bounding_lon_left or origlon >= ChicagoMap.bounding_lon_right or origlat <= ChicagoMap.bounding_lat_bottom or origlat >= ChicagoMap.bounding_lat_top or destlon <= ChicagoMap.bounding_lon_left or destlon >= ChicagoMap.bounding_lon_right or destlat <= ChicagoMap.bounding_lat_bottom or destlat >= ChicagoMap.bounding_lat_top): sys.stderr.write("[exit_outside_bounding_box," + str(time.time()) + "]\n") raise RoutingException #return '<?xml version="1.0"?><routes></routes>' # initialize spt to 'None' object spt = None # initialize walk options object wo = WalkOptions() # create database connections pgosmdb_conn = self.pgosmdb.create_pgosmdb_connection() pggtfsdb_conn = self.pggtfsdb.create_pggtfsdb_connection() try: # if the departure time is not specified, set it if (dep_time == 0): dep_time = int(time.time()) # if the timezone is not specified, default to "America/Chicago" if (timezone == ""): timezone = "America/Chicago" # get origin and destination nodes from osm map sys.stderr.write("[get_osm_vertex_from_coords," + str(time.time()) + "]\n") orig_osm, orig_osm_dist = self.pgosmdb.get_osm_vertex_from_coords(pgosmdb_conn, origlon, origlat) dest_osm, dest_osm_dist = self.pgosmdb.get_osm_vertex_from_coords(pgosmdb_conn, destlon, destlat) #print "\nOrigin OSM: " + str(orig_osm) + " (" + str(orig_osm_dist) + ")" #print "Destination OSM: " + str(dest_osm) + " (" + str(dest_osm_dist) + ")\n" # get origin and destination nodes from gtfs database sys.stderr.write("[get_station_vertex_from_coords," + str(time.time()) + "]\n") orig_sta, orig_sta_dist = self.pggtfsdb.get_station_vertex_from_coords(pggtfsdb_conn, origlon, origlat) dest_sta, dest_sta_dist = self.pggtfsdb.get_station_vertex_from_coords(pggtfsdb_conn, destlon, destlat) #print "Origin STA: " + str(orig_sta) + " (" + str(orig_sta_dist) + ")" #print "Destination STA: " + str(dest_sta) + " (" + str(dest_sta_dist) + ")\n" # get coordinates for origin node if (orig_osm_dist < orig_sta_dist): origin = orig_osm sys.stderr.write("[get_coords_for_osm_vertex," + str(time.time()) + "]\n") orig_node_lat, orig_node_lon = self.pgosmdb.get_coords_for_osm_vertex(pgosmdb_conn, origin) else: origin = orig_sta sys.stderr.write("[get_coords_for_station_vertex," + str(time.time()) + "]\n") orig_node_lat, orig_node_lon = self.pggtfsdb.get_coords_for_station_vertex(pggtfsdb_conn, origin) # get coordinates for destination node if (dest_osm_dist < dest_sta_dist): dest = dest_osm sys.stderr.write("[get_coords_for_osm_vertex," + str(time.time()) + "]\n") dest_node_lat, dest_node_lon = self.pgosmdb.get_coords_for_osm_vertex(pgosmdb_conn, dest) else: dest = dest_sta sys.stderr.write("[get_coords_for_station_vertex," + str(time.time()) + "]\n") dest_node_lat, dest_node_lon = self.pggtfsdb.get_coords_for_station_vertex(pggtfsdb_conn, dest) #print "Origin: " + str(origin) #print "Destination: " + str(dest) + "\n" #print "Origin coords: " + str(orig_node_lat) + ", " + str(orig_node_lon) #print "Destination coords: " + str(dest_node_lat) + ", " + str(dest_node_lon) # determine distance from actual origin/destination to osm nodes orig_distance = vincenty(float(origlat), float(origlon), orig_node_lat, orig_node_lon) dest_distance = vincenty(dest_node_lat, dest_node_lon, float(destlat), float(destlon)) #print "Origin distance: " + str(orig_distance) #print "Destination distance: " + str(dest_distance) # calculate time to origin and destination nodes (seconds) time_to_orig = int(round(float( float(orig_distance) / float(walking_speed) ))) time_to_dest = int(round(float( float(dest_distance) / float(walking_speed) ))) #print "Origin time: " + str(time_to_orig) #print "Destination time: " + str(time_to_dest) # adjust departure time by time needed to reach origin node dep_time = (dep_time + time_to_orig) # adjust arrival time by time needed to reach destination node if (arr_time != 0): arr_time = (arr_time - time_to_dest) #print "Adjusted departure time: " + str(dep_time) # set walk options wo.transfer_penalty=transfer_penalty wo.walking_speed=walking_speed wo.walking_reluctance=walking_reluctance wo.max_walk=max_walk wo.walking_overage=walking_overage # check for wheelchair street_mode if (street_mode == "wheelchair"): wo.with_wheelchair = int(True) wo.transfer_penalty = 180 else: wo.with_wheelchair = int(False) # check for bike street_mode if (street_mode == "bike"): wo.transfer_penalty = 120 # check for transit_mode if (transit_mode == "Both"): wo.transit_types = int(14) elif (transit_mode == "Bus"): wo.transit_types = int(8) elif (transit_mode == "Rail"): wo.transit_types = int(6) elif (transit_mode == "None"): wo.transit_types = int(0) # check for less_walking flag if (less_walking == "True"): wo.walking_reluctance *= 10.0 # create RouteInfo object route_info = RouteInfo() route_info.origlat = origlat route_info.origlon = origlon route_info.dep_time_diff = time_to_orig route_info.destlat = destlat route_info.destlon = destlon route_info.arr_time_diff = time_to_dest route_info.street_mode = street_mode yield "--multipart-path_xml-boundary1234\n"; # loop to create multiple responses for q in range(max_results): if (spt is not None): spt.destroy_no_hash() route_info.first_edge = True route_info.last_edge = False # initialize return string ret_string = 'Content-Type: text/xml\n\n<?xml version="1.0"?><routes>' if (arr_time == 0): (spt, edges, vertices) = self.shortest_path(origin,dest,dep_time,wo) else: (spt, edges, vertices) = self.shortest_path_retro(origin,dest,arr_time,wo) # if there are no edges or vertices (i.e., there is no path found) if ((edges is None) or (vertices is None)): raise RoutingException # create WalkPath object walk_path = WalkPath() walk_path.lastlat = origlat walk_path.lastlon = origlon walk_path.timezone = timezone # string to store returned route curr_route = "" route_info.actual_dep_time = vertices[0].payload.time - time_to_orig route_info.actual_arr_time = vertices[-1].payload.time + time_to_dest # iterate through all edges in the route sys.stderr.write("[edges_loop," + str(time.time()) + "]\n") # determine the number of edges edges_len = len(edges) for i in range(edges_len): if (i == (edges_len-1)): route_info.last_edge = True elif (i == (edges_len-2) and edges[i+1].payload.__class__ == graphserver.core.Link): route_info.last_edge = True elif (i == (edges_len-3) and edges[i+1].payload.__class__ == graphserver.core.Link and edges[i+2].payload.__class__ == graphserver.core.Link): route_info.last_edge = True edgetype = edges[i].payload.__class__ if edgetype in self.event_dispatch: (new_event, walk_path, route_info) = self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1], walk_path, route_info, pgosmdb_conn, pggtfsdb_conn) curr_route += new_event ret_string += '<route dep_time="' + str(route_info.actual_dep_time) + '" req_dep_time="' + str(dep_time - time_to_orig) + '" arr_time="' + str(route_info.actual_arr_time) + '" req_arr_time="' + str(arr_time) + '" origlat="' + str(origlat) + '" origlon="' + str(origlon) + '" destlat="' + str(destlat) + '" destlon="' + str(destlon) + '" timezone="' + timezone + '" total_time="' + str(route_info.actual_arr_time - route_info.actual_dep_time) + '" total_walk_distance="' + str(int(round(walk_path.total_distance)) + int(round(orig_distance)) + int(round(dest_distance))) + '" walking_speed="' + str(walking_speed) + '" seqno="' + str(seqno) + '" version="' + str(version) + '">' + curr_route + '</route>' # close return string if q == max_results: ret_string += '</routes>\n\n--multipart-path_xml-boundary1234--\n\n' else: ret_string += '</routes>\n\n--multipart-path_xml-boundary1234\n' sys.stderr.write("[xml_path_exit_point," + str(time.time()) + "]\n") # return routes xml yield xstr(str(ret_string)) if arr_time == 0: dep_time = route_info.actual_dep_time + time_to_orig + 60 else: arr_time = route_info.actual_arr_time - time_to_dest - 60 except RoutingException: if (spt is not None): spt.destroy_no_hash() yield '\n\n--multipart-path_xml-boundary1234\nContent-Type: text/xml\n\n<?xml version="1.0"?><routes></routes>--multipart-path_xml-boundary1234--\n\n ' finally: #yield '\n\n--multipart-path_xml-boundary1234--\n\n' # close database connections self.pgosmdb.close_pgosmdb_connection(pgosmdb_conn) self.pggtfsdb.close_pggtfsdb_connection(pggtfsdb_conn) # destroy WalkOptions object wo.destroy() # destroy shortest path tree if (spt is not None): spt.destroy_no_hash()
def test_get_route(self): "Check it finds the route we expect" g = Graph() reader = csv.reader(open("../performance_test/map.csv")) for wayid, fromv, tov, length in reader: g.add_vertex(fromv) g.add_vertex(tov) g.add_edge(fromv, tov, Street(wayid, float(length))) v85thStreet = "53184534" vBeaconAve = "53072051" idealVertices = [ '53184534', '53193013', '69374666', '53193014', '69474340', '53185600', '53077802', '69474361', '53090673', '53193015', '53193016', '53193017', '53193018', '53189027', '53193019', '53193020', '53112767', '53193021', '69516594', '53132048', '69516588', '53095152', '53132049', '53239899', '53147269', '53138815', '69516553', '53138764', '53194375', '53185509', '53194376', '53144840', '53178633', '53178635', '53194364', '53125622', '53045160', '53194365', '53194366', '53194367', '53194368', '53185796', '53194369', '53086028', '90251330', '90251121', '30789993', '30789998', '31394282', '31393878', '29977892', '124205994', '31428350', '29545469', '94008501', '29545421', '29545417', '29545423', '29484769', '29484785', '29545373', '29979589', '30078988', '30079048', '244420183', '29979596', '29979598', '30230262', '30230264', '30279409', '30279408', '30230266', '30230273', '30230277', '30230281', '30230300', '30230506', '30231231', '30230962', '60878121', '53224639', '53210038', '53081902', '53052413', '53210039', '53224626', '53168444', '53224629', '53224632', '53208783', '53083017', '53083040', '53208784', '53187334', '53187337', '53089335', '53066732', '53208785', '53178012', '53208786', '53152490', '53183929', '53146692', '53146065', '53083086', '53083102', '53113957', '53113944', '53190685', '53203056', '53167007', '53129046', '53098715', '53208787', '53208788', '53180738', '53072051' ] idealEdges = [ '9112003-8', '6438432-0', '6438432-1', '6438432-2', '6438432-3', '6438432-4', '6438432-5', '6438432-6', '6438432-7', '6438432-8', '6438432-9', '6438432-10', '6438432-11', '6438432-12', '6438432-13', '6438432-14', '6438432-15', '6438432-16', '6438432-17', '6386686-0', '6386686-1', '6386686-2', '6497278-2', '6497278-3', '6497278-4', '6497278-5', '6497278-6', '6514850-51', '6439614-0', '6439614-1', '6439614-2', '6439614-3', '15255537-1', '6439607-0', '6439607-1', '6439607-2', '6439607-3', '6439607-4', '6439607-5', '6439607-6', '6439607-7', '6439607-8', '6439607-9', '6439607-10', '10497741-3', '10497743-3', '4709507-4', '4709507-5', '4709507-6', '4709507-7', '4709507-8', '4869151-0', '4869146-0', '4869146-1', '4869146-2', '4869146-3', '4869146-4', '4644156-0', '4722460-0', '4722460-1', '4722460-2', '4722460-3', '4722460-4', '4722460-5', '4722460-6', '14017470-0', '14017470-1', '5130429-0', '13866257-0', '13866256-0', '4748963-0', '4748962-0', '4748962-1', '15257844-0', '15257848-0', '15257848-1', '4743936-0', '4743934-0', '4743897-3', '4743897-4', '8116116-0', '6457969-20', '6457969-21', '6457969-22', '6476943-0', '6476943-1', '6476943-2', '6476943-3', '6476943-4', '6456455-20', '6456455-21', '6456455-22', '6456455-23', '6456455-24', '6456455-25', '6456455-26', '6456455-27', '6456455-28', '6456455-29', '6456455-30', '6456455-31', '6456455-32', '6456455-33', '6456455-34', '6456455-35', '6456455-36', '6456455-37', '6456455-38', '6456455-39', '6456455-40', '6456455-41', '6456455-42', '6456455-43', '6456455-44', '6456455-45', '6456455-46' ] t0 = time.time() spt = g.shortest_path_tree(v85thStreet, vBeaconAve, State(g.numagencies, 0), WalkOptions()) t1 = time.time() print "time:", (t1 - t0) * 1000 vertices, edges = spt.path(vBeaconAve) assert spt.get_vertex("53072051").payload.time == 31439 assert spt.get_vertex("53072051").payload.weight == 17311963 assert spt.get_vertex("53072051").payload.dist_walked == 26774.100248 assert (False not in [ l == r for l, r in zip([v.label for v in vertices], idealVertices) ]) assert (False not in [ l == r for l, r in zip([e.payload.name for e in edges], idealEdges) ]) vBallardAve = "53115442" vLakeCityWay = "124175598" idealVertices = [ '53115442', '53115445', '53115446', '53227448', '53158020', '53105937', '53148458', '53077817', '53077819', '53077821', '53077823', '53077825', '60413953', '53097655', '60413955', '53196479', '53248412', '53245437', '53153886', '53181632', '53246786', '53078069', '53247761', '53129527', '53203543', '53248413', '53182343', '53156127', '53227471', '53240242', '53109739', '53248420', '53234775', '53170822', '53115167', '53209384', '53134650', '53142180', '53087702', '53184534', '53193013', '69374666', '53193014', '69474340', '53185600', '53077802', '69474361', '53090673', '53193015', '53193016', '53193017', '53193018', '53189027', '53193019', '53193020', '53112767', '53193021', '53183554', '53213063', '53197105', '53213061', '53090659', '53213059', '53157290', '53062869', '53213057', '53213055', '53213054', '53184527', '67507140', '67507145', '67507034', '67507151', '67507040', '67507158', '67507048', '67507166', '67507051', '67507176', '67507057', '67507126', '53233319', '53147253', '53233320', '53233321', '60002786', '60002787', '88468933', '53125662', '53195800', '88486410', '53228492', '88486425', '53215121', '88486457', '53199820', '53185765', '53233322', '53227223', '88486676', '53086030', '53086045', '53204778', '88486720', '53204762', '88486429', '53139133', '53139142', '88486453', '53072465', '30790081', '30790104', '53072467', '124181376', '30759113', '53072469', '53072472', '53072473', '53072475', '53072476', '53072477', '53072478', '124175598' ] idealEdges = [ '6372784-0', '6372784-1', '6480699-3', '6517019-4', '6517019-5', '6517019-6', '6517019-7', '6346366-0', '6346366-1', '6346366-2', '6346366-3', '10425981-2', '8072147-2', '8072147-3', '6441828-10', '22758990-0', '6511156-0', '6511156-1', '6511156-2', '6511156-3', '6511156-4', '6511156-5', '6511156-6', '6511156-7', '6511156-8', '6511156-9', '6511156-10', '6511156-11', '6511156-12', '6511156-13', '6511156-14', '9112003-0', '9112003-1', '9112003-2', '9112003-3', '9112003-4', '9112003-5', '9112003-6', '9112003-7', '9112003-8', '6438432-0', '6438432-1', '6438432-2', '6438432-3', '6438432-4', '6438432-5', '6438432-6', '6438432-7', '6438432-8', '6438432-9', '6438432-10', '6438432-11', '6438432-12', '6438432-13', '6438432-14', '6438432-15', '10425996-0', '10425996-1', '10425996-2', '10425996-3', '10425996-4', '10425996-5', '10425996-6', '10425996-7', '10425996-8', '10425996-9', '10425996-10', '10425996-11', '10425996-12', '9116336-2', '9116336-3', '9116346-1', '9116346-2', '9116346-3', '9116346-4', '9116346-5', '9116346-6', '9116346-7', '9116346-8', '9116346-9', '6488959-1', '6488959-2', '6488959-3', '6488959-4', '6488959-5', '6488959-6', '6488959-7', '6488959-8', '6488959-9', '6488959-10', '6488959-11', '6488959-12', '6488959-13', '6488959-14', '6488959-15', '6488959-16', '6488959-17', '6488959-18', '6488959-19', '6488959-20', '6488959-21', '6488959-22', '6488959-23', '6488959-24', '6488959-25', '6488959-26', '6488959-27', '6488959-28', '6488959-29', '6344932-0', '6344932-1', '6344932-2', '13514591-0', '13514602-0', '13514602-1', '13514602-2', '8591344-0', '8591344-1', '8591344-2', '8591344-3', '8591344-4', '8591344-5' ] t0 = time.time() spt = g.shortest_path_tree(vBallardAve, vLakeCityWay, State(g.numagencies, 0), WalkOptions()) t1 = time.time() print "time: ", (t1 - t0) * 1000 vertices, edges = spt.path(vLakeCityWay) assert spt.get_vertex("124175598").payload.time == 13684 assert spt.get_vertex("124175598").payload.weight == 190321 assert (False not in [ l == r for l, r in zip([v.label for v in vertices], idealVertices) ]) assert (False not in [ l == r for l, r in zip([e.payload.name for e in edges], idealEdges) ]) #one last time vSandPointWay = "32096172" vAirportWay = "60147448" idealVertices = [ '32096172', '60411560', '32096173', '32096176', '53110403', '32096177', '32096180', '53208261', '32096181', '60411559', '32096184', '53164136', '32096185', '32096190', '32096191', '32096194', '53123806', '32096196', '32096204', '53199337', '32096205', '32096208', '60411513', '32096209', '53040444', '32096212', '60411512', '53208255', '32096216', '53079385', '53079384', '32096219', '31192107', '31430499', '59948312', '31430457', '31430658', '29973173', '31430639', '29977895', '30012801', '31430516', '30012733', '29464742', '32271244', '31430321', '29464754', '31430318', '29973106', '31429815', '29464758', '31429758', '32103448', '60701659', '29464594', '29463661', '59677238', '59677231', '29463657', '29463479', '29449421', '29449412', '29545007', '29545373', '29979589', '30078988', '30079048', '244420183', '29979596', '29979598', '30230262', '30230264', '30279409', '30279408', '30230266', '30230273', '30230277', '30230281', '30230300', '30230506', '30231566', '30231379', '30230524', '30887745', '30887637', '30887631', '30887106', '60147424', '53131178', '53128410', '53131179', '53027159', '60147448' ] idealEdges = [ '4910430-0', '4910430-1', '4910417-0', '4910416-0', '4910416-1', '4910414-0', '4910413-0', '4910413-1', '4910412-0', '4910412-1', '4910410-0', '4910410-1', '4910408-0', '4910405-0', '4910405-1', '4910405-2', '4910405-3', '4910402-0', '4910399-0', '4910399-1', '4910397-0', '4910394-0', '4910394-1', '4910392-0', '4910392-1', '4910385-0', '4910385-1', '4910385-2', '4910385-3', '4910385-4', '4910385-5', '4910384-0', '4910384-1', '4869358-0', '4869358-1', '4869358-2', '4869358-3', '4869357-0', '4869357-1', '4869357-2', '4869357-3', '4869357-4', '4869357-5', '4636137-0', '4636137-1', '4636137-2', '4636137-3', '4636137-4', '4636137-5', '4636137-6', '4708973-0', '4708973-1', '4708973-2', '4708973-3', '4636201-0', '4708972-0', '4708972-1', '4708972-2', '4636105-0', '4636093-0', '4729956-0', '4644053-0', '4644064-0', '4722460-2', '4722460-3', '4722460-4', '4722460-5', '4722460-6', '14017470-0', '14017470-1', '5130429-0', '13866257-0', '13866256-0', '4748963-0', '4748962-0', '4748962-1', '15257844-0', '15257848-0', '15257848-1', '15257848-2', '15257848-3', '15257848-4', '4810339-0', '4810342-0', '4810342-1', '4810337-0', '4810290-0', '8044406-0', '15240328-7', '15240328-8', '15240328-9', '15240328-10' ] spt = g.shortest_path_tree(vSandPointWay, vAirportWay, State(g.numagencies, 0), WalkOptions()) vertices, edges = spt.path(vAirportWay) assert spt.get_vertex("60147448").payload.time == 21082 print spt.get_vertex("60147448").payload.weight assert spt.get_vertex("60147448").payload.weight == 4079909 assert (False not in [ l == r for l, r in zip([v.label for v in vertices], idealVertices) ]) assert (False not in [ l == r for l, r in zip([e.payload.name for e in edges], idealEdges) ])
def run(self): self.gtfsdb = GTFSDatabase(self.gtfsdb) self.gdb = GraphDatabase(self.gdb) # Calculate an origin-destination matrix for the graph's stations print "Loading Graphserver DB..." self.emit(QtCore.SIGNAL("say(QString)"), QtCore.QString("Loading SQLite Graphserver graph...")) g = self.gdb.incarnate() # Set up distance-preserving projection system # Make a grid over the study area and save its geographic coordinates MARGIN = 8000 # meters beyond all stations, diagonally min_lon, min_lat, max_lon, max_lat = self.gtfsdb.extent() geod = pyproj.Geod(ellps="WGS84") min_lon, min_lat, arc_dist = geod.fwd(min_lon, min_lat, 180 + 45, MARGIN) max_lon, max_lat, arc_dist = geod.fwd(max_lon, max_lat, 45, MARGIN) proj = pyproj.Proj(proj="sinu", ellps="WGS84") min_x, min_y = proj(min_lon, min_lat) proj = pyproj.Proj( proj="sinu", ellps="WGS84", lon_0=min_lon, y_0=-min_y ) # why doesn't m parameter work for scaling by 100? grid_dim = array(proj(max_lon, max_lat), dtype=int32) / 100 max_x, max_y = grid_dim print "\nMaking grid with dimesions: ", max_x, max_y self.emit(QtCore.SIGNAL("say(QString)"), QtCore.QString("Making %i by %i grid..." % (max_x, max_y))) # later, use reshape/flat to switch between 1d and 2d array representation grid_latlon = empty((max_x, max_y, 2), dtype=float32) for y in range(0, max_y): self.emit(QtCore.SIGNAL("progress(int, int)"), y, max_y) for x in range(0, max_x): # inverse project meters to lat/lon grid_latlon[x, y] = proj(x * 100, y * 100, inverse=True) station_vertices = [v for v in g.vertices if v.label[0:4] == "sta-"] station_labels = [v.label for v in station_vertices] n_stations = len(station_vertices) print "Finding station coordinates..." self.emit(QtCore.SIGNAL("say(QString)"), QtCore.QString("Projecting station coordinates...")) station_coords = empty((n_stations, 2), dtype=float32) for i, label in enumerate(station_labels): stop_id, stop_name, lat, lon = self.gtfsdb.stop(label[4:]) station_coords[i] = proj(lon, lat) if i % 20 == 0: self.emit(QtCore.SIGNAL("progress(int, int)"), i, n_stations) station_coords /= 100 # ELIMINATE STATIONS WITH SAME INTEGRAL COORDINATES # self.emit( QtCore.SIGNAL( 'say(QString)' ), QtCore.QString( 'Eliminating equivalent stations...' ) ) # while len(station_coords) > 0 : # coord = # mask = station_coords != station_coords[i] # station_coords = station_coords[mask] # newer version follows # self.emit( QtCore.SIGNAL( 'say(QString)' ), QtCore.QString( 'Eliminating equivalent stations...' ) ) # station_labels = np.array(station_labels) # station_coords_new = [] # station_labels_new = [] # while len(station_coords) > 0 : # coord = np.round(station_coords[0]) # minIdx = np.argmin(np.sum(np.abs(station_coords - coord), axis=1)) # station_labels_new.append(station_labels[minIdx]) # station_coords_new.append(station_coords[minIdx]) # mask = np.any(np.round(station_coords) != coord, axis=1) # #print mask # #print len(station_coords) # #print coord # #print station_coords[np.logical_not(mask)] # station_coords = station_coords[mask][:] # station_labels = station_labels[mask][:] # self.emit( QtCore.SIGNAL( 'progress(int, int)' ), n_stations - len(station_coords_new), n_stations ) # # station_labels = station_labels_new # station_coords = station_coords_new # station_vertices = [g.get_vertex(slabel) for slabel in station_labels_new] # n_stations = len(station_labels) # print len(station_labels), len(station_coords), len(station_vertices) print "Making OD matrix..." os.environ["TZ"] = "US/Pacific" time.tzset() t0s = "Tue Mar 09 08:00:00 2010" t0t = time.strptime(t0s) d0s = time.strftime("%a %b %d %Y", t0t) t0 = int(time.mktime(t0t)) print "search date: ", d0s print "search time: ", time.ctime(t0), t0 wo = WalkOptions() wo.max_walk = 20000 wo.walking_overage = 0.1 wo.walking_speed = 1 # trimet uses 0.03 miles / 1 minute wo.transfer_penalty = 60 * 10 wo.walking_reluctance = 2 wo.max_transfers = 40 wo.transfer_slack = 60 * 4 matrix = zeros( (n_stations, n_stations), dtype=float ) # dtype could be uint16 except that there are inf's ---- why? colortable = [QtGui.QColor(i, i, i).rgb() for i in range(256)] colortable[254] = QtGui.QColor(050, 128, 050).rgb() colortable[255] = QtGui.QColor(255, 050, 050).rgb() matrixImage = QtGui.QImage(max_x, max_y, QtGui.QImage.Format_Indexed8) matrixImage.fill(0) matrixImage.setColorTable(colortable) for origin_idx in range(n_stations): sys.stdout.write("\rProcessing %i / %i ..." % (origin_idx, n_stations)) sys.stdout.flush() self.emit( QtCore.SIGNAL("say(QString)"), QtCore.QString("Making OD matrix (station %i/%i)..." % (origin_idx, n_stations)), ) self.emit(QtCore.SIGNAL("progress(int, int)"), origin_idx, n_stations) origin_label = station_labels[origin_idx] # g.spt_in_place(origin_label, None, State(1, t0), wo) spt = g.shortest_path_tree(origin_label, None, State(1, t0), wo) for dest_idx in range(n_stations): dest_label = station_labels[dest_idx] dest_vertex = spt.get_vertex(dest_label) # first board time should be subtracted here # if dest_vertex.payload is None : if dest_vertex is None: print "Unreachable vertex. Set to infinity.", dest_idx, dest_label delta_t = inf else: # delta_t = dest_vertex.best_state.time - t0 bs = dest_vertex.best_state delta_t = bs.time - t0 - bs.initial_wait if delta_t < 0: print "Negative trip time; set to 0." delta_t = 0 matrix[origin_idx, dest_idx] = delta_t # sys.stdout.write( '%i %i\n' % (delta_t, dest_vertex.payload.initial_wait) ) # sys.stdout.flush() # time.sleep(0.5) if dest_idx == origin_idx - 1: color = 254 elif dest_idx == origin_idx: color = 255 else: color = 253 - delta_t * 3 / 60 if color < 0: color = 0 coord = station_coords[dest_idx] x = coord[0] y = coord[1] if color >= 254: for x2 in range(x - 1, x + 2): for y2 in range(y - 1, y + 2): matrixImage.setPixel(x2, y2, color) else: matrixImage.setPixel(x, y, color) self.emit(QtCore.SIGNAL("display(QImage)"), matrixImage) spt.destroy() # time.sleep(1) print x * y, "points, done." self.emit(QtCore.SIGNAL("say(QString)"), QtCore.QString("Saving as gzipped numpy ndarrays...")) savez( "od_matrix.npz", station_labels=station_labels, station_coords=station_coords, grid_dim=grid_dim, grid_latlon=grid_latlon, matrix=matrix, )
stop = v.state.time start_time = datetime.fromtimestamp(start) stop_time = datetime.fromtimestamp(stop) start_time -= origin_walk_penalty finally: if tree: tree.destroy() return (start_time, stop_time) #narrative = list(postprocess_path(vertices, edges)) #return json.dumps({ 'narrative': narrative }, indent=3, cls=SelfEncoderHelper) walk_options = WalkOptions() walk_options.transfer_penalty = 0 walk_options.walking_speed = 1.0 walk_options.hill_reluctance = 1.5 #walk_options.turn_penalty = #walk_options.walking_reluctance = #walk_options.max_walk = # The full Metro King County boundaries #lon_start = -122.506729 #lon_stop = -121.785828 #lat_start = 47.9323654 #lat_stop = 47.1891136 # Seattle & the East side
class Proccessing(): def get_gs_vertex(self, point_id): self.cursor.execute('SELECT vertex_label FROM cal_corres_vertices WHERE point_id=%s', ( point_id, )) return self.cursor.fetchone()[0] def get_point(self, vertex_label): self.cursor.execute('SELECT point_id FROM cal_corres_vertices WHERE vertex_label=%s', ( vertex_label, )) return [x[0] for x in self.cursor][0] def prepare_times(self, start_time, end_time): times = [] start = time.mktime(start_time.timetuple()) end = time.mktime(end_time.timetuple()) t = start while t <= end: times.append(t) t += self.time_step return times def get_route_dict(self): self.cursor.execute('SELECT origin, destination, time FROM cal_routes WHERE NOT done LIMIT 1') row = self.cursor.fetchone() if row: origin, destination, time = row else: # there are no routes to Compute return None self.cursor.execute('SELECT start_time, end_time, is_arrival_time FROM cal_times WHERE id=%s', ( time, )) start_time, end_time, is_arrival = self.cursor.fetchone() if is_arrival: return self.get_retro_dict(destination, time, start_time, end_time) else: return self.get_dict(origin, time, start_time, end_time) def get_retro_dict(self, destination, time, start_time, end_time): self.cursor.execute('''SELECT id, origin FROM cal_routes WHERE destination=%s AND time=%s''', ( destination, time )) origins = list(self.cursor.fetchall()) self.cursor.execute('UPDATE cal_routes SET done=%s WHERE destination=%s AND time=%s', ( True, destination, time )) self.conn.commit() return { 'destination':self.get_gs_vertex(destination), 'times':self.prepare_times(start_time, end_time), 'arrival':True, 'origins':[ ( self.get_gs_vertex(orig[1]), orig[0] ) for orig in origins ] } def get_dict(self, origin, time, start_time, end_time): self.cursor.execute('''SELECT id, destination FROM cal_routes WHERE origin=%s AND time=%s''', ( origin, time )) destinations = list(self.cursor.fetchall()) self.cursor.execute('UPDATE cal_routes SET done=%s WHERE origin=%s AND time=%s', ( True, origin, time )) self.conn.commit() return { 'origin':self.get_gs_vertex(origin), 'times':self.prepare_times(start_time, end_time), 'arrival':False, 'destinations':[ ( self.get_gs_vertex(dest[1]), dest[0] ) for dest in destinations ] } def process_paths(self, routes): for t in routes['times']: s = State(1, t) # build the shortest path tree at time 't' try: if len(routes['destinations']) > 1: spt = self.graph.shortest_path_tree(routes['origin'], None, s, self.walk_ops) else: spt = self.graph.shortest_path_tree(routes['origin'],routes['destinations'][0][0], s, self.walk_ops) # faster but only ONE destination except: pass # extract the actual routes and write them into the database for dest in routes['destinations']: try: vertices, edges = spt.path(dest[0]) if not vertices: raise Exception() except: self.write_error_trip(t, dest[1]) else: self.write_trip(vertices, dest[1]) # cleanup try: spt.destroy() except: pass def process_retro_paths(self, routes): for t in routes['times']: s = State(1, t) # build the shortest path tree at time 't' try: if len(routes['origins']) > 1: spt = self.graph.shortest_path_tree_retro(None, routes['destination'], s,self.walk_ops) else: spt = self.graph.shortest_path_tree_retro(routes['origins'][0][0], routes['destination'], s, self.walk_ops) # faster but only ONE destination except: pass # extract the actual routes and write them into the database for orig in routes['origins']: try: vertices, edges = spt.path_retro(orig[0]) if not vertices: raise Exception() except: self.write_error_trip(t, orig[1]) else: self.write_retro_trip(vertices, orig[1]) # cleanup try: spt.destroy() except: pass def run(self): ''' method for processing (calculating shortest paths) all routes stored inside the databases associated with this object. [only routes with the processed flag not set will be processed] ''' routes = self.get_route_dict() while ( routes ): if routes['arrival']: self.process_retro_paths(routes) else: self.process_paths(routes) routes = self.get_route_dict() def write_retro_trip(self, vertices, route_id): ''' in retro_paths the walking distance is counted in the wrong direction. this method corrects this. ''' # now done in write_results self.write_trip(vertices, route_id) def write_trip(self, vertices, route_id): current_trip_id = str(self.trip_id) self.trip_id += 1 start_time = datetime.datetime.fromtimestamp(vertices[0].state.time) end_time = datetime.datetime.fromtimestamp(vertices[-1].state.time) self.cursor.execute('INSERT INTO cal_paths VALUES (%s,%s,%s,%s,%s)', ( self.trip_prefix + current_trip_id, route_id, start_time, end_time, (vertices[-1].state.time - vertices[0].state.time ) )) for c, v in enumerate(vertices): time = datetime.datetime.fromtimestamp(v.state.time) self.cursor.execute('INSERT INTO cal_paths_details VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', ( self.trip_prefix + current_trip_id, c, v.label, time, v.state.weight, v.state.dist_walked, v.state.num_transfers, v.state.trip_id )) if not self.trips_calculated % 1000: self.conn.commit() self.logfile.write('%s routes calculated by %s, last route: %s \n' %(self.trips_calculated, self.trip_prefix, route_id)) self.logfile.flush() self.trips_calculated += 1 ''' this method will write a very long trip into the database. ''' def write_error_trip(self, start_time, route_id): current_trip_id = str(self.trip_id) self.trip_id += 1 start_date_time = datetime.datetime.fromtimestamp(start_time) end_time = datetime.datetime(2030,12,31) self.cursor.execute('INSERT INTO cal_paths VALUES (%s,%s,%s,%s,%s)', (self.trip_prefix + current_trip_id, route_id, start_date_time, end_time, (time.mktime(end_time.timetuple()) - start_time ) )) def __init__(self, graph, db_connection_string, time_step=240, walking_speed=1.2, max_walk=1080, walking_reluctance=2, trip_prefix='', logfile = None): self.trip_prefix = trip_prefix self.time_step = time_step self.walk_ops = WalkOptions() self.walk_ops.walking_speed = walking_speed self.walk_ops.max_walk = max_walk self.walk_ops.walking_reluctance = walking_reluctance self.graph = graph self.conn = psycopg2.connect(db_connection_string) self.cursor = self.conn.cursor() self.trip_id = 0 self.trips_calculated = 0 self.logfile = logfile self.run() def __del__(self): self.walk_ops.destroy() self.cursor.close() self.conn.commit() self.graph.destroy()
def path(self, lat1, lng1, lat2, lng2, transfer_penalty=0, walking_speed=1.0, hill_reluctance=20, narrative=True, jsoncallback=None): t0 = time.time() origin = "osm-%s" % self.osmdb.nearest_node(lat1, lng1)[0] dest = "osm-%s" % self.osmdb.nearest_node(lat2, lng2)[0] endpoint_find_time = time.time() - t0 print origin, dest t0 = time.time() wo = WalkOptions() #wo.transfer_penalty=transfer_penalty #wo.walking_speed=walking_speed wo.walking_speed = 4 wo.walking_overage = 0 wo.hill_reluctance = 20 wo.turn_penalty = 15 edgepayloads = self.ch.shortest_path(origin, dest, State(1, 0), wo) wo.destroy() route_find_time = time.time() - t0 t0 = time.time() names = [] geoms = [] profile = Profile() total_dist = 0 total_elev = 0 if narrative: names, total_dist = get_full_route_narrative( self.osmdb, edgepayloads) for edgepayload in edgepayloads: geom, profile_seg = self.shortcut_cache.get( edgepayload.external_id) #geom = get_ep_geom( self.osmdb, edgepayload ) #profile_seg = get_ep_profile( self.profiledb, edgepayload ) geoms.extend(geom) profile.add(profile_seg) route_desc_time = time.time() - t0 ret = json.dumps( (names, encode_pairs([(lat, lon) for lon, lat in geoms]), profile.concat(300), { 'route_find_time': route_find_time, 'route_desc_time': route_desc_time, 'endpoint_find_time': endpoint_find_time, }, { 'total_dist': total_dist, 'total_elev': total_elev })) if jsoncallback: return "%s(%s)" % (jsoncallback, ret) else: return ret
def get_walk( lat1, lon1, lat2, lon2 ): vertex1 = get_nearest_vertex( lat1, lon1 ) vertex2 = get_nearest_vertex( lat2, lon2 ) wo = WalkOptions() wo.transfer_penalty = 0 wo.walking_speed = 1.0 wo.hill_reluctance = 1.5 try: spt = graph.shortest_path_tree( vertex1, vertex2, State(1, starttime), wo ) vertices, edges = spt.path( vertex2 ) except Exception: print "couldn't find a path between (%.4f,%4f) and (%.4f,%.4f)" % ( lat1, lon1, lat2, lon2) return None first_walk_time = None last_walk_time = None walk_streets = [] walk_points = [] walk_distance = 0 for edge1,vertex1,edge2,vertex2 in zip( [None]+edges, vertices, edges+[None], vertices[1:]+[None,None] ): edge1payload = edge1.payload if edge1 else None edge2payload = edge2.payload if edge2 else None if edge2 is not None and isinstance(edge2.payload, Street): # Add the street geometry for this edge to our walk_points geometry_chunk = osmdb.edge( edge2.payload.name )[5] if edge2.payload.reverse_of_source: walk_points.extend( reversed( geometry_chunk ) ) else: walk_points.extend( geometry_chunk ) # Add this edge's distance in meters to the walk_distance walk_distance += edge2.payload.length if edge1 and edge2 and isinstance(edge1.payload, Street) and edge1.payload.way != edge2.payload.way: # We hit a turn walk_streets.append(get_street_name_for_edge(edge2)) if (edge1 is None or not isinstance(edge1.payload, Street)) and (edge2 and isinstance(edge2.payload, Street)): # We started walking walk_streets.append(get_street_name_for_edge(edge2)) if first_walk_time is None: first_walk_time = vertex1.state.time if (edge1 and isinstance(edge1.payload, Street)) and (edge2 is None or not isinstance(edge2.payload, Street)): # We stopped walking last_walk_time = vertex1.state.time if first_walk_time is None or last_walk_time is None: walk_time = 0 else: walk_time = last_walk_time - first_walk_time ret = Walk() ret.time = walk_time ret.streets = walk_streets ret.points = walk_points ret.distance = walk_distance / 1609.344 # convert to miles return ret
os.environ['TZ'] = 'US/Pacific' time.tzset() t0s = "Mon May 17 08:50:00 2010" t0t = time.strptime(t0s) d0s = time.strftime('%a %b %d %Y', t0t) t0 = time.mktime(t0t) print 'search date: ', d0s print 'search time: ', time.ctime(t0), t0 gtfsdb = GTFSDatabase ('./trimet.gtfsdb') gdb = GraphDatabase ('./test.gdb' ) osmdb = OSMDB ('./testgrid.osmdb' ) g = gdb.incarnate () # FOOT - would be better if i could specify 0 boardings not 0 transfers wo = WalkOptions() wo.max_walk = 2000 wo.walking_overage = 0.0 wo.walking_speed = 1.0 # trimet uses 0.03 miles / 1 minute - but it uses straight line distance as well wo.transfer_penalty = 99999 wo.walking_reluctance = 1 wo.max_transfers = 0 # make much higher? wo.transfer_slack = 60 * 5 wo_foot = wo # TRANSIT wo = WalkOptions() wo.max_walk = 2000 wo.walking_overage = 0.0 wo.walking_speed = 1.0 # trimet uses 0.03 miles / 1 minute - but it uses straight line distance as well
def test_get_route_retro(self): "Check it finds the route we expect, in reverse" g = Graph() reader = csv.reader(open("../performance_test/map.csv")) for wayid, fromv, tov, length in reader: g.add_vertex(fromv) g.add_vertex(tov) g.add_edge(fromv, tov, Street(wayid, float(length))) v85thStreet = "53184534" vBeaconAve = "53072051" idealVertices = [ '53184534', '53193013', '69374666', '53193014', '69474340', '53185600', '53077802', '69474361', '53090673', '53193015', '53193016', '53193017', '53193018', '53189027', '53193019', '53193020', '53112767', '53193021', '69516594', '53132048', '69516588', '53095152', '53132049', '53239899', '53147269', '53138815', '69516553', '53138764', '53194375', '53185509', '53194376', '53144840', '53178633', '53178635', '53194364', '53125622', '53045160', '53194365', '53194366', '53194367', '53194368', '53185796', '53194369', '53086028', '90251330', '90251121', '30789993', '30789998', '31394282', '31393878', '29977892', '124205994', '31428350', '29545469', '29545479', '29545426', '29545421', '29545417', '29545423', '29484769', '29484785', '29545373', '29979589', '30078988', '30079048', '244420183', '29979596', '29979598', '30230262', '30230264', '30279409', '30279408', '30230266', '30230273', '30230277', '30230281', '30230300', '30230506', '30231231', '30230962', '60878121', '53224639', '53210038', '53081902', '53052413', '53210039', '53224626', '53168444', '53224629', '53224632', '53208783', '53083017', '53083040', '53208784', '53187334', '53187337', '53089335', '53066732', '53208785', '53178012', '53208786', '53152490', '53183929', '53146692', '53146065', '53083086', '53083102', '53113957', '53113944', '53190685', '53203056', '53167007', '53129046', '53098715', '53208787', '53208788', '53180738', '53072051' ] idealEdges = [ '9112003-8', '6438432-0', '6438432-1', '6438432-2', '6438432-3', '6438432-4', '6438432-5', '6438432-6', '6438432-7', '6438432-8', '6438432-9', '6438432-10', '6438432-11', '6438432-12', '6438432-13', '6438432-14', '6438432-15', '6438432-16', '6438432-17', '6386686-0', '6386686-1', '6386686-2', '6497278-2', '6497278-3', '6497278-4', '6497278-5', '6497278-6', '6514850-51', '6439614-0', '6439614-1', '6439614-2', '6439614-3', '15255537-1', '6439607-0', '6439607-1', '6439607-2', '6439607-3', '6439607-4', '6439607-5', '6439607-6', '6439607-7', '6439607-8', '6439607-9', '6439607-10', '10497741-3', '10497743-3', '4709507-4', '4709507-5', '4709507-6', '4709507-7', '4709507-8', '4869151-0', '4869146-0', '4644189-0', '4644192-0', '4644159-0', '4869146-3', '4869146-4', '4644156-0', '4722460-0', '4722460-1', '4722460-2', '4722460-3', '4722460-4', '4722460-5', '4722460-6', '14017470-0', '14017470-1', '5130429-0', '13866257-0', '13866256-0', '4748963-0', '4748962-0', '4748962-1', '15257844-0', '15257848-0', '15257848-1', '4743936-0', '4743934-0', '4743897-3', '4743897-4', '8116116-0', '6457969-20', '6457969-21', '6457969-22', '6476943-0', '6476943-1', '6476943-2', '6476943-3', '6476943-4', '6456455-20', '6456455-21', '6456455-22', '6456455-23', '6456455-24', '6456455-25', '6456455-26', '6456455-27', '6456455-28', '6456455-29', '6456455-30', '6456455-31', '6456455-32', '6456455-33', '6456455-34', '6456455-35', '6456455-36', '6456455-37', '6456455-38', '6456455-39', '6456455-40', '6456455-41', '6456455-42', '6456455-43', '6456455-44', '6456455-45', '6456455-46' ] spt = g.shortest_path_tree_retro(v85thStreet, vBeaconAve, State(g.numagencies, 31505), WalkOptions()) vertices, edges = spt.path_retro(v85thStreet) assert spt.get_vertex(v85thStreet).payload.time == 63 assert spt.get_vertex(v85thStreet).payload.weight == 17022003 assert [v.label for v in vertices] == idealVertices assert [e.payload.name for e in edges] == idealEdges vBallardAve = "53115442" vLakeCityWay = "124175598" idealVertices = [ '53115442', '53115445', '53115446', '53227448', '53158020', '53105937', '53148458', '53077817', '53077819', '53077821', '53077823', '53077825', '53077826', '53077828', '53077830', '53077832', '53077833', '53153886', '53181632', '53246786', '53078069', '53247761', '53129527', '53203543', '53248413', '53182343', '53156127', '53227471', '53240242', '53109739', '53248420', '53234775', '53170822', '53115167', '53209384', '53134650', '53142180', '53087702', '53184534', '53193013', '69374666', '53193014', '69474340', '53185600', '53077802', '69474361', '53090673', '53193015', '53193016', '53193017', '53193018', '53189027', '53193019', '53193020', '53112767', '53193021', '53183554', '53213063', '53197105', '53213061', '53090659', '53213059', '53157290', '53062869', '53213057', '53213055', '53213054', '53184527', '67507140', '67507145', '67507034', '67507151', '67507040', '67507158', '53210973', '53147258', '53210974', '53210975', '60002793', '60002790', '60002789', '60002786', '60002787', '88468933', '53125662', '53195800', '88486410', '53228492', '88486425', '53215121', '88486457', '53199820', '53185765', '53233322', '53227223', '88486676', '53086030', '53086045', '53204778', '88486720', '53204762', '88486429', '53139133', '53139142', '88486453', '53072465', '30790081', '30790104', '53072467', '124181376', '30759113', '53072469', '53072472', '53072473', '53072475', '53072476', '53072477', '53072478', '124175598' ] idealEdges = [ '6372784-0', '6372784-1', '6480699-3', '6517019-4', '6517019-5', '6517019-6', '6517019-7', '6346366-0', '6346366-1', '6346366-2', '6346366-3', '6346366-4', '6346366-5', '6346366-6', '6346366-7', '6346366-8', '10379527-1', '6511156-2', '6511156-3', '6511156-4', '6511156-5', '6511156-6', '6511156-7', '6511156-8', '6511156-9', '6511156-10', '6511156-11', '6511156-12', '6511156-13', '6511156-14', '9112003-0', '9112003-1', '9112003-2', '9112003-3', '9112003-4', '9112003-5', '9112003-6', '9112003-7', '9112003-8', '6438432-0', '6438432-1', '6438432-2', '6438432-3', '6438432-4', '6438432-5', '6438432-6', '6438432-7', '6438432-8', '6438432-9', '6438432-10', '6438432-11', '6438432-12', '6438432-13', '6438432-14', '6438432-15', '10425996-0', '10425996-1', '10425996-2', '10425996-3', '10425996-4', '10425996-5', '10425996-6', '10425996-7', '10425996-8', '10425996-9', '10425996-10', '10425996-11', '10425996-12', '9116336-2', '9116336-3', '9116346-1', '9116346-2', '9116346-3', '6459254-1', '6459254-2', '6459254-3', '6459254-4', '6459254-5', '4794350-10', '4794350-11', '4794350-12', '6488959-6', '6488959-7', '6488959-8', '6488959-9', '6488959-10', '6488959-11', '6488959-12', '6488959-13', '6488959-14', '6488959-15', '6488959-16', '6488959-17', '6488959-18', '6488959-19', '6488959-20', '6488959-21', '6488959-22', '6488959-23', '6488959-24', '6488959-25', '6488959-26', '6488959-27', '6488959-28', '6488959-29', '6344932-0', '6344932-1', '6344932-2', '13514591-0', '13514602-0', '13514602-1', '13514602-2', '8591344-0', '8591344-1', '8591344-2', '8591344-3', '8591344-4', '8591344-5' ] spt = g.shortest_path_tree_retro(vBallardAve, vLakeCityWay, State(g.numagencies, 13684)) vertices, edges = spt.path_retro(vBallardAve) assert spt.get_vertex(vBallardAve).payload.time == -8 assert spt.get_vertex(vBallardAve).payload.weight == 196300 assert [v.label for v in vertices] == idealVertices assert [e.payload.name for e in edges] == idealEdges
start_time = datetime.fromtimestamp(start) stop_time = datetime.fromtimestamp(stop) start_time -= origin_walk_penalty finally: if tree: tree.destroy() return (start_time, stop_time) #narrative = list(postprocess_path(vertices, edges)) #return json.dumps({ 'narrative': narrative }, indent=3, cls=SelfEncoderHelper) walk_options = WalkOptions() walk_options.transfer_penalty = 0 walk_options.walking_speed = 1.0 walk_options.hill_reluctance = 1.5 #walk_options.turn_penalty = #walk_options.walking_reluctance = #walk_options.max_walk = # The full Metro King County boundaries #lon_start = -122.506729 #lon_stop = -121.785828 #lat_start = 47.9323654 #lat_stop = 47.1891136 # Seattle & the East side
from graphserver.core import Graph, Street, State, WalkOptions os.environ['TZ'] = 'US/Pacific' time.tzset() t0s = "Tue Mar 09 08:50:00 2010" t0t = time.strptime(t0s) d0s = time.strftime('%a %b %d %Y', t0t) t0 = time.mktime(t0t) print d0s print time.ctime(t0), t0 gtfsdb = GTFSDatabase ('/home/andrew/data/pdx/trimet-2010-02-28.gtfsdb') gdb = GraphDatabase ('/home/andrew/data/pdx/trimet.gsdb' ) g = gdb.incarnate () wo = WalkOptions() wo.max_walk = 1600 wo.walking_overage = 0.0 wo.walking_speed = 1.0 # trimet uses 0.03 miles / 1 minute - but it uses straight line distance as well wo.transfer_penalty = 60 * 10 wo.walking_reluctance = 2 wo.max_transfers = 5 wo.transfer_slack = 60 * 4 while(True) : input = raw_input('o d t / wo > ').split(' ') if len(input) == 0 : sys.exit(0) elif input[0] == 'wo' : try : wo.max_walk = int(raw_input('wo.max_walk = ')) wo.walking_overage = float(raw_input('wo.walking_overage = '))
nodes = {} for id, tags, lat, lon, endnode_refs in osmdb.nodes(): nodes['osm-' + id] = (lat, lon) for id, name, lat, lon in gtfsdb.stops(): nodes['sta-' + id] = (lat, lon) os.environ['TZ'] = 'US/Pacific' time.tzset() t0s = "Tue Nov 16 07:50:30 2010" t0t = time.strptime(t0s) d0s = time.strftime('%a %b %d %Y', t0t) t0 = time.mktime(t0t) print 'search date: ', d0s print 'search time: ', time.ctime(t0), t0 wo = WalkOptions() wo.max_walk = 2000 wo.walking_overage = 0.0 wo.walking_speed = 1.0 wo.transfer_penalty = 60 * 6 wo.walking_reluctance = 1.0 wo.max_transfers = 4 orig = 'osm-37476896' #wes dest = 'sta-2575' #to eas assist_spt = ag.shortest_path_tree(dest, None, State(1, 0)) spt_a = g.shortest_path_tree_assist(assist_spt, orig, dest, State(1, t0), wo) spt_b = g.shortest_path_tree(orig, dest, State(1, t0), wo) for spt in [spt_a, spt_b]: print '-----search and path-----' print 'number of vertices in spt:', len(spt.vertices)
spt.get_vertex("E").best_state.narrate() from graphserver.core import Graph, State, WalkOptions from graphserver.graphdb import GraphDatabase import time, os os.environ['TZ'] = 'US/Pacific' time.tzset() t0s = "Mon Jan 20 08:50:00 2010" t0t = time.strptime(t0s) d0s = time.strftime('%a %b %d %Y', t0t) t0 = time.mktime(t0t) print 'search date: ', d0s print 'search time: ', time.ctime(t0), t0 wo = WalkOptions() wo.max_walk = 1600 wo.walking_overage = 0.1 wo.walking_speed = 0.8 # trimet uses 0.03 miles / 1 minute wo.transfer_penalty = 60 * 10 wo.walking_reluctance = 2 gdb = GraphDatabase('../../data/trimet-linked-20100117.gsdb') g = gdb.incarnate() spt = g.shortest_path_tree( "sta-9677", "sta-13070", State(1, t0), wo ) spt = g.shortest_path_tree( "sta-10120", "osm-40508580", State(1, t0), wo ) spt = g.shortest_path_tree( "sta-3277", "osm-40508580", State(1, t0), wo ) spt = g.shortest_path_tree( "sta-7777", "sta-8340", State(1, t0), wo ) spt = g.shortest_path_tree( "sta-10120", "sta-408", State(1, t0), wo ) spt = g.shortest_path_tree( "sta-9677", "sta-7777", State(1, t0), wo ) spt = g.shortest_path_tree( "sta-7777", None, State(1, t0), wo )
def getUrbanExplorerBlob(self, origlon, origlat, destlon, destlat, arrive_time, street_mode="walk", transit_mode="Both", less_walking="False", transfer_penalty=60, walking_speed=1.0, walking_reluctance=1.0, max_walk=10000, walking_overage=0.1,start_time=0,switch=1): # get origin and destination nodes from osm map sys.stderr.write("[get_osm_vertex_from_coords," + str(time.time()) + "]\n") orig_osm, orig_osm_dist = self.pgosmdb.get_osm_vertex_from_coords(origlon, origlat) dest_osm, dest_osm_dist = self.pgosmdb.get_osm_vertex_from_coords(destlon, destlat) # get origin and destination nodes from gtfs database sys.stderr.write("[get_station_vertex_from_coords," + str(time.time()) + "]\n") orig_sta, orig_sta_dist = self.pggtfsdb.get_station_vertex_from_coords(origlon, origlat) dest_sta, dest_sta_dist = self.pggtfsdb.get_station_vertex_from_coords(destlon, destlat) # get coordinates for origin node if (orig_osm_dist < orig_sta_dist): origin = orig_osm sys.stderr.write("[get_coords_for_osm_vertex," + str(time.time()) + "]\n") orig_node_lat, orig_node_lon = self.pgosmdb.get_coords_for_osm_vertex(origin) else: origin = orig_sta sys.stderr.write("[get_coords_for_station_vertex," + str(time.time()) + "]\n") orig_node_lat, orig_node_lon = self.pggtfsdb.get_coords_for_station_vertex(origin) # get coordinates for destination node if (dest_osm_dist < dest_sta_dist): dest = dest_osm sys.stderr.write("[get_coords_for_osm_vertex," + str(time.time()) + "]\n") dest_node_lat, dest_node_lon = self.pgosmdb.get_coords_for_osm_vertex(dest) else: dest = dest_sta sys.stderr.write("[get_coords_for_station_vertex," + str(time.time()) + "]\n") dest_node_lat, dest_node_lon = self.pggtfsdb.get_coords_for_station_vertex(dest) wo = WalkOptions() wo.transfer_penalty=transfer_penalty wo.walking_speed=walking_speed wo.walking_reluctance=walking_reluctance wo.max_walk=max_walk wo.walking_overage=walking_overage # check for wheelchair street_mode if (street_mode == "wheelchair"): wo.with_wheelchair = int(True) wo.transfer_penalty = 180 else: wo.with_wheelchair = int(False) # check for bike street_mode if (street_mode == "bike"): wo.transfer_penalty = 120 # check for transit_mode if (transit_mode == "Both"): wo.transit_types = int(14) elif (transit_mode == "Bus"): wo.transit_types = int(8) elif (transit_mode == "Rail"): wo.transit_types = int(6) elif (transit_mode == "None"): wo.transit_types = int(0) # check for less_walking flag if (less_walking == "True"): wo.walking_reluctance *= 10.0 if (start_time == 0): start_time = int(time.time()) if (switch == 1): graphserver.core.makeImage(self.graph.soul, origin, dest, State(self.graph.num_agencies,start_time), State(self.graph.num_agencies,arrive_time), wo) return open("explorerimages/blah.png", "rb").read() else: graphserver.core.makeUrbanExplorerBlob(self.graph.soul, origin, dest, State(self.graph.num_agencies,start_time), State(self.graph.num_agencies,arrive_time), wo) return open("explorerimages/blah2.png", "rb").read()