A high-performance Rust service for downloading, processing, and batching OpenStreetMap (OSM) data of Bangladesh via gRPC. This service provides efficient batch processing of OSM full imports and delta updates for geographic data pipelines.
The OSM Batching Tool is a complete solution for:
- OSM Data Downloads: Automatically downloads OSM PBF files (full imports) and OSC.GZ files (delta updates) from Geofabrik
- Format Conversion: Converts PBF files to XML using osmium-tool
- Data Batching: Splits large OSM XML files into manageable batches by element type (nodes, ways, relations)
- gRPC API: Provides a gRPC interface for requesting specific batches with proper validation and status tracking
- Background Processing: Handles long-running downloads and processing tasks asynchronously
osm_batching_tool/
├── src/
│ ├── main.rs # gRPC server implementation & request handling
│ └── lib.rs # Core OSM processing logic (download, convert, batch)
├── proto/
│ └── osm_import.proto # gRPC service definitions
├── build.rs # Protobuf compilation build script
├── Cargo.toml # Rust dependencies and project config
└── README.md # This file
src/main.rs - gRPC Service Layer:
- Implements the
OSMImportgRPC service withPingandFetchImportBatchendpoints - Handles request validation for date formats (DDMMYY) and ABC formats (AAA/BBB/CCC)
- Manages file system checks for existing batches and completion markers
- Spawns background processing tasks for new import requests
- Returns appropriate responses: batch content, completion status, pending, or errors
src/lib.rs - OSM Processing Engine:
process_osm_import(): Main orchestration function for full/delta importsprocess_full_import(): Downloads OSM PBF files and converts to XMLprocess_delta_import(): Downloads OSC.GZ delta files and decompressesbatch_osm_xml(): Core XML parsing and batching logic using quick-xmldownload_file(): Streaming file downloader with progress loggingconvert_pbf_to_xml(): Wrapper for osmium-tool PBF to XML conversion
proto/osm_import.proto - API Contract:
- Defines gRPC service interface
FetchImportBatchRequest: Supports both full date imports and delta ABC importsFetchImportBatchResponse: Handles multiple response types (pending/content/complete/error)
# Request batch 0 of nodes from Bangladesh data for September 1, 2025
grpcurl -plaintext -proto proto/osm_import.proto -d '{"batch_number": 0, "full_date": "250901", "element_type": "node"}' localhost:8080 osm_import.OSMImport/FetchImportBatch# Request batch 0 of ways from delta update 000/000/001
grpcurl -plaintext -proto proto/osm_import.proto -d '{"batch_number": 0, "delta_abc": "000/000/001", "element_type": "way"}' localhost:8080 osm_import.OSMImport/FetchImportBatch- Request: Client requests a specific batch via gRPC
- Validation: Server validates date/ABC format and checks for existing files
- Background Processing (if needed):
- Download OSM data from Geofabrik servers
- Convert PBF to XML format (full imports only)
- Parse XML and split into batches by element type
- Create completion markers when done
- Response: Return batch XML content, completion status, or pending indicator
The service organizes data in this structure:
./data/
├── full/
│ └── 250901/ # Date-based full import
│ ├── 250901.osm.pbf # Downloaded PBF file
│ ├── 250901.osm # Converted XML file
│ ├── lock # Processing lock file
│ └── batches/
│ ├── node/ # Node batches
│ ├── way/ # Way batches
│ └── relation/ # Relation batches
└── delta/
└── 000_000_001/ # ABC-based delta update
├── 000_000_001.osc.gz # Downloaded delta file
├── 000_000_001.osc # Decompressed delta file
└── batches/ # Same structure as full
Required for Running:
-
osmium-tool - Required for PBF to XML conversion at runtime
# Windows (Conda - Recommended) conda install conda-forge::osmium-tool # Linux/Ubuntu sudo apt-get install osmium-tool # macOS brew install osmium-tool
For detailed installation instructions and other platforms, see: https://osmcode.org/osmium-tool/
Required for Building:
- protoc - Protocol Buffer compiler (needed to compile the project)
# Windows # Download from: https://github.com/protocolbuffers/protobuf/releases # Or install via chocolatey: choco install protoc # Linux/Ubuntu sudo apt-get install protobuf-compiler # macOS brew install protobuf
For Testing/Development:
- grpcurl - Command-line gRPC client (for testing the API)
# Windows # Download from: https://github.com/fullstorydev/grpcurl/releases # Or install via chocolatey: choco install grpcurl # Linux/Ubuntu sudo apt-get install grpcurl # macOS brew install grpcurl
Core Dependencies (handled by Cargo):
tokio: Async runtime for concurrent operationstonic: gRPC framework for service implementationprost: Protocol Buffer implementationquick-xml: Fast XML parsing for batchingreqwest: HTTP client for file downloadsflate2: GZ decompression for delta filesanyhow: Error handlingtracing: Structured logging
# Development
cargo run
# Production build
cargo build --release
# Set environment variables
export SERVER_PORT=8080
export RUST_LOG=info- Streaming Downloads: Large files downloaded with progress tracking
- Memory Efficient: XML parsing without loading entire files into memory
- Concurrent Processing: Background tasks don't block gRPC requests
- Batch Size Optimization: 500 elements per batch (full), 1000 (delta)
- Lock File Protection: Prevents duplicate processing of same import
The service handles various error conditions gracefully:
- Invalid date/ABC format validation
- Network failures during downloads
- Missing osmium-tool dependency
- Corrupted or incomplete files
- XML parsing errors
- File system permission issues
All errors are logged with structured tracing and returned as gRPC error responses to clients.