-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kuanb/kuanb-join-on-new-points
Identify and generate x-feed transfer edges based proximity threshold
- Loading branch information
Showing
3 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import Tuple | ||
|
||
import numpy as np | ||
import osmnx as ox | ||
import pandas as pd | ||
|
||
|
||
def great_circle_vec(lat1: float, | ||
lng1: float, | ||
lat2: float, | ||
lng2: float, | ||
earth_radius: int=6371009): | ||
# This method wraps the same in OSMnx, source: | ||
# https://github.com/gboeing/osmnx/blob/ | ||
# b32f8d333c6965a0d2f27c1f3224a29de2f08d55/osmnx/utils.py#L262 | ||
return ox.utils.great_circle_vec(lat1, lng1, lat2, lng2, earth_radius) | ||
|
||
|
||
def generate_graph_node_dataframe(G): | ||
# This method breaks out a portion of a similar method from | ||
# OSMnx's get_nearest_node; source: | ||
# https://github.com/gboeing/osmnx/blob/ | ||
# b32f8d333c6965a0d2f27c1f3224a29de2f08d55/osmnx/utils.py#L326 | ||
if not G or (G.number_of_nodes() == 0): | ||
raise ValueError('G argument must be not be empty or ' | ||
'should contain at least one node') | ||
|
||
# Dump graph node coordinates array | ||
clist = [] | ||
for node, data in G.nodes(data=True): | ||
# Ensure that each items is cast as the correct typegi | ||
x = float(data['x']) | ||
y = float(data['y']) | ||
clist.append([node, x, y]) | ||
coords = np.array(clist) | ||
|
||
# Then make into a Pandas DataFrame, with the node as index | ||
return pd.DataFrame(coords, columns=['node', 'x', 'y']).set_index('node') | ||
|
||
|
||
def get_nearest_node(df_orig: pd.DataFrame, | ||
point: Tuple[float, float]): | ||
# This method breaks out a portion of a similar method from | ||
# OSMnx's get_nearest_node; source: | ||
# https://github.com/gboeing/osmnx/blob/ | ||
# b32f8d333c6965a0d2f27c1f3224a29de2f08d55/osmnx/utils.py#L326 | ||
|
||
# Make a copy of the DataFrame to prevent mutation outside of function | ||
df = df_orig.copy() | ||
|
||
# Add second column of reference points | ||
df['reference_y'] = point[0] | ||
df['reference_x'] = point[1] | ||
|
||
# TODO: OSMnx supports euclidean as well, for now we have a stumped | ||
# version of this same function | ||
|
||
# Ensure each vectorized series is typed correctly | ||
ref_ys = df['reference_y'].astype(float) | ||
ref_xs = df['reference_x'].astype(float) | ||
ys = df['y'].astype(float) | ||
xs = df['x'].astype(float) | ||
|
||
# Calculate distance vector using great circle distances (ie, for | ||
# spherical lat-long geometries) | ||
distances = great_circle_vec(lat1=ref_ys, | ||
lng1=ref_xs, | ||
lat2=ys, | ||
lng2=xs) | ||
|
||
# Calculate the final results to be returned | ||
nearest_node = str(distances.idxmin()) | ||
nn_dist = distances.loc[nearest_node] | ||
|
||
# Returna as tuple | ||
return (nearest_node, nn_dist) |