Road network (quakecascade.network)¶
What it computes¶
RoadNetwork turns road centre lines into a routable graph and returns
network costs between origins (homes, grid cells, depots) and targets
(hospitals, shelters). It is the link between a debris-closure stage and the
access, isolation and allocation stages: closed roads are removed by their
edge id with without(), and the cost matrices before and after the event are
compared cell by cell.
- Graph. Each input line is one edge between its first and last vertex;
an end point within
snap_tolerance_mof an existing node joins the nearest one, otherwise it starts a node of its own (leader clustering: no chaining, a node's end points all lie within the tolerance of it). A MultiLineString is split into its parts, which share the row's edge id. Lines must already be noded at junctions (lines that cross without sharing a vertex are not connected). - Edge ids. The caller's
edge_idcolumn or values, otherwise the input row position0..n-1. They are stable acrosswithout()andlargest_component(), so a list of closed ids produced elsewhere (e.g. byquakecascade.debris) can be applied directly. - Shortest paths. Dijkstra (1959) on CSR arrays. SciPy's
sparse.csgraph.dijkstrais used when SciPy is installed; a pure-Python binary-heap Dijkstra gives identical distances otherwise (both are tested). With a limit \(L\) the search stops at \(L\) and every target beyond it isinf. - Snapping. Each point goes to the nearest node within
max_dist_m(KD-tree, or a block brute-force search without SciPy). Derived networks keep the node table, so a point snapped before the event keeps its node after it: a cut road shows up asinf, not as a different snap.
The routing cost of an edge of length \(\ell\) (m) is
for a class speed (speed_by_class), or the walking time with Tobler's
(1993) hiking function
where \(s\) is the signed grade (rise over run) in the direction of travel;
the reverse direction uses \(-s\) (tobler_walk_cost(col, reverse=True)).
Without a cost the edge length in metres is used.
Outputs and status codes¶
| Output | Meaning |
|---|---|
travel_time_matrix(...) |
(n_origins, n_targets) array; inf = unreachable (or beyond limit_s); NaN = origin or target not snapped |
snap(...) |
node (-1 = not snapped), snap_dist_m (NaN when not snapped), nearest_dist_m, snap_status: computed, no_node, no_xy |
edges |
per input line: edge_id, length_m, cost, reverse_cost, oneway, edge_status (computed, no_geometry, no_cost), closed, routed |
nearest(...) |
nearest target by one multi-source search over the reversed graph, so one-way streets and direction-dependent costs are measured origin to target; statuses as access.nearest_time (partial when some targets are not snapped, no_travel_time when none of the known ones is reachable, no_node for an unsnapped origin) |
shortest_path(o, t) |
(cost, [edge ids]); a node id outside the network (e.g. -1) raises IndexError |
An edge whose cost cannot be computed (an unknown road class, a missing
value) is not routed and is reported as no_cost; it is never given a
substitute speed or its length in place of a time.
Parameters¶
| Parameter | Unit | Default | Source / note |
|---|---|---|---|
cost |
s (or caller's unit) | length in m | caller; speed_by_class(mapping, column) needs the caller's class→km/h table (no default table: speeds are a local planning choice) |
reverse_cost |
as cost |
= cost |
caller |
oneway |
– | two-way | 0/False both, 1/True coordinate order, -1 reverse |
snap_tolerance_m |
m | 0.01 | joins digitising round-off only; not a topology repair |
max_dist_m, max_snap_m |
m | required | how far a point may lie from the graph depends on the data |
limit_s |
cost unit | none | search bound |
| Tobler coefficients 6, 3.5, 0.05 | km/h, –, – | fixed | Tobler (1993) |
All coordinates must be metres in a projected CRS; a GeoDataFrame's CRS is
checked with quakecascade.crs.require_metric.
Applicability and caveats¶
- Free-flow class speeds are a planning impedance, not observed traffic.
largest_component()drops small disconnected pieces (overpasses that are not noded, digitising gaps). Use it on the pre-event network and derive the post-event network from it withwithout(), so both share the snap targets.- Tobler's function was fitted to hiking data; on stairs, ramps or steep urban grades it is an approximation.
Example¶
import numpy as np
import pandas as pd
from shapely.geometry import LineString
from quakecascade.network import RoadNetwork, speed_by_class
roads = [LineString([(0, 0), (100, 0)]), LineString([(100, 0), (200, 0)]),
LineString([(0, 0), (0, 300), (200, 300), (200, 0)])]
attrs = pd.DataFrame({"cls": ["local", "local", "arterial"]})
cost = speed_by_class({"local": 30.0, "arterial": 50.0}, "cls")(
attrs, np.array([r.length for r in roads]))
net = RoadNetwork.from_lines(roads, cost=cost, edge_id=["a", "b", "ring"], crs=32635)
homes = net.snap([[0, 1], [100, 1]], max_dist_m=10)
hospital = net.snap([[200, 1]], max_dist_m=10)
pre = net.travel_time_matrix(homes, hospital) # seconds
post = net.without(["b"]).travel_time_matrix(homes, hospital)
print(pre.ravel().round(1), post.ravel().round(1)) # [24. 12.] [57.6 69.6]
Provenance¶
CSR graph, Dijkstra, multi-source search, shortest-path tree and route
reconstruction are ported from the PlanX engine (engine/graphs.py,
engine/paths.py, same author). The bounded many-to-many matrix, the
largest-component rule and the snap-once-compare-twice practice follow the
method of the DİRİ platform router, re-implemented; the test
test_travel_time_matrix_matches_the_python_dijkstra checks the matrix
against an independent per-origin Python Dijkstra.
References¶
- Dijkstra, E. W. (1959). A note on two problems in connexion with graphs. Numerische Mathematik 1, 269–271. doi:10.1007/BF01386390
- Tobler, W. (1993). Three presentations on geographical analysis and modeling. NCGIA Technical Report 93-1, University of California, Santa Barbara.
- Virtanen, P. et al. (2020). SciPy 1.0: fundamental algorithms for scientific computing in Python. Nature Methods 17, 261–272. doi:10.1038/s41592-019-0686-2