def __init__(self, place): self.place = place if str != type(place): place = json.dumps(place) _ghsh = utils.calculateMD5ForString(place) graph_file = os.path.join('cache', '{0}.graph.p'.format(_ghsh)) edges_file = os.path.join('cache', '{0}.edges.p'.format(_ghsh)) # nodes_file = os.path.join('cache', '{0}.nodes.p'.format(_ghsh)) if os.path.exists(graph_file): Logger.debug("Loading graph from cached file") self.Graph = pickle.load(open(graph_file, "rb")) self.Edges = pickle.load(open(edges_file, "rb")) # self.Nodes = pickle.load( open( nodes_file, "rb" ) ) else: Logger.debug("Building graph from osm data") if str == type(self.place): try: self.Graph = ox.graph_from_place(self.place, network_type='drive', simplify=False, retain_all=True) except: self.Graph = ox.graph_from_place(self.place, network_type='drive', simplify=False, retain_all=True, which_result=2) else: # try: self.Graph = ox.graph_from_bbox(self.place['north'], self.place['south'], self.place['east'], self.place['west'], network_type='drive', simplify=False, retain_all=True) # except: # self.Graph = ox.graph_from_bbox( # self.place['north'], # self.place['south'], # self.place['east'], # self.place['west'], # network_type='drive', simplify=False, retain_all=True, which_result=2) self.Edges = ox.graph_to_gdfs(self.Graph, nodes=False, edges=True) # self.Nodes = ox.graph_to_gdfs(self.Graph, nodes=True, edges=false) Logger.debug("Saving graph to cached file") pickle.dump(self.Graph, open(graph_file, "wb")) pickle.dump(self.Edges, open(edges_file, "wb"))
def get_network(city, n_type='all', infrastructure='way["highway"]'): try: G = ox.graph_from_place(city, network_type=n_type, simplify=True, which_result=1, infrastructure=infrastructure) except: G = ox.graph_from_place(city, network_type=n_type, simplify=True, which_result=2, infrastructure=infrastructure) return ox.project_graph(G)
def download(): G = ox.graph_from_place('Chaoyang District, Beijing, China', network_type='drive') ox.save_graphml(G, filepath='data/Chaoyang.graphml') G = ox.graph_from_place('Haidian District, Beijing, China', network_type='drive') ox.save_graphml(G, filepath='data/Haidian.graphml') G = ox.graph_from_address('北京邮电大学', dist=2000, network_type='all') ox.save_graphml(G, filepath='data/BUPT.graphml') G = ox.graph_from_place('Beijing, China', which_result=2, network_type='drive') ox.save_graphml(G, filepath='data/Beijing.graphml')
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 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 get_graph(place: str): G = None try: G = ox.graph_from_place(place, network_type='drive') G = nx.relabel.convert_node_labels_to_integers(G) except Exception as ex: print(place, '1: ', ex) try: G = ox.graph_from_place(place, which_result=2, network_type='drive') G = nx.relabel.convert_node_labels_to_integers(G) except Exception as ex: print(place, '2: ', ex) return G
def download_osm(query="New York City, New York, USA", network_type="drive"): """ Create graph from OSM within the boundaries of some geocodable place(s). The query must be geocodable and OSM must have polygon boundaries for the geocode result. If OSM does not have a polygon for this place, you can instead get its street network using the graph_from_address function, which geocodes the place name to a point and gets the network within some distance of that point. If OSM does have polygon boundaries for this place but you're not finding it, try to vary the query string, pass in a structured query dict, or vary the which_result argument to use a different geocode result. If you know the OSM ID of the place, you can retrieve its boundary polygon using the geocode_to_gdf function, then pass it to the graph_from_polygon function. Parameters ---------- query : string or dict or list the query or queries to geocode to get place boundary polygon(s) default : "drive" network_type : string {"all_private", "all", "bike", "drive", "drive_service", "walk"} what type of street network to get default: "drive" Returns ------- G : networkx.MultiDiGraph """ return ox.graph_from_place(query, network_type=network_type)
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 __init__(self, eps=1e-5, city='Amherst, Massachusetts, USA', network_type='drive', timeout=100): ''' Parameters: eps (float): epsilon value for the inverse grade calculation city (str): city of the map network_type (str): type of the map timeout (int): timeout for path finding algorithm ''' self._eps = eps # parameter for the inverse calculation self.pathFinder = PathFinder(timeout) # get map data graph for some city self.G = ox.graph_from_place(city, network_type=network_type) self.min_x = float('inf') self.max_x = -float('inf') self.min_y = float('inf') self.max_y = -float('inf') for node in self.G.nodes: nd = self.G.nodes[node] if nd['x'] > self.max_x: self.max_x = nd['x'] if nd['x'] < self.min_x: self.min_x = nd['x'] if nd['y'] > self.max_y: self.max_y = nd['y'] if nd['y'] < self.min_y: self.min_y = nd['y']
def load_roadnet(): """ Loads road network graph edges of Singapore as a GeoDataFrame. The data is downloaded from OpenStreetMap through osmnx package. run `!pip install osmnx` if not yet installed documentation: https://github.com/gboeing/osmnx returns ------- roadnet (GeoDataFrame): road network graph edges of Singapore """ import osmnx as ox # download from OpenStreetMap if local datasets is not available print("Trying to download the network of Singapore through OSM Nominatim.") # try different query results n = 1 while n <= 5: try: G = ox.graph_from_place(query='Singapore', network_type='drive', which_result=n) break except: n += 1 roadnet = ox.graph_to_gdfs(G, nodes=False, edges=True) print("Singapore roadnet is downloaded and loaded as a GeoDataFrame.") return roadnet
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 get_map_data_with_elevation(self, city_name, key): # get model data from osmnx of 2d and elevation #city_name = 'Amherst' # 'Springfield' place_query = { 'city': city_name, 'state': 'Massachusetts', 'country': 'USA' } graph_orig = ox.graph_from_place(place_query, network_type='walk') #add Elevation data from GoogleMaps key = self.get_key() graph_orig = ox.add_node_elevations( graph_orig, api_key=key ) # 'AIzaSyDVqjj0iKq0eNNHlmslH4fjoFgRj7n-5gs') # from elevation graph_orig = ox.add_edge_grades(graph_orig) pkl.dump(graph_orig, open("data/" + city_name + "_city_graph.pkl", "wb")) #project map on to 2D space graph_project = ox.project_graph(graph_orig) pkl.dump(graph_project, open("data/" + city_name + "_city_graph_projected.pkl", "wb")) #print ("pkl: ", type(graph_orig)) return graph_project, graph_orig
def download_data(self): all_g = [] for address in self.addresses: print('Address: ', address) if self.check_data_exists(address): G = pickle.load(open(self.save_path + '/addresses/{}.p'.format(address), 'rb')) else: G = ox.graph_from_place(address, network_type='drive') G = ox.project_graph(G) pickle.dump(G, open(self.save_path + '/addresses/{}.p'.format(address), 'wb')) all_g.append(G) for i, g in enumerate(all_g): if i == 0: raw_data = pd.DataFrame([data for u, v, key, data in g.edges(keys=True, data=True)]) else: d = nx.to_pandas_edgelist(g) raw_data = pd.concat(d, raw_data) raw_data.to_csv(self.save_path + '/raw_data/raw.csv') return raw_data
def add_danger_weight(self, G, origin, destination, crash): """This function will create a new column in our dataframe which is the weight of danger for each street, it will be based on the number of accident by street and will return the changed dataframe""" G = ox.graph_from_place('New york city, New York, USA', network_type='drive') df = self.import_csv_file('nyc-navigation/data/datweight.csv') edges_list = list(G.edges(keys=True, data=True)) self.create_edge_csv_file(edges=edges_list, path='nyc-navigation/data/edges_new.csv') edges = self.import_csv_file('nyc-navigation/data/edges_new.csv') df_B = (df.rename(columns={ "filtered_street": "name", "WEIGHT": "danger_weight" })) df_merged = edges.merge( right=df_B, how='left', # if an entry is in A, but not in B, add NA values on=["name"], # property to merge on ) df_merged.fillna(0) return df_merged
def __init__ (self, village, province, country, roads_type='drive'): """ Initialize. :param village: <str> The village to download from OpenMaps :param province: <str> The province the villange belongs to :param country: <str> The country :param roads_type: <str> The type of roads to download. Possibilities are drive, bike, and walk. :attr place_name: <str> The place downloaded from OpenMaps :attr nodes: <dict> The list of nodes :attr edges: <dict> The list of edges :attr graph: <networkx.classes.multidigraph.MultiDiGraph> The full graph object """ self.place_name = ", ".join((village, province, country)) print (f"Scraping of {self.place_name}...", end="") g = ox.project_graph(ox.graph_from_place (self.place_name, network_type=roads_type)) self.nodes_gpd, self.edges_gpd = ox.graph_to_gdfs (g, nodes=True, edges=True) self.nodes = {i : Node(n, n.osmid, n.x, n.y, n.lat, n.lon, n.geometry) for i, n in self.nodes_gpd.iterrows()} self.edges = {i : Edge(e, e.osmid, e.name, e.highway, e.oneway, e.length, e.geometry, e.maxspeed, e.lanes, e.u, e.v) for i, e in self.edges_gpd.iterrows()} self.graph = g print ("done")
def test_plots(): G = ox.graph_from_place('Piedmont, California, USA', network_type='drive', simplify=False) G2 = ox.simplify_graph(G, strict=False) # test getting colors co = ox.get_colors(n=5, return_hex=True) nc = ox.get_node_colors_by_attr(G2, 'osmid') ec = ox.get_edge_colors_by_attr(G2, 'length') # save a plot to disk as png fig, ax = ox.plot_graph(G, save=True, file_format='png') # save a plot to disk as svg G_simplified = ox.simplify_graph(G) fig, ax = ox.plot_graph(G_simplified, show=False, save=True, close=True, file_format='svg') G_projected = ox.project_graph(G_simplified) fig, ax = ox.plot_graph(G_projected) fig, ax = ox.plot_graph(G_projected, fig_height=5, fig_width=5, margin=0.05, axis_off=False, bgcolor='y', file_format='png', filename='x', dpi=180, annotate=True, node_color='k', node_size=5, node_alpha=0.1, node_edgecolor='b', node_zorder=5, edge_color='r', edge_linewidth=2, edge_alpha=0.1, use_geom=False, show=False, save=True, close=True) fig, ax = ox.plot_figure_ground(G=G_simplified, file_format='png') fig, ax = ox.plot_figure_ground(point=(33.694981, -117.841375), file_format='png') fig, ax = ox.plot_figure_ground(address='Denver, Colorado, USA', file_format='png')
def download_osm_layer(area, mode): """Download a single-mode layer from OSM Parameters ---------- area : str or list String of geocoded place or list of [north, south, east, west] mode : str Mode choice of {‘walk’, ‘bike’, ‘drive’, ‘drive_service’, ‘all’, ‘all_private’, ‘none’} Returns ------- layer : networkx multidigraph OSM map layer of specific mode """ if isinstance(area, str): layer = ox.graph_from_place(area, network_type=mode) elif isinstance(area, list) and len(area) == 4: layer = ox.graph_from_bbox(area[0], area[1], area[2], area[3], network_type=mode) else: raise Exception('Graph area not geocoded place nor bounding box') return layer
def use_osmnx() -> None: graph = ox.graph_from_place('Leuven, Belgium', network_type='drive') graph_proj = ox.project_graph(graph) # Create GeoDataFrames # Approach 1 nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True) for nid, row in nodes_proj[['x', 'y']].iterrows(): map_con.add_node(nid, (row['x'], row['y'])) for nid, row in edges_proj[['u', 'v']].iterrows(): map_con.add_edge(row['u'], row['v']) # Approach 2 nodes, edges = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True) nodes_proj = nodes.to_crs("EPSG:3395") edges_proj = edges.to_crs("EPSG:3395") for nid, row in nodes_proj.iterrows(): map_con.add_node(nid, (row['lat'], row['lon'])) # adding edges using networkx graph for nid1, nid2, _ in graph.edges: map_con.add_edge(nid1, nid2)
def get_map(self, name): file_path = os.path.dirname(os.path.abspath(__file__)) file_path = file_path[:len(file_path) - 7] + 'maps/' try: with open(file_path + name + '.p', 'rb') as f: self._graph = pickle.load(f) except: try: self._graph = ox.graph_from_place(name, network_type='drive') except: self._graph = ox.graph_from_address(name, distance=250, network_type='drive') with open(file_path + name + '.p', 'wb') as f: pickle.dump(self._graph, f) self._graph_proj = ox.project_graph(self._graph) self._nodes, self._edges = ox.graph_to_gdfs(self._graph_proj, nodes=True, edges=True)
def mapRoute(points): #Reference: https://stackoverflow.com/questions/60578408/is-it-possible-to-draw-paths-in-folium ox.config(log_console=True, use_cache=True) G_walk = ox.graph_from_place('Vancouver, British Columbia, Canada', network_type='walk') nodes = [] routes = [] for index, row in points.iterrows(): nodes.append(ox.get_nearest_node(G_walk, (row['lat'], row['lon']))) if index > 0: routes.append( nx.shortest_path(G_walk, nodes[index - 1], nodes[index], weight='length')) for route in routes: route_map = ox.plot_route_folium(G_walk, route) return route_map
def pick_location(trip_type: str = "drive", most_dangerous: bool = False) -> str: """ Parameters ---------- trip_type : str Type of vehicle roads like drive (cars, motorbikes, ...), walk (pedestrian) or bikes. most_dangerous : bool Boolean that says if we are looking for the most dangerous road. Returns ------- str Message to congratulate our machine. """ # Generate the network of NYC based on the trip_type G = ox.graph_from_place("New York City, New York, USA", network_type=trip_type) G = ox.add_edge_speeds(G) G = ox.add_edge_travel_times(G) # nodes_to_csv(G, "drive_safe_node.csv") # csv file with the danger scores data_danger = pd.read_csv( r"C:\Users\Guillaume\Documents\git\nyc-navigation\CSV\street.csv") G_danger = add_edge_danger(data_danger, G, trip_type, most_dangerous) osmnx.io.save_graphml(G_danger, filepath="bike_dangerous.graphml") return "Done"
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 inverse_streetTalk(street_name, street_from, street_to, cityname): if cityname not in globals(): try: #If the map of city is downloaded, use them directly globals()[cityname] = gpd.read_file('data/'+cityname+'/edges/edges.shp') #print('the map of city is downloaded') except: #If the map is not downloaded, download the map of city via OpenStreetMap G = ox.graph_from_place(cityname + ', USA', network_type=networkType) #Save the map in shapefile locally ox.save_load.save_graph_shapefile(G, filename=cityname, folder=None, encoding='utf-8') #print('Map of ' + cityname + 'is saved in shapefile locally!') #citymap_node = gpd.read_file('data/'+cityname+'/nodes/nodes.shp') globals()[cityname] = gpd.read_file('data/'+cityname+'/edges/edges.shp') citymap_edge = globals()[cityname] on_st = citymap_edge[citymap_edge['name'] == street_name] from_st = citymap_edge[citymap_edge['name'] == street_from] to_st = citymap_edge[citymap_edge['name'] == street_to] for i in on_st.index: if ((on_st.loc[i,'from'] in from_st['from'].values) or (on_st.loc[i,'from'] in from_st['to'].values)\ or (on_st.loc[i,'to'] in from_st['from'].values) or (on_st.loc[i,'to'] in from_st['to'].values)) \ and ((on_st.loc[i,'from'] in to_st['from'].values) or (on_st.loc[i,'from'] in to_st['to'].values) \ or (on_st.loc[i,'to'] in to_st['from'].values) or (on_st.loc[i,'to'] in to_st['to'].values)): p = on_st[on_st.index==i].centroid.values[0] return p.x, p.y
def setup_graph_place(self, place, which_result=1): print('+' * 15, 'setup graph from place', '+' * 15) graph_name = 'graph_' + place + '.pkl' pickle_path = self.path_pickle + os.sep + graph_name if os.path.isfile(pickle_path): # Read previously downloaded graph print('Reading pickle file', os.path.basename(pickle_path)) self.graph = pickle.load(open(pickle_path, 'rb')) else: print('setting up graph for place', place, ': downloading', end=' ... ') self.graph = osmnx.graph_from_place( query=place, which_result=which_result, network_type='all' ) # could use bike but this might be to restrictive print('converting to undirected graph', end=' ... ') self.graph = osmnx.get_undirected( self.graph ) # forget the direction, this might be to restrictive # print('projecting to UTM', end=' ... ') # self.graph = osmnx.project_graph(self.graph) # convert to x,y projection for faster euclidean calculations print('saving to', pickle_path) pickle.dump(self.graph, open(pickle_path, 'bw')) print('-' * 42)
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 get_network(place, mode): """Returns network in LL""" G = ox.graph_from_place(place, network_type=mode) #makes UTM of LAT LONG #G = ox.project_graph(G) #ox.plot_graph(G) return G
def main(): city = ox.gdf_from_place('San Francisco, California, USA') G = ox.graph_from_place('San Francisco, California, USA') fullTransition = collections.defaultdict(dict) for node in G.nodes(): #print G.node[node].keys() #print node['lat'] nodeLat = G.nodes[node]['x'] nodeLong = G.nodes[node]['y'] key = (nodeLat, nodeLong) if key not in fullTransition: allEdgeList = [] allEdges = G.edges(node) for edge in allEdges: transition = edge[1] if transition in G.nodes(): transitionLat = G.nodes[transition]['x'] transitionLong = G.nodes[transition]['y'] allEdgeList.append((transitionLat, transitionLong)) fullTransition[key] = allEdgeList #fullTransition[key] = 0 new_df = [] for k, v in fullTransition.iteritems(): for value in v: new_df.append([k, value]) ndf = pd.DataFrame(new_df, columns=['s', 's*']) ndf.to_csv("edges.csv")
def type_transport(transport): """This function plot the shortest path for different type of transport from La Maison du Lez, Montpellier, France to Place to Eugène Bataillon, Montpellier, France. Parameters ---------- transport : string, type of transport to choose to plot his path. Returns ------- a map showing the shortest path. """ # download the map as a graph object G = ox.graph_from_place( 'Montpellier, Hérault, France', network_type=transport) # define origin and desination locations origin_point = ox.geo_utils.geocode('Maison du Lez, Montpellier, France') destination_point = ox.geo_utils.geocode( 'Place Eugène Bataillon, Montpellier, France') # get the nearest nodes to the locations origin_node = ox.get_nearest_node(G, origin_point) destination_node = ox.get_nearest_node(G, destination_point) # finding the shortest path route = nx.shortest_path(G, origin_node, destination_node) # plot the map graph fig, ax = ox.plot_graph_route(G, route, origin_point=origin_point, destination_point=destination_point, route_linewidth=2) plt.show return()
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 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 __init__(self, node_name, place_name, publish_rate): #initialise class variables self._place_name = place_name self._ros_node_name = node_name self._road_info = pointsList() self._road_points = pointsList() self._publish_rate = publish_rate #create publisher to the topic "road_info" self._road_info_publisher = rospy.Publisher("road_info", pointsList, queue_size=100) self._road_points_publisher = rospy.Publisher("road_points", pointsList, queue_size=100) #create a node rospy.init_node(self._ros_node_name, anonymous=False) self._publish_rate = rospy.Rate(self._publish_rate) # 1hz #convert place in a map to a graph self._graph = ox.graph_from_place(self._place_name, network_type='drive') self._graph_proj = ox.project_graph(self._graph) #extract edges and nodes #edges define the geometry of the road #nodes define the start and end points of each road self._nodes, self._edges = ox.graph_to_gdfs(self._graph_proj, nodes=True, edges=True)
def test_osm_xml_output(): G = ox.graph_from_place('Piedmont, California, USA') ox.save_graph_osm(G)
place_names = ['Ho Chi Minh City, Vietnam', #'Beijing, China', #'Jakarta, Indonesia', 'London, UK', 'Los Angeles, California, USA', 'Manila, Philippines', #'Mexico City, Mexico', 'New Delhi, India', 'Sao Paulo, Brazil', 'New York, New York, USA', 'Seoul', 'Singapore', #'Tokyo, Japan', #'Nairobi, Kenya', #'Bangalore, India' ] # In this for-loop, we save all the shapefiles for the valid cities. for city in place_names: city_admin_20kmbuff = ox.gdf_from_place(city, gdf_name = 'global_cities', buffer_dist = 20000) fig, ax = ox.plot_shape(city_admin_20kmbuff) ox.save_gdf_shapefile(city_admin_20kmbuff, filename = city) # In this for-loop, we save all the street networks for the valid cities. for city in place_names: grid = ox.graph_from_place(city, network_type = 'drive', retain_all = True) grid_projected = ox.project_graph(grid) ox.save_graph_shapefile(grid_projected, filename = city + '_grid') ox.plot_graph(grid_projected)
edge_length = edge[0]['length'] * 0.00062137 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'])
ox.config(log_console=True, use_cache=True, data_folder=data_path) def ensure_dir(file_path): directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) for place in places: name = (place.replace(",","").replace(" ","")) # make better place_names print('working on: ', name) #make a geodataframe of the street network (outline) from openstreetmap place names # use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent) G = ox.graph_from_place(place, network_type='drive', retain_all=True) G = ox.project_graph(G) #make a geodataframe of the shape (outline) from openstreetmap place names gdf = ox.gdf_from_place(place) gdf = ox.project_gdf(gdf) ox.save_graph_shapefile(G, filename=name) print(name, ' has crs:' ) gdf.to_crs({'init': 'epsg:3395'}) # Confirm big step of projection change # calculate basic stats for the shape # TODO adjust this to calculate stats based on neighborhoods stats = ox.basic_stats(G, area=gdf['geometry'].area[0])