UrbanAccessAnalyzer.routing¶
Shortest-path routing core: multi-source Dijkstra for isochrones.
Polars has no graph primitives, and implementing Dijkstra as iterative
join/group-by relaxation rounds in Polars would need one full-table pass per
network hop (a 15-minute, ~1200 m walk isochrone over ~100 m edges needs
20-40+ hop rounds through real, non-grid street topology), which is both slow
and only Bellman-Ford-equivalent, not true Dijkstra. Instead this module
converts the Polars edge table into a SciPy sparse CSR adjacency matrix once
and delegates the actual shortest-path search to
:func:scipy.sparse.csgraph.dijkstra, a compiled, well-tested Fibonacci-heap
Dijkstra implementation that natively supports multiple sources and a distance
cutoff in a single call. SciPy is a NumPy/Polars-adjacent numerical library,
not a graph-modeling framework like networkx, so using it here does not
reintroduce the dependency this refactor removes. Polars is used for all data
prep and result assembly around that one call.
Methodology
Multi-source Dijkstra computes, for a set of source nodes, the shortest
(weighted) distance from any source to every reachable node, which is
exactly the node set of a street-network isochrone: "all points reachable
within N meters of travel from these origins." This is equivalent to
adding a virtual super-source node connected to every real source with
zero-weight edges and running single-source Dijkstra from it; SciPy's
indices parameter does this internally without materializing the
virtual node.
Source: SciPy docs, https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.dijkstra.html
build_csr(edges, nodes)
¶
Build a SciPy CSR adjacency matrix from Polars node/edge tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
DataFrame
|
DataFrame with directed edge columns |
required |
nodes
|
DataFrame
|
DataFrame with a |
required |
Returns:
| Name | Type | Description |
|---|---|---|
csgraph |
csr_matrix
|
|
node_id_to_index |
dict
|
dict mapping OSM/graph |
index_to_node_id |
ndarray
|
numpy array such that
|
multi_source_distances(nodes, edges, source_node_ids, radius, directed=True, csr=None)
¶
Compute shortest-path distance from the nearest source to every reachable node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
DataFrame
|
Node table with a |
required |
edges
|
DataFrame
|
Directed edge table with |
required |
source_node_ids
|
Sequence[int]
|
Node ids to search from simultaneously (e.g. street nodes nearest to a set of POIs). |
required |
radius
|
float
|
Maximum distance (in the same units as |
required |
directed
|
bool
|
If |
True
|
csr
|
Optional[tuple]
|
Optional pre-built |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with columns |
DataFrame
|
(the nearest source that reaches this node), |
DataFrame
|
one row per node reachable within |
DataFrame
|
including the sources themselves at |
DataFrame
|
"lower is better" quantity, so unreached nodes are simply absent |
DataFrame
|
rather than given a sentinel value; if a caller left-joins this |
DataFrame
|
against a full node table, unreached nodes correctly come out as |
DataFrame
|
|
DataFrame
|
fill them with 0, which would mean "already there." |
multi_source_distances_per_source(nodes, edges, source_node_ids, radius, directed=True)
¶
Like :func:multi_source_distances, but keeps every (source, node) pair.
Useful when the caller needs to know all sources within range of a node,
not just the nearest one (e.g. counting how many POIs are reachable).
More expensive: runs one Dijkstra sweep per source instead of a single
min_only sweep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
DataFrame
|
Node table with a |
required |
edges
|
DataFrame
|
Directed edge table with |
required |
source_node_ids
|
Sequence[int]
|
Node ids to search from. |
required |
radius
|
float
|
Maximum search distance. |
required |
directed
|
bool
|
Whether to respect edge direction. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Polars DataFrame with columns |
DataFrame
|
one row per (source, reachable node) pair. |
induced_edges(edges, node_distances)
¶
Filter an edge table down to edges whose endpoints are both reachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
DataFrame
|
Full directed edge table with |
required |
node_distances
|
DataFrame
|
Output of :func: |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Subset of |
DataFrame
|
|
DataFrame
|
|