Created
June 7, 2017 06:27
-
-
Save kuanb/3ed225fd2600750e73b1096485704725 to your computer and use it in GitHub Desktop.
Sketch of using networkx to calculate accessibility sheds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import numpy as np | |
import pandas as pd | |
import time | |
from dgna.utils import format_pandana_edges_nodes | |
# read in osm data, load into top level variable | |
osm_edges = pd.read_csv('./data/osm_edges.csv') | |
osm_nodes = pd.read_csv('./data/osm_nodes.csv') | |
# let's hardcode that we are looking jusr at transit accessibility | |
travel_speed_km = 4.8 # avg walk speed in kilometers per hour | |
osm_edges['weight'] = (osm_edges['distance'] / 1000) / travel_speed_km * 60 | |
# prep nodes and edge for pdna | |
osm_edges, osm_nodes = format_pandana_edges_nodes(osm_edges, osm_nodes) | |
ids = osm_nodes.index.values | |
osm_nodes['id'] = ids | |
G = nx.DiGraph() | |
for i in ids: | |
G.add_node(i, amenity=np.random.randint(20)) | |
all_edges = osm_edges[['from_int', 'to_int', 'weight']].values | |
for edge in all_edges: | |
G.add_edge(edge[0], edge[1], weight=edge[2]) | |
print('preparing to calculate accessibility for {} geometries'.format(len(ids))) | |
start = time.time() | |
# get all amenities accessible within 15 minutes | |
result_rows = [] | |
for i in ids: | |
accessible = nx.single_source_dijkstra_path_length(G, i, cutoff=15) | |
amenities_accessible = 0 | |
for a in accessible.keys(): | |
amenities_accessible += G.node[a]['amenity'] | |
result_rows.append((i, amenities_accessible)) | |
# log a teaser of the outputs | |
print('example results:') | |
for r in result_rows[:5]: | |
print('{}: {}'.format(*r)) | |
end = time.time() | |
print("Completion time: %.2f s" % (end - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment