Skip to content

Commit

Permalink
Enable bidirectional route creation on synthetic routes (#29)
Browse files Browse the repository at this point in the history
* udpate tests

* bidirectional check

* refactor for loop through directions

* udpate tests
  • Loading branch information
kuanb authored Mar 6, 2018
1 parent 762d110 commit f6ba139
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
62 changes: 36 additions & 26 deletions peartree/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd
import partridge as ptg
from fiona import crs
from shapely.geometry import shape
from shapely.geometry import LineString, Point, shape

from .settings import WGS84
from .summarizer import (generate_edge_and_wait_values,
Expand Down Expand Up @@ -211,37 +211,47 @@ def make_synthetic_system_network(
sid_lookup = {}
all_nodes = None
for feat in reference_geojson['features']:
ref_shape = shape(feat['geometry'])

# Pull out required properties
props = feat['properties']
headway = props['headway']
avg_speed = props['average_speed']
stop_dist = props['stop_distance_distribution']

# Generate reference geometry data
chunks = generate_meter_projected_chunks(ref_shape, stop_dist)
all_pts = generate_stop_points(chunks)

# Give each stop a unique id
stop_ids = generate_stop_ids(len(all_pts))

# Produce graph components
nodes = generate_nodes_df(stop_ids, all_pts, headway)
edges = generate_edges_df(stop_ids, all_pts, chunks, avg_speed)

# Mutates the G network object
sid_lookup_sub = _add_nodes_and_edges(G, name, nodes, edges)

# Update the parent sid with new values
for key, val in sid_lookup_sub.items():
sid_lookup[key] = val

# Then add to the running tally of nodes
if all_nodes is None:
all_nodes = nodes.copy()
else:
all_nodes = all_nodes.append(nodes)
ref_shape_1 = shape(feat['geometry'])
ref_shapes = [ref_shape_1]

# Check if want to do bidirectional (optional)
if 'bidirectional' in props and bool(props['bidirectional']):
coord_array = [Point(p) for p in ref_shape_1.coords]
ref_shape_2 = LineString(reversed(coord_array))
ref_shapes.append(ref_shape_2)

# For either the one specified direction or both, create
# and add imputed nodes and edges from supplied shape
for ref_shape in ref_shapes:
# Generate reference geometry data
chunks = generate_meter_projected_chunks(ref_shape, stop_dist)
all_pts = generate_stop_points(chunks)

# Give each stop a unique id
stop_ids = generate_stop_ids(len(all_pts))

# Produce graph components
nodes = generate_nodes_df(stop_ids, all_pts, headway)
edges = generate_edges_df(stop_ids, all_pts, chunks, avg_speed)

# Mutates the G network object
sid_lookup_sub = _add_nodes_and_edges(G, name, nodes, edges)

# Update the parent sid with new values
for key, val in sid_lookup_sub.items():
sid_lookup[key] = val

# Then add to the running tally of nodes
if all_nodes is None:
all_nodes = nodes.copy()
else:
all_nodes = all_nodes.append(nodes)

# Generate cross feed edge values
exempt_nodes = []
Expand Down
20 changes: 17 additions & 3 deletions tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,30 @@ def test_synthetic_network():
with open(geojson_path, 'r') as gjf:
reference_geojson = json.load(gjf)

G = load_synthetic_network_as_graph(reference_geojson)
G1 = load_synthetic_network_as_graph(reference_geojson)

# This fixture gets broken into 15 chunks, so 15 + 1 = 16
nodes = list(G.nodes())
nodes = list(G1.nodes())
assert len(nodes) == 16

# And since it is one-directional, it gets the same edges as chunks
edges = list(G.edges())
edges = list(G1.edges())
assert len(edges) == 15

# Go back to the GeoJSON and set optional bidirectional flag
for i in range(len(reference_geojson['features'])):
reference_geojson['features'][i]['properties']['bidirectional'] = True

G2 = load_synthetic_network_as_graph(reference_geojson)

# This fixture gets broken into 15 chunks, so 15 + 1 = 16
nodes = list(G2.nodes())
assert len(nodes) == 16 * 2

# And since it is one-directional, it gets the same edges as chunks
edges = list(G2.edges())
assert len(edges) == 62


def test_feed_to_graph_path():
path_1 = fixture('caltrain-2017-07-24.zip')
Expand Down

0 comments on commit f6ba139

Please sign in to comment.