1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2014-2015 Guillaume Pinot <texitoi(a)texitoi.eu>
//
// This work is free. You can redistribute it and/or modify it under
// the terms of the Do What The Fuck You Want To Public License,
// Version 2, as published by Sam Hocevar. See the COPYING file for
// more details.

//! This crate provide an interface to easily read [OpenStreetMap PBF
//! files](http://wiki.openstreetmap.org/wiki/PBF_Format).  Its main
//! inspiration is
//! [libosmpbfreader](https://github.com/CanalTP/libosmpbfreader).
//!
//! # Usage
//!
//! You can add `osmpbfreader` to your dependencies in your project's
//! `Cargo.toml`.
//!
//! ```toml
//! [dependencies]
//! osmpbfreader = "0.6"
//! ```
//!
//! and this to your crate root:
//!
//! ```rust
//! 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 path = std::path::Path::new("/dev/null");
//! let r = std::fs::File::open(&path).unwrap();
//! let mut pbf = osmpbfreader::OsmPbfReader::new(r);
//! let objs = osmpbfreader::get_objs_and_deps(&mut pbf, |obj| {
//!         obj.way().map_or(false, |w| w.tags.contains_key("highway"))
//!     })
//!     .unwrap();
//! for (id, obj) in &objs {
//!     println!("{:?}: {:?}", id, obj);
//! }
//!```
//!
//! # Readding without error handling
//!
//! The easiest way to read a PBF file is to directly iterate on the
//! `OsmObj`.
//!
//! ```rust
//! 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.
//!
//! ```rust
//! 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.
//!
//! ```rust
//! 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.

// #![deny(missing_docs)]

extern crate protobuf;
extern crate flate2;
extern crate byteorder;
extern crate flat_map;

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};

/// Generated from protobuf.
#[allow(non_snake_case, missing_docs)]pub mod fileformat;

/// Generated from protobuf.
#[allow(missing_docs)]pub mod osmformat;

pub mod error;
pub mod objects;
pub mod groups;
pub mod blocks;
pub mod borrowed_iter;
pub mod reader;

use std::collections::{BTreeSet, BTreeMap};
use std::io::{Seek, Read};


/// This function give you the ability to find all the objects validating
/// a predicate and all there dependencies.
///
/// # Examples
/// If you want to extract all the administrative boundaries
/// and all there dependencies you can do something like that:
///
/// ```
/// fn is_admin(obj: &osmpbfreader::OsmObj) -> bool{
///     match *obj {
///        osmpbfreader::OsmObj::Relation(ref rel) => {
///            rel.tags.get("boundary").map_or(false, |v| v == "administrative")
///         }
///        _ => false,
///     }
/// }
///
/// 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 osmpbfreader::get_objs_and_deps(&mut pbf, is_admin) {
///     println!("{:?}", obj);
/// }
/// ```
pub fn get_objs_and_deps<R, F>(reader: &mut OsmPbfReader<R>,
                               mut pred: F)
                               -> Result<BTreeMap<OsmId, OsmObj>>
    where R: Read + Seek,
          F: FnMut(&OsmObj) -> bool
{
    let mut finished = false;
    let mut deps = BTreeSet::new();
    let mut objects = BTreeMap::new();
    while !finished {
        finished = true;
        for block in reader.primitive_blocks() {
            let block = try!(block);
            for obj in blocks::iter(&block) {
                if !deps.contains(&obj.id()) && !pred(&obj) {
                    continue;
                }
                finished = match obj {
                    OsmObj::Relation(ref rel) => {
                        rel.refs.iter().fold(finished, |accu, r| !deps.insert(r.member) && accu)
                    }
                    OsmObj::Way(ref way) => {
                        way.nodes
                           .iter()
                           .fold(finished, |accu, n| !deps.insert(OsmId::Node(*n)) && accu)
                    }
                    OsmObj::Node(_) => finished,
                };
                objects.insert(obj.id(), obj);
            }
        }
        try!(reader.rewind());
    }
    Ok(objects)
}