Mobility Module¶
The mobility module provides functions for processing Origin-Destination (OD) matrices and creating mobility networks for urban flow analysis.
Functions¶
Mobility / OD matrix utilities.
This module introduces the public function od_matrix_to_graph which
converts Origin-Destination (OD) data (adjacency matrices or edge lists)
into spatial graph representations used throughout the city2graph
ecosystem.
Notes
This module includes a complete implementation of od_matrix_to_graph:
input validation, zone alignment, conversion to a canonical edgelist,
thresholding and self-loop handling, optional geometry creation, and an
optional NetworkX export path.
Examples:
See the function docstring for usage examples with adjacency matrices, NumPy arrays and edge lists (single/multi weight columns).
Functions:
| Name | Description |
|---|---|
od_matrix_to_graph |
Convert OD data (edge list or adjacency matrix) into graph structures. |
od_matrix_to_graph ¶
od_matrix_to_graph(
od_data,
zones_gdf,
zone_id_col=None,
*,
matrix_type="edgelist",
source_col="source",
target_col="target",
weight_cols=None,
threshold=None,
threshold_col=None,
include_self_loops=False,
compute_edge_geometry=True,
directed=True,
as_nx=False
)
Convert OD data (edge list or adjacency matrix) into graph structures.
Creates spatially-aware graphs from OD data following city2graph's
GeoDataFrame-first design. Supports adjacency matrices (DataFrame or
ndarray) and edgelists with one or multiple numeric weight columns.
By default, this function returns a pair of GeoDataFrames representing
nodes and edges. When directed=False, the output is undirected: for
each unordered pair {u, v}, the edge weight equals the sum of directed
weights in both directions (u->v plus v->u). When a threshold is provided
in undirected mode, it is applied after this summation. By default edges
are directed and thresholded with the rule weight >= threshold (or, when
no threshold provided, strictly > 0). Optionally, it can return a NetworkX
graph when as_nx=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
od_data
|
DataFrame | ndarray
|
|
required |
zones_gdf
|
GeoDataFrame
|
GeoDataFrame of zones. Must contain unique identifiers in
|
required |
zone_id_col
|
str
|
Name of the zone ID column in |
None
|
matrix_type
|
('edgelist', 'adjacency')
|
Declares how to interpret |
'edgelist','adjacency'
|
source_col
|
str
|
Column names for origins / destinations when using an edge list. |
'source','target'
|
target_col
|
str
|
Column names for origins / destinations when using an edge list. |
'source','target'
|
weight_cols
|
Sequence[str] | None
|
Edge list weight (flow) columns to preserve. A single column acts as
the canonical weight. If multiple columns are provided a
|
None
|
threshold
|
float
|
Minimum flow retained (>=) applied to |
None
|
threshold_col
|
str
|
Column among |
None
|
include_self_loops
|
bool
|
Keep flows where origin == destination (defaults drop when False). |
False
|
compute_edge_geometry
|
bool
|
Whether to build LineString geometries from zone centroids. |
True
|
directed
|
bool
|
Whether to build a directed graph. If False, reciprocal edges are merged by summing their weights (and all provided weight columns). |
True
|
as_nx
|
bool
|
If True, final output will be an NetworkX graph ( |
False
|
Returns:
| Type | Description |
|---|---|
tuple[GeoDataFrame, GeoDataFrame] or Graph or DiGraph
|
The graph representation in the requested format:
|