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 node_tags(g, tag='highway'): n = [n for n in g.nodes(data=True) if tag in n[1].keys()] nid, val = zip(*n) ndf = pd.DataFrame(val, index=nid) ndf = ndf.rename(columns={'x': 'lon', 'y': 'lat'}) x, y = zip(*project_point(list(zip(ndf.lon, ndf.lat)))) ndf = ndf.assign(x=x, y=y, junction=list(ndf.street_count > 2)) g = ox.add_edge_bearings(g, precision=1) edf = ox.graph_to_gdfs(g, nodes=False, edges=True) edf.reset_index(inplace=True) edf = edf[['u', 'v', 'osmid', 'bearing']] edf = edf.explode('osmid', ignore_index=True) edf.set_index('u', inplace=True) ndf = pd.merge(ndf, edf[['osmid', 'bearing']], how='left', left_index=True, right_index=True) # check for na values ndf_na = ndf[ndf.osmid.isna()].copy() ndf.drop(ndf_na.index, inplace=True) ndf_na.drop(['osmid', 'bearing'], axis=1, inplace=True) edf_na = edf.reset_index() edf_na.set_index('v', inplace=True) ndf_na = pd.merge(ndf_na, edf_na[['osmid', 'bearing']], how='left', left_index=True, right_index=True) ndf = pd.concat([ndf, ndf_na], axis=0) ndf = ndf.astype({'osmid': 'int64'}) ndf = gpd.GeoDataFrame(ndf, geometry=gpd.points_from_xy(ndf.lon, ndf.lat)) return ndf
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 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 plot_radar(city): string = city.split(',')[0] #G = ox.graph_from_place(city, network_type='drive') G = get_graph(city) G = ox.add_edge_bearings(G) bearings = pd.Series( [data['bearing'] for u, v, k, data in G.edges(keys=True, data=True)]) #ax = bearings.hist(bins=30, zorder=2, alpha=0.8) #xlim = ax.set_xlim(0, 360) #ax.set_title('{} street network compass bearings'.format(city)) #plt.show() fig = pl.figure() n = 30 bearings = bearings[bearings > 0] count, division = np.histogram( bearings, bins=[ang * 360 / n for ang in range(0, n + 1)]) division = division[0:-1] width = 2 * np.pi / n fig = pl.figure() ax = fig.add_subplot(111, projection='polar') ax = pl.subplot(111, projection='polar') ax.set_theta_zero_location('N') ax.set_theta_direction('clockwise') bars = ax.bar(division * np.pi / 180 - width * 0.5, count, width=width, bottom=0.0) ax.set_title('{} street network compass bearings'.format(city), y=1.07) fig.savefig('data/{a}/{a}_radar_plot.png'.format(a=string), dpi=300) return
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 load_city_graph(city_name): G = ox.load_graphml('data/%s_drive_network_original.graphml' % city_name) G = nx.convert_node_labels_to_integers(G, ordering='default') G = ox.add_edge_bearings(G) G = ox.add_edge_speeds(G) G = ox.add_edge_travel_times(G) # for e in G.edges(data=True): # u, v, info = e # if type(info['highway']) == list: # info['highway'] = ":".join(info['highway']) return G
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 test_stats(): location_point = (37.791427, -122.410018) G = ox.graph_from_point(location_point, distance=500, distance_type='network') G = ox.add_edge_bearings(G) 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 main(): # Stolen from https://github.com/gboeing/osmnx-examples/blob/3acb26f/notebooks/15-calculate-visualize-edge-bearings.ipynb G = ox.graph_from_place('Pittsburgh, Pennsylvania', network_type='drive') G = ox.add_edge_bearings(G) bearings = pd.Series([data['bearing'] for u, v, k, data in G.edges(keys=True, data=True)]) # polar plot# polar n = 30 count, division = np.histogram(bearings, bins=[ang*360/n for ang in range(0,n+1)]) division = division[0:-1] width = 2 * np.pi/n ax = plt.subplot(111, projection='polar') ax.set_theta_zero_location('N') ax.set_theta_direction('clockwise') bars = ax.bar(division * np.pi/180 - width * 0.5 , count, width=width, bottom=20.0) ax.set_title('Pittsburgh street network edge bearings', y=1.1) plt.show()
def prepare_graph(self, g): """Takes in a graph, add speed and travel times, updates travel times per edge Input: graph Output: graph """ new_g = ox.add_edge_speeds(g) new_g = ox.add_edge_bearings(new_g) edges_g = ox.graph_to_gdfs(new_g, nodes=False) # Graph edges DF nodes_g = ox.graph_to_gdfs(new_g, edges=False) # Graph nodes DF edges_g_updated = self.update_speed_in_edges( edges_g) # change the speed for calamity roads edges_g_updated['travel_time2'] = edges_g_updated.apply( lambda x: self.travel_time_recalc(x), axis=1) new_graph = ox.graph_from_gdfs( nodes_g, edges_g_updated) ## Putting the DF back to the graph return new_graph, edges_g_updated
def get_bearings(places, weight_by_length=False): bearings = {} for place in sorted(places.keys()): query = places[place] G = ox.graph_from_place(query, network_type='drive') Gu = ox.add_edge_bearings(ox.get_undirected(G)) if weight_by_length: city_bearings = [] for u, v, k, d in Gu.edges(keys=True, data=True): city_bearings.extend([d['bearing']] * int(d['length'])) b = pd.Series(city_bearings) bearings[place] = pd.concat([b, b.map(reverse_bearing) ]).reset_index(drop='True') else: b = pd.Series([ d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True) ]) bearings[place] = pd.concat([b, b.map(reverse_bearing) ]).reset_index(drop='True') return bearings
def get_orientation_entropy(G, weight=None): Gu = ox.add_edge_bearings(ox.get_undirected(G)) if weight != None: # weight bearings by NUMERIC attribute city_bearings = [] for u, v, k, d in Gu.edges(keys=True, data=True): city_bearings.extend([d['bearing']] * int(d[weight])) b = pd.Series(city_bearings) bearings = pd.concat([b, b.map(_reverse_bearing) ]).reset_index(drop='True') else: # don't weight bearings, just take one value per street segment b = pd.Series( [d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True)]) bearings = pd.concat([b, b.map(_reverse_bearing) ]).reset_index(drop='True') counts = _count_and_merge(36, bearings) probs = counts / counts.sum() return (-np.log(probs) * probs).sum()
def get_bearing(place, query): try: graph = ox.graph_from_place(query, network_type='drive') except Exception: return # calculate edge bearings graph = ox.add_edge_bearings(graph) if weight_by_length: # weight bearings by length (meters) streets = [(d['bearing'], int(d['length'])) for u, v, k, d in graph.edges(keys=True, data=True)] city_bearings = [] for street in streets: city_bearings.extend([street[0]] * street[1]) return graph, pd.Series(city_bearings) else: # don't weight bearings, just take one value per street segment return graph, pd.Series([ data['bearing'] for u, v, k, data in graph.edges(keys=True, data=True) if data['bearing'] != 0.0 ])
def enrich_network(network, clean_dead_ends=True, elevation_api_key=None, drop_keys=[ 'place_id', 'license', 'osm_type', 'osmid', ' lat', 'lon', 'display_name', 'country', 'country_code', 'state', 'state_district', 'county', 'city' ], email=None, postcode_delim=' '): """ Enrich a street network by adding further attributes to the edges in the network. These can then be used in clustering, compression, graph embeddings, shortest paths, etc. Depending on the size of the network, this method may incur a large number of requests and time to run. If anprx.settings['cache'] is set to True, as is by default, responses will be cached and subsequent calls to this method, for the same or intersecting networks, should be faster. Parameters ---------- network : nx.MultiDiGraph a street network clean_dead_ends : bool true if dead end nodes should be removed from the graph elevation_api_key : string Google API key necessary to access the Elevation API. If None, elevation. drop_keys: list keys to ignore from the nominatim response containing address details email : string Valid email address in case you are making a large number of requests. postcode_delim : string postcode delimiter used to split the main postcode into two parts: outer and inner. Use None to skip postcode splitting. Returns ------- network : nx.MultiDiGraph The same network, but with additional edge attributes """ start_time = time.time() log("Enriching network with {} nodes and {} edges. This may take a while.."\ .format(len(network), network.number_of_edges()), level = lg.INFO) if clean_dead_ends: remove_dead_end_nodes(network) # Add bearings network = ox.add_edge_bearings(network) # Elevation if elevation_api_key: start_time_local = time.time() # add elevation to each of the nodes,using the google elevation API network = ox.add_node_elevations(network, api_key=elevation_api_key) # then calculate edge grades network = ox.add_edge_grades(network) log("Added node elevations and edge grades in {:,.3f} seconds"\ .format(time.time() - start_time_local), level = lg.INFO) # lookup addresses network = add_address_details(network, drop_keys, email) # Split post code into outward and inward # assume that there is a space that can be used for string split for (u, v, k, postcode) in network.edges(keys=True, data='postcode'): if postcode: postcode_l = postcode.split(postcode_delim) if len(postcode_l) != 2: log("Could not split postcode {}".format(postcode), level=lg.WARNING) else: network[u][v][k]['out_postcode'] = postcode_l[0] network[u][v][k]['in_postcode'] = postcode_l[1] log("Enriched network in {:,.3f} seconds"\ .format(time.time() - start_time), level = lg.INFO) return network
def calculate_orientation_entropy(Gu, threshold=10, entropy_bins=entropy_bins): # get edge bearings and calculate entropy of undirected network Gu = ox.add_edge_bearings(Gu) bearings = get_unweighted_bearings(Gu, threshold=threshold) entropy = calculate_entropy(bearings.dropna(), entropy_bins) return entropy
def network_dfs(self): g = self.graph_latlon if not self.strict: g = ox.simplify_graph(g, strict=self.strict) g = ox.add_edge_bearings(g, precision=1) n, e = ox.graph_to_gdfs(g) e = e.reset_index() # Method graph_to_gdfs changed to multiindex df network_edges, network_nodes_small = dbl_cleaning(ndf=n, edf=e) network_edges = network_edges.join(network_nodes_small, on='u') network_edges = network_edges.rename(columns={ 'u': 'n1', 'y': 'lat1', 'x': 'lon1' }) network_edges = network_edges.join(network_nodes_small, on='v') network_edges = network_edges.rename(columns={ 'v': 'n2', 'y': 'lat2', 'x': 'lon2' }) x1, y1 = zip( *project_point(list(zip(network_edges.lon1, network_edges.lat1)))) x2, y2 = zip( *project_point(list(zip(network_edges.lon2, network_edges.lat2)))) network_edges = network_edges.assign(x1=x1, y1=y1, x2=x2, y2=y2) network_edges['edge'] = list( zip(network_edges['n1'].values, network_edges['n2'].values)) network_edges.reset_index( inplace=True ) # From hereon the unique index of an edge is just its position in df network_edges = network_edges.rename(columns={'index': '_id'}) self.graph_latlon = g self.graph_xy = ox.project_graph(self.graph_latlon) self.network_edges = network_edges self._get_network_nodes(network_edges) # link node_tags to specific edge, osmid not unique over edges after simplification nearest = ox.get_nearest_edges(self.graph_xy, self.node_tags.x.to_list(), self.node_tags.y.to_list(), method='kdtree', dist=1) n1, n2, _ = zip(*nearest) test_b1 = network_edges[['_id', 'edge', 'bearing']][network_edges.edge.isin( list(zip(n1, n2)))].values test_b2 = network_edges[['_id', 'edge', 'bearing']][network_edges.edge.isin( list(zip(n2, n1)))].values self.node_tags['edge'] = [ij for ij in zip(n1, n2)] self.node_tags.reset_index(inplace=True) self.node_tags = self.node_tags.merge( self.network_edges[['edge', 'bearing']], on='edge', suffixes=('', '_edge')) diff_b = abs(self.node_tags['bearing'] - self.node_tags['bearing_edge']) for i, j in diff_b.iteritems(): if (j > 45) and not self.node_tags.junction[i]: self.node_tags.at[i, 'edge'] = (self.node_tags.at[i, 'edge'][1], self.node_tags.at[i, 'edge'][0]) self.node_tags.drop('bearing_edge', axis=1, inplace=True) self.node_tags = self.node_tags.merge( self.network_edges[['_id', 'edge', 'bearing']], on='edge', suffixes=('', '_edge')) diff_b2 = abs(self.node_tags['bearing'] - self.node_tags['bearing_edge']) breakpoint() # check if nearest edge is in right direction, problem with two way streets self.node_tags.set_index('index', inplace=True) self.node_tags.sort_index(inplace=True)
def network_dfs(self, print_info=False): G = self.graph_latlon new_graph = ox.add_edge_bearings(G) edge_bus_lanes_left = list( new_graph.edges.data('busway:left', default=False)) edge_bus_lanes_right = list( new_graph.edges.data('busway:right', default=False)) left = [j[2] for i, j in enumerate(edge_bus_lanes_left)] right = [j[2] for i, j in enumerate(edge_bus_lanes_right)] n, e = ox.graph_to_gdfs(new_graph) e = e.assign(dbl_left=left) e = e.assign(dbl_right=right) e = e.drop(['busway:left', 'busway:right'], axis=1) dbl_bool = np.logical_and(e['dbl_left'].values, e['oneway'].values) gdf_val = e[['u', 'v', 'bearing']].values new_rows = [] new_index = len(e) for row, val in e.iterrows(): if dbl_bool[row]: if print_info: print(row) new_row = val.copy() new_row['u'] = int(gdf_val[row][1]) new_row['v'] = int(gdf_val[row][0]) new_row['lanes'] = 1 new_row['bearing'] = gdf_val[row][2] - 180 new_row['osmid'] = 'new_edge' new_row['geometry'] = [ LineString([ n['geometry'][gdf_val[row][1]], n['geometry'][gdf_val[row][0]] ]) ] # print(dict(new_row), dict(val)) new_row = gpd.GeoDataFrame(dict(new_row), index=[new_index]) new_index += 1 new_rows.append(new_row) if new_rows: new_rows = pd.concat(new_rows, axis=0) if print_info: print(new_rows) e = pd.concat([e, new_rows], axis=0) new_graph = ox.gdfs_to_graph(n, e) n, e = ox.graph_to_gdfs(new_graph) network_matrix = e.loc[:, [ 'u', 'v', 'oneway', 'osmid', 'highway', 'length', 'bearing', 'geometry', 'lanes', 'dbl_left', 'dbl_right' ]] network_nodes_small = n.loc[:, ['y', 'x']] else: network_matrix = e.loc[:, [ 'u', 'v', 'oneway', 'osmid', 'highway', 'length', 'bearing', 'geometry', 'lanes', 'dbl_left', 'dbl_right' ]] network_nodes_small = n.loc[:, ['y', 'x']] network_matrix = network_matrix.join(network_nodes_small, on='u') network_matrix = network_matrix.rename(columns={ 'u': 'N1', 'y': 'Lat1', 'x': 'Long1' }) network_matrix = network_matrix.join(network_nodes_small, on='v') network_matrix = network_matrix.rename(columns={ 'v': 'N2', 'y': 'Lat2', 'x': 'Long2' }) cols = [ 'osmid', 'N1', 'Lat1', 'Long1', 'N2', 'Lat2', 'Long2', 'length', 'lanes', 'oneway', 'bearing', 'highway', 'dbl_left', 'dbl_right', 'geometry' ] network_matrix = network_matrix[ cols] # rearranging columns (reader's convenience) network_matrix.reset_index( inplace=True ) # From hereon the unique index of an edge is just its position in df self.graph_latlon = new_graph self.graph = ox.project_graph(self.graph_latlon) self.network_matrix = network_matrix return network_matrix, new_graph, new_rows
return x + 180 if x < 180 else x - 180 from datetime import datetime start = datetime.now() bearings = {} for place in sorted(places.keys()): # get the graph query = places[place] G = ox.graph_from_place(query, network_type='drive') # calculate edge bearings Gu = ox.add_edge_bearings(ox.get_undirected(G)) if weight_by_length: # weight bearings by length (meters) city_bearings = [] for u, v, k, d in Gu.edges(keys=True, data=True): city_bearings.extend([d['bearing']] * int(d['length'])) b = pd.Series(city_bearings) bearings[place] = pd.concat([b, b.map(reverse_bearing) ]).reset_index(drop='True') else: # don't weight bearings, just take one value per street segment b = pd.Series( [d['bearing'] for u, v, k, d in Gu.edges(keys=True, data=True)]) bearings[place] = pd.concat([b, b.map(reverse_bearing)
# rand_int = np.random.randint # p = .7 # Location for place in Minneapolis and size of # area to consider lat, lon = 44.977, -93.265 distance = 750 # Get networkx graph of location only considering # drive locations and add edge bearings G = ox.graph_from_point((lat, lon), distance=distance, network_type='drive') # G = ox.graph_from_address('Omaha, Nebraska', network_type='drive', distance=2000) G = ox.project_graph(G) G = ox.add_edge_bearings(G) # start_node = 582111787 # 33380473 # end_node = 757562572 # 33358310 start_node = np.random.choice(G.nodes()) end_node = np.random.choice(G.nodes()) # Add UTM coordinates to nodes for n in G.nodes(): # G.nodes[n]['utm'] = utm.from_latlon(G.nodes[n]['y'],G.nodes[n]['x']) G.nodes[n]['utm'] = (G.nodes[n]['x'], G.nodes[n]['y']) # Get stats of network nodes stats = ox.basic_stats(G) avg_len = stats['street_length_avg']