pbf-craft 1.0.3

A Rust library for reading and writing OpenSteetMap PBF file format.
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Element models for OpenStreetMap data.
//!
//! The three OSM element types are [`crate::models::Node`], [`crate::models::Way`] and [`crate::models::Relation`], which all share the
//! common metadata fields of [`crate::models::ElementBase`] (id, version, timestamp, user, changeset id,
//! visible flag and tags) and are carried polymorphically by the [`crate::models::Element`] enum.
//!
//! # Units
//!
//! Coordinates are stored as **integer nanodegrees** — the raw unit used by the PBF format
//! (1e9 nanodegrees = 1 degree). This avoids floating-point precision loss on round-trips.
//! Divide by `1e9` to obtain degrees. [`crate::models::Bound`] fields use the same unit.
//!
//! # The `visible` flag and metadata defaults
//!
//! Per the PBF spec the `visible` flag is assumed `true` when absent. All element types
//! therefore default `visible` to `true`, and `timestamp`/`user` are `Option`s that are
//! `None` when the source data carries no such metadata. `version`/`changeset_id` default to
//! `-1` (the convention used by osmosis for "no version"/"no changeset").
use std::str::FromStr;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// A bounding box from the PBF file header.
///
/// Coordinates are in integer **nanodegrees** (1e9 per degree). `origin` is the data source
/// string recorded in the header.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bound {
    pub left: i64,
    pub right: i64,
    pub top: i64,
    pub bottom: i64,
    pub origin: String,
}

/// The user associated with an element (a mapper account name and id).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OsmUser {
    pub id: i32,
    pub name: String,
}

/// A polymorphic OSM element: either a [`Node`], a [`Way`] or a [`Relation`].
///
/// Serialized with a `type` tag (`"node"`, `"way"`, `"relation"`).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Element {
    Node(Node),
    Way(Way),
    Relation(Relation),
}

impl Element {
    /// Returns the element's `(type, id)` pair.
    pub fn get_meta(&self) -> (ElementType, i64) {
        match self {
            Element::Node(e) => (ElementType::Node, e.id),
            Element::Way(e) => (ElementType::Way, e.id),
            Element::Relation(e) => (ElementType::Relation, e.id),
        }
    }
}

/// The type of an OSM element.
///
/// Can be parsed from the lowercase strings `"node"`, `"way"` and `"relation"` via
/// [`FromStr`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ElementType {
    Node,
    Way,
    Relation,
}

impl FromStr for ElementType {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "node" => Ok(ElementType::Node),
            "way" => Ok(ElementType::Way),
            "relation" => Ok(ElementType::Relation),
            _ => Err(anyhow!("Illegal element_type: {}", s)),
        }
    }
}

/// Common metadata shared by [`Node`], [`Way`] and [`Relation`].
///
/// See the [module docs](self) for the default values (`visible = true`,
/// `version = changeset_id = -1`, `timestamp`/`user` = `None`).
#[derive(Debug)]
pub struct ElementBase {
    pub id: i64,
    pub version: i32,
    pub timestamp: Option<DateTime<Utc>>,
    pub user: Option<OsmUser>,
    pub changeset_id: i64,
    pub visible: bool,
    pub tags: Vec<Tag>,
}

// `visible` defaults to true: the PBF spec states the flag "MUST be assumed to be true" when
// absent, and a derived `Default` would yield `false` for the `bool`, silently marking every
// freshly-created element as deleted on write.
impl Default for ElementBase {
    fn default() -> Self {
        Self {
            id: 0,
            version: -1,
            timestamp: None,
            user: None,
            changeset_id: -1,
            visible: true,
            tags: Vec::new(),
        }
    }
}

impl ElementBase {
    /// Creates base metadata for an element with only an id and tags (no version, timestamp
    /// or user information).
    pub fn new_with_tags(id: i64, tags: Vec<Tag>) -> Self {
        Self {
            id,
            tags,
            visible: true,
            ..Default::default()
        }
    }
}

/// A `key=value` pair attached to an element.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Tag {
    pub key: String,
    pub value: String,
}

/// An OSM node: a point with a coordinate.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Node {
    pub id: i64,
    pub version: i32,
    pub timestamp: Option<DateTime<Utc>>,
    pub user: Option<OsmUser>,
    pub changeset_id: i64,
    /// Latitude in integer **nanodegrees** (divide by 1e9 for degrees).
    pub latitude: i64,
    /// Longitude in integer **nanodegrees** (divide by 1e9 for degrees).
    pub longitude: i64,
    /// `false` marks a deleted/historical object; defaults to `true` (see module docs).
    pub visible: bool,
    pub tags: Vec<Tag>,
}

// See the comment on `ElementBase::default()`: `visible` must default to true, not to the
// derived `bool` default of false.
impl Default for Node {
    fn default() -> Self {
        Self {
            id: 0,
            version: -1,
            timestamp: None,
            user: None,
            changeset_id: -1,
            latitude: 0,
            longitude: 0,
            visible: true,
            tags: Vec::new(),
        }
    }
}

impl From<ElementBase> for Node {
    fn from(el: ElementBase) -> Self {
        Self {
            id: el.id,
            version: el.version,
            timestamp: el.timestamp,
            user: el.user,
            changeset_id: el.changeset_id,
            visible: el.visible,
            tags: el.tags,
            latitude: 0,
            longitude: 0,
        }
    }
}

/// An OSM way: an ordered list of node references ([`WayNode`]s).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Way {
    pub id: i64,
    pub version: i32,
    pub timestamp: Option<DateTime<Utc>>,
    pub user: Option<OsmUser>,
    pub changeset_id: i64,
    /// `false` marks a deleted/historical object; defaults to `true` (see module docs).
    pub visible: bool,
    pub tags: Vec<Tag>,
    /// The way's nodes in order. Coordinates are present only when the file declares the
    /// `LocationsOnWays` feature.
    pub way_nodes: Vec<WayNode>,
}

// `visible` defaults to true — see `ElementBase::default()`.
impl Default for Way {
    fn default() -> Self {
        Self {
            id: 0,
            version: -1,
            timestamp: None,
            user: None,
            changeset_id: -1,
            visible: true,
            tags: Vec::new(),
            way_nodes: Vec::new(),
        }
    }
}

impl From<ElementBase> for Way {
    fn from(el: ElementBase) -> Self {
        Self {
            id: el.id,
            version: el.version,
            timestamp: el.timestamp,
            user: el.user,
            changeset_id: el.changeset_id,
            visible: el.visible,
            tags: el.tags,
            way_nodes: Vec::new(),
        }
    }
}

/// A reference to a node within a [`Way`], optionally carrying the node's coordinates.
///
/// Coordinates are in integer **nanodegrees** and are only populated when the PBF file
/// carries node locations on ways (`LocationsOnWays` optional feature).
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct WayNode {
    /// The referenced node's id.
    pub id: i64,
    pub latitude: Option<i64>,
    pub longitude: Option<i64>,
}

impl WayNode {
    /// Creates a node reference without coordinates.
    pub fn new_without_coords(id: i64) -> Self {
        Self {
            id,
            latitude: None,
            longitude: None,
        }
    }

    /// Creates a node reference with coordinates (in integer nanodegrees).
    pub fn new(id: i64, latitude: i64, longitude: i64) -> Self {
        Self {
            id,
            latitude: Some(latitude),
            longitude: Some(longitude),
        }
    }
}

/// An OSM relation: a set of typed member references ([`RelationMember`]s).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Relation {
    pub id: i64,
    pub version: i32,
    pub timestamp: Option<DateTime<Utc>>,
    pub user: Option<OsmUser>,
    pub changeset_id: i64,
    /// `false` marks a deleted/historical object; defaults to `true` (see module docs).
    pub visible: bool,
    pub tags: Vec<Tag>,
    pub members: Vec<RelationMember>,
}

// `visible` defaults to true — see `ElementBase::default()`.
impl Default for Relation {
    fn default() -> Self {
        Self {
            id: 0,
            version: -1,
            timestamp: None,
            user: None,
            changeset_id: -1,
            visible: true,
            tags: Vec::new(),
            members: Vec::new(),
        }
    }
}

impl From<ElementBase> for Relation {
    fn from(el: ElementBase) -> Self {
        Self {
            id: el.id,
            version: el.version,
            timestamp: el.timestamp,
            user: el.user,
            changeset_id: el.changeset_id,
            visible: el.visible,
            tags: el.tags,
            members: Vec::new(),
        }
    }
}

/// A member of a [`Relation`]: a typed reference to another element plus a role.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RelationMember {
    /// The referenced element's id.
    pub member_id: i64,
    /// The referenced element's type.
    pub member_type: ElementType,
    /// The member's role within the relation (e.g. `"outer"`, `"inner"`).
    pub role: String,
}

/// Common accessors implemented by [`Node`], [`Way`] and [`Relation`].
pub trait BasicElement: Clone {
    fn get_element_type() -> ElementType;
    fn get_id(&self) -> i64;
    fn get_version(&self) -> i32;
    fn get_timestamp(&self) -> Option<DateTime<Utc>>;
    fn get_changeset_id(&self) -> i64;
    fn is_visible(&self) -> bool;
    fn get_tags(&self) -> &Vec<Tag>;
    fn get_user(&self) -> Option<&OsmUser>;
}

impl BasicElement for Node {
    fn get_element_type() -> ElementType {
        ElementType::Node
    }

    fn get_id(&self) -> i64 {
        self.id
    }

    fn get_version(&self) -> i32 {
        self.version
    }

    fn get_timestamp(&self) -> Option<DateTime<Utc>> {
        self.timestamp
    }

    fn get_changeset_id(&self) -> i64 {
        self.changeset_id
    }

    fn is_visible(&self) -> bool {
        self.visible
    }

    fn get_tags(&self) -> &Vec<Tag> {
        &self.tags
    }

    fn get_user(&self) -> Option<&OsmUser> {
        self.user.as_ref()
    }
}

impl BasicElement for Way {
    fn get_element_type() -> ElementType {
        ElementType::Way
    }

    fn get_id(&self) -> i64 {
        self.id
    }

    fn get_version(&self) -> i32 {
        self.version
    }

    fn get_timestamp(&self) -> Option<DateTime<Utc>> {
        self.timestamp
    }

    fn get_changeset_id(&self) -> i64 {
        self.changeset_id
    }

    fn is_visible(&self) -> bool {
        self.visible
    }

    fn get_tags(&self) -> &Vec<Tag> {
        &self.tags
    }

    fn get_user(&self) -> Option<&OsmUser> {
        self.user.as_ref()
    }
}

impl BasicElement for Relation {
    fn get_element_type() -> ElementType {
        ElementType::Relation
    }

    fn get_id(&self) -> i64 {
        self.id
    }

    fn get_version(&self) -> i32 {
        self.version
    }

    fn get_timestamp(&self) -> Option<DateTime<Utc>> {
        self.timestamp
    }

    fn get_changeset_id(&self) -> i64 {
        self.changeset_id
    }

    fn is_visible(&self) -> bool {
        self.visible
    }

    fn get_tags(&self) -> &Vec<Tag> {
        &self.tags
    }

    fn get_user(&self) -> Option<&OsmUser> {
        self.user.as_ref()
    }
}