Created
February 24, 2016 22:42
-
-
Save fscottfoti/c00698ce3869f09cbf92 to your computer and use it in GitHub Desktop.
a good start at linking gtfs nodes to osm nodes
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
from sklearn.neighbors import KDTree | |
import pandas as pd | |
from geopy.distance import vincenty | |
WALKING_SPEED = 3 # walking assumed at 3 miles per hour | |
TRANSFER_TIME = 10 # transfer time in minutes | |
# assume df1 and df2 each have 2 float columns specifying x and y | |
# in the same order and coordinate system and no nans. returns the indexes | |
# from df1 that are closest to each row in df2 | |
def nearest_neighbor(df1, df2): | |
kdt = KDTree(df1.as_matrix()) | |
indexes = kdt.query(df2.as_matrix(), k=1, return_distance=False) | |
return df1.index.values[indexes] | |
df1 = pd.HDFStore("2015_06_01_osm_bayarea4326.h5").nodes | |
df2 = pd.read_csv("nodes_v4.csv", index_col="node_id") | |
df2["nearest_osm_node"] = nearest_neighbor(df1[['x', 'y']], | |
df2[['x', 'y']]) | |
new_edges = [] | |
for transit_node_id, row in df2.iterrows(): | |
# need to make a new edge between the node in df2 (the transit node) | |
# and the node in openstreetmap (the street node) | |
osm_node_id = int(row.nearest_osm_node) | |
osm_row = df1.loc[osm_node_id] | |
distance = vincenty((row.y, row.x), | |
(osm_row.y, osm_row.x)).miles | |
time = distance / WALKING_SPEED * 60 + TRANSFER_TIME | |
# save the edge | |
new_edges.append((transit_node_id, osm_node_id, time)) | |
# make the edge bi-directional as it's a walking edge | |
new_edges.append((osm_node_id, transit_node_id, time)) | |
new_edges = pd.DataFrame(new_edges, columns=["from", "to", "weight"]) | |
print new_edges.describe() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment