Building-to-Service Travel Time Matrix with Intermodal Graph¶
This notebook demonstrates how to compute a time-based adjacency matrix between two GeoDataFrames (e.g., buildings and services) using a multimodal transport graph.
The method utilizes the IduEdu library to:
Construct a multimodal graph (e.g., walk + public transport)
Calculate travel time-based adjacency matrix from one GeoDataFrame to another
This matrix can be used in ObjectNat for further service provision analysis.
# %%
# Install required packages (uncomment if needed)
# !pip install iduedu pyarrow
# Import necessary libraries
from iduedu import get_intermodal_graph, od_matrix
import geopandas as gpd
import pandas as pd
from shapely.ops import unary_union
1. Load Input Geospatial Data¶
Load the GeoDataFrames of buildings (origins) and services (destinations).
# Read building and service datasets
buildings = gpd.read_parquet('examples_data/buildings.parquet')
services = gpd.read_parquet('examples_data/services.parquet')
2. Create Coverage Polygon for Graph Download¶
Compute a polygon that encompasses both datasets to define the spatial extent for graph download. This is done by computing a convex hull over all geometries and buffering it slightly.
polygon = unary_union(
buildings.to_crs(4326).geometry.to_list() + services.to_crs(4326).geometry.to_list()
).convex_hull.buffer(0.001)
3. Download and Clip Intermodal Graph¶
Download the intermodal (multi-modal) network graph using the defined polygon. This includes walking paths and public transport networks.
# Load multimodal graph clipped to polygon
G_intermodal = get_intermodal_graph(territory=polygon, clip_by_territory=True)
2026-07-10 13:05:42.759 | WARNING | Removing 393 nodes outside the largest strongly connected component. Retaining 21167 of 21560 nodes.
4. Compute Adjacency Matrix (Travel Time)¶
Calculate a travel-time-based adjacency matrix from buildings to services.
Parameters:
weight: edge attribute used for cost (e.g., “time_min”)threshold: maximum allowed travel time (in minutes)
# Compute travel time matrix (in minutes)
matrix: pd.DataFrame = od_matrix(
G_intermodal,
gdf_origins=buildings,
gdf_destinations=services,
weight="time_min",
threshold=45,
)
5. Save Adjacency Matrix¶
Export the result for further processing, e.g., with ObjectNat’s service provision tools.
# Save matrix to Parquet format
# IduEdu returns a sparse DataFrame to keep large OD matrices compact in memory.
# Parquet writers do not support pandas SparseDtype columns, so convert to a dense
# DataFrame before saving. Unreachable pairs stay encoded as np.inf.
matrix_dense = matrix.sparse.to_dense()
matrix_dense.to_parquet('examples_data/matrix_time.parquet')