def print_stats(self, graph=None, type="basic", log=False): """ This function print out the statistics of the graph @param:\n graph {networkx Graph}: Graph for which statistics should be calculated\n \n @param:\n type {str}: setting which defines the level of output (default: {"basic"})\n log {bool}: if set true print out the statistics (default: {False})\n \n @return:\n stats {str}: statistics of graph """ if graph is None: graph = self.Graph if type == "basic": stats = ox.basic_stats(graph) for key, value in stats.items(): stats[key] = value if log: print(pd.Series(stats)) else: return stats if type == "pro": stats = ox.extended_stats(graph) for key, value in stats.items(): stats[key] = value if log: print(pd.Series(stats)) else: return stats if type == "expert": stats = ox.extended_stats(graph, connectivity=True, ecc=True, cc=True, bc=True) for key, value in stats.items(): stats[key] = value if log: print(pd.Series(stats)) else: return stats if type == "all": stats = ox.extended_stats(graph, connectivity=True, ecc=True, anc=True, cc=True, bc=True) for key, value in stats.items(): stats[key] = value if log: print(pd.Series(stats)) else: return stats
def test_stats(): location_point = (37.791427, -122.410018) G = ox.graph_from_point(location_point, distance=500, distance_type='network') stats1 = ox.basic_stats(G) stats1 = ox.basic_stats(G, area=1000) stats2 = ox.extended_stats(G, connectivity=True, anc=True, ecc=True, bc=True, cc=True)
def test_stats(): # create graph, add bearings, project it G = ox.graph_from_point(location_point, dist=500, network_type="drive") G = ox.add_edge_bearings(G) G_proj = ox.project_graph(G) # calculate stats stats = ox.basic_stats(G) stats = ox.basic_stats(G, area=1000) stats = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist="euclidean") # calculate extended stats stats = ox.extended_stats(G, connectivity=True, anc=False, ecc=True, bc=True, cc=True) # test cleaning and rebuilding graph G_clean = ox.consolidate_intersections(G_proj, tolerance=10, rebuild_graph=True, dead_ends=True)
def test_stats(): # create graph, add bearings, project it location_point = (37.791427, -122.410018) G = ox.graph_from_point(location_point, distance=500, distance_type='network') G = ox.add_edge_bearings(G) G_proj = ox.project_graph(G) # calculate stats stats1 = ox.basic_stats(G) stats2 = ox.basic_stats(G, area=1000) stats3 = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist='euclidean') # calculate extended stats stats4 = ox.extended_stats(G, connectivity=True, anc=False, ecc=True, bc=True, cc=True)
def get_network_features(row, london_network, radius=700): start_time = time.time() # get subset of graph around radius of node G = nx.ego_graph(london_network, row['node'], radius, distance='length') #if there is not subset return none if G.size() == 0: print('no network') return None else: #calculate basic stats and return start_time = time.time() basic_stats = ox.basic_stats(G) #Calculate features start_time = time.time() extended_stats = ox.extended_stats(G,bc=True,cc=True) return {'id': row['id'], 'n': basic_stats['n'], 'm': basic_stats['m'], 'k_avg': basic_stats['k_avg'], 'intersection_count': basic_stats['intersection_count'], 'edge_length_total': basic_stats['edge_length_total'], 'edge_length_avg': basic_stats['edge_length_avg'], 'street_length_total': basic_stats['street_length_total'], 'street_length_avg': basic_stats['street_length_avg'], 'street_segments_count': basic_stats['street_segments_count'], 'circuity_avg': basic_stats['circuity_avg'], 'self_loop_proportion': basic_stats['self_loop_proportion'], 'avg_neighbor_degree_avg': extended_stats['avg_neighbor_degree_avg'], 'degree_centrality_avg': extended_stats['degree_centrality_avg'], 'clustering_coefficient_avg': extended_stats['clustering_coefficient_avg'], 'closeness_centrality_avg': extended_stats['closeness_centrality_avg'], 'betweenness_centrality_avg': extended_stats['betweenness_centrality_avg']}
def test_stats(): # create graph, add bearings, project it location_point = (37.791427, -122.410018) G = ox.graph_from_point(location_point, distance=500, distance_type='network') G = ox.add_edge_bearings(G) G_proj = ox.project_graph(G) # calculate stats stats1 = ox.basic_stats(G) stats2 = ox.basic_stats(G, area=1000) stats3 = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist='euclidean') try: stats4 = ox.extended_stats(G, connectivity=True, anc=True, ecc=True, bc=True, cc=True) except NetworkXNotImplemented as e: warnings.warn( "Testing coordinates results in a MultiDigraph, and extended stats are not available for it" ) warnings.warn(e.args)
def compute_extended_stats(graph): data = ox.extended_stats(graph, connectivity=True, cc=True, ecc=True, bc=True) return { k: v for k, v in data.items() if isinstance(v, float) or isinstance(v, int) }
def test_stats(): location_point = (37.791427, -122.410018) with httmock.HTTMock(get_mock_response_content('overpass-response-5.json.gz')): G = ox.graph_from_point(location_point, distance=500, distance_type='network') G = ox.add_edge_bearings(G) G_proj = ox.project_graph(G) stats1 = ox.basic_stats(G) stats2 = ox.basic_stats(G, area=1000) stats3 = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist='euclidean') stats4 = ox.extended_stats(G, connectivity=True, anc=True, ecc=True, bc=True, cc=True)
def test_stats(): # create graph, add bearings, project it location_point = (37.791427, -122.410018) G = ox.graph_from_point(location_point, distance=500, distance_type='network') G = ox.add_edge_bearings(G) G_proj = ox.project_graph(G) # calculate stats stats1 = ox.basic_stats(G) stats2 = ox.basic_stats(G, area=1000) stats3 = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist='euclidean') stats4 = ox.extended_stats(G, connectivity=True, anc=True, ecc=True, bc=True, cc=True)
def stats (self): """ This property returns some default statistics concerning the scraded graph. :return: <panda.Series> Default statistics """ g, e = self.graph, self.edges_gpd area = e.unary_union.convex_hull.area stats = ox.basic_stats(g, area=area) extended_stats = ox.extended_stats(g, ecc=True, bc=True, cc=True) for key, value in extended_stats.items(): stats[key] = value return pd.Series(stats)
def test_stats(): # create graph, add a new node, add bearings, project it G = ox.graph_from_point(location_point, dist=500, network_type="drive") G.add_node(0, x=location_point[1], y=location_point[0]) _ = ox.bearing.get_bearing((0, 0), (1, 1)) G = ox.add_edge_bearings(G) G_proj = ox.project_graph(G) # calculate stats cspn = ox.utils_graph.count_streets_per_node(G) stats = ox.basic_stats(G) stats = ox.basic_stats(G, area=1000) stats = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist="euclidean") # calculate extended stats stats = ox.extended_stats(G, connectivity=True, anc=False, ecc=True, bc=True, cc=True) # calculate entropy Gu = ox.get_undirected(G) entropy = ox.bearing.orientation_entropy(Gu, weight="length") fig, ax = ox.bearing.plot_orientation(Gu, area=True, title="Title") fig, ax = ox.bearing.plot_orientation(Gu, ax=ax, area=False, title="Title") # test cleaning and rebuilding graph G_clean = ox.consolidate_intersections(G_proj, tolerance=10, rebuild_graph=True, dead_ends=True) G_clean = ox.consolidate_intersections(G_proj, tolerance=10, rebuild_graph=True, reconnect_edges=False) G_clean = ox.consolidate_intersections(G_proj, tolerance=10, rebuild_graph=False) # try consolidating an empty graph G = nx.MultiDiGraph(crs="epsg:4326") G_clean = ox.consolidate_intersections(G, rebuild_graph=True) G_clean = ox.consolidate_intersections(G, rebuild_graph=False)
def lab_4(): G = ox.load_graphml('data/Haidian.graphml') print(len(G.nodes)) # 7844 print(len(G.edges)) # 17602 G_projected = ox.project_graph(G) extended_stats = ox.extended_stats(G, ecc=True, bc=True, cc=True) def get_color_list(n, color_map='plasma', start=0, end=1): return [cm.get_cmap(color_map)(x) for x in np.linspace(start, end, n)] def get_node_colors_by_stat(G, data, start=0, end=1): df = pd.DataFrame(data=pd.Series(data).sort_values(), columns=['value']) df['colors'] = get_color_list(len(df), start=start, end=end) df = df.reindex(G.nodes()) return df['colors'].tolist() nc = get_node_colors_by_stat(G_projected, data=extended_stats['betweenness_centrality']) ox.plot_graph(G, figsize=(20, 20), node_color=nc, node_size=20, bgcolor='k', node_zorder=2, edge_linewidth=2, edge_color='#333333')
def get_centrality_stats(place): import numpy as np string = place.split(',')[0] try: edges = gpd.read_file("data/{}/edges/edges.shp".format(string)) if 'edge_centr' in edges.columns: df = pd.DataFrame() df['edge_centr'] = edges.edge_centr.astype(float) df['edge_centr_avg'] = np.nansum(df.edge_centr.values) / len( df.edge_centr) df.to_csv("data/{a}/Extended_stats_{a}.csv".format(a=string)) except FileNotFoundError: print("Edges file doesn't exist. Running edge_centrality function.") G = get_graph(G) extended_stats = ox.extended_stats(G, bc=True) dat = pd.DataFrame.from_dict(extended_stats) dat.to_csv('data/{a}/Extended_Stats_{b}.csv'.format(a=string, b=string)) except Exception as e: print('Exception Occurred', e)
##Name_of_stat_file=string centrality.csv import osmnx as ox, geopandas as gpd, networkx as nx from networkx.algorithms import centrality as cen from itertools import chain from collections import defaultdict import matplotlib.pyplot as plt import pandas as pd import shp2osmnx as s2nx import csv #Polygon_layer='{}'.format(Polygon_layer) #Polygon_in_crs='{}'.format(Polygon_in_crs) #Folder_to_load_from= '{}'.format(Folder_to_load_from) Folder_to_save_graphs = str(Folder_to_save_graphs) Name_of_stat_file = '{}'.format(Name_of_stat_file) #create graph G = ox.save_load.load_graphml(Graphml_File_Name, folder=Folder_to_load_from) #Calculate the extended stats stats = ox.extended_stats(G, connectivity=connectivity, anc=average_node_connectivity, ecc=eccentricity, bc=betweenness_centrality, cc=closeness_centrality) #statsfile=Graphml_File_Name+str('/centrality.csv') filepath = Folder_to_save_graphs + '/' + Name_of_stat_file df = pd.DataFrame.from_dict(stats) df.to_csv(filepath)
popup_attribute=None, tiles='cartodbpositron', zoom=1, fit_bounds=True, edge_width=4, edge_opacity=1) AAA.save("prova_centrality.html") for u, v, key, attr in grafo.edges(keys=True, data=True): print(attr) print(attr["length"]) attr['length'] = attr.get("cost") # grafo.add_edge(u, v, key, attr_dict=attr) grafo.add_edge(u, v, key) ox.extended_stats(grafo, bc=True) ox.extended_stats(grafo, ecc=True, bc=True, cc=True) ####################################################### ####################################################### ####################################################### from_n = np.random.choice(Catania.nodes) to_n = np.random.choice(Catania.nodes) # calculate the shortest path between two points...meters )this is a list of nodes based on the shortest time route = nx.shortest_path(Catania, from_n, to_n, weight='cost') # calculate the length of the route (this is a time) lr = nx.shortest_path_length(Catania, from_n, to_n, weight='cost') print(lr) route = nx.shortest_path(Catania, from_n, to_n, weight='length') # create pairs of edge for the shortest route path_edges = list(zip(route, route[1:]))
# 'street_length_total': 15733068.697000204, # 'street_length_avg': 97.83212407270503, # 'street_segments_count': 160817, # 'node_density_km': 68.12488326555749, # 'intersection_density_km': 50.972434122631846, # 'edge_density_km': 15774.41226110788, # 'street_density_km': 8552.886051199652, # 'circuity_avg': 1.0599049219728784, # 'self_loop_proportion': 0.005155509908984642, # 'clean_intersection_count': 73152, # 'clean_intersection_density_km': 39.76724010215823} # see more stats (mostly topological stuff) with extended_stats #This takes a while too more_stats = ox.extended_stats( london, ecc=True, bc=True, cc=True) #use arguments to turn other toplogical analyses on/off # #more_stats = ox.extended_stats(london, ecc=True, bc=True, cc=True) #use arguments to turn other toplogical analyses on/off #Traceback (most recent call last): # # File "<ipython-input-11-a01a026d6a5f>", line 1, in <module> # more_stats = ox.extended_stats(london, ecc=True, bc=True, cc=True) #use arguments to turn other toplogical analyses on/off # # File "C:\Users\Hugh\Anaconda3\lib\site-packages\osmnx\stats.py", line 382, in extended_stats # sp = {source:dict(nx.single_source_dijkstra_path_length(G_strong, source, weight='length')) for source in G_strong.nodes()} # # File "C:\Users\Hugh\Anaconda3\lib\site-packages\osmnx\stats.py", line 382, in <dictcomp> # sp = {source:dict(nx.single_source_dijkstra_path_length(G_strong, source, weight='length')) for source in G_strong.nodes()} #
# c += 1 # if c == 10: break # edge_length(G) # basic stats # 'n': , 'm': ,'k_avg': , # 'edge_length_total': , # 'edge_length_avg': , # 'node_density_km': , # 'edge_density_km': , # 'circuity_avg': , # # nodes_proj = ox.graph_to_gdfs(G_proj, edges=False) # graph_area = nodes_proj.unary_union.convex_hull.area stat = ox.extended_stats(G, connectivity=True, cc=True, ecc=True, bc=True) # stat = {k: v for k, v in stat if isinstance(v, float) or isinstance(v, int)} for k, v in stat.items(): # if isinstance(v, float) or isinstance(v, int): print(k, v) # stat = ox.basic_stats(G, area=graph_area, clean_intersects=True, circuity_dist='euclidean') # stat['area_m2'] = graph_area # stat['city'] = city # # to_remove = ['streets_per_node_avg', 'streets_per_node_proportion','street_length_total','street_length_avg', # 'street_segments_count', 'street_density_km', 'self_loop_proportion','clean_intersection_count', # 'clean_intersection_density_km', 'intersection_count' ] # columns = ['n', 'm', 'k_avg', 'edge_length_total', 'edge_length_avg', # 'node_density_km','edge_density_km'] # df = pd.DataFrame(stat) # df.drop(columns=[c for c in df.columns if c not in columns], inplace=True)
"Check location manually. Place: ", place_name) # Calculate the area that the network covers in square meters. G_proj = ox.project_graph(G) nodes_proj = ox.graph_to_gdfs(G_proj, edges=False) graph_area_m = nodes_proj.unary_union.convex_hull.area print("Area calculated.", end=" ") # Calculate the basic and extended stats. NB! Without connectivity and other measures, see docs for details. basic_stats = ox.basic_stats(G, area=graph_area_m) ext_stats = ox.extended_stats(G, connectivity=False, anc=False, ecc=False, bc=False, cc=False) print("Stats calculated.") # Concatenate the statistics dictionaries and add city name and area to the # beginning. combined_stats = { "city_name": place_name, "graph_area": graph_area_m, **basic_stats, **ext_stats }
import networkx as nx import osmnx as ox import json data = {} ox.config(use_cache=True, log_console=True) G = ox.graph_from_bbox(40.19, 39.70, 116.75, 116.05, network_type='drive') stats = ox.basic_stats(G) extended_stats = ox.extended_stats(G, ecc=True, bc=True, cc=True) stats.update(extended_stats) ox.save_graphml(G, "network1.gml") nx.set_node_attributes(G, stats.get("avg_neighbor_degree"), "avg_neighbor_degree") nx.set_node_attributes(G, stats.get("degree_centrality"), "degree_centrality") nx.set_node_attributes(G, stats.get("pagerank"), "pagerank") nx.set_node_attributes(G, stats.get("closeness_centrality"), "closeness_centrality") nx.set_node_attributes(G, stats.get("betweenness_centrality"), "betweenness_centrality") ox.save_graphml(G, "network2.gml") links = list() vertex = list() edges = G.edges for edge in edges: current_edge = edges[edge] links.append({ 'id': edge, 'osmid':
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
print('num_archi',Bracciano.number_of_edges()) #print((attr_edge)) # print edges (only highway attribute) edges = ox.graph_to_gdfs(Bracciano, nodes=False, edges=True) for i in edges['highway']: if i is not list: print(edges['highway']) # calculate basic and extended network stats, merge them together, and display stats = ox.basic_stats(Bracciano) extended_stats = ox.extended_stats(Bracciano, ecc=True, bc=True, cc=True) # get a color for each node def get_color_list(n, color_map='plasma', start=0, end=1): return [cm.get_cmap(color_map)(x) for x in np.linspace(start, end, n)] def get_node_colors_by_stat(Bracciano, data, start=0, end=1): df = pd.DataFrame(data=pd.Series(data).sort_values(), columns=['value']) df['colors'] = get_color_list(len(df), start=start, end=end) df = df.reindex(Bracciano.nodes()) return df['colors'].tolist() nc = get_node_colors_by_stat(Bracciano, data=extended_stats['betweenness_centrality']) fig, ax = ox.plot_graph(Bracciano, node_color=nc, node_edgecolor='gray', node_size=20, node_zorder=2) # make an interactive map # path = ox.plot_route_folium(Bracciano, route, route_color='green')
def main(cityName, placeName): ox.config(log_console=True, use_cache=True) graph = ox.core.graph_from_address(placeName) ox.plot_graph(graph, save=True, filename=cityName, file_format="png") ox.extended_stats(graph, ecc=True) saveData(graph)
# In[8]: # Get the Convex Hull of the network convex_hull = edges_proj.unary_union.convex_hull # Show output convex_hull # In[9]: # Calculate the area area = convex_hull.area # Calculate statistics with density information stats = ox.basic_stats(graph_proj, area=area) extended_stats = ox.extended_stats(graph_proj, ecc=True, bc=True, cc=True) for key, value in extended_stats.items(): stats[key] = value pd.Series(stats) # In[10]: G_projected = ox.project_graph(G) max_node, max_bc = max(extended_stats['betweenness_centrality'].items(), key=lambda x: x[1]) max_node, max_bc # In[11]: # get a color for each node
# ox.plot_graph(B) # You can also specify several different network types: # drive - get drivable public streets (but not service roads) # drive_service - get drivable streets, including service roads # walk - get all streets and paths that pedestrians can use (this network type ignores one-way directionality) # bike - get all streets and paths that cyclists can use # all - download all non-private OSM streets and paths # all_private - download all OSM streets and paths, including private-access ones basic_stats = ox.basic_stats(B) print(basic_stats['circuity_avg']) # ans: [1.1167206203103612] # In this street network, the streets are 12% more circuitous than the straight-lines paths would be. extended_stats = ox.extended_stats(B) print(extended_stats['pagerank_max_node']) # Create place boundary shapefiles from OpenStreetMap Bracciano_shp = ox.gdf_from_place('Bracciano, Italy') ox.save_gdf_shapefile(Bracciano_shp) # using NetworkX to calculate the shortest path between two random nodes route = nx.shortest_path(B, np.random.choice(B.nodes), np.random.choice(B.nodes)) ox.plot_graph_route(B, route, fig_height=10, fig_width=10) # save street network as GraphML file B_projected = ox.project_graph(B) ox.save_graphml(B_projected, filename='network_Bracciano_6km_epgs4326.graphml')
bgcolor=sty.BKG, node_size=sty.NS*5, node_color=sty.NC, node_zorder=sty.NZ, node_alpha=.35, edge_linewidth=sty.ES, edge_color=sty.EC, edge_alpha=sty.EA ) if EXPORT: fig.savefig( '{}/img/{}-{}-O.{}'.format(PTH_BASE, idStr, netType, FMT), bbox_inches='tight', pad_inches=.01 ) ############################################################################### # Network stats ############################################################################### stats = ox.basic_stats(G, area=area) extended_stats = ox.extended_stats(G, bc=True) for key, value in extended_stats.items(): stats[key] = value if EXPORT: statSeries = pd.Series(stats) statSeries.to_csv('{}/dta/{}-{}-S.csv'.format(PTH_BASE, idStr, netType)) file = open('{}/dta/{}-{}-S.pickle'.format(PTH_BASE, idStr, netType), 'wb') pkl.dump(stats, file) file.close() ############################################################################### # Betwenness Centrality ############################################################################### nc = fun.get_node_colors_by_stat( G, data=extended_stats['betweenness_centrality']