Skip to main content

OSM data is an improv jam session, osm-chordify lets you chop it up, cut the takes, and bounce exactly what you need for routing, simulation, and GIS workflows.

Project description

osm-chordify

Download, filter, and export OSM road networks using population-density-based boundaries. Intersect the resulting geometries with polygon grids (TAZ, census tracts, ISRM cells, etc.) and map them to BEAM/MATSim simulation network.

Installation

Requires Python 3.10+.

pip install git+https://github.com/LBNL-UCB-STI/osm-chordify.git

For development:

git clone https://github.com/LBNL-UCB-STI/osm-chordify.git
cd osm-chordify
pip install -e ".[dev]"

For diagnostics (connectivity checks, link-length histograms):

pip install -e ".[diagnostics]"

Census API key

build_osm_by_pop_density uses the Census Bureau API to fetch population data for density-based filtering. Set your API key as an environment variable:

export CENSUS_API_KEY="your_key_here"

To make it persistent, add the line above to your ~/.bashrc, ~/.zshrc, or .env file.

Get a free key at https://api.census.gov/data/key_signup.html.

API

from osm_chordify import (
    build_osm_by_pop_density,
    intersect_road_network_with_zones,
    map_osm_with_beam_network,
    match_road_network_geometries,
    diagnose_osm,
)

build_osm_by_pop_density

Download and build a multi-layer OSM network. Each layer can target a different geographic level (county, census block group, tract) and apply a population density threshold to limit residential road downloads to urban areas.

build_osm_by_pop_density(
    work_dir="~/Workspace/Simulation/sfbay",
    osm_config=osm_config,   # osmnx settings, graph layers, filters
    area_config=area_config,  # name, state/county FIPS, census year
    geo_config=geo_config,    # UTM EPSG, TAZ shapefile path
)

The osm_config dict defines one or more graph_layers, each with a custom_filter (Overpass QL highway filter), geo_level, optional min_density_per_km2, and buffer_zone_in_meters. Layers are downloaded independently and merged into a single network exported as GeoJSON + GPKG.

intersect_road_network_with_zones

Intersect road-network edges with zone polygons. Computes the proportion of each edge that sits within each zone. All attributes from both inputs are carried through, prefixed with edge_ and zone_. Use proportional_cols to specify edge columns that should be scaled by the intersection proportion. Both road_network and zones accept either a file path (GPKG, GeoJSON, Shapefile) or a GeoDataFrame.

# From files — scale edge_length and vmt by proportion
intersect_road_network_with_zones(
    road_network="sfbay.gpkg",
    road_network_epsg=26910,
    zones="isrm_polygon.shp",
    zones_epsg=26910,
    proportional_cols=["edge_length", "vmt"],
    output_path="intersection.geojson",
    output_epsg=26910,
)

# From GeoDataFrames
result_gdf = intersect_road_network_with_zones(
    road_network=edges_gdf,
    road_network_epsg=26910,
    zones=polygons_gdf,
    zones_epsg=26910,
    proportional_cols="edge_length",
)

map_osm_with_beam_network

Join a BEAM network CSV to a zone-network intersection result on the shared OSM ID. All columns from both inputs are included in the output.

map_osm_with_beam_network(
    osm_path="sfbay.gpkg",
    network_path="network.csv.gz",
    network_osm_id_col="attributeOrigId",
    output_path="mapping.geojson",
)

match_road_network_geometries

Spatially match link geometries between two road networks. Accepts file paths or GeoDataFrames. Use matching="strict" for high-overlap matches only, or matching="flexible" (default) for partial and nearby matches.

result_gdf = match_road_network_geometries(
    network_a="beam_osm.geojson",
    network_a_epsg=4326,
    network_b="hpms_network.shp",
    network_b_epsg=26910,
    matching="flexible",
    output_path="matched.geojson",
    output_epsg=26910,
)

Note: This function is a placeholder and not yet implemented.

diagnose_osm

Run connectivity and link-length diagnostics on a .osm.pbf file. Reports connected components, short/long link anomalies, and saves a histogram. Requires the diagnostics extra.

diagnose_osm("sfbay.osm.pbf", epsg_utm=26910)

Examples

See the examples/ directory for complete, runnable scripts:

Script Description
build_sfbay.py Build OSM network for the SF Bay Area (9 counties)
build_seattle.py Build OSM network for Seattle metro (4 counties + ferry)
intersect_and_map_sfbay.py Intersect OSM with polygon grid and map BEAM network
diagnose_sfbay.py Run diagnostics on a downloaded PBF

Project structure

src/osm_chordify/
    __init__.py          # Public API
    main.py              # Orchestration pipeline
    osm/
        graph.py         # OSM network download and preparation
        export.py        # Network export (GeoJSON, GPKG)
        intersect.py     # Edge-polygon intersection
        analyze.py       # PBF analysis (osmium)
        diagnostics.py   # Network validation
        tags.py          # OSM tag parsing
    utils/
        geo.py           # Geographic utilities
        data_collection.py  # Census data and boundaries
        network.py       # Network-to-intersection mapping
        io.py            # File I/O helpers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osm_chordify-0.1.1.tar.gz (40.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osm_chordify-0.1.1-py3-none-any.whl (45.1 kB view details)

Uploaded Python 3

File details

Details for the file osm_chordify-0.1.1.tar.gz.

File metadata

  • Download URL: osm_chordify-0.1.1.tar.gz
  • Upload date:
  • Size: 40.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for osm_chordify-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0959e32b0953013968cba097c2aca52ecda8305cb0fc64e611f93e82a0e9e21f
MD5 25cffadde0596079eb54875858c65c0d
BLAKE2b-256 53fc329a46cd9b747ef37af7e4a51982d4d53607ab0ca6fec9740bb086ea0175

See more details on using hashes here.

File details

Details for the file osm_chordify-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: osm_chordify-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 45.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for osm_chordify-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82cc9dc4f7b0c9b615bdc9b59ec490024f267da3658d15091aa2893c26faa3a3
MD5 f5c4f9eb41989dc6f8e8b67c573de745
BLAKE2b-256 113ad146477ca59b0749a73d297906a1d3c1340b78812db2750261b94def0c23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page