def reincarnate_ch(basename): chdowndb = GraphDatabase( basename+".down.gdb" ) chupdb = GraphDatabase( basename+".up.gdb" ) upgg = chupdb.incarnate() downgg = chdowndb.incarnate() return ContractionHierarchy(upgg, downgg)
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 main(): usage = """usage: python gdb_import_gtfs.py [options] <graphdb_filename> <gtfsdb_filename> [<agency_id>]""" parser = OptionParser(usage=usage) parser.add_option("-n", "--namespace", dest="namespace", default="0", help="agency namespace") parser.add_option("-m", "--maxtrips", dest="maxtrips", default=None, help="maximum number of trips to load") parser.add_option("-d", "--date", dest="sample_date", default=None, help="only load transit running on a given day. YYYYMMDD" ) (options, args) = parser.parse_args() if len(args) != 2: parser.print_help() exit(-1) graphdb_filename = args[0] gtfsdb_filename = args[1] agency_id = args[2] if len(args)==3 else None print "importing from gtfsdb '%s' into graphdb '%s'"%(gtfsdb_filename, graphdb_filename) gtfsdb = GTFSDatabase( gtfsdb_filename ) gdb = GraphDatabase( graphdb_filename, overwrite=False ) maxtrips = int(options.maxtrips) if options.maxtrips else None gdb_load_gtfsdb( gdb, options.namespace, gtfsdb, gdb.get_cursor(), agency_id, maxtrips=maxtrips, sample_date=options.sample_date) gdb.commit() print "done"
def main(): usage = """usage: python gdb_link_osm_gtfs.py <graphdb_filename> <osmdb_filename> <gtfsdb_filename>""" parser = OptionParser(usage=usage) (options, args) = parser.parse_args() if len(args) != 3: parser.print_help() exit(-1) graphdb_filename = args[0] osmdb_filename = args[1] gtfsdb_filename = args[2] gtfsdb = GTFSDatabase( gtfsdb_filename ) osmdb = OSMDB( osmdb_filename ) gdb = GraphDatabase( graphdb_filename ) n_stops = gtfsdb.count_stops() for i, (stop_id, stop_name, stop_lat, stop_lon) in enumerate( gtfsdb.stops() ): print "%d/%d"%(i,n_stops) nd_id, nd_lat, nd_lon, nd_dist = osmdb.nearest_node( stop_lat, stop_lon ) station_vertex_id = "sta-%s"%stop_id osm_vertex_id = "osm-%s"%nd_id print station_vertex_id, osm_vertex_id gdb.add_edge( station_vertex_id, osm_vertex_id, Link() ) gdb.add_edge( osm_vertex_id, station_vertex_id, Link() )
def handle(self, *args, **options): self.stdout.write('Loading boston.osmdb\n') self.nodedb = osmdb.OSMDB(settings.DATA_DIR + '/boston.osmdb') self.stdout.write('Importing Boston street network...\n') gdb = GraphDatabase(settings.DATA_DIR + '/boston.gdb') self.graph = gdb.incarnate() self.stdout.write('Importing trip network...\n') tripdb = osmdb.OSMDB(settings.DATA_DIR + '/trip_data.db') batchgeom = [] count = 0 # For each station for tnode in tripdb.nodes(): lat1 = float(tnode[2]) lng1 = float(tnode[3]) # get all trips departing this station cursor = tripdb.get_cursor() tedges = cursor.execute("select * from edges where start_nd = ?", [tnode[0]]) # For each trip for tedge in tedges: if tedge[3] == '': if int(options['verbosity']) > 1: self.stdout.write( 'Start and end nodes are the same.\n') continue dnode = tripdb.node(tedge[3]) lat2 = float(dnode[2]) lng2 = float(dnode[3]) dnode = None if lat2 == lat1 and lng2 == lng1: continue geom = self._spt(lat1, lng1, lat2, lng2) count += 1 batchgeom.append(( count, geom, )) if len(batchgeom) >= options['number']: self.dropfile(batchgeom, count, **options) batchgeom = [] geom = None # Don't keep a history of the execute tranactions tedges = None cursor = None
def __init__(self, graphdb_filename, vertex_events, edge_events, vertex_reverse_geocoders, config={}): graphdb = GraphDatabase(graphdb_filename) self.graph = graphdb.incarnate() self.vertex_events = vertex_events self.edge_events = edge_events self.vertex_reverse_geocoders = vertex_reverse_geocoders
def main(): usage = """usage: python gdb_import_osm.py <graphdb_filename> <osmdb_filename>""" parser = OptionParser(usage=usage) parser.add_option( "-n", "--namespace", dest="namespace", default="osm", help="prefix all imported vertices with namespace string") parser.add_option( "-s", "--slog", action="append", dest="slog_strings", default=[], help= "specify slog for highway type, in highway_type:slog form. For example, 'motorway:10.5'" ) parser.add_option( "-p", "--profiledb", dest="profiledb_filename", default=None, help="specify profiledb to annotate streets with rise/fall data") (options, args) = parser.parse_args() if len(args) != 2: parser.print_help() exit(-1) slogs = {} for slog_string in options.slog_strings: highway_type, slog_penalty = slog_string.split(":") slogs[highway_type] = float(slog_penalty) print "slog values: %s" % slogs graphdb_filename = args[0] osmdb_filename = args[1] print "importing osmdb '%s' into graphdb '%s'" % (osmdb_filename, graphdb_filename) profiledb = ProfileDB( options.profiledb_filename) if options.profiledb_filename else None osmdb = OSMDB(osmdb_filename) gdb = GraphDatabase(graphdb_filename, overwrite=False) gdb_import_osm(gdb, osmdb, options.namespace, slogs, profiledb) print "done"
def test_basic(self): g = Graph() g.add_vertex("A") g.add_vertex("B") g.add_edge("A", "B", Link()) g.add_edge("A", "B", Street("foo", 20.0)) gdb_file = os.path.dirname(__file__) + "unit_test.db" if os.path.exists(gdb_file): os.remove(gdb_file) gdb = GraphDatabase(gdb_file) gdb.populate(g) list(gdb.execute("select * from resources")) assert "A" in list(gdb.all_vertex_labels()) assert "B" in list(gdb.all_vertex_labels()) assert glen(gdb.all_edges()) == 2 assert glen(gdb.all_outgoing("A")) == 2 assert glen(gdb.all_outgoing("B")) == 0 assert glen(gdb.all_incoming("A")) == 0 assert glen(gdb.all_incoming("B")) == 2 assert glen(gdb.resources()) == 0 assert gdb.num_vertices() == 2 assert gdb.num_edges() == 2 g.destroy() g = gdb.incarnate() list(gdb.execute("select * from resources")) assert "A" in list(gdb.all_vertex_labels()) assert "B" in list(gdb.all_vertex_labels()) assert glen(gdb.all_edges()) == 2 assert glen(gdb.all_outgoing("A")) == 2 assert glen(gdb.all_outgoing("B")) == 0 assert glen(gdb.all_incoming("A")) == 0 assert glen(gdb.all_incoming("B")) == 2 assert glen(gdb.resources()) == 0 assert gdb.num_vertices() == 2 assert gdb.num_edges() == 2 os.remove(gdb_file)
def main(): usage = """usage: python gdb_link_gtfs_gtfs.py <graphdb_filename> <gtfsdb_filename> <range>""" parser = OptionParser(usage=usage) (options, args) = parser.parse_args() if len(args) != 3: parser.print_help() exit(-1) graphdb_filename = args[0] gtfsdb_filename = args[1] range = float(args[2]) gtfsdb = GTFSDatabase(gtfsdb_filename) gdb = GraphDatabase(graphdb_filename) n_stops = gtfsdb.count_stops() for i, (stop_id, stop_name, stop_lat, stop_lon) in enumerate(gtfsdb.stops()): print "%d/%d %s" % (i, n_stops, stop_id), station_vertex_id = "sta-%s" % stop_id for link_stop_id, link_stop_name, link_stop_lat, link_stop_lon in gtfsdb.nearby_stops( stop_lat, stop_lon, range): if link_stop_id == stop_id: continue print ".", link_length = vincenty(stop_lat, stop_lon, link_stop_lat, link_stop_lon) link_station_vertex_id = "sta-%s" % link_stop_id gdb.add_edge(station_vertex_id, link_station_vertex_id, Street("link", link_length)) print ""
def main(): if len(argv) < 2: print "usage: python import_ned.py graphdb_filename profiledb_filename" return graphdb_filename = argv[1] profiledb_filename = argv[2] gdb = GraphDatabase(graphdb_filename) profiledb = ProfileDB(profiledb_filename) n = gdb.num_edges() for i, (oid, vertex1, vertex2, edge) in enumerate(list(gdb.all_edges(include_oid=True))): if i % 500 == 0: print "%s/%s" % (i, n) if isinstance(edge, Street): rise, fall = get_rise_and_fall(profiledb.get(edge.name)) edge.rise = rise edge.fall = fall gdb.remove_edge(oid) gdb.add_edge(vertex1, vertex2, edge)
def main(): usage = """usage: python new_gdb.py [options] <graphdb_filename> """ parser = OptionParser(usage=usage) parser.add_option("-o", "--overwrite", action="store_true", dest="overwrite", default=False, help="overwrite any existing database") (options, args) = parser.parse_args() if len(args) != 1: parser.print_help() exit(-1) graphdb_filename = args[0] if not os.path.exists(graphdb_filename) or options.overwrite: print "Creating graph database '%s'" % graphdb_filename graphdb = GraphDatabase(graphdb_filename, overwrite=options.overwrite) else: print "Graph database '%s' already exists. Use -o to overwrite" % graphdb_filename
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)
print "Geocoder range", geocoder_range print "Using time", dt.ctime() print "Bounding box tlbr: %f, %f, %f, %f" % (lat_start, lon_start, lat_stop, lon_stop) print "Checking %d routes" % (lat_grid * lon_grid) minimum = sys.maxint maximum = -sys.maxint - 1 min_lon = sys.maxint max_lon = -sys.maxint - 1 min_lat = sys.maxint max_lat = -sys.maxint - 1 gdb = GraphDatabase('king.highway.gdb') graph = gdb.incarnate() rows = [] #missing_value = None missing_value = 50000 for i in xrange(0, lat_grid): row = [] print "Row %d" % i lat = lat_start + (i * lat_increment) if lat < min_lat:
def __init__(self, graphdb_filename): self.graphdb = GraphDatabase(graphdb_filename)
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 __init__(self, ch_basename, osmdb_filename, profiledb_filename): graphdb = GraphDatabase(graphdb_filename) self.osmdb = OSMDB(osmdb_filename) self.profiledb = ProfileDB(profiledb_filename) self.ch = reincarnate_ch(ch_basename) self.shortcut_cache = ShortcutCache(ch_basename + ".scc")
def main(): usage = """usage: python gdb_import_osm.py <graphdb_filename> <osmdb_filename>""" parser = OptionParser(usage=usage) parser.add_option( "-n", "--namespace", dest="namespace", default="osm", help="prefix all imported vertices with namespace string") parser.add_option( "-s", "--slog", action="append", dest="slog_strings", default=[], help= "specify slog for highway type, in highway_type:slog form. For example, 'motorway:10.5'" ) parser.add_option( "-p", "--profiledb", dest="profiledb_filename", default=None, help="specify profiledb to annotate streets with rise/fall data") parser.add_option( "-c", "--slog_config", dest="slog_config", default=None, metavar="CONFIG.yaml", help="file containing slog parameters for highways, cycleways, etc") (options, args) = parser.parse_args() if len(args) != 2: parser.print_help() exit(-1) slogs = {} slog_config = {} if options.slog_config: slog_config = yaml.load(open(options.slog_config).read()) for highway_type, slog_penalty in slog_config.get('slogs', {}).items(): slogs[highway_type] = float(slog_penalty) for slog_string in options.slog_strings: highway_type, slog_penalty = slog_string.split(":") slogs[highway_type] = float(slog_penalty) print "slog values: %s" % slogs slog_config['slogs'] = slogs if slog_config.get('slog_function'): slog_config['slog_function'] = import_object( slog_config['slog_function']) graphdb_filename = args[0] osmdb_filename = args[1] print "importing osmdb '%s' into graphdb '%s'" % (osmdb_filename, graphdb_filename) profiledb = ProfileDB( options.profiledb_filename) if options.profiledb_filename else None osmdb = OSMDB(osmdb_filename) gdb = GraphDatabase(graphdb_filename, overwrite=False) gdb_import_osm(gdb, osmdb, options.namespace, slog_config, profiledb) print "done"