Skip to content

Commit

Permalink
Merge c271161 into eade24e
Browse files Browse the repository at this point in the history
  • Loading branch information
pitrz committed Jan 1, 2018
2 parents eade24e + c271161 commit 4c93979
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
16 changes: 8 additions & 8 deletions peartree/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def generate_cross_feed_edges(G,
lat = float(row.stop_lat)
lon = float(row.stop_lon)
point = (lat, lon)
(nn, nn_dist) = get_nearest_node(node_df, point)
nearest_nodes = get_nearest_node(node_df, point, connection_threshold)

# Iterate through series results and add to output
for node,distance in nearest_nodes.iteritems():
stop_ids.append(sid)
to_nodes.append(node)
edge_costs.append(distance)

# Only generate a connector edge if it satisfies the
# meter distance threshold
if nn_dist < connection_threshold:
stop_ids.append(sid)
to_nodes.append(nn)
edge_costs.append(nn_dist)

return pd.DataFrame({'stop_id': stop_ids,
'to_node': to_nodes,
Expand Down Expand Up @@ -149,7 +149,7 @@ def populate_graph(G: nx.MultiDiGraph,

# Use the lookup table to get converted stop id name
full_sid = sid_lookup[sid]

# Convert to km/hour
kmph = (d / 1000) / walk_speed_kmph

Expand Down
22 changes: 15 additions & 7 deletions peartree/summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ def generate_wait_times(trips_and_stop_times: pd.DataFrame

# Handle both inbound and outbound directions
for direction in [0, 1]:
constraint_1 = (trips_and_stop_times.direction_id == direction)
constraint_2 = (trips_and_stop_times.stop_id == stop_id)
both_constraints = (constraint_1 & constraint_2)
direction_subset = trips_and_stop_times[both_constraints]
# Check if direction_id exists in source data
if 'direction_id' in trips_and_stop_times:
constraint_1 = (trips_and_stop_times.direction_id == direction)
constraint_2 = (trips_and_stop_times.stop_id == stop_id)
both_constraints = (constraint_1 & constraint_2)
direction_subset = trips_and_stop_times[both_constraints]
else:
direction_subset = trips_and_stop_times

# Only run if each direction is contained
# in the same trip id
Expand All @@ -55,9 +59,13 @@ def generate_all_observed_edge_costs(trips_and_stop_times: pd.DataFrame
tst_sub = trips_and_stop_times[tst_mask]

# Just in case both directions are under the same trip id
for direction in [0, 1]:
dir_mask = (tst_sub.direction_id == direction)
tst_sub_dir = tst_sub[dir_mask]
for direction in [1]:
# Check if direction_id exists in source data
if 'direction_id' in tst_sub:
dir_mask = (tst_sub.direction_id == direction)
tst_sub_dir = tst_sub[dir_mask]
else:
tst_sub_dir = tst_sub

tst_sub_dir = tst_sub_dir.sort_values('stop_sequence')
deps = tst_sub_dir.departure_time[:-1]
Expand Down
12 changes: 6 additions & 6 deletions peartree/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def generate_graph_node_dataframe(G):


def get_nearest_node(df_orig: pd.DataFrame,
point: Tuple[float, float]):
point: Tuple[float, float],
connection_threshold):
# This method breaks out a portion of a similar method from
# OSMnx's get_nearest_node; source:
# https://github.com/gboeing/osmnx/blob/
Expand Down Expand Up @@ -69,8 +70,7 @@ def get_nearest_node(df_orig: pd.DataFrame,
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)
# Filter out nodes outside connection threshold and self (distance = 0)
nearest_nodes = distances[(distances > 0.0) & (distances < connection_threshold)]
# Return filtered series
return nearest_nodes

0 comments on commit 4c93979

Please sign in to comment.