Skip to content

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 u, v, length_m (or another weight column selected by the caller before calling this function; the weight column must be named length_m).

required
nodes DataFrame

DataFrame with a node_id column listing every node that should exist in the matrix (including isolated ones), so index positions are stable across repeated calls with the same graph.

required

Returns:

Name Type Description
csgraph csr_matrix

scipy.sparse.csr_matrix of shape (n_nodes, n_nodes) with edge weights.

node_id_to_index dict

dict mapping OSM/graph node_id to matrix row/column index.

index_to_node_id ndarray

numpy array such that index_to_node_id[i] is the node_id at matrix index i.

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 node_id column.

required
edges DataFrame

Directed edge table with u, v, length_m columns (as produced by :func:UrbanAccessAnalyzer.osm_io.load_pbf or :mod:UrbanAccessAnalyzer.graph_ops).

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 length_m, normally meters) to search out to; nodes farther than this are dropped from the result, not returned with inf.

required
directed bool

If False, edges are treated as traversable in both directions regardless of the oneway flag already encoded in edges (i.e. the CSR matrix is symmetrized). Street networks from :mod:osm_io already contain both directions for two-way ways, so this is normally left True; set False to ignore one-way restrictions (e.g. for a pure walking network where one-way tags shouldn't apply).

True
csr Optional[tuple]

Optional pre-built (csgraph, node_id_to_index, index_to_node_id) tuple, as returned by :func:build_csr, to reuse across repeated calls against the same nodes/edges (the graph topology) with different source_node_ids/ radius (e.g. one call per access-score tier). When given, nodes and edges are not re-read to build the matrix -- only used as documentation of which graph the caller intends; pass the same nodes/edges used to build csr. When None (default), the CSR is built fresh from nodes/ edges on every call, exactly matching prior behavior.

None

Returns:

Type Description
DataFrame

Polars DataFrame with columns node_id (i64), source_id (i64)

DataFrame

(the nearest source that reaches this node), dist (f64). Contains

DataFrame

one row per node reachable within radius of at least one source,

DataFrame

including the sources themselves at dist = 0. dist is a

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

null (representing an effectively infinite distance) -- never

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 node_id column.

required
edges DataFrame

Directed edge table with u, v, length_m columns.

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 node_id, source_id, dist,

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 u, v columns.

required
node_distances DataFrame

Output of :func:multi_source_distances (or similar), with a node_id column of reachable nodes.

required

Returns:

Type Description
DataFrame

Subset of edges where both u and v are present in

DataFrame

node_distances, with u_dist/v_dist columns joined in from

DataFrame

node_distances.dist.