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 create_and_populate_edges_table( self, tolerant=False ): self.set_endnode_ref_counts() self.index_endnodes() print "splitting ways and inserting into edge table" c = self.conn.cursor() c.execute( "DROP TABLE IF EXISTS osm_edges" ) c.execute( """CREATE TABLE osm_edges (id TEXT PRIMARY KEY, parent_id TEXT REFERENCES osm_ways ON DELETE CASCADE, start_nd TEXT REFERENCES osm_nodes ON DELETE CASCADE, end_nd TEXT REFERENCES osm_nodes ON DELETE CASCADE, dist FLOAT, geom TEXT)""" ) for i, way in enumerate(self.ways()): try: if i%5000==0: print i subways = [] curr_subway = [ way.nds[0] ] # add first node to the current subway for nd in way.nds[1:-1]: # for every internal node of the way curr_subway.append( nd ) if self.node(nd)[4] > 1: # node reference count is greater than one, node is shared by two ways subways.append( curr_subway ) curr_subway = [ nd ] curr_subway.append( way.nds[-1] ) # add the last node to the current subway, and store the subway subways.append( curr_subway ); #insert into edge table for i, subway in enumerate(subways): coords = [(lambda x:(x[3],x[2]))(self.node(nd)) for nd in subway] packt = pack_coords( coords ) dist = sum([vincenty(lat1, lng1, lat2, lng2) for (lng1, lat1), (lng2, lat2) in cons(coords)]) c.execute( "INSERT INTO osm_edges VALUES (%s, %s, %s, %s, %s, %s)", ( "%s-%s"%(way.id, i), way.id, subway[0], subway[-1], dist, packt) ) except IndexError: if tolerant: continue else: raise print "indexing edges...", c.execute( "CREATE INDEX osm_edges_parent_id ON osm_edges (parent_id)" ) c.execute( "CREATE INDEX osm_edges_start_nd ON osm_edges (start_nd)" ) c.execute( "CREATE INDEX osm_edges_end_nd ON osm_edges (end_nd)" ) print "done" self.conn.commit() c.close()
def length(self): """nodedir is a dictionary of nodeid->node objects""" ret = 0 for i in range(len(self.nd_ids) - 1): thisnode = self.osm.nodes[self.nd_ids[i]] nextnode = self.osm.nodes[self.nd_ids[i + 1]] ret += vincenty(thisnode.lat, thisnode.lon, nextnode.lat, nextnode.lon) return ret
def split_line_segment(lng1, lat1, lng2, lat2, max_section_length): # Split line segment defined by (x1, y1, x2, y2) into a set of points # (x,y,displacement) spaced less than max_section_length apart if lng1==lng2 and lat1==lat2: yield [lng1, lat1, 0] yield [lng2, lat2, 0] return street_len = vincenty(lat1, lng1, lat2, lng2) n_sections = int(street_len/max_section_length)+1 geolen = ((lat2-lat1)**2 + (lng2-lng1)**2)**0.5 section_len = geolen/n_sections street_vector = (lng2-lng1, lat2-lat1) unit_vector = [x/geolen for x in street_vector] for i in range(n_sections+1): vec = [x*section_len*i for x in unit_vector] vec = [lng1+vec[0], lat1+vec[1], (street_len/n_sections)*i] yield vec
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 ""