def save_graph(target, graph_name, distance=1000, is_address=True): # Load graph from OSM if is_address: graph = ox.graph_from_address(address=target, distance=distance, network_type='drive') else: graph = ox.graph_from_place(query=target, network_type='drive') # Project graph graph_proj = ox.project_graph(graph) folder_path = path.join('graphs', graph_name) if not path.exists(folder_path): mkdir(folder_path) graph_data = graph_proj.graph serialize_dict(dictionary=graph_data, file_path=path.join(folder_path, 'graph_data.pkl.gz')) # We make the graph strongly connected to ensure that any combination of source / sink # constitutes a valid problem graph_component = ox.get_largest_component(graph_proj, strongly=True).to_directed() # Save pictures of the graph plot_road_graph(graph_component, graph_name=graph_name, file_path=path.join(folder_path, 'graph')) # Save graph ox.save_graphml(graph_component, filename='graph.graphml', folder=folder_path, gephi=True) # Save a selection of graph-wide stats. stats_file = path.join(folder_path, 'stats.csv') delete_if_exists(stats_file) n_nodes = graph_component.number_of_nodes() n_edges = graph_component.number_of_edges() avg_in_deg = np.average([d for _, d in graph_component.in_degree()]) avg_out_deg = np.average([d for _, d in graph_component.out_degree()]) diam = nx.diameter(graph_component) append_row_to_log(['Number of Nodes', n_nodes], stats_file) append_row_to_log(['Number of Edges', n_edges], stats_file) append_row_to_log(['Average In Degree', avg_in_deg], stats_file) append_row_to_log(['Average Out Degree', avg_out_deg], stats_file) append_row_to_log(['Diameter', diam], stats_file) cleaned_target = target.replace('\"', '').replace('\'', '').replace(',', '') query = '{0}; Dist: {1}'.format(cleaned_target, distance) if is_address else cleaned_target append_row_to_log(['Query', query], stats_file) return graph_component
def download_graph(polygon, city, network_type='walk', save=True): """Download a graph from a bounding box, and saves it to disk Arguments: polygon {polygon} -- polygon to use as boundary to download the network city {str} -- string with the name of the city Keyword Arguments: network_type {str} -- String with the type of network to download (drive, walk, bike, all_private, all) for more details see OSMnx documentation save {bool} -- Save the graph to disk or not (default: {True}) Returns: nx.MultiDiGraph """ try: G = ox.load_graphml('../data/raw/network_{}_{}.graphml'.format( city, network_type)) print(f'{city} retrived graph') return G except: print(f'{city} graph not in data/raw') G = ox.graph_from_polygon(polygon, network_type=network_type, simplify=True, retain_all=False, truncate_by_edge=False) print('downloaded') if save: ox.save_graphml(G, filename='raw/network_{}_{}.graphml'.format( city, network_type)) return G
def load_map(location='Manhattan, New York, USA', cache_directory='/tmp', force_redownload=False): cached_filename = hashlib.md5(location).hexdigest() + '.graphml' try: if force_redownload: raise IOError('Forcing re-download of graph.') logging.info('Trying to load map from "%s"', os.path.join(cache_directory, cached_filename)) graph = ox.load_graphml(cached_filename, folder=cache_directory) except IOError: logging.info('Downloading map from "%s"', location) graph = ox.graph_from_place(location, network_type='drive') # Keep the largest strongly connected component. logging.info('Finding largest component') graph = max(nx.strongly_connected_component_subgraphs(graph), key=len) graph = ox.project_graph(graph) logging.info('Saving map to "%s"', os.path.join(cache_directory, cached_filename)) ox.save_graphml(graph, filename=cached_filename, folder=cache_directory) # Add dummy speed and length information. for u, v, key, data in graph.edges(data=True, keys=True): if 'time' not in data: time = data['length'] / _DEFAULT_SPEED data['time'] = time data['speed'] = _DEFAULT_SPEED else: data['time'] = float(data['time']) data['speed'] = float(data['speed']) graph.add_edge(u, v, **data) return graph
def Save_OSM_G(polygon, network_type= 'walk', placename=placename, suffix=suffix, folder=OSM_folder): """ save a graphml from a single polygon. Parameters ---------- polygon : shapely Polygon or MultiPolygon the shape to get network data within. coordinates should be in units of latitude-longitude degrees. network_type : string what type of street network to get. Default type is pedestrain network placename: string place name suffix: string output data date folder : string where to save the shapefile, specify local folder path for OSM resource Returns ------- none """ #The graph_from_polygon function requires a polygon in units of lat-long (epsg 4326) G = ox.graph_from_polygon(polygon, network_type=network_type, retain_all = True) #save network as graphml ox.save_graphml(G, filename='{studyregion}_{network_type}{suffix}.graphml'.format( studyregion = placename, network_type=network_type, suffix = suffix), folder=folder) ox.plot_graph(G)
def loadOSMnxMap(file=None, download=False, saveMap=False): point1 = (45.1904, 5.7607) #todo load point from given gpx distance = 1000 distance_type_var = 'network' network_type_var = 'walk' print("============== Loading map ==================") if (not file and not download): print('No map specified') elif (file): graph = osmnx.load_graphml(filename=file, folder=None) print("Map loaded") g, a = osmnx.plot_graph(graph, show=False, close=False) return g, a elif (download): graph = osmnx.graph_from_point(point1, distance, distance_type=distance_type_var, network_type=network_type_var) if (saveMap): osmnx.save_graphml(graph, filename='map.hraphml') print('Map saved') print("Map loaded") return osmnx.plot_graph(graph, show=False, close=False) return None, None
def load_map(place, type): ox.config(log_console=True, use_cache=True) G = ox.graph_from_place(place, network_type=type) ox.save_graphml(G, filename='network.graphml') with open('network_type.yml', 'w') as file: file.write(type) print('Map is downloaded and saved in network.graphml')
def test_network_saving_loading(): # save/load graph as shapefile and graphml file G = ox.graph_from_place('Piedmont, California, USA') G_projected = ox.project_graph(G) ox.save_graph_shapefile(G_projected) ox.save_graphml(G_projected) ox.save_graphml(G_projected, filename='gephi.graphml', gephi=True) G2 = ox.load_graphml('graph.graphml') G3 = ox.load_graphml('graph.graphml', node_type=str) # convert graph to node/edge GeoDataFrames and back again gdf_edges = ox.graph_to_gdfs(G, nodes=False, edges=True, fill_edge_geometry=False) gdf_nodes, gdf_edges = ox.graph_to_gdfs(G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True) G4 = ox.gdfs_to_graph(gdf_nodes, gdf_edges) # find graph nodes nearest to some set of points X = gdf_nodes['x'].head() Y = gdf_nodes['y'].head() nn1 = ox.get_nearest_nodes(G, X, Y) nn2 = ox.get_nearest_nodes(G, X, Y, method='kdtree') nn3 = ox.get_nearest_nodes(G, X, Y, method='balltree') # find graph edges nearest to some set of points ne1 = ox.get_nearest_edges(G, X, Y) ne2 = ox.get_nearest_edges(G, X, Y, method='kdtree') ne3 = ox.get_nearest_edges(G, X, Y, method='kdtree', dist=50)
def test_network_saving_loading(): # save graph as shapefile and geopackage G = ox.graph_from_place(place1, network_type="drive") ox.save_graph_shapefile(G) ox.save_graph_geopackage(G) # save/load graph as graphml file ox.save_graphml(G, gephi=True) ox.save_graphml(G, gephi=False) filepath = os.path.join(ox.settings.data_folder, "graph.graphml") G = ox.load_graphml(filepath, node_type=str) # test osm xml output default_all_oneway = ox.settings.all_oneway ox.settings.all_oneway = True G = ox.graph_from_point(location_point, dist=500, network_type="drive") ox.save_graph_xml(G, merge_edges=False) # test osm xml output merge edges ox.save_graph_xml(G, merge_edges=True, edge_tag_aggs=[("length", "sum")]) # test osm xml output from gdfs nodes, edges = ox.graph_to_gdfs(G) ox.save_graph_xml([nodes, edges]) # test ordered nodes from way df = pd.DataFrame({ "u": [54, 2, 5, 3, 10, 19, 20], "v": [76, 3, 8, 10, 5, 20, 15] }) ordered_nodes = ox.io._get_unique_nodes_ordered_from_way(df) assert ordered_nodes == [2, 3, 10, 5, 8] ox.settings.all_oneway = default_all_oneway
def graph_from_place(): values = request.get_json() location = values.get('location') network_type = values.get('network_type') print(location) print(network_type) if location is None: return "Error, please supply a valid location", 400 if network_type is None: return "Error, please supply a valid network_type", 400 G = ox.graph_from_place(location, network_type=network_type) ox.save_graphml(G, filename="graph_from_place.graphml", folder="/app/output") #jdata = json_graph.tree_data(G,root=1) #graphml_json = json_graph.tree_graph(jdata) #return jsonify(graphml_json), 200 # return 'ok', 200 content = get_file('output/graph_from_place.graphml') return Response(content, mimetype="application/xml")
def graph(place_country, distance): # filter # filter out some attributes # filter = ('["highway"!~"residential|unclassified|living_street|track|abandoned|path|footway|service|pedestrian|road|' # 'raceway|cycleway|steps|construction"]') # filter = ( # '["highway"!~"residential|unclassified|living_street|track|abandoned|path|footway|service|pedestrian|road|' # 'raceway|cycleway|steps|construction|primary|secondary|tertiary"]') # filter = ( # '["highway"!~"living_street|abandoned|footway|pedestrian|raceway|cycleway|steps|construction|' # 'bus_guideway|bridleway|corridor|escape|rest_area|track|sidewalk|proposed|path"]') # filter = ( # '["highway"!~"living_street|abandoned|steps|construction|' # 'bus_guideway|bridleway|corridor|escape|rest_area|track|sidewalk|proposed|path"]') filter = ( '["highway"!~"living_street|abandoned|steps|construction|service|pedestrian|' 'bus_guideway|bridleway|corridor|escape|rest_area|track|sidewalk|proposed|path|footway"]' ) # filter = filter # import grapho (graphml) G = ox.graph_from_address(str(place_country), distance=distance, network_type='drive', custom_filter=filter) # import shapefile # G_shp = ox.gdf_from_place(place_country) # import shapefile with edges and nodes # as the data is in WGS84 format, we might first want to reproject our data into metric system # so that our map looks better with the projection of the graph data in UTM format # G_projected = ox.project_graph(G) # save street network as ESRI shapefile (includes NODES and EDGES) name_place_country = re.sub('[/," ",:]', '_', place_country) # ox.save_graph_shapefile(G_projected, filename='network_' + name_place_country + '-shape') ox.save_graphml(G, filename=name_place_country + '.graphml')
def detect_drive_network_from_point(lat=13.14633, lon=77.514386, distance=2000, save=True, filename='icts2000'): G = ox.graph_from_point((lat, lon), distance=distance, network_type='drive', simplify=False) hwy_types = ['primary', 'motorway', 'trunk'] gdf = ox.graph_to_gdfs(G, nodes=False) mask = ~gdf['highway'].map(lambda x: isinstance(x, str) and x in hwy_types) edges = zip(gdf[mask]['u'], gdf[mask]['v'], gdf[mask]['key']) G.remove_edges_from(edges) G = ox.remove_isolated_nodes(G) G_projected = ox.project_graph(G) filename += '-' + str(distance) fig, ax = ox.plot_graph(G_projected, show=False, save=save, filename=filename, file_format='svg') plt.scatter(*utm.from_latlon(lat, lon)[:2]) plt.show() ox.save_graphml(G, filename=filename + '.graphml') return G
def save_stuff(Graph,a): pass ## #ox.save_graph_shapefile(Graph, filename='network-shape') ## ox.save_graphml(Graph, filename='network.graphml') ## fig, ax = ox.plot_graph(Graph, show=False, save=True, filename='network', file_format='svg') ## ## #G2 = ox.load_graphml('network.graphml') #load ## fig, ax = ox.plot_graph(G2) ## ## gdf = ox.footprints_from_place(place='Piedmont, California, USA') #save building footprints ## gdf.drop(labels='nodes', axis=1).to_file('data/piedmont_bldgs') if (0 in a): ox.save_graphml(Graph,'network.graphml') if (1 in a): fh=open("test.adjlist",'wb') nx.write_adjlist(Graph, fh, delimiter=',') fh.close() if (2 in a): A = nx.adjacency_matrix(Graph) #A=A.todense() #np.savetxt('file1',A,delimiter=',') sp.sparse.save_npz('file1', A, compressed=True) if (3 in a): fire.to_pickle('fire.pkl') healthcare.to_pickle('healthcare.pkl') shops.to_pickle('shops.pkl') return
def download_data(place, data_path): place_ref = str(place['state']) print('OSM data requested for city: ' + str(place_ref)) path_to_output = data_path # if folder not exist create folder with place name if not (os.path.isdir(path_to_output)): os.makedirs(path_to_output) poi_file = path_to_output + '/poi.geojson' building_file = path_to_output + '/buildings.geojson' street_file = path_to_output + '/network.graphml' # Requesting polygon of place polygon = get_polygon(place) # Requesting POI data within polygon poi_data = get_poi_data(place, polygon) # saving POI data as geojson store_geodataframe(poi_data, poi_file) # Requesting building data of city using polygon buildings_data = get_buildings(place, polygon) # saving building data as geojson store_geodataframe(buildings_data, building_file) # Requesting street network using polygon street_data = ox.graph_from_polygon(polygon, network_type='drive') # Save street network as GraphML file ox.save_graphml(street_data, filename=street_file) print('Stored OSM data files for city: ' + place_ref)
def save_osm_graph(graph, filename, folder): """ Save the given graph in a .graphml file. Detect if the filename ends with '.bz2', and realise a bz2 compression accordingly. :param graph: saved graph :param filename: name of the save file :param folder: name of the save folder """ # check bz2 extension if filename.endswith(".bz2"): to_bz2 = True filename = filename[:-4] else: to_bz2 = False # check filename if not filename.endswith(".graphml"): raise ValueError( "OSM graph filename must end with .graphml or .graphml.bz2") # save the graph filepath = folder + filename ox.save_graphml(graph, filepath) # compress to bz2 if necessary if to_bz2: subprocess.run(["bzip2", "-z", "-f", filepath]) print("Saved osm graph at " + filepath + ".bz2") else: print("Saved osm graph at " + filepath)
def read_proj_graphml(proj_graphml_filepath, ori_graphml_filepath, to_crs): """ Read a projected graph from local disk if exist, otherwise, reproject origional graphml to the UTM zone appropriate for its geographic location, and save the projected graph to local disk Parameters ---------- proj_graphml_filepath: string the projected graphml filepath ori_graphml_filepath: string the original graphml filepath to_crs: dict or string or pyproj.CRS project to this CRS Returns ------- networkx multidigraph """ # if the projected graphml file already exist in disk, then load it from the path if os.path.isfile(proj_graphml_filepath): print("Read network from disk.") return ox.load_graphml(proj_graphml_filepath) # else, read original study region graphml and reproject it else: print("Reproject network, and save the projected network to disk") # load and project origional graphml from disk G = ox.load_graphml(ori_graphml_filepath) G_proj = ox.project_graph(G, to_crs=to_crs) # save projected graphml to disk ox.save_graphml(G_proj, proj_graphml_filepath) return G_proj
def test_network_saving_loading(): # save graph as shapefile and geopackage G = ox.graph_from_place(place1, network_type="drive") ox.save_graph_shapefile(G) ox.save_graph_geopackage(G) # save/load graph as graphml file ox.save_graphml(G, gephi=True) ox.save_graphml(G, gephi=False) filepath = os.path.join(ox.settings.data_folder, "graph.graphml") G2 = ox.load_graphml(filepath) # verify everything in G is equivalent in G2 for (n1, d1), (n2, d2) in zip(G.nodes(data=True), G2.nodes(data=True)): assert n1 == n2 assert d1 == d2 for (u1, v1, k1, d1), (u2, v2, k2, d2) in zip(G.edges(keys=True, data=True), G2.edges(keys=True, data=True)): assert u1 == u2 assert v1 == v2 assert k1 == k2 assert tuple(d1.keys()) == tuple(d2.keys()) assert tuple(d1.values()) == tuple(d2.values()) for (k1, v1), (k2, v2) in zip(G.graph.items(), G2.graph.items()): assert k1 == k2 assert v1 == v2 assert tuple(G.graph["streets_per_node"].keys()) == tuple( G2.graph["streets_per_node"].keys()) assert tuple(G.graph["streets_per_node"].values()) == tuple( G2.graph["streets_per_node"].values()) # test custom data types nd = {"osmid": str} ed = {"length": str, "osmid": float} G2 = ox.load_graphml(filepath, node_dtypes=nd, edge_dtypes=ed) # test osm xml output default_all_oneway = ox.settings.all_oneway ox.settings.all_oneway = True G = ox.graph_from_point(location_point, dist=500, network_type="drive") ox.save_graph_xml(G, merge_edges=False) # test osm xml output merge edges ox.save_graph_xml(G, merge_edges=True, edge_tag_aggs=[("length", "sum")]) # test osm xml output from gdfs nodes, edges = ox.graph_to_gdfs(G) ox.save_graph_xml([nodes, edges]) # test ordered nodes from way df = pd.DataFrame({ "u": [54, 2, 5, 3, 10, 19, 20], "v": [76, 3, 8, 10, 5, 20, 15] }) ordered_nodes = ox.io._get_unique_nodes_ordered_from_way(df) assert ordered_nodes == [2, 3, 10, 5, 8] ox.settings.all_oneway = default_all_oneway
def downloadNetwork(place: str, modeOfTransport: str, networksPath: str) -> bool: ox.config(all_oneway=True, log_console=True, use_cache=True) pathForStorage = networksPath + place + ', ' + modeOfTransport G = ox.graph_from_place(place, network_type=modeOfTransport, simplify=True) ox.save_graphml(G, filepath=pathForStorage) return G
def Save_OSM_G_proj(placename=placename, network_type= 'walk', suffix=suffix, folder=OSM_folder, to_crs=None): """ save a projected graphml from graphml file Parameters ---------- network_type : string what type of street network to get. Default type is pedestrain network placename: string place name suffix: string output data date folder : string where to save the shapefile, specify local folder path for OSM resource Returns ------- none """ G = ox.load_graphml(filename='{studyregion}_{network_type}{suffix}.graphml'.format( studyregion = placename, network_type=network_type, suffix = suffix), folder=folder) #set up project projection, and save the projected graph (project to UTM so we can use metres as a unit when buffering) G_proj = ox.project_graph(G, to_crs=to_crs) ox.save_graphml(G_proj, filename='{studyregion}_proj_{network_type}{suffix}.graphml'.format( studyregion = placename, network_type=network_type, suffix = suffix), folder=folder) ox.plot_graph(G_proj)
def test_network_saving_loading(): # save/load graph as shapefile and graphml file G = ox.graph_from_place('Piedmont, California, USA') G_projected = ox.project_graph(G) ox.save_graph_shapefile(G_projected) ox.save_graphml(G_projected) ox.save_graphml(G_projected, filename='gephi.graphml', gephi=True) G2 = ox.load_graphml('graph.graphml') # convert graph to node/edge GeoDataFrames and back again gdf_edges = ox.graph_to_gdfs(G, nodes=False, edges=True, fill_edge_geometry=False) gdf_nodes, gdf_edges = ox.graph_to_gdfs(G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True) G3 = ox.gdfs_to_graph(gdf_nodes, gdf_edges) # find graph nodes nearest to some set of points X = gdf_nodes['x'].head() Y = gdf_nodes['y'].head() nn1 = ox.get_nearest_nodes(G, X, Y) nn2 = ox.get_nearest_nodes(G, X, Y, method='kdtree') nn3 = ox.get_nearest_nodes(G, X, Y, method='balltree')
def get_data(G, district_name): # print(nx.info(G)) print(f'File Path = {PATH}') # save as graphml path = f'{PATH}/{district_name}.xml' ox.save_graphml(G, path) # save as .osm path = f'{PATH}/{district_name}.osm' ox.config(all_oneway=True) if not os.path.exists(path): ox.save_graph_xml(G, filepath=path) # save as folium html path = f'{PATH}/{district_name}_folium.html' if not os.path.exists(path): map_folium = ox.folium.plot_graph_folium(G) map_folium.save(path) # save as SVG path = f'{PATH}/{district_name}_image.svg' fig, ax = ox.plot_graph(G, show=False, save=True, close=True, filepath=path) # save graph as a shapefile and .csv path = f'{PATH}/{district_name}_shape' ox.save_graph_shapefile(G, filepath=path) make_adjacency_matrix(district_name) clean_csv(district_name) make_adjacency_required_matrix(district_name)
def test_graph_save_load(): # save graph as shapefile and geopackage G = ox.graph_from_place(place1, network_type="drive") ox.save_graph_shapefile(G) ox.save_graph_geopackage(G, directed=False) # save/load geopackage and convert graph to/from node/edge GeoDataFrames fp = ".temp/data/graph-dir.gpkg" ox.save_graph_geopackage(G, filepath=fp, directed=True) gdf_nodes1 = gpd.read_file(fp, layer="nodes").set_index("osmid") gdf_edges1 = gpd.read_file(fp, layer="edges").set_index(["u", "v", "key"]) G2 = ox.graph_from_gdfs(gdf_nodes1, gdf_edges1) G2 = ox.graph_from_gdfs(gdf_nodes1, gdf_edges1, graph_attrs=G.graph) gdf_nodes2, gdf_edges2 = ox.graph_to_gdfs(G2) assert set(gdf_nodes1.index) == set(gdf_nodes2.index) == set(G.nodes) == set(G2.nodes) assert set(gdf_edges1.index) == set(gdf_edges2.index) == set(G.edges) == set(G2.edges) # create random boolean graph/node/edge attributes attr_name = "test_bool" G.graph[attr_name] = False bools = np.random.randint(0, 2, len(G.nodes)) node_attrs = {n: bool(b) for n, b in zip(G.nodes, bools)} nx.set_node_attributes(G, node_attrs, attr_name) bools = np.random.randint(0, 2, len(G.edges)) edge_attrs = {n: bool(b) for n, b in zip(G.edges, bools)} nx.set_edge_attributes(G, edge_attrs, attr_name) # save/load graph as graphml file ox.save_graphml(G, gephi=True) ox.save_graphml(G, gephi=False) filepath = Path(ox.settings.data_folder) / "graph.graphml" G2 = ox.load_graphml( filepath, graph_dtypes={attr_name: ox.io._convert_bool_string}, node_dtypes={attr_name: ox.io._convert_bool_string}, edge_dtypes={attr_name: ox.io._convert_bool_string}, ) # verify everything in G is equivalent in G2 assert tuple(G.graph.keys()) == tuple(G2.graph.keys()) assert tuple(G.graph.values()) == tuple(G2.graph.values()) z = zip(G.nodes(data=True), G2.nodes(data=True)) for (n1, d1), (n2, d2) in z: assert n1 == n2 assert tuple(d1.keys()) == tuple(d2.keys()) assert tuple(d1.values()) == tuple(d2.values()) z = zip(G.edges(keys=True, data=True), G2.edges(keys=True, data=True)) for (u1, v1, k1, d1), (u2, v2, k2, d2) in z: assert u1 == u2 assert v1 == v2 assert k1 == k2 assert tuple(d1.keys()) == tuple(d2.keys()) assert tuple(d1.values()) == tuple(d2.values()) # test custom data types nd = {"osmid": str} ed = {"length": str, "osmid": float} G2 = ox.load_graphml(filepath, node_dtypes=nd, edge_dtypes=ed)
def osm_downloader(boundary=None, osm_path=None, regenerating_shp=False): """ Download drive network within a certain geographical boundary. :param boundary: geodataframe of the area for downloading the network. :param osm_path: file path to save the network in .shp and .graphml. :param regenerating_shp: if needs to regenerating the shape file. :return: None """ minx, miny, maxx, maxy = boundary.geometry.total_bounds new_network = osm_path + 'drive.graphml' new_network_shp = osm_path + 'drive.shp' def shp_processor(G, new_network_shp): print('Processing graphml to GeoDataframe...') gdf_n = ox.graph_to_gdfs(G) edge = gdf_n[1] edge = edge.loc[:, [ 'geometry', 'highway', 'junction', 'length', 'maxspeed', 'name', 'oneway', 'osmid', 'u', 'v', 'width', 'lanes' ]] fields = ['osmid', 'u', 'v', 'length', 'maxspeed', 'oneway'] df_inter = pd.DataFrame() for f in fields: df_inter[f] = edge[f].astype(str) gdf_edge = gpd.GeoDataFrame(df_inter, geometry=edge["geometry"]) gdf_edge = gdf_edge.rename(columns={ 'osmid': 'osm_id', 'maxspeed': 'max_speed' }) print('Saving as shp...') gdf_edge.to_file(new_network_shp) if not os.path.exists(new_network): print('Downloading graphml...') G = ox.graph_from_bbox(maxy, miny, maxx, minx, network_type='drive') print('Saving as graphml...') ox.save_graphml(G, filepath=new_network) if not os.path.exists(new_network_shp): shp_processor(G, new_network_shp) else: if regenerating_shp: print('Loading graphml...') G = ox.load_graphml(new_network) shp_processor(G, new_network_shp) if not os.path.exists(new_network_shp): print('Loading graphml...') G = ox.load_graphml(new_network) shp_processor(G, new_network_shp) else: print('Drive networks exist. Skip downloading.')
def load_Italy(self): filepath = 'data/italy.graphml' my_file = Path(filepath) if my_file.is_file(): self.map = ox.load_graphml(filepath) else: filta = '["highway"~"motorway|motorway_link|primary"]' self.map = ox.graph_from_place('Italy', network_type='drive', custom_filter=filta) ox.save_graphml(self.map, filepath)
def get_graph(): key = "<<put key here>>" place_query = ['Natick, Massachusetts, USA'] G = ox.graph_from_place(place_query, network_type='walk') G = ox.add_node_elevations(G, key) G = ox.add_edge_grades(G) # G_projected = ox.project_graph(G) ox.save_graphml(G, filename='natick.graphml') print("done")
def save(self): ox.save_graphml(self.G, os.path.join("..", graph_path, self.name + ".ml")) ox.save_graphml( self.H, os.path.join("..", graph_path, "sub_" + self.name + ".ml")) joblib.dump(filename=os.path.join(data_path, "dict_sd_" + self.name + ".jbl"), value=self.dict_sd)
def save_graph(graph, path): """ Saves graph as an OSM (XML) file :param graph: networkx object :param path: location to save """ ox.save_graphml(graph, folder=os.path.dirname(path), filename=os.path.basename(path))
def download_graph(): graph = ox.graph_from_bbox(north=bbox['north'], south=bbox['south'], east=bbox['east'], west=bbox['west'], network_type='drive', truncate_by_edge=True, simplify=True) ox.save_graphml(graph, filepath=f'{file_name}')
def exportBuildingCoordinates(point, name): # point must be a tuple (lat,long) buildings = ox.buildings_from_point(point=point, distance=1800) centroidList = buildings.centroid placeCoordCSV = open(name + "_LatLongs.csv", "w") for i in centroidList: placeCoordCSV.write(str(i.y) + ',' + str(i.x) + '\n') placeCoordCSV.close() network = ox.graph_from_point(point, distance=1800) ox.save_graphml(network, filename=name + '.grpahml', folder='network') ox.save_graph_shapefile(network, filename=name, folder='network')
def __init__(self): start_time = time.time() G = ox.graph_from_place("Florianópolis, Brazil", network_type="drive") #fig, ax = ox.plot_graph(G) ox.save_graphml(G, filepath=arquivo_graphml) ox.save_graph_shapefile(G, filepath=diretorio_shape_network) end_time = time.time() print("Tempo de execucao = %s segundos." % (end_time - start_time))
def test_network_saving_loading(): G = ox.graph_from_place('Piedmont, California, USA') G_projected = ox.project_graph(G) ox.save_graph_shapefile(G_projected) ox.save_graphml(G_projected) G2 = ox.load_graphml('graph.graphml') gdf_edges = ox.graph_to_gdfs(G, nodes=False, edges=True, fill_edge_geometry=False) gdf_nodes, gdf_edges = ox.graph_to_gdfs(G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True) G3 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)
def read_proj_graphml(proj_graphml_filepath, ori_graphml_filepath, to_crs,undirected=True, retain_fields=None): """ Read a projected graph from local disk if exist, otherwise, reproject origional graphml to the CRS appropriate for its geographic location, and save the projected graph to local disk Parameters ---------- proj_graphml_filepath: string the projected graphml filepath ori_graphml_filepath: string the original graphml filepath to_crs: dict or string or pyproj.CRS project to this CRS undirected: bool (default: True) make graph undirected retain_edge_attributes = list (default: None) explicitly retain only a subset of edge attributes, else keep all (default) Returns ------- networkx multidigraph """ # if the projected graphml file already exist in disk, then load it from the path if os.path.isfile(proj_graphml_filepath): print("Read network from disk.") G_proj=ox.load_graphml(proj_graphml_filepath,int) if undirected: print(" - Ensure graph is undirected.") if G_proj.is_directed(): G_proj = G_proj.to_undirected() return(G_proj) # else, read original study region graphml and reproject it else: print("Prepare network resources...") print(" - Read network from disk.") # load and project origional graphml from disk G = ox.load_graphml(ori_graphml_filepath,int) if retain_fields is not None: print(" - Remove unnecessary key data from edges") att_list = set([k for n in G.edges for k in G.edges[n].keys() if k not in ['osmid','length']]) capture_output = [[d.pop(att, None) for att in att_list] for n1, n2, d in tqdm(G.edges(data=True),desc=' '*18)] del(capture_output) print(" - Project graph") G_proj = ox.project_graph(G, to_crs=to_crs) if undirected: print(" - Ensure graph is undirected.") if G_proj.is_directed(): G_proj = G_proj.to_undirected() print(" - Save projected graphml to disk") ox.save_graphml(G_proj, proj_graphml_filepath) return(G_proj)
except: edge_length = 0 total_length = total_length + edge_length i = i + 1 return(total_length) print('Getting road network') if os.path.isfile(root+'data/'+filename): # Read in the file G = ox.load_graphml(root+'data/'+filename) else: # Create file since it doesn't exist ox.config(use_cache=True, log_console=False) G = ox.graph_from_place('Dutchess County, New York, USA', network_type='drive') ox.save_graphml(G, filename=filename) stations = pd.read_excel(root+'Drive Distance Data.xlsx', 'Stations') events = pd.read_excel(root+'Drive Distance Data.xlsx', 'Events') length_data = list() for index, row in stations.iterrows(): station_name = row['Station'] station = (row['Lat'], row['Lon']) station_node= ox.get_nearest_node(G, station) for index, e in events.iterrows(): percent_complete = station_name + ' ' + str(int(index / (events.shape[0]-1) * 100)) + '% complete' print(percent_complete, end='') print('\r', end='') event = (e['Lat'], e['Lon']) event_node = ox.get_nearest_node(G, event)