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.

No AI Training Use: The contents of this repository may not be used to train, fine-tune, evaluate, benchmark, or improve machine learning or artificial intelligence models without prior written permission. See NOAI.

Installation

Requires Python 3.10+.

From PyPI:

pip install osm-chordify

From GitHub:

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]"

U.S. Census API key

build_osm_by_pop_density uses the U.S. 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.

You can also pass the key directly to the example scripts with --census-api-key.

API

from osm_chordify import (
    build_area_mask_from_counties,
    build_osm_by_pop_density,
    create_osm_highway_filter,
    intersect_polygons_with_zones,
    intersect_road_network_with_county_zones,
    intersect_road_network_with_zones,
    spatial_left_join_with_zones,
    intersect_zones_with_zones,
    map_osm_with_beam_network,
)

Core functions:

  • build_area_mask_from_counties: build a fused county-based land mask or whole-area mask from FIPS codes
  • build_osm_by_pop_density: build a multi-layer OSM network from county / tract / CBG boundaries and density filters
  • intersect_polygons_with_zones: cascade an already-intersected polygon layer into a new zone layer
  • intersect_road_network_with_county_zones: intersect a network with county polygons fetched by state/county FIPS codes
  • intersect_road_network_with_zones: intersect network edges with zone polygons
  • spatial_left_join_with_zones: spatially left-join any geometry layer with zone attributes while keeping unmatched rows
  • intersect_zones_with_zones: intersect one polygon zone layer with another
  • map_osm_with_beam_network: join BEAM network attributes onto OSM geometries

Intersection outputs always include:

  • proportion
  • link_length_m

These columns always describe the latest intersection step. In chained intersections, earlier polygon columns are carried forward unchanged, and only the current step adds new unlabeled or <label>_... metrics plus the new zone attributes.

For dense grids, intersect_road_network_with_zones(...) can optionally prefilter zones to the overall road-network bounding box before exact intersection:

  • prefilter_zones_to_network_bbox=False disables the prefilter
  • prefilter_zones_to_network_bbox=True keeps only zones intersecting the network bounding box

This reduces the zone set before exact intersection and shows a dedicated Filtering zones progress bar. Zones kept by the bounding-box prefilter that still contain no intersecting link pieces are preserved instead of being dropped; the fixed numeric intersection columns remain null for those rows.

For rectangular or polygon link workflows, use intersect_polygons_with_zones(...). It computes overlap proportions from polygon area, derives <label>_link_length_m (or link_length_m without a label) from the source polygon length column, and prefilters zones to the input-layer bounding box by default.

For cascading polygon workflows, use:

  • intersect_polygons_with_zones(...)
    • preserves all existing columns from the input polygon layer
    • adds new zone attributes with a label prefix such as inmap_* or aermod_*
    • computes current-step metrics:
      • <label>_proportion
      • <label>_link_length_m
  • spatial_left_join_with_zones(...)
    • keeps all input rows
    • appends zone attributes where matched, using a label prefix such as county_*
    • leaves zone columns null where no zone intersects

Mask generation semantics:

  • include_water=False
    • land mask
    • uses shoreline-clipped cartographic county boundaries
    • fuses counties without taking a convex hull
    • optional buffer_m is applied to that non-convex land geometry
  • include_water=True
    • whole-area mask
    • uses full TIGER/Line county boundaries
    • takes the convex hull of the fused counties
    • optional buffer_m is applied to that convex whole-area geometry

Networks can be exported as GraphML, PKL, GPKG, OSM XML, OSM PBF, and GeoJSON.

You can also run the main workflows via CLI:

python -m osm_chordify.main --help

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)
compare_osm_pbf.py Compare validation and diagnostics metrics across two built .osm.pbf artifacts
intersect_zones.py Intersect one polygon zone layer with another
intersect_and_map_sfbay.py Intersect OSM with polygon grid and map BEAM network
diagnose_osm_pbf.py Validate and diagnose a built .osm.pbf artifact

For commands, output layout, validation behavior, and project structure, see docs/USAGE.md.

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.2.4.tar.gz (96.0 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.2.4-py3-none-any.whl (62.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for osm_chordify-0.2.4.tar.gz
Algorithm Hash digest
SHA256 e85c1b632c84291d8b7b988af53870b68efbe205a3ca8ccbaff458fac220bc7d
MD5 7bdd57b97ce7ee7a77e5ad40abb6e154
BLAKE2b-256 05c0d5e09e13010f274ea52f26e62bd28b4f0c36fbd8f6949a77d6b4442897f8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for osm_chordify-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 45f4a1d5193fac0002b4e38b2c78aa9b719e80b028f5390be6ffd769a0b4917f
MD5 50467827008f4cc051e4b199c2e35247
BLAKE2b-256 996c6aa6c392d133acad6f40d110acb0d42504b110177a9a4ff8a2500ddd046d

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