pbf-craft
A pure-Rust library for reading and writing OpenStreetMap PBF (Protocolbuffer Binary Format) files.
Features
- Multiple readers for different scenarios:
PbfReader— sequential streaming reader, with parallel filtering (par_find) and byte progress reporting.IterableReader— iterator-based reader withReaderProgress.IndexedReader— random access by element id, backed by a.pifindex file, with an in-memory blob cache and dependency resolution.
- Dense and sparse node encoding; reads
raw,zlib,lz4andzstdblobs; writeszlib-compressed blobs. - Result-based error handling: malformed or truncated input surfaces as errors, not panics.
- Pure Rust, no C dependencies.
Usage
Add this to your Cargo.toml:
[]
= "1"
Reading a PBF file:
use PbfReader;
let mut reader = from_path.unwrap;
reader.read.unwrap;
Finding an element using the index feature. IndexedReader creates an index file for the
PBF file, which allows you to quickly locate and retrieve an element when looking for it
using its ID. IndexedReader has a cache option, with which you can fetch an element with
its dependencies more efficiently.
use ElementType;
use IndexedReader;
let mut indexed_reader = from_path_with_cache.unwrap;
let element_list = indexed_reader.get_with_deps.unwrap;
Writing a PBF file:
use ;
use PbfWriter;
let mut writer = from_path.unwrap;
writer.write.unwrap;
writer.finish.unwrap;
Data format notes
- Coordinates:
Node/WayNode/Boundcoordinates are i64 nanodegrees (the raw PBF unit; divide by 1e9 for degrees). - Ordering: the PBF format does not require sorted elements, but the conventional
layout (all nodes by id, then all ways by id, then all relations by id) is assumed by
IndexedReaderand most other tools.PbfWriterstores elements in the order written — the caller is responsible for the order;IndexedReaderrejects unordered files with an error when building its index. - Compression: reading supports
raw,zlib,lz4andzstdblobs; writing produceszlib-compressed blobs. - visible flag: elements default to
visible = true(per spec, the flag is assumed true when absent). Elements explicitly markedvisible = falseare written with the requiredHistoricalInformationfeature declared in the header. - Error handling: all fallible operations return
anyhow::Result; malformed input surfaces as errors rather than panics.
MSRV
Current stable Rust (1.70+). This crate is tested against the latest stable toolchain.
License
MIT