How rust-transit/osm4routing2 works
Project Type and Purpose
• Type: A Rust-based command-line application that also exposes its core functionality as a library.
• Purpose: Read an OpenStreetMap .pbf file, apply filtering/merging rules, then output two CSV files (nodes.csv and edges.csv) representing the road network. Optionally import those CSVs into PostgreSQL via a provided shell script.
High-Level Analysis of the File Structure
• Top-level metadata and build scripts:
• Cargo.toml, .travis.yml, .github/workflows/tests.yml
• create_tables.sql & import_postgres.sh (for DB ingestion)
• Source code under src/:
• main.rs – CLI entry point
• lib.rs – library façade
• osm4routing/ – core modules:
– reader.rs – PBF parsing and filter/merge configuration
– models.rs – data structures (Node, Edge)
– categorize.rs – tag-based classification and merge logic
– test_data/ – sample OSM files for unit tests
Key Architectural Insights from README
• The CLI simply delegates to functions in the osm4routing crate.
• The library offers a builder (Reader::new()) to configure filters (reject, require, merge_ways, read_tag), then runs .read(path) producing (nodes, edges).
• A shell script (import_postgres.sh) and SQL schema file (create_tables.sql) support loading generated CSVs into a Postgres DB.
Main Components
a. Input Layer
– .pbf file (OpenStreetMap data)
b. CLI Layer (src/main.rs)
– Parses command-line flags and file path
– Instantiates osm4routing::Reader and triggers .read(...)
c. Core Library (osm4routing crate)
– Reader Module (reader.rs)
• Configures filters and transformations
• Iterates PBF blobs (likely via an external osmpbf-parsing crate)
– Models Module (models.rs)
• Defines in-memory structs: Node, Edge
– Categorize Module (categorize.rs)
• Applies tagging logic: required vs. rejected tags
• Merges ways when tags change (tunnels, railways, etc.)
– Writers Module (writers.rs)
• Serializes Vec<Node> and Vec<Edge> to CSV files
d. Output Layer
– nodes.csv, edges.csv
e. Optional Database Import
– import_postgres.sh & create_tables.sql
– Connects to a PostgreSQL instance, creates tables, loads CSVs
Relationships & Data Flow
1. User invokes osm4routing path/to/data.pbf (or cargo run --release -- ...).
2. CLI → sets up a configured Reader.
3. Reader → reads raw PBF blobs → produces streams of OSM entities.
4. Streams pass through Categorize filters & merge_ways logic.
5. Filtered/merged entities are mapped into Models (Node, Edge).
6. Writers consume those models, generate nodes.csv & edges.csv.
7. Optionally, import_postgres.sh reads the CSVs → applies create_tables.sql → loads data into PostgreSQL.
Architectural Patterns & Principles
• Separation of Concerns:
• CLI, parsing, model definition, business logic (categorization), serialization, and DB import are all in separate modules.
• Builder Pattern for configuring the Reader.
• Pipeline/Streaming approach: data flows through successive transformation stages to minimize memory footprint.
• Exposes a library API for programmatic reuse, not just CLI.