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 download_graph(loc_list, **kwargs): simplify = kwargs.get("simplify",True) # get rid of non-important nodes? auto_bbox = kwargs.get("auto_bbox",True) # auto query polygon bbox show_plot = kwargs.get("show_plot",True) distance = kwargs.get("distance",20000) print("osmnx download_graph()...") if isinstance(loc_list,list) and isinstance(loc_list[0],float) and len(loc_list)>2: # [lats,latn,lngw,lnge] north=loc_list[1] south=loc_list[0] east=loc_list[3] west=loc_list[2] G=ox.graph_from_bbox(north,south,east,west,network_type='drive',simplify=simplify) elif isinstance(loc_list,list) and isinstance(loc_list[0],float) and len(loc_list)==2:# [lat,lng] G=ox.graph_from_point(loc_list, distance=distance,network_type='drive',simplify=simplify) elif isinstance(loc_list, str) or (isinstance(loc_list,list) and isinstance(loc_list[0], str)): if auto_bbox: # addr or [addr1,addr2,] use auto bbox G=ox.graph_from_place(loc_list,network_type='drive',simplify=simplify)# No-distance arg else: # addr or [addr1,addr2,] use distance G=ox.graph_from_address(loc_list,network_type='drive',distance=distance,simplify=simplify) else: print(__file__+" download_graph() input error: ",loc_list) return None if show_plot and iprint and Has_Display: if iprint>=2: print("download_graph showing downloaded graph...") fig, ax = ox.plot_graph(G) if not simplify: simplify_graph(G) if iprint>=2: print ox.basic_stats(G) return G
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 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 analyse_local_nh(G_proj, orig_point, buffer=buffer_local, length=distance, intersection_tolerance = 15): """ Get sausage buffer and sample point stats/variables Parameters ---------- G_proj : graphml OSM street network graphml orig_point : int the current node to start from Returns ------- dict """ buffer = create_sausage_buffer_G(G_proj, orig_point) orig_node = ox.get_nearest_node(G_proj, orig_point, return_dist=True) #get stats area_sqm = buffer[1].area area_sqkm = area_sqm*1e-06 # count number of intersection if it is euqal to zero node_ids = set(buffer[0].nodes()) intersection_count = len([True for node, count in buffer[0].graph['streets_per_node'].items() if (count > 1) and (node in node_ids)]) if intersection_count > 0: stats = ox.basic_stats(buffer[0], area=area_sqm, clean_intersects=True, circuity_dist='euclidean') elif area_sqm > 0: stats = ox.basic_stats(buffer[0], area=area_sqm, clean_intersects=False, circuity_dist='euclidean') else: stats = {'n': 0, 'm': 0, 'k_avg': 0, 'intersection_count': 0, 'streets_per_node_avg': 0, 'streets_per_node_counts': 0, 'streets_per_node_proportion': 0, 'edge_length_total': 0, 'edge_length_avg': 0, 'street_length_total': 0, 'street_length_avg': 0, 'street_segments_count': 0, 'node_density_km': 0, 'intersection_density_km': 0, 'edge_density_km': 0, 'street_density_km': 0, 'circuity_avg': 0, 'self_loop_proportion': 0, 'clean_intersection_count': 0, 'clean_intersection_density_km': 0} return({ 'origin_node_id': orig_node[0], 'area_sqkm': area_sqkm, 'stats': stats, 'origin_node_snap_dist': orig_node[1]})
def calc_road_cycleway_ratio(self): """ Calculates ratio of cycleways to roads. Uses osmnx.basic_stats. cycleways: (networkx.MultiDiGraph) cycleways info from get_city func, or osmnx.graph_from_place roads: (networkx.MultiDiGraph) road info from get_city func, or ox.graph_from_place rc_ratio: (float) ratio of roads to cycleways in the city """ c = ox.basic_stats(self.cycleways) r = ox.basic_stats(self.roads) self.rc_ratio = r['edge_length_total'] / c['edge_length_total']
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 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 run_osmnx(i): start = time.perf_counter() print(i) ag_sub = ag[i:i + 1] polygon = ag_sub['geometry'].iloc[0] G = ox.graph_from_polygon(polygon, network_type='drive') basic_stats = ox.basic_stats(G) node_closeness_centrality = nx.closeness_centrality(G) a = max(node_closeness_centrality.values()) b = min(node_closeness_centrality.values()) factor = a - b for k in node_closeness_centrality: node_closeness_centrality[k] = (node_closeness_centrality[k] - b) / factor res = statistics.mean(node_closeness_centrality.values()) row = [ag_sub['codigo'][0], res, basic_stats['circuity_avg']] csv_writer = writer( open( 'C:/Users/Diego/OneDrive/IPEA/OSMnx/saida_OSMnx_CC_normalized.csv', "a+", newline='')) csv_writer.writerow(row) finish = time.perf_counter() print(f'Terminou em {round(finish-start,2)} segundos')
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 load_OSM_basic_stats(G_filename, folder=OSM_folder): """ retains all the basic stats for pedestrain network within study regions graphml from a local folder Parameters ---------- G_filename : string the name of the graphml file (including file extension) OSM_folder : string the folder containing the OSM file, if None, use default data folder Returns ------- DataFrame """ df = pd.DataFrame() #load street network data from local directory G = ox.load_graphml(G_filename, folder=folder) gdf_nodes = ox.graph_to_gdfs(G, edges=False) graph_area_m = gdf_nodes.unary_union.convex_hull.area stats = ox.basic_stats(G, area=graph_area_m, clean_intersects=True, circuity_dist='euclidean', tolerance=15) df1 = pd.DataFrame.from_dict(stats, orient='index', columns=['OSM_pedestrain_network']) df = pd.concat([df, df1], axis=1) return df
def run_osmnx(i): try: start = time.perf_counter() print(i) ag = gpd.read_file( "C:/Users/b35143921880/Downloads/urban_extent_cutoff_20_shape/" + temp["codigo"][i]) ag["codigo"] = ag["name_uca_case"] ag = ag.dissolve(by='name_uca_case') ag_reprojc = ag.to_crs({'init': 'epsg:5070'}) area = ag_reprojc.area ag = ag.to_crs({'init': 'epsg:4326'}) ag_sub = ag area1 = area polygon = ag_sub['geometry'].iloc[0] G = ox.graph_from_polygon(polygon, network_type='drive') basic_stats = ox.basic_stats(G, area=area1) row = [ag_sub['codigo'][0], basic_stats['intersection_density_km']] csv_writer = writer( open('C:/Users/b35143921880/Downloads/saida_OSMnx_v6.csv', "a+", newline='')) csv_writer.writerow(row) finish = time.perf_counter() print(f'Terminou em {round(finish-start,2)} segundos') except Exception as e: raise Exception(str(e))
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 generateWS(point): max = 740239.4 min = 1500 NewMax = 100 NewMin = 0 OldRange = (max - min) NewRange = (NewMax - NewMin) try: G = ox.core.graph_from_point(point, distance = 500, network_type='walk') all_pois = ox.pois_from_point(point, distance = 500) AmenityCount = all_pois['amenity'].value_counts() AmenityCount = AmenityCount.to_json() #AmenityCount = str(AmenityCount) stats = ox.basic_stats(G, area=500) POIshapes = all_pois['geometry'].centroid POICoordslist = map(getXY, POIshapes) WeightedPOIDistances = [] for coordset in POICoordslist: orig_node = ox.get_nearest_node(G, (point)) dest_node = ox.get_nearest_node(G, (coordset)) # The issue has to be here because the individual lat lon list for POIs are accurate but the 'nodes' all display as if they are in the same place. Also its returning a distance of 688.8 m which isn't within 500m Dpoi = nx.shortest_path_length(G, orig_node, dest_node, weight='length') if Dpoi > 0: weight = 1/(Dpoi) else: weight = 1 WeightedPOIDistances.append(weight * Dpoi) rawWalkability = stats['street_density_km'] * .007 + stats['node_density_km'] *.30 + stats['street_segments_count']*.30 + (sum(WeightedPOIDistances)) * 1000 Walkscore = int(((rawWalkability - min) * NewRange) / OldRange) + NewMin if rawWalkability > max: Walkscore = 100 if rawWalkability < min: Walkscore = 0 poidata = [] poidata = [] for i, row in all_pois.iterrows(): geom = getXY(row['geometry'].centroid) latitude = geom[0] longitude = geom[1] poidata.append([row["amenity"], latitude, longitude]) col = ["amenity", "latitude", "longitude"] poi_df = pd.DataFrame(poidata, columns = col) poiGJ = df_to_geojson(poi_df, col) ##poiGJ2 = json.dumps(poiGJ) poiGJ2 = poi_df.to_json(orient = 'records') statsJSON = { "street_density_km": str(stats['street_density_km']), "node_density_km": str(stats['node_density_km']), "street_segments_count": str(stats['street_segments_count']) } except: Walkscore = 1 AmenityCount = "0" poiGJ2 = "0" statsJSON = "0" #poiGJ = df_to_geojson(poi_df, col) #poiGJ = json.dumps(poiGJ) return Walkscore, AmenityCount, poiGJ2, statsJSON
def get_area_and_basic_stats(graph): """ Gibt Fläche in km2 an und holt sich Daten zum Graphen. """ G_proj = ox.project_graph(graph) nodes_proj = ox.graph_to_gdfs(G_proj, edges=False) graph_area_m = nodes_proj.unary_union.convex_hull.area return graph_area_m / 1000000, ox.basic_stats(G_proj, area=graph_area_m, clean_intersects=True, circuity_dist='euclidean')
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 get_bc_graph_plots(place): string = place.split(',')[0] G = nx.read_gpickle("data/{a}/{b}".format(a=string, b=string)) b = ox.basic_stats(G) #G_projected = ox.project_graph(G) node_lis = glob('data/{}/nodes/nodes.shp'.format(string)) extended_path_lis = glob('data/{}/Extended_*.csv'.format(string)) gdf_node = gpd.GeoDataFrame.from_file(node_lis[0]) exten = pd.read_csv(extended_path_lis[0]) exten = exten.rename(columns={'Unnamed: 0': 'osmid'}) exten['betweenness_centrality'] = exten['edge_centr'] * 100 max_node = exten[exten.betweenness_centrality == max( exten.betweenness_centrality)]['osmid'].values[0] max_bc = max(exten.betweenness_centrality) nc = ['r' if node == max_node else '#336699' for node in G.nodes()] ns = [80 if node == max_node else 8 for node in G.nodes()] print( '{}: The most critical node has {:.2f}% of shortest journeys passing through it. \n' .format(place, max_bc)) print('The road network of {} has {} nodes and {} edges \n\n'.format( string, b['n'], b['m'])) fig, ax = ox.plot_graph(G, node_size=ns, node_color=nc, node_zorder=2, node_alpha=0.8, edge_alpha=0.8, fig_height=8, fig_width=8) gdf_node[gdf_node.osmid == max_node].plot(ax=ax, color='red', zorder=3) #ax.set_title('{}: {:.2f}% of shortest paths between all nodes \n in the network through this node'.format(string, max_bc), fontsize=15) print('\n\n\n') fig.savefig('data/{}/{}_bc_graph_plot.png'.format(string, string), dpi=300) return
def osmnx_street_len(city): ''' osmnx_street_len() - Uses OSMNX to calculate average street length for a city Parameters ---------- city : str City name Return ------ float: Average street length ''' import osmnx stats = osmnx.basic_stats(city) return stats['street_length_avg']
def basic_stats_from_point(): values = request.get_json() latitude = values.get('latitude') longitude = values.get('longitude') network_type = values.get('network_type') if network_type is None: return "Error, please supply a valid network_type", 400 print(latitude, longitude) print(network_type) coord = (latitude, longitude) G = ox.graph_from_point(coord, network_type=network_type) basic_stats = ox.basic_stats(G) # ox.save_graphml(G, filename="graph_from_location.graphml", folder="/app") # content = get_file('graph_from_location.graphml') return Response(basic_stats, mimetype="application/json")
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) ensure_dir(path) with open(path + '_stats.json', 'wb') as f: json.dump(stats, f)
"""https://github.com/chenqian19910610/network-clustering/blob/master/network-clustering-simple.ipynb""" import osmnx as ox import networkx as nx import pandas as pd import numpy as np from scipy.sparse import csr_matrix from sklearn.cluster import DBSCAN ox.config(log_console=True, use_cache=True) stats = ox.basic_stats( ox.graph_from_place('Emeryville, California, USA', network_type='bike')) # print(stats) place = 'Emeryville, California, USA' gdf = ox.gdf_from_place(place) area = ox.project_gdf(gdf).unary_union.area G = ox.graph_from_place(place, network_type='bike', buffer_dist=500) eps = 300 minpts = 3 n_firms = 30 n_clusters = 3 np.random.seed(7) firm_centers = np.random.choice(G.nodes(), size=n_clusters, replace=False) xs = [] ys = [] for osmid in firm_centers: x = G.node[osmid]['x']
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
colnames = ['0','WS','lat', 'lon'] df1 = pd.read_csv('C:\MCDWSF.csv', index_col = None, usecols=['WS','lat','lon']) df1 = df1.apply(pd.to_numeric, errors='coerce') df1 = df1.dropna(thresh=2) df1s = df_elements = df1.sample(n=1000) counter = 0 with open('lbasicStatsAll5.csv', 'wb') as f: writer = csv.writer(f) header = ('lat', 'lon', 'WS','street_density_km','node_density_km', 'street_segments_count') writer.writerow(header) while True: try: for i, row in df1s.iterrows(): counter += 1 WS = row['WS'] lat = row['lat'] lon = row['lon'] point = (lat,lon) G = ox.core.graph_from_point(point, distance = 500, network_type='walk') print "basic stats working", counter stats = ox.basic_stats(G, area=500) print "writing row", counter rowtowrite = ('lat', 'lon', 'WS',row['street_density_km'],row['node_density_km'], row['street_segments_count']) writer.writerow(rowtowrite) print "row",counter, "written" except: pass #add stats['street_segments_count']
lineterminator='\n', ) npols = shpfile.shape[0] for i in range(npols): if not (i % 100): print(i) try: pol = shpfile['geometry'][i] # reproject to calculate the area polutm, _ = project_geometry(pol) areapol = polutm.area G = ox.graph_from_polygon(pol, network_type='drive') country = shpfile.iloc[i, :]['country'] output_path = '/Users/rodrigo/Documents/tfg/data/ups/connectivity/' + country + '/' + country + str( i) + ".graphml" ox.io.save_graphml(G, output_path) basic_stats = ox.basic_stats(G, area=areapol) print(basic_stats) writer.writerow([i] + list(basic_stats.values())) except: print("error", i) writer.writerow([i] + ["NA"])
for index, city in enumerate(city_list): coords_data = pd.read_csv("city_coords/" + str(city) + ".csv") north_bbox = coords_data.loc[1, "max"] south_bbox = coords_data.loc[1, "min"] east_bbox = coords_data.loc[0, "max"] west_bbox = coords_data.loc[0, "min"] print(city) print(north_bbox, south_bbox, east_bbox, west_bbox) G = ox.graph_from_bbox(north_bbox, south_bbox, east_bbox, west_bbox, network_type='walk') approx = ox.basic_stats(G)["n"] / 20 node_centrality = nx.degree_centrality(G) df = pd.DataFrame(data=pd.Series(node_centrality).sort_values(), columns=['cc']) df['colors'] = ox.get_colors(n=len(df), cmap='inferno', start=0.2) df = df.reindex(G.nodes()) nc = df['colors'].tolist() fig, ax = ox.plot_graph(G, bgcolor='k', node_size=4, node_color=nc, node_edgecolor='none', node_zorder=2, edge_color="#46454b", edge_linewidth=1.5,
gdf = ox.project_gdf( gdf, to_crs=crs) # projection to meters to calculate the area correctly area_m2 = gdf.unary_union.area area_km2 = area_m2 / 1000000 for layer in networks: start_layer = time.time() G = ox.load_graphml('{}_{}.graphml'.format(name, layer), folder=path) print(' Starting with {}'.format(layer)) if len(G.nodes) > 0: # Same as with the area, project the graph to meters G = ox.project_graph(G, to_crs=crs) print(' + Getting the stats') stats = ox.basic_stats(G, area=area_m2) row = {} row['$Area\ km^2$'] = round(area_km2, 3) row['$N$'] = G.number_of_nodes() row['$L$'] = G.number_of_edges() row['$<k>$'] = round(stats['k_avg'], 3) row['$Node\ density\ km^2$'] = round( (G.number_of_nodes() / area_km2), 3) row['$Edge\ density\ km^2$'] = round( (G.number_of_edges() / area_km2), 3) row['$Edge\ length\ avg$'] = round(stats['edge_length_avg'], 3) row['$Edge\ length\ total$'] = round(stats['edge_length_total'], 3) row['$Connected\ Components$'] = len( list(nx.weakly_connected_component_subgraphs(G))) data_temp[layer] = row row = {}
point1 = (lat1,lon1) point2 = (lat2,lon2) #create the OSMNX graph of walkable surface within a 500m radius G = ox.core.graph_from_point(point1, distance = 500, network_type='walk') G2 = ox.core.graph_from_point(point2, distance = 500, network_type='walk') # collect all of the POIS in that radius and create dictionaries of the number of each type all_pois1 = ox.pois_from_point(point1, distance = 500) all_pois2 = ox.pois_from_point(point2, distance = 500) AmenityCount1 = all_pois1['amenity'].value_counts() AmenityCount2 = all_pois2['amenity'].value_counts() # calculte the network-theory stats for each G, graph stats = ox.basic_stats(G, area=500) stats2 = ox.basic_stats(G2, area=500) # define a location for each POI, point 1 POIshapes = all_pois1['geometry'].centroid #create a list of each POI location's coordinates POICoordslist = map(getXY, POIshapes) print POICoordslist POIdistances = [] WeightedPOIDistances = [] #iterate through each set of coordinates in the POIs, for each location: define the distance between the points VIA the network, assign a weight based on distance. # weight of 1 is assigned if the POI's nearest node is the same node as the origin node
int(np.max(way_dict.get(i))), ',', int(np.min(way_dict.get(i))), ']') print('num_nodi', Catania.number_of_nodes()) print('num_archi', Catania.number_of_edges()) #print((attr_edge)) # print edges edges = ox.graph_to_gdfs(Catania, 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(Catania) type(Catania) extended_stats = ox.extended_stats(Catania, 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(Catania, 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(Catania.nodes()) return df['colors'].tolist()
# then plot network fig, ax = ox.plot_graph(london) # looks correct. big win #network size # this takes > 30 minutes to run #london_proj = ox.project_graph(london) #nodes_proj = ox.graph_to_gdfs(london_proj, edges = False) #graph_area_m = nodes_proj.unary_union.convex_hull.area #graph_area_m #1839504069.4823053 sq meters # basic network stats ox.basic_stats(london_proj, area=graph_area_m, clean_intersects=True, circuity_dist='euclidean') # #{'n': 125316, # 'm': 295994, # 'k_avg': 4.7239618245076445, # 'intersection_count': 93764, # 'streets_per_node_avg': 2.5574228350729356, # 'streets_per_node_counts': {0: 0, # 1: 31552, # 2: 1380, # 3: 83606, # 4: 8554, # 5: 206, # 6: 16, # 7: 2},
B = ox.graph_from_place('Bracciano, Italy', network_type='all_private') # ox.plot_graph(B) B = ox.graph_from_place('Bracciano, Italy', network_type='drive') # 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)
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]) print('area', gdf['area'][0] / 10**6, 'sqkm') # save to file: def ensure_dir(file_path): directory = os.path.dirname(file_path) if not os.path.exists(directory): os.makedirs(directory) # define path and save to file path = '../../data/vector/city_networks/' + name + '/' ensure_dir(path) with open(path + 'stats.json', 'wb') as f: json.dump(stats, f) print('graph stats for ', name, 'success!')