Crate osmpbfreader [] [src]

This crate provide an interface to easily read OpenStreetMap PBF files. Its main inspiration is libosmpbfreader.

# Usage

You can add osmpbfreader to your dependencies in your project's Cargo.toml.

 [dependencies]
 osmpbfreader = "0.8"

and this to your crate root:

 extern crate osmpbfreader;

# Getting objects and their dependencies

Most of the time, you'll want a subset of the OSM objects and its dependencies (i.e. the nodes inside a way, and not only the ids of the nodes of this way). For that, an easy to use function is availlable.

 let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::Cursor::new([]));
 let objs = pbf.get_objs_and_deps(|obj| {
         obj.way().map_or(false, |w| w.tags.contains_key("highway"))
     })
     .unwrap();
 for (id, obj) in &objs {
     println!("{:?}: {:?}", id, obj);
 }

# Readding

The easiest way to read a PBF file is to directly iterate on the OsmObj.

 use std::process::exit;
 let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::empty());
 for obj in pbf.iter() {
     // error handling:
     let obj = obj.unwrap_or_else(|e| {println!("{:?}", e); exit(1)});

     println!("{:?}", obj);
 }

There is also a parallel version of this iterator. The file is decoded in parallel.

 use std::process::exit;
 let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::empty());
 for obj in pbf.par_iter() {
     // error handling:
     let obj = obj.unwrap_or_else(|e| {println!("{:?}", e); exit(1)});

     println!("{:?}", obj);
 }

# Into the details

This crate is build around basic iterators on different parts of the structure of the PBF format. Then, several higher level iterator are proposed. It is then possible to iterate on the file using the low level iterators.

 use osmpbfreader::{primitive_block_from_blob, groups};
 let mut pbf = osmpbfreader::OsmPbfReader::new(std::io::empty());
 for block in pbf.blobs().map(|b| primitive_block_from_blob(&b.unwrap())) {
     let block = block.unwrap();
     for group in block.get_primitivegroup().iter() {
         for node in groups::simple_nodes(&group, &block) {
             println!("{:?}", node);
         }
         for node in groups::dense_nodes(&group, &block) {
             println!("{:?}", node);
         }
         for way in groups::ways(&group, &block) {
             println!("{:?}", way);
         }
         for relation in groups::relations(&group, &block) {
             println!("{:?}", relation);
         }
     }
 }

Notice that primitive_block_from_blob can be costy as it uncompress the blob. Using some kind of parallel map can then improve the reading speed of the PBF file.

Reexports

pub use objects::*;
pub use error::Error;
pub use error::Result;
pub use reader::OsmPbfReader;
pub use reader::primitive_block_from_blob;

Modules

blocks
error
groups
iter
objects

This module proposes objects to modelize OpenStreetMap objects.

par

Parallel iterator for OsmPbfReader.

reader