odflows

Origin-destination (OD) flow analysis for mobility and commuting: OD matrices, desire lines, net/gross flows, flow clustering, self-containment, Intramax functional regions and flowmap.blue / kepler.gl export.

Version 0.1.2 · Python >=3.9 · MIT licence
pip install odflows
PyPISourceIssuesChangelogCite (DOI)

odflows

PyPI version Python versions pipeline status License: MIT DOI Downloads

Origin-destination flow toolkit for mobility and commuting analysis.

odflows covers the usual steps of an OD study in one small package that needs only numpy:

pip install odflows            # numpy only
pip install "odflows[pandas]"  # + DataFrame helpers
pip install "odflows[geo]"     # + shapely, for area-weighted lookups from polygons

Requires Python 3.9 or newer.

Quick start

from odflows import (
    ODMatrix, read_od_csv, centroids_from_geojson, desire_lines, write_geojson,
    zone_summary, intramax, region_summary, filter_top_k,
)

od = read_od_csv("commute.csv")                 # origin,destination,flow
zones = centroids_from_geojson("zones.geojson", id_field="zone")

for row in zone_summary(od):                     # self-containment, job ratio, ...
    print(row["zone"], round(row["supply_sc"], 3), round(row["job_ratio"], 2))

lines = desire_lines(filter_top_k(od, 20), zones, style="curved")
write_geojson(lines, "desire_lines.geojson")

hierarchy = intramax(od)                         # full Intramax hierarchy
membership = hierarchy.membership(n_regions=4)   # cut it at 4 regions
print(region_summary(od, membership))

A runnable example with synthetic data is in examples/quickstart.py.

Matrices can also be built in memory:

od = ODMatrix.from_long([("A", "B", 120), ("B", "A", 40), ("A", "A", 300)])
od = ODMatrix(["A", "B"], [[300, 120], [40, 0]])  # rows = origins, columns = destinations
od.net().to_long()   # [('A', 'B', 80.0)]

Command line

odflows summary commute.csv -o zones.csv              # per-zone indicators
odflows summary commute.csv --json                    # matrix statistics
odflows lines commute.csv --centroids zones.geojson --id-field zone \
        --style curved --min-flow 50 -o lines.geojson
odflows filter commute.csv --min-share 0.1 --basis origin -o main_flows.csv
odflows transform commute.csv net -o net.csv          # net | gross | symmetric | ...
odflows aggregate commute.csv --lookup lookup.csv -o districts.csv
odflows regions commute.csv -n 5 -o regions.csv --summary regions_sc.csv
odflows cluster commute.csv --centroids zones.geojson --id-field zone \
        --n-clusters 8 -o bundles.geojson
odflows export commute.csv --centroids zones.geojson --id-field zone \
        --format flowmap -o mymap                       # mymap-locations.csv, mymap-flows.csv

Every command accepts --matrix for square-matrix input and --origin-col, --dest-col, --flow-col for other column names. Centroids may be a GeoJSON file or a CSV (--id-field, --x-col, --y-col).

API overview

Area Functions / classes
Container ODMatrix (from_long, to_long, outflows, inflows, intrazonal, without_intrazonal, net, gross, symmetric, transpose, asymmetry_index, subset)
Input / output read_od_csv, read_matrix_csv, write_od_csv, write_matrix_csv, centroids_from_geojson, read_centroids_csv, geometry_centroid, write_geojson
Desire lines desire_lines, line_coordinates, haversine_km
Zone systems ZoneLookup (from_records, from_mapping, from_csv, from_geojson), reaggregate, aggregate
Filtering filter_min_flow, filter_top_k, filter_share, filter_cumulative, dominant_flows
Indicators degree, self_containment, job_ratio, net_balance, zone_summary, matrix_summary
Clustering FlowSet, flow_distance_matrix, flow_neighbour_counts, hierarchical_clusters, flow_dbscan, bundle_flows
Regions intramax, IntramaxResult, region_summary
Visualisation export write_flowmap_blue, write_kepler_csv
pandas from_dataframe, to_dataframe

All filters and transformations return a new ODMatrix, so they chain: filter_top_k(filter_share(od, 0.1), 50).without_intrazonal().

Methods and sources

Conventions. T[i, j] is the flow from zone i to zone j. For commuting, rows are places of residence and columns places of work, so O_i = Σ_j T_ij are resident workers and D_j = Σ_i T_ij are jobs; both include intrazonal flows T_ii.

Net and gross flows. Gross G_ij = T_ij + T_ji; net N_ij = max(T_ij − T_ji, 0). The asymmetry index is Σ_{i<j} |T_ij − T_ji| / Σ_{i<j} (T_ij + T_ji).

Desire lines. Curved lines are quadratic Bézier curves whose control point is the midpoint moved to the right of the direction of travel by bend × length; offset lines are the segment shifted to the right by the same amount. A→B and B→A therefore fall on opposite sides. Lengths are haversine kilometres for longitude/latitude centroids (mean Earth radius 6371.0088 km) and planar units otherwise.

Polygon centroids use the shoelace (area-weighted) formula on the exterior ring minus holes, treating coordinates as planar.

Zone re-aggregation. With a row-normalised lookup W (W[s, t] = share of source zone s in target zone t), T' = Wᵀ T W. Flows are split assuming origins and destinations are spread within each source zone in proportion to the weights - areal weighting (Goodchild & Lam 1980). Different origin and destination lookups (e.g. population vs. employment weights) are supported; totals are preserved.

Filtering. filter_share(basis="origin") keeps T_ij / O_i ≥ s; dominant_flows keeps each origin's largest outgoing flow, the first step of nodal region analysis (Nystuen & Dacey 1961).

Flow distance. Tao & Thill (2016) define, for flows i and j with origin distance d_O and destination distance d_D, FD = sqrt(α d_O² + β d_D²) with α + β = 2, and the length-normalised dissimilarity FDS = sqrt((α d_O² + β d_D²) / (L_i L_j)). Coordinates must be projected. flow_neighbour_counts gives the local counting term of their flow K-function (no edge correction or significance test).

Clustering. Agglomerative clustering uses the Lance-Williams (1967) update for single, complete and average (UPGMA) linkage; DBSCAN follows Ester et al. (1996), with an option to count flow volume instead of flow numbers. bundle_flows replaces every cluster by the flow from its volume-weighted mean origin to its volume-weighted mean destination.

Self-containment. Supply-side SC_s = T_ii / O_i (share of resident workers who work locally) and demand-side SC_d = T_ii / D_i (share of local jobs filled by residents), as used for local labour market areas (Goodman 1970; Casado-Díaz 2000). The commuting job ratio is D_i / O_i.

Intramax. Hierarchical aggregation (Masser & Brown 1975) that repeatedly merges the pair of current regions maximising T_ij / (O_i D_j) + T_ji / (O_j D_i) on the aggregated matrix; merged flows become intrazonal. The full hierarchy is stored and can be cut at any number of regions. An optional contiguity constraint only merges adjacent regions (Drobne & Lakner 2016). This is Intramax, not a modularity / Louvain method.

Export formats. flowmap.blue reads a Google Sheet with a locations sheet (id,name,lat,lon) and a flows sheet (origin,dest,count); write_flowmap_blue writes one CSV per sheet. write_kepler_csv writes origin_lat/origin_lng and dest_lat/dest_lng column pairs, which kepler.gl detects as point fields for its arc and line layers.

References

Limitations

Academic citation

If you use odflows in research, please cite it:

@software{eminoglu_odflows,
  author  = {Eminoğlu, Yusuf},
  title   = {odflows},
  year    = {2026},
  version = {0.1.2},
  doi     = {10.5281/zenodo.24085057},
  url     = {https://gitlab.com/geophilo1/odflows_sdk}
}

See also CITATION.cff.

Related Geophilo SDKs

License

MIT © 2026 Yusuf Eminoğlu. See LICENSE. Contact: [email protected]

Other Geophilo SDKs