network¶
A routable road network with stable edge ids.
quakecascade.network
¶
Road network: build, snap, close edges and compute travel-time matrices.
RoadNetwork wraps the CSR graph and Dijkstra kernels ported from the
PlanX engine (_graphs, _paths). It keeps a stable edge id for every
input line (the caller's edge_id or the input row order), so a list of
closed edges produced elsewhere (for example by debris blockage) can be
removed with RoadNetwork.without and the matrices before and after the
event compared node for node.
Conventions
- Coordinates are metres in a projected CRS (a GeoDataFrame's CRS is checked
with
quakecascade.crs.require_metric). infin a cost matrix means unreachable on the network;NaNmeans not computed (the origin or target could not be snapped).- An edge whose cost cannot be computed (unknown road class, missing value)
is left out of routing and reported with
edge_status = "no_cost"; it is never given a substitute cost.
Speeds have no default table: speed_by_class takes the caller's
class-to-km/h mapping.
NO_GEOMETRY
module-attribute
¶
Edge status: the line has no usable geometry; the edge is left out.
NO_COST
module-attribute
¶
Edge status: the cost is NaN or negative (for example an unknown road class); the edge is left out.
NO_NODE
module-attribute
¶
Snap status: the point is farther than the snapping distance from every node.
NO_XY
module-attribute
¶
Snap status: the point has no finite coordinates.
CostSpec
module-attribute
¶
How a routing cost is given: None (length in metres), a column name, a callable
f(attributes_frame, length_m) -> array or one value per line.
NodeGraph
¶
Weighted graph over polyline endpoints; optionally directed per edge.
edge_direction holds 0 (both ways), 1 (coordinate order only) or -1
(reverse only). adj_edge maps each CSR entry back to its edge index.
RoadNetwork
¶
RoadNetwork(
*,
node_xy,
part_from,
part_to,
part_len,
part_cost,
part_rev_cost,
part_dir,
part_row,
edges,
crs=None,
cost_unit="",
closed=None,
excluded=None,
snap_mask=None,
)
A routable road network with stable edge ids.
Build it with from_lines. Every derived network
(without, largest_component) keeps the same node table,
so node ids from snap stay valid across the pre- and post-event
networks and a cut road shows up as inf, not as a different snap.
from_lines
classmethod
¶
from_lines(
lines,
cost=None,
*,
reverse_cost=None,
edge_id=None,
oneway=None,
snap_tolerance_m=0.01,
crs=None,
)
Build a network from road lines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
(GeoDataFrame, GeoSeries or sequence)
|
LineStrings (MultiLineStrings are split into parts that share the
row's edge id) or |
required |
cost
|
(None, str, callable or array_like)
|
Routing cost per line in the forward (coordinate) direction:
|
None
|
reverse_cost
|
same as ``cost``
|
Cost against the coordinate order; defaults to |
None
|
edge_id
|
str or array_like
|
Stable id per line (column name or values); defaults to the row position 0..n-1. Ids must be unique. |
None
|
oneway
|
str or array_like
|
Per line 0/False = both ways, 1/True = coordinate order only, -1 = reverse only. |
None
|
snap_tolerance_m
|
float
|
An end point within this distance of an existing node joins the nearest one (leader clustering, no chaining). The default 1 cm only merges digitising round-off; it does not join lines that do not touch. |
0.01
|
crs
|
optional
|
CRS of plain geometries; checked to be metric when given. |
None
|
without
¶
A new network with the given edges closed (removed from routing).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_ids
|
iterable
|
Edge ids as given to |
required |
strict
|
bool
|
Raise |
True
|
largest_component
¶
A new network keeping only the largest weakly connected component.
Nodes keep their ids; only nodes of that component remain snap targets, and edges outside it are no longer routed.
snap
¶
Snap points to their nearest network node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points_xy
|
(M, 2) array, sequence of shapely Points or GeoSeries
|
Coordinates in the network CRS (metres). |
required |
max_dist_m
|
float
|
Largest accepted snap distance. Required: how far a point may be from the road graph is a property of the data, not of the method. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
travel_time_matrix
¶
Network cost from every origin to every target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origins
|
node ids, (M, 2) coordinates or a <autoref identifier="quakecascade.network.RoadNetwork.snap" optional><code>snap</code></autoref> frame
|
Integer 1-D arrays are node ids (-1 = not snapped); coordinates
are snapped with |
required |
targets
|
node ids, (M, 2) coordinates or a <autoref identifier="quakecascade.network.RoadNetwork.snap" optional><code>snap</code></autoref> frame
|
Integer 1-D arrays are node ids (-1 = not snapped); coordinates
are snapped with |
required |
limit_s
|
float
|
Costs above it are |
None
|
max_snap_m
|
float
|
Snap distance for coordinate inputs (required for them). |
None
|
cancel
|
callable
|
Polled between origin blocks; see |
None
|
Returns:
| Type | Description |
|---|---|
(ndarray, shape(n_origins, n_targets))
|
|
nearest
¶
Cost from each origin to its nearest target, with one multi-source search.
The search starts at the targets and runs over the reversed graph, so
one-way streets and direction-dependent costs (e.g.
tobler_walk_cost) are measured from origin to target, exactly
as travel_time_matrix(...).min(axis=1).
Returns a frame with nearest_time, nearest_target (position in
targets, -1 if none) and nearest_status, with the semantics of
quakecascade.access.nearest_time: computed (inf when
no target is reachable); partial when some targets could not be
snapped (the time is an upper bound); no_travel_time (NaN) when
none of the snapped targets is reachable and some targets are
unknown; no_node (NaN) for an origin that could not be snapped.
shortest_path
¶
Cost and edge ids of the shortest route between two nodes.
Returns (cost, edge_ids); (inf, []) when unreachable. A
MultiLineString row traversed in several parts appears once. Node ids
outside 0..n_nodes - 1 (such as -1 for an unsnapped point) raise
IndexError.
build_node_graph
¶
Build the primal graph from a list of (k_i, 2) coordinate arrays.
An endpoint within tolerance (Euclidean, in the coordinate units) of
an existing node joins the nearest one; otherwise it starts a new node at
its own coordinates (leader clustering: no chaining, every endpoint of a
node lies within tolerance of the node). costs (optional, one per polyline) replaces
length as the routing cost; the metric length is always kept.
directions uses 0 = both, 1 = coordinate order only, -1 = reverse only.
component_labels
¶
Weak components, SciPy connected_components when available.
Both paths return the same partition; label numbers may differ.
nearest_nodes
¶
Nearest node of node_xy for each of points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_xy
|
(N, 2) array
|
Node coordinates. |
required |
points
|
(M, 2) array
|
Query coordinates; rows with a non-finite coordinate get id -1 and distance NaN. |
required |
candidates
|
(N,) bool array
|
Only these nodes may be returned. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ids |
(M,) int64
|
Node id, -1 where no candidate exists or the point is not finite. |
dist |
(M,) float64
|
Euclidean distance to that node (coordinate units). |
weak_components
¶
Weak component id per node and the component count (pure Python walk).
many_to_many
¶
Distances from each source to every node, shape (len(sources), n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indptr
|
array_like
|
CSR adjacency (row pointer, column index, edge weight). |
required |
adj
|
array_like
|
CSR adjacency (row pointer, column index, edge weight). |
required |
weights
|
array_like
|
CSR adjacency (row pointer, column index, edge weight). |
required |
n
|
int
|
Number of nodes. |
required |
sources
|
array_like of int
|
Source node ids. |
required |
cutoff
|
float
|
Costs above it are reported as |
None
|
cancel
|
callable
|
Polled between sources (between blocks of 256 sources on the SciPy
path); when it returns True |
None
|
multi_source
¶
Minimum cost from any source.
Returns (dist[n], nearest_source_pos[n]); nearest_source_pos is the
position in sources of the winning source (-1 where unreachable).
reconstruct_path
¶
Walk the predecessor arrays back from target to source.
Returns (nodes, edges) in travel order (nodes has one more entry
than edges); both empty when the target is unreachable.
shortest_path_tree
¶
Dijkstra with predecessor tracking for route reconstruction.
Returns (dist, pred_node, pred_edge): for every node its cost from
source, the previous node on the shortest path and the id of the edge
used to arrive (both -1 at the source and where unreachable). Pure heapq
on purpose: it must report which parallel edge was taken, which the
SciPy kernel cannot; distances equal many_to_many.
tobler_walk_speed
¶
Walking speed in km/h from Tobler's hiking function.
\(v = 6\,\exp(-3.5\,|s + 0.05|)\) km/h, with s the signed grade
(rise over run, a fraction, positive uphill in the direction of travel).
The maximum, 6 km/h, is reached at s = -0.05.
Source: Tobler, W. (1993). Three presentations on geographical analysis and
modeling. NCGIA Technical Report 93-1, Eq. on p. 2 ("hiking function").
Ported from PlanX engine/comfort.tobler_speed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slope
|
float or array_like
|
Signed grade as a fraction (0.10 = 10 % uphill). NaN stays NaN. |
required |
speed_by_class
¶
Cost callable giving travel seconds from a road-class speed mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
mapping
|
Road class -> free-flow speed in km/h, supplied by the caller (there is no default table: speeds are a local planning choice). |
required |
column
|
str
|
Attribute column holding the class of each line. |
required |
Returns:
| Type | Description |
|---|---|
callable
|
|
tobler_walk_cost
¶
Cost callable giving walking seconds with tobler_walk_speed.
slope_column holds the signed grade in the line's coordinate order.
Use reverse=True for the reverse_cost of the same network (the
grade changes sign when walked the other way).