def test_get_network_methods(): from shapely import wkt # graph from bounding box north, south, east, west = 37.79, 37.78, -122.41, -122.43 G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service') G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service', truncate_by_edge=True) # graph from point location_point = (37.791427, -122.410018) bbox = ox.bbox_from_point(location_point, project_utm=True) G2 = ox.graph_from_point(location_point, distance=750, distance_type='bbox', network_type='drive') G3 = ox.graph_from_point(location_point, distance=500, distance_type='network') # graph from address G4 = ox.graph_from_address(address='350 5th Ave, New York, NY', distance=1000, distance_type='network', network_type='bike') # graph from list of places places = ['Los Altos, California, USA', {'city':'Los Altos Hills', 'state':'California'}, 'Loyola, California'] G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False) # graph from polygon polygon = wkt.loads('POLYGON ((-122.418083 37.754154, -122.418082 37.766028, -122.410909 37.766028, -122.410908 37.754154, -122.418083 37.754154))') G6 = ox.graph_from_polygon(polygon, network_type='walk') # test custom query filter filtr = ('["area"!~"yes"]' '["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]' '["foot"!~"no"]' '["service"!~"private"]' '["access"!~"private"]') G = ox.graph_from_point(location_point, network_type='walk', custom_filter=filtr)
def get_unwalkable_network(extent_poly_wgs=None): cust_filter_no_tunnels = '["area"!~"yes"]["highway"!~"trunk_link|motor|proposed|construction|abandoned|platform|raceway"]["foot"!~"no"]["service"!~"private"]["access"!~"private"]["highway"~"service"]["layer"~"-1|-2|-3|-4|-5|-6|-7"]' # query graph g = ox.graph_from_polygon(extent_poly_wgs, custom_filter=cust_filter_no_tunnels, retain_all=True) print('loaded graph of', g.number_of_edges(), 'edges') # convert graph to undirected graph g_u = ox.get_undirected(g) print('converted graph to undirected graph of', g_u.number_of_edges(), 'edges') # project graph g_u_proj = ox.project_graph(g_u, from_epsg(3879)) return g_u_proj
def graph_from_circle(query, radius=1000, network_type='all_private', dual=False, return_igraph=False, save_pickle=False, fname='graphs\\city_graph', osmnx_query_kws={}): """ Like fetching a graph with osmnx but with a circle instead of a square. Parameters ---------- query : string or dict Location to query Nominatim. radius: float Radius of the circle. network_type: string see osmnx network types dual: bool if true converts graph to its dual form return_igraph: bool if true retruns the graph as iGraph save_pickle: bool if True saves file as a pickle in fname directory fname: string Directory to save. osmnx_query_kws: dict options for osmnx query. See osmnx properties at https://osmnx.readthedocs.io/en/stable/osmnx.html. Returns ------- iGraph or NetworkX graph """ pt = ox.geocode(query) poly = circle_from_lat_lon(*pt, radius) G = ox.graph_from_polygon(poly, network_type=network_type, **osmnx_query_kws) G.graph['kind'] = 'primal' if dual: G = get_dual(G) if return_igraph: G = get_full_igraph(G) if save_pickle and return_igraph: _save_pickle_file(G, fname, extention='ig') elif save_pickle and not return_igraph: _save_pickle_file(G, fname, extention='nx') return G
def load(self, options): log.info('fetching OSM data bounded by polygon') self.g = osmnx.graph_from_polygon(self.region, network_type='bike', simplify=False, custom_filter=self.custom_filter) log.debug('original g=%s, g=%s', self.g, type(self.g)) log.info('original nodes=%s, edges=%s', self.g.order(), self.g.size()) if options.simplify: log.info('simplifying graph') self.g = osmnx.simplification.simplify_graph(self.g, strict=False, remove_rings=False) pass return
def fetch_osm_data(name): """ Fetch OSM data for a given place. :param str name: name of the place. """ logging.info("Fetching city graph for {!r}".format(name)) # Find the place by name and convert it to a polygon gdf = osmnx.gdf_from_place(name) logging.info("Found geodata frame") polygon = gdf["geometry"].unary_union if not isinstance(polygon, (Polygon, MultiPolygon)): raise ValueError( "invalid geometry. " "Expected Polygon or MultiPolygon, but got {} instead. " "Perhabs the place does not exist.".format(type(polygon))) # Fetch all the networks and public transportation routes networks = {} for path_type in list(TransportType): if path_type in TRANSPORT_TO_NETWORK: logging.info("Fetching {} network".format(path_type)) network_type = TRANSPORT_TO_NETWORK[path_type] networks[path_type] = osmnx.graph_from_polygon( polygon, network_type=network_type, simplify=False) elif path_type in TRANSPORT_TO_ROUTE: logging.info("Fetching {} routes".format(path_type)) route_type = TRANSPORT_TO_ROUTE[path_type] networks[path_type] = routes_from_polygon(polygon, route_type=route_type) else: logging.warning( "TransportType {} is not recognized as either network type " "or route type. Ignoring it".format(path_type)) # Fetch all the building footprints logging.info("Importing footprints") footprints = osmnx.footprints_from_polygon(polygon) footprints["name"] = footprints["name"].fillna(value="") # Fetch all the points of interest logging.info("Importing points of interest") pois = pois_from_polygon(polygon) pois["name"] = pois["name"].fillna(value="") return gdf, networks, footprints, pois
def post(self): #try: # データをフロントエンドから受け取る input_data = request.json # 辞書 -> 文字列 -> geojsonに変換 src = json.dumps(input_data['GeoJson']) src = geojson.loads(src) # shapelyのポリゴン形式に変更 ftr = src.get('features')[0] polygon = shape(ftr.get('geometry')) # ポリゴンからnetworkxオブジェクトを生成 G_osm = ox.graph_from_polygon(polygon, simplify=False, network_type=input_data['NetworkType']) # xy座標系に変換 # 変換の際の中心座標 locating_point = fnc.culc_center_latlon(G_osm) G, _ = fnc.G_geo_to_G_xy(G_osm, locating_point) del G_osm gc.collect() # limit limit_degree = input_data['SimplifyRate'] simplify_G = fnc.recreate_G(G, limit_degree, input_data['DeleteOneNode']) simplify_G = fnc.renumber(simplify_G) del G gc.collect() # ネットワークをjson形式に変更 graph_json = fnc.G_to_JSON(simplify_G) # session変数に基礎ネットワークを作成済みかを入力 session['done_network'] = 1 # すべてが成功した場合 return_data = {'Status': 1, 'Network': graph_json} #except: # エラーの場合 #return_data = {'Status':0} return jsonify(return_data)
def FindRoute(self, waypoints=[]): multiPoint = MultiPoint([(point['lng'], point['lat']) for point in waypoints]) polygon = multiPoint.envelope polygon = polygon.buffer(0.005) # bounds = multiPoint.bounds # distance = Point(bounds[0], bounds[0]).distance(Point(bounds[1], bounds[1])) # Logger.info(distance) self.Graph = ox.graph_from_polygon(polygon, network_type='drive', simplify=False, retain_all=True) self.Edges = ox.graph_to_gdfs(self.Graph, nodes=False, edges=True) return self.findRoute(waypoints)
def pickle_graph(shp_ll, path_out): """ Given a path to a shapefile in (LONG LAT) format, get geometry and pickle the road network contained within the boundary. Road network is not simplified. i.e. roads that are not straight/linear are shaped by a series of nodes. (if simplify=True, then curved roads are represented by a few nodes, reducing the accuracy of the a* algorithm :param shp_ll: path to shapefile in long lat format :param path_out: path to where to pickle :return: None """ geometry = gpd.read_file(shp_ll).at[0, 'geometry'] G = ox.graph_from_polygon(geometry, network_type='drive', simplify=False) nx.write_gpickle(G, path_out)
def get_streets(perimeter=None, point=None, radius=None, dilate=6, custom_filter=None): if perimeter is not None: # Boundary defined by polygon (perimeter) streets = ox.graph_from_polygon(union(perimeter.geometry), custom_filter=custom_filter) streets = ox.project_graph(streets) streets = ox.graph_to_gdfs(streets, nodes=False) #streets = ox.project_gdf(streets) streets = MultiLineString(list(streets.geometry)).buffer(dilate) elif (point is not None) and (radius is not None): # Boundary defined by polygon (perimeter) streets = ox.graph_from_point(point, dist=radius, custom_filter=custom_filter) crs = ox.graph_to_gdfs(streets, nodes=False).crs streets = ox.project_graph(streets) perimeter = GeoDataFrame(geometry=[Point(point[::-1])], crs=crs) perimeter = ox.project_gdf(perimeter).geometry[0].buffer(radius) streets = ox.graph_to_gdfs(streets, nodes=False) streets = MultiLineString( list( filter( # Filter lines with at least 2 points lambda line: len(line) >= 2, # Iterate over lines in geometry map( # Filter points within perimeter lambda line: list( filter(lambda xy: Point(xy).within(perimeter), zip(*line.xy))), streets.geometry)))).buffer(dilate) # Dilate lines if not isinstance(streets, Iterable): streets = [streets] streets = list(map(pathify, streets)) return streets, perimeter
def get_streckennetz_from_osm(point_cloud=None, place=None, cache=True) -> nx.MultiGraph: """ Load Streckennetz from OpenStreetMap. Returns ------- nx.Graph: Streckennetz without stations. """ if place is None and point_cloud is None: raise ValueError("Must either supply point_cloud or place, not none") if place is not None and point_cloud is not None: raise ValueError("Must either supply point_cloud or place, not both") try: if not cache: raise FileNotFoundError streckennetz = nx.read_gpickle("cache/original_osm_rail_graph.gpickle") print( 'Using chached streckennetz insted of downloading new one from osm' ) except FileNotFoundError: rail_filter = '["railway"~"rail|tram|narrow_gauge|light_rail"]' if place: streckennetz = ox.graph_from_place(place, simplify=True, network_type='none', truncate_by_edge=False, custom_filter=rail_filter) else: plt.plot(point_cloud[:, 0], point_cloud[:, 1], 'o') plt.plot(*MultiPoint(point_cloud).convex_hull.exterior.xy) plt.gca().set_aspect('equal', 'datalim') plt.show() streckennetz = ox.graph_from_polygon( MultiPoint(point_cloud).convex_hull, simplify=True, network_type='none', truncate_by_edge=False, custom_filter=rail_filter) print('projecting streckennetz') streckennetz = ox.project_graph(streckennetz, to_crs='EPSG:3857') # Convert to non directional graph which only has half the number of edges and is way faster to compute streckennetz = nx.MultiGraph(streckennetz) nx.write_gpickle(streckennetz, "cache/original_osm_rail_graph.gpickle") return streckennetz
def extended_hulls_query(self): aois = query_geometries(self.hulls_query()) aois = aois.to_crs(fiona.crs.from_epsg(4326)) central_nodes = [] start = time.time() for aoi in tqdm(aois.geometry): try: aoi_graph = ox.graph_from_polygon(aoi.buffer(0.001), network_type='all') closeness_centrality = nx.closeness_centrality(aoi_graph) sorted_nodes = sorted(closeness_centrality.items(), key=operator.itemgetter(1), reverse=True) central_nodes += [ node[0] for node in sorted_nodes[:len(sorted_nodes) // 10] ] except KeyboardInterrupt: # leave on ctrl-c sys.exit(0) except: logging.debug("fetching graph failed for {}".format(aoi)) central_nodes_ids = ', '.join([f'{key}' for key in central_nodes]) logging.debug( "calculating most central nodes for aois took {}s".format( time.time() - start)) return """ WITH hulls AS ({hulls_query}), intersecting_lines AS ( SELECT hulls.cid, ST_Intersection(way, ST_Buffer(hulls.geometry, 50)) AS geometry FROM planet_osm_line, hulls WHERE osm_id = ANY( SELECT id FROM planet_osm_ways WHERE nodes && ARRAY[{central_nodes_ids}]::bigint[] ) AND ST_DWithin(planet_osm_line.way, hulls.geometry, 50) ) SELECT 1 AS color, ST_ConcaveHull(ST_Union(geometry), 0.99) AS geometry FROM ( SELECT cid, geometry FROM hulls UNION SELECT cid, geometry FROM intersecting_lines ) AS tmp GROUP BY cid """.format(hulls_query=self.hulls_query(), central_nodes_ids=central_nodes_ids)
def create_district_knots_and_edges_model( district_gdf: gpd.geodataframe.GeoDataFrame) -> KnotsEdges: """Given a district geo dataframe this extracts the district polygon from a list of all coordinates and the corresponding graph is created from the district polygon. Args: district_gdf (gpd.geodataframe.GeoDataFrame): District geo dataframe. Returns: KnotsEdges: Tuple of the district polygon and district graph. """ g = [i for i in district_gdf.geometry] district_all_coords = list(mapping(g[0])["coordinates"][0]) district_poly = Polygon(district_all_coords) district_graph = ox.graph_from_polygon(district_poly) return district_poly, district_graph
def test_get_network_methods(): import geopandas as gpd north, south, east, west = 37.79, 37.78, -122.41, -122.43 G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service') G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service', truncate_by_edge=True) location_point = (37.791427, -122.410018) bbox = ox.bbox_from_point(location_point, project_utm=True) G2 = ox.graph_from_point(location_point, distance=750, distance_type='bbox', network_type='drive') G3 = ox.graph_from_point(location_point, distance=500, distance_type='network') G4 = ox.graph_from_address(address='350 5th Ave, New York, NY', distance=1000, distance_type='network', network_type='bike') places = [ 'Los Altos, California, USA', { 'city': 'Los Altos Hills', 'state': 'California' }, 'Loyola, California' ] G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False) calif = gpd.read_file('examples/input_data/ZillowNeighborhoods-CA') mission_district = calif[(calif['CITY'] == 'San Francisco') & (calif['NAME'] == 'Mission')] polygon = mission_district['geometry'].iloc[0] G6 = ox.graph_from_polygon(polygon, network_type='walk')
def __init__(self, region: regions.Region, level: int = 18, load_directory: Text = None): self.map_name = region.name self.s2_graph = None self.level = level self.polygon_area = region.polygon self.num_poi_add = 0 if load_directory is None: logging.info("Preparing map.") logging.info("Extracting POI.") self.poi, self.streets = self.get_poi() self.num_poi = self.poi.shape[0] logging.info("Constructing graph.") self.nx_graph = ox.graph_from_polygon(self.polygon_area, network_type='walk') distance = nx.get_edge_attributes(self.nx_graph, 'length') nx.set_edge_attributes(self.nx_graph, distance, 'true_length') logging.info("Add POI to graph.") self.add_poi_to_graph() osmid = nx.get_node_attributes(self.nx_graph, 'osmid') osmid_str = dict(zip(osmid, map(str, osmid.values()))) nx.set_node_attributes(self.nx_graph, osmid_str, 'osmid') nx.relabel_nodes(self.nx_graph, osmid_str, copy=False) self.poi['osmid'] = self.poi['osmid'].astype(str) else: logging.info("Loading map from directory.") self.load_map(load_directory) self.num_poi = self.poi.shape[0] logging.info("Calculate S2Cell covering.") self.calc_s2cells(level) self.nodes, self.edges = ox.graph_to_gdfs(self.nx_graph) self.process_param()
def test_get_network_methods(): # graph from bounding box _ = ox.utils_geo.bbox_from_point(location_point, project_utm=True, return_crs=True) north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=500) G = ox.graph_from_bbox(north, south, east, west, network_type="drive") G = ox.graph_from_bbox( north, south, east, west, network_type="drive_service", truncate_by_edge=True ) # truncate graph by bounding box north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=400) G = ox.truncate.truncate_graph_bbox(G, north, south, east, west) # graph from address G = ox.graph_from_address(address=address, dist=500, dist_type="bbox", network_type="bike") # graph from list of places G = ox.graph_from_place([place1], network_type="drive", clean_periphery=False) # graph from polygon G = ox.graph_from_polygon(polygon, network_type="walk", truncate_by_edge=True) # test custom query filter cf = ( '["highway"]' '["area"!~"yes"]' '["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]' '["foot"!~"no"]' '["service"!~"private"]' '["access"!~"private"]' ) G = ox.graph_from_point( location_point, dist=500, custom_filter=cf, dist_type="bbox", network_type="all" ) G = ox.graph_from_point( location_point, dist=500, dist_type="network", network_type="all_private", )
def get_graph(row): global count_failed global count_success global count_already global count_small global failed_list try: # graph name = country + country iso + uc + uc id graph_name = '{}-{}-{}-{}'.format(row['CTR_MN_NM'], row['CTR_MN_ISO'], row['UC_NM_MN'], row['ID_HDC_G0']) graphml_folder = '{}/{}-{}'.format(output_graphml_path, row['CTR_MN_NM'], row['CTR_MN_ISO']) graphml_file = '{}-{}.graphml'.format(row['UC_NM_MN'], row['ID_HDC_G0']) filepath = os.path.join(graphml_folder, graphml_file) if not os.path.exists(filepath): # get graph print(ox.ts(), graph_name) G = ox.graph_from_polygon(polygon=row['geometry'].buffer(0), network_type=network_type, retain_all=retain_all, simplify=simplify, truncate_by_edge=truncate_by_edge) # don't save graphs if they have fewer than 3 nodes if len(G) > 2: ox.save_graphml(G, filepath=filepath) count_success = count_success + 1 else: count_small = count_small + 1 else: count_already = count_already + 1 except Exception as e: count_failed = count_failed + 1 failed_list.append(graph_name) ox.log('"{}" failed: {}'.format(graph_name, e), level=lg.ERROR) print(e, graph_name)
def make_graph(bounding_zone): G = ox.graph_from_polygon(bounding_zone, network_type='walk') # Project the graph from lat-long to the UTM zone appropriate for its geographic location. G = ox.project_graph(G) hwy_speeds = { "residential": 40, "unclassified": 40, "tertiary": 56, "secondary": 56, "primary": 80, "trunk": 56 } G = ox.add_edge_speeds(G, hwy_speeds) G = ox.add_edge_travel_times(G) return G
def getIntersections(boundary, projection, tolerance=100): ''' INPUT: boundary shap projection in UTM int (in meters) representing tolerance of clean_intersections() function for osmnx package OUTPUT: GeoSeries of Intersections ''' # Create Graph G = ox.graph_from_polygon(boundary, network_type='drive') ox.plot_graph(G) plt.show() # Clean Intersections G_proj = ox.project_graph(G, to_crs=projection) intersections = ox.clean_intersections(G_proj, tolerance=tolerance, dead_ends=False) return intersections
def test_get_network_methods(): # graph from bounding box north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=500) G = ox.graph_from_bbox(north, south, east, west, network_type="drive") G = ox.graph_from_bbox( north, south, east, west, network_type="drive_service", truncate_by_edge=True ) # graph from address G = ox.graph_from_address(address=address, dist=500, dist_type="bbox", network_type="bike") # graph from list of places G = ox.graph_from_place([place1], network_type="drive", clean_periphery=False) # graph from polygon G = ox.graph_from_polygon(polygon, network_type="walk", truncate_by_edge=True) # test custom query filter cf = ( '["highway"]' '["area"!~"yes"]' '["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]' '["foot"!~"no"]' '["service"!~"private"]' '["access"!~"private"]' ) G = ox.graph_from_point( location_point, dist=500, custom_filter=cf, dist_type="bbox", network_type="all" ) # test custom settings cs = '[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]' G = ox.graph_from_point( location_point, dist=500, custom_settings=cs, dist_type="network", network_type="all_private", )
def setup(db_name: str = DB_NAME): db = db_connection(db_name) # Get the boundary of Downingtown PA gdf_bounds = ox.geocode_to_gdf({"city": "Downingtown", "state": "PA"}) gdf_bounds = gdf_bounds.to_crs("EPSG:26918") one_mile_in_meters = 1609.34 five_miles_in_meters = one_mile_in_meters * 5 gdf_bounds_buffer = gdf_bounds.copy() gdf_bounds_buffer["geometry"] = gdf_bounds.geometry.buffer( five_miles_in_meters) # Get all OSM road features polygon = gdf_bounds_buffer.to_crs("EPSG:4326").geometry[0] g = ox.graph_from_polygon(polygon) g = g.to_undirected() nodes, edges = ox.graph_to_gdfs(g) db.import_geodataframe(gdf_bounds, "boundary") db.import_geodataframe(gdf_bounds_buffer, "boundary_5mi_buffer") db.import_geodataframe(edges, "osm_edges") db.import_geodataframe(nodes, "osm_nodes") # Reproject from 4326 to 26918 to facilitate analysis queries db.table_reproject_spatial_data("osm_edges", 4326, 26918, "LINESTRING") db.table_reproject_spatial_data("osm_nodes", 4326, 26918, "POINT") # Make a uuid column make_id_query = """ CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; alter table osm_edges add column osmuuid uuid; update osm_edges set osmuuid = uuid_generate_v4(); """ db.execute(make_id_query)
def osm_net_retrieve_polygon(polygon, network_type, osm_folder="OSM/"): """ Download .shp format road network within the specified polygon :param polygon: polygon geodataframe :param network_type: string e.g., 'drive' :param osm_folder: string, path to save the downloaded shapefile :return: None """ G = ox.graph_from_polygon(polygon, network_type=network_type) ox.save_graphml(G, filepath=os.path.join(osm_folder, network_type + '_network.graphml')) gdf = ox.graph_to_gdfs(G) edge = gdf[1] edge = edge.loc[:, ['geometry', 'highway', 'junction', 'length', 'maxspeed', 'name', 'oneway', 'osmid', 'u', 'v', 'width']] fields = ['highway', 'junction', 'length', 'maxspeed', 'name', 'oneway', 'osmid', 'u', 'v', 'width'] df_inter = pd.DataFrame() for f in fields: df_inter[f] = edge[f].astype(str) gdf_edge = GeoDataFrame(df_inter, geometry=edge["geometry"]) gdf_edge.to_file(osm_folder + network_type + "_net.shp")
def load_london_network(): #check if graph already exist to load from memory if os.path.isfile('data/london_network.graphml'): print('reading network from file') return ox.save_load.load_graphml('london_network.graphml', folder='data') #if graph does not exist, download using osmnx else: print('creating network form OSM') place = 'London, Greater London, England, United Kingdom' gdf_place = ox.gdf_from_place(place, which_result=2, buffer_dist=500) london_polygon = gdf_place['geometry'].unary_union #get exterior coords and create new polygon, as city of london is a hole london_polygon = Polygon(london_polygon.exterior) london_network = ox.graph_from_polygon(london_polygon) #project graph to BNG london_network = ox.project_graph(london_network, to_crs={'init':'epsg:27700'}) #create a data folder if it does not exist if not os.path.exists('data'): os.makedirs('data') #save to file ox.save_load.save_graphml(london_network, filename='london_network.graphml', folder='data', gephi=False) return london_network
def osm_graph_from_polygon(polygon_points, network_type, simplify): """ Import an OSM graph of the area within the polygon. :param polygon_points: list of (lon, lat) points delimiting the network zone :param network_type: osm network type :param simplify: boolean indicating if the graph should be simplified :return: a networkx graph """ if polygon_points is None: print( "The polygon parameter must be specified when importing graph from polygon." ) exit(1) # create a shapely polygon with (lat, lon) coordinates from the list of points shapely_polygon = shapely_polygon_from_points(polygon_points) return ox.graph_from_polygon(shapely_polygon, network_type=network_type, simplify=simplify)
def query_OSM(directory, boundary, graph_pickle_file, query='drive_main'): """ Query OpenStreetMap and save it as a pickle file. Parameters ---------- directory : directory path where inputs and outputs should be boundary : Boundary for road network which can be either polygon boundary file or bounding box coordinates [north, south, west, east] network_type : string {'walk', 'bike', 'drive_all', 'drive_main', 'drive_main_links_included', 'drive_service', 'all', 'all_private', 'none'} what type of street or other network to get """ if type(boundary) == str: # boundary .shp file path polygon = gpd.read_file(boundary) polygon = polygon.get_value(0, 'geometry') mainG = ox.graph_from_polygon(polygon, network_type=query, simplify=False, retain_all=True, truncate_by_edge=False) else: # bounding box [] mainG = qr.graph_from_bbox(*boundary) nx.write_gpickle(mainG, graph_pickle_file)
def __init__(self, boundingBox): self.graph = ox.graph_from_polygon(boundingBox.polygon(), network_type='drive') self.nodesGdf, self.edgesGdf = self.graphToGdfs()
for w, v2, t in graph.get(v1, ()): if v2 in seen: continue # Not every edge will be calculated. The edge which can improve the value of node in heap will be useful. if v2 not in dist or weight + w < dist[v2]: dist[v2] = weight + w # push the element into the piority queue heappush(q, (weight + w, v2, path)) return None # =========================================== QUERY DATA =========================================== # # Walk query data G_walk = ox.graph_from_polygon(polygon, retain_all=True, truncate_by_edge=True, network_type='walk') nodes, edges = ox.graph_to_gdfs(G_walk) nodes.to_csv('data/walk_nodes.csv') edges.to_csv('data/walk_edges.csv') # Train query data from overpass API train_query_str = '[out:json][timeout:180];(relation["network"="Singapore Rail"]["route"="monorail"]' \ '(1.3920,103.8955,1.4191,103.9218); >;);out;' train_response_json = overpass_request(data={'data': train_query_str}, timeout=180) train_graph = create_graph(train_response_json, name='unnamed') G_train = ox.truncate_graph_polygon(train_graph, polygon, truncate_by_edge=True,
def __init__(self, area, output, centrality=True, useful_tags_path=None): """ The class gets a name of polygon and saves shapefile which includes pedestrian network within the polygon extent :param area: the place polygon to get the walk network from :param output: where to store the network as shapefile :param centrality: add centrality indices to edges :param useful_tags_path: tags to download from OSM """ # define the tags that should be downloaded with the network if useful_tags_path is None: useful_tags_path = ['highway', 'handrail', 'footway', 'crossing', 'sidewalk'] useful_tags_path.extend(list(accessibility_dic.keys())) ox.utils.config(useful_tags_way=useful_tags_path) # downloaded the graph based on the specified location and make undirected it project it print('download data - {}'.format(datetime.now())) if isinstance(area, str): graph = ox.graph_from_place(area, network_type='walk') else: graph = ox.graph_from_polygon(area, network_type='walk') # make the graph undirected graph then project the graph self.graph_pr = ox.project_graph(graph) self.graph_pr = self.graph_pr.to_undirected() if centrality: # Calculate betweenness/choice and closeness/integration. # to work properly the algorithm work on graph. to run # graph_to_gdfs later the code convert it back to MultiGraph. print('calculate integration at {}'.format(datetime.now())) self.graph_pr = nx.Graph(self.graph_pr) # line_graph convert graph to line graph so edges become nodes edge_centrality = nx.closeness_centrality(nx.line_graph(self.graph_pr)) nx.set_edge_attributes(self.graph_pr, edge_centrality, 'integ') print("calculate choice- {}".format(datetime.now())) dic = edge_betweenness_centrality(self.graph_pr) nx.set_edge_attributes(self.graph_pr, dic, 'choice') self.graph_pr = nx.MultiGraph(self.graph_pr) # from graph to geodataframe , save the point dateframe for later use gdf_format = ox.graph_to_gdfs(self.graph_pr) gdf_format[0].to_crs(epsg=3857).to_file('pnt_' + output) # create dataframe with the necessary columns and convert to shapefile edges = gdf_format[1] self.columns = [value for value in useful_tags_path if value in list(edges.columns)] edges_new = edges.apply(lambda row: self.list_to_str(row), axis=1) edges_new.crs = edges.crs self.columns.append('geometry') if centrality: self.columns.append('choice') self.columns.append('integ') edges_shp = edges_new.loc[:, self.columns] # rename long name (accessibility tags) edges_shp = get_acc_tags_shp(edges_shp) # project the file to compatible coordinate system edges_shp.to_crs(epsg=3857).to_file(output)
ox.config(log_console=True, use_cache=True, data_folder='data/vector/city_networks/') scriptpath = os.path.dirname(__file__) cities = glob.glob("data/vector/city_boundaries/*.shp") #indir = "data/vector/city_boundaries/" for city in cities: name = os.path.basename(city).split('.')[0] place = gpd.read_file(city) place_simple = place.unary_union # disolving boundaries based on attributes # Use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent) G = ox.graph_from_polygon(place_simple, network_type='drive', retain_all=True) G_projected = ox.project_graph(G) # save the shapefile to disk #name = os.path.basename("beijing.shp")).split(".")[0] # make better place_names ox.save_graph_shapefile(G_projected, filename=name) area = ox.project_gdf(place).unary_union.area stats = ox.basic_stats(G, area=area) # save to file: def ensure_dir(file_path): directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) path = os.path.join('data/vector/city_networks/', name)
crs = boundary_geom.srid # then convert to shapely shapely_geom = to_shape(boundary_geom) # buffer to get valid outer ring polygon instead of invalid multipolygon. valid_poly = shapely_geom.buffer(0) # success = get_network(shapely_geom, filter = 'all_private', name = 'london_all', filename = "all_priv_inner_london.graphml") # call function for getting and saving network from OSMnx # lines for getting and saving graph to graphml graph = ox.graph_from_polygon(valid_poly, name='london_all') # file = ox.save_graphml(graph, filename = 'all.graphml', gephi = True) # multidigraph to geodataframes nodes, edges = ox.graph_to_gdfs(graph, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True) # not clear how to check the shapely object # query inner borough table for polygon # make polygon a shapely object
def osmnx_coefficient_computation(gdf, net_type, basic_stats, extended_stats, connectivity=False, anc=False, ecc=False, bc=False, cc=False): ''' Apply osmnx's graph from polygon to query a city's street network within a geometry. This may be a long procedure given the hexagon layer resolution. Parameters ---------- gdf: GeoDataFrame GeoDataFrame with geometries to download graphs contained within them. net_type: str Network type to download. One of {'drive', 'drive_service', 'walk', 'bike', 'all', 'all_private'} basic_stats: list List of basic stats to compute from downloaded graph extended_stats: list List of extended stats to compute from graph connectivity: bool. Default False. Compute node and edge connectivity anc: bool. Default False. Compute avg node connectivity ecc: bool. Default False. Compute shortest paths, eccentricity and topological metric bc: bool. Default False. Compute node betweeness centrality cc: bool. Default False. Compute node closeness centrality For more detail about these parameters, see https://osmnx.readthedocs.io/en/stable/osmnx.html#module-osmnx.stats Returns ------- gdf: Input GeoDataFrame with updated columns containing the selected metrics Examples -------- >>> hexagons = urbanpy.geom.gen_hexagons(8, lima) >>> urbanpy.geom.osmnx_coefficient_computation(hexagons.head(), 'walk', ['circuity_avg'], []) On record 1: There are no nodes within the requested geometry On record 3: There are no nodes within the requested geometry hex | geometry | circuity_avg 888e62c64bfffff | POLYGON ((-76.89763 -12.03869, -76.90194 -12.0... | 1.021441 888e6212e1fffff | POLYGON ((-76.75291 -12.19727, -76.75722 -12.2... | NaN 888e62d333fffff | POLYGON ((-77.09253 -11.83762, -77.09685 -11.8... | 1.025313 888e666c2dfffff | POLYGON ((-76.93109 -11.79031, -76.93540 -11.7... | NaN 888e62d4b3fffff | POLYGON ((-76.87935 -12.03688, -76.88366 -12.0... | 1.044654 ''' #May be a lengthy download depending on the amount of features for index, row in tqdm(gdf.iterrows()): try: graph = ox.graph_from_polygon(row['geometry'], net_type) b_stats = ox.basic_stats(graph) ext_stats = ox.extended_stats(graph, connectivity, anc, ecc, bc, cc) for stat in basic_stats: gdf.loc[index, stat] = b_stats.get(stat) for stat in extended_stats: gdf.loc[index, stat] = ext_stats.get(stat) except Exception as err: print(f'On record {index}: ', err) return gdf
aoi = r'prboundingwgs84.shp' # Graph didn't populate on the smaller islands when using the Puerto Rico admin boundary. # Created a rectangular bounding box (in QGIS) to use as the AOI instead. shp = gpd.read_file(os.path.join(pth, aoi)) bound = shp.geometry.iloc[0] bound # Check that it's a rectangle (short and wide) #%% """ Get driving network for all islands in Puerto Rico. Travel measured in length (meters). """ gDrive = ox.graph_from_polygon(bound, network_type='drive') # This took about half an hour. # Note: length is measured in meters. # Save all road nodes (points on the road) to file. gDrive_node_gdf = gn.node_gdf_from_graph(gDrive) gDrive_node_gdf.to_csv(os.path.join(pth, 'drive_dist_node.csv')) #%% """ Add a time measure using a speed dictionary. """ speed_dict = { 'residential': 20, # kmph
def download_osm_graph(city, cities): polygon = gpd.read_file(cities[city]['polygon_path'])['geometry'].values[0] cities[city]['G'] = ox.graph_from_polygon(polygon, network_type='drive') cities[city].update(dt.get_osm(city, cities[city]['G'])) return cities