Expand description
A pure-Rust library for reading and writing OpenStreetMap PBF (Protocolbuffer Binary Format) files.
§Modules
models— the OSM element model:models::Node,models::Way,models::Relationand themodels::Elementenum, with coordinates in integer nanodegrees.readers— a family of readers for different scenarios:readers::PbfReader— sequential streaming reader (single- or multi-threaded filtering viareaders::PbfReader::par_find).readers::IterableReader— iterator-based reader with byte progress reporting (readers::ReaderProgress).readers::IndexedReader— random access by element id, backed by a.pifindex file, with an in-memory blob cache and dependency resolution (readers::IndexedReader::get_with_deps).
writers—writers::PbfWriter, a streaming writer producing zlib-compressed blobs with dense-node support.
§Examples
Read PBF data from a file:
use pbf_craft::readers::PbfReader;
let mut reader = PbfReader::from_path("resources/andorra-latest.osm.pbf").unwrap();
reader.read(|header, element| {
if let Some(header_reader) = header {
// Process header
}
if let Some(element) = element {
// Process element
}
}).unwrap();Read PBF data with dependencies:
use pbf_craft::models::ElementType;
use pbf_craft::readers::IndexedReader;
let mut indexed_reader =
IndexedReader::from_path_with_cache("resources/andorra-latest.osm.pbf", 1000).unwrap();
let element_list = indexed_reader.get_with_deps(&ElementType::Way, 12345678).unwrap();Write PBF data to a file:
use pbf_craft::models::{Element, Node};
use pbf_craft::writers::PbfWriter;
let mut writer = PbfWriter::from_path(std::env::temp_dir().join("output.osm.pbf"), true).unwrap();
writer.write(Element::Node(Node::default())).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. - 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; the feature is auto-detected from elements seen before the first flushed block, so callers streaming historical data whose invisible elements may arrive later should declare it up front viawriters::PbfWriter::set_historical_data. - Error handling: all fallible operations return
anyhow::Result; malformed or truncated input surfaces as errors rather than panics.