Skip to content

Instantly share code, notes, and snippets.

@wwymak
Last active June 9, 2019 15:49
Show Gist options
  • Save wwymak/964dc5b847fc298c66fe4b1e01b7c12e to your computer and use it in GitHub Desktop.
Save wwymak/964dc5b847fc298c66fe4b1e01b7c12e to your computer and use it in GitHub Desktop.
import pandana as pdna
import pandas as pd
berlin_drive_nodes_df = pd.DataFrame([x[1] for x in list(berlin_drive_net.nodes(data=True))])[["x", "y", "osmid"]]
# this line is important--your nodes dataframe must have the nodes id as index for pandana to construct the network
# properly. Ref https://github.com/UDST/pandana/issues/88
berlin_drive_nodes_df.set_index("osmid", inplace=True)
berlin_drive_net_edges = list(berlin_drive_net.edges.data('length'))
berlin_drive_net_edges = [{"from": x[0], "to": x[1], "weight": x[2]} for x in berlin_drive_net_edges]
berlin_drive_edges_df = pd.DataFrame(berlin_drive_net_edges)
# create the pandana network as per http://udst.github.io/pandana/tutorial.html, passing in the x, y
# coordinates of the nodes, the node ids of the start and end of edges, and also the 'weight' of each edge
# in our use case, this is the distance between nodes
pdnet=pdna.Network(berlin_drive_nodes_df["x"], berlin_drive_nodes_df["y"], berlin_drive_edges_df["from"], berlin_drive_edges_df["to"],
berlin_drive_edges_df[["weight"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment