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.3"

and this to your crate root:

extern crate osmpbfreader;

Readding without error handling

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

let path = std::path::Path::new("/dev/null");
let r = std::fs::File::open(&path).unwrap();
let mut pbf = osmpbfreader::OsmPbfReader::new(r);
for obj in pbf.iter() {
    println!("{:?}", obj);
}

Notice that, in case of any error, it'll panic.

Readding with error handling

To manage error handling, a little more work is needed. First, iteration on the different blocks is done, and then, for each blocks, after error handling, iteration on the OsmObj can be done.

use std::process::exit;
use osmpbfreader::blocks;
let path = std::path::Path::new("/dev/null");
let r = std::fs::File::open(&path).unwrap();
let mut pbf = osmpbfreader::OsmPbfReader::new(r);
for block in pbf.primitive_blocks() {
    // error handling:
    let block = block.unwrap_or_else(|e| {println!("{:?}", e); exit(1)});

    for obj in blocks::iter(&block) {
        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 path = std::path::Path::new("/dev/null");
let r = std::fs::File::open(&path).unwrap();
let mut pbf = osmpbfreader::OsmPbfReader::new(r);
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::{OsmObj, Node, Way, Relation, Ref, OsmId, Tags};
pub use error::Error;
pub use error::Result;
pub use reader::{OsmPbfReader, primitive_block_from_blob};

Modules

blocks
borrowed_iter
error
fileformat

Generated from protobuf.

groups
objects
osmformat

Generated from protobuf.

reader

Functions

get_objs_and_deps

This function give you the ability to find all the objects validating a predicate and all there dependencies.