pbfhogg 0.5.0

Fast OpenStreetMap PBF reader and writer for Rust. Read, write, and merge .osm.pbf files with pipelined parallel decoding.
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
//! Iterate over the dense nodes in a `PrimitiveGroup`

use super::block::{get_stringtable_key_value, str_from_stringtable};
use super::wire::{
    Cursor, PackedBoolIter, PackedInt32Iter, PackedSint32Iter, PackedSint64Iter, WireBlock,
    WireDenseInfo, WireDenseNodes,
};
use crate::error::Result;

/// An OpenStreetMap node element from a compressed array of dense nodes.
#[derive(Clone, Debug)]
pub struct DenseNode<'a> {
    block: &'a WireBlock<'static>,

    /// The node id.
    pub(crate) id: i64,
    lat: i64,
    lon: i64,
    /// Raw packed varint bytes for this node's tags (key/value pairs, no delimiter).
    tag_bytes: &'a [u8],
    info: Option<DenseNodeInfo<'a>>,
    granularity: i64,
    lat_offset: i64,
    lon_offset: i64,
}

impl<'a> DenseNode<'a> {
    /// Returns the node id.
    #[inline]
    pub fn id(&self) -> i64 {
        self.id
    }

    /// return optional metadata about the node
    #[inline]
    pub fn info(&'a self) -> Option<&'a DenseNodeInfo<'a>> {
        self.info.as_ref()
    }

    /// Returns the latitude coordinate in nanodegrees (10^-9).
    #[inline]
    pub fn nano_lat(&self) -> i64 {
        self.lat_offset + self.granularity * self.lat
    }

    /// Returns the longitude in nanodegrees (10^-9).
    #[inline]
    pub fn nano_lon(&self) -> i64 {
        self.lon_offset + self.granularity * self.lon
    }

    crate::impl_coordinate_conversions!();

    /// Returns an iterator over the tags of this node.
    pub fn tags(&self) -> DenseTagIter<'a> {
        DenseTagIter {
            block: self.block,
            cursor: Cursor::new(self.tag_bytes),
        }
    }

    /// Returns an iterator over the tags of this node as raw index pairs.
    pub fn raw_tags(&self) -> DenseRawTagIter<'a> {
        DenseRawTagIter {
            cursor: Cursor::new(self.tag_bytes),
        }
    }
}

/// An iterator over dense nodes. It decodes the delta encoded values.
pub struct DenseNodeIter<'a> {
    block: &'a WireBlock<'static>,
    dids: PackedSint64Iter<'a>,
    cid: i64,
    dlats: PackedSint64Iter<'a>,
    clat: i64,
    dlons: PackedSint64Iter<'a>,
    clon: i64,
    /// Cursor over the raw packed int32 bytes of keys_vals.
    /// We scan forward through varints to find 0 delimiters between nodes' tags.
    kv_data: &'a [u8],
    kv_pos: usize,
    info_iter: Option<DenseNodeInfoIter<'a>>,
    granularity: i64,
    lat_offset: i64,
    lon_offset: i64,
}

impl<'a> DenseNodeIter<'a> {
    pub(crate) fn new(
        block: &'a WireBlock<'static>,
        dense: WireDenseNodes<'a>,
    ) -> DenseNodeIter<'a> {
        let info_iter = dense
            .info_data
            .and_then(|data| WireDenseInfo::parse(data).ok())
            .map(|info| DenseNodeInfoIter::new(block, info));
        DenseNodeIter {
            block,
            dids: PackedSint64Iter::new(dense.id_data),
            cid: 0,
            dlats: PackedSint64Iter::new(dense.lat_data),
            clat: 0,
            dlons: PackedSint64Iter::new(dense.lon_data),
            clon: 0,
            kv_data: dense.keys_vals_data,
            kv_pos: 0,
            info_iter,
            granularity: i64::from(block.granularity),
            lat_offset: block.lat_offset,
            lon_offset: block.lon_offset,
        }
    }

    pub(crate) fn new_skip_metadata(
        block: &'a WireBlock<'static>,
        dense: WireDenseNodes<'a>,
    ) -> DenseNodeIter<'a> {
        DenseNodeIter {
            block,
            dids: PackedSint64Iter::new(dense.id_data),
            cid: 0,
            dlats: PackedSint64Iter::new(dense.lat_data),
            clat: 0,
            dlons: PackedSint64Iter::new(dense.lon_data),
            clon: 0,
            kv_data: dense.keys_vals_data,
            kv_pos: 0,
            info_iter: None,
            granularity: i64::from(block.granularity),
            lat_offset: block.lat_offset,
            lon_offset: block.lon_offset,
        }
    }

    pub(crate) fn empty(block: &'a WireBlock<'static>) -> DenseNodeIter<'a> {
        DenseNodeIter {
            block,
            dids: PackedSint64Iter::empty(),
            cid: 0,
            dlats: PackedSint64Iter::empty(),
            clat: 0,
            dlons: PackedSint64Iter::empty(),
            clon: 0,
            kv_data: &[],
            kv_pos: 0,
            info_iter: None,
            granularity: i64::from(block.granularity),
            lat_offset: block.lat_offset,
            lon_offset: block.lon_offset,
        }
    }
}

impl<'a> Iterator for DenseNodeIter<'a> {
    type Item = DenseNode<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match (
            self.dids.next(),
            self.dlats.next(),
            self.dlons.next(),
            self.info_iter.as_mut().and_then(Iterator::next),
        ) {
            (Some(did), Some(dlat), Some(dlon), info) => {
                self.cid += did;
                self.clat += dlat;
                self.clon += dlon;

                // Scan through packed int32 varints to find the 0 delimiter
                // that separates this node's tags from the next node's.
                //
                // PBF dense node tag encoding:
                //   The `keys_vals` array stores tags for ALL dense nodes in a block
                //   as a flat interleaved sequence of varints:
                //     [k1, v1, k2, v2, ..., 0, k1, v1, 0, 0, ...]
                //   Each node's tags are a run of (key_index, val_index) pairs,
                //   terminated by a single 0 varint delimiter.
                let tag_start = self.kv_pos;
                let mut cursor = Cursor::new(&self.kv_data[self.kv_pos..]);

                // Scan forward, decoding varints until we hit a 0 key.
                while !cursor.is_empty() {
                    let before = cursor.remaining();
                    match cursor.read_varint() {
                        Ok(0) => {
                            // 0 delimiter found. Record position past the delimiter.
                            let consumed = before - cursor.remaining();
                            self.kv_pos += consumed;
                            break;
                        }
                        Ok(_key) => {
                            // Skip the corresponding value varint
                            if cursor.read_varint().is_err() {
                                break;
                            }
                            let consumed = before - cursor.remaining();
                            self.kv_pos += consumed;
                        }
                        Err(_) => break,
                    }
                }

                // tag_bytes covers the key-value pairs for this node (before the 0 delimiter).
                // The kv_pos now points past the 0 delimiter for the next node.
                let tag_end = self.kv_pos.saturating_sub(if tag_start < self.kv_pos {
                    // Subtract the size of the 0 delimiter varint (always 1 byte: 0x00)
                    1
                } else {
                    0
                });
                let tag_bytes = &self.kv_data[tag_start..tag_end.min(self.kv_data.len())];

                Some(DenseNode {
                    block: self.block,
                    id: self.cid,
                    lat: self.clat,
                    lon: self.clon,
                    tag_bytes,
                    info,
                    granularity: self.granularity,
                    lat_offset: self.lat_offset,
                    lon_offset: self.lon_offset,
                })
            }
            _ => None,
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.dids.size_hint()
    }
}

/// Optional metadata with non-geographic information about a dense node.
///
/// **Osmosis sentinel values:** Osmosis writes -1 for version and changeset
/// when metadata is absent. These fields use plain `i32`/`i64` (not `Option`)
/// because they are decoded from packed arrays in the dense node hot path -
/// changing to `Option` would add overhead to the tightest loop in the library.
/// The -1 sentinel is instead normalized at write/conversion boundaries
/// (see `dense_node_metadata`, `dense_node_raw_metadata`, sort's
/// `read_dense_node`, and `stream_merge::convert_node`). Non-dense elements
/// normalize -1 → None directly in `WireInfo::parse`. See CORRECTNESS.md.
#[derive(Clone, Debug)]
pub struct DenseNodeInfo<'a> {
    block: &'a WireBlock<'static>,
    version: i32,
    timestamp: i64,
    changeset: i64,
    uid: i32,
    user_sid: i32,
    visible: bool,
    date_granularity: i64,
}

impl<'a> DenseNodeInfo<'a> {
    /// Returns the version of this element.
    #[inline]
    pub fn version(&self) -> i32 {
        self.version
    }

    /// Returns the changeset id.
    #[inline]
    pub fn changeset(&self) -> i64 {
        self.changeset
    }

    /// Returns the user id.
    #[inline]
    pub fn uid(&self) -> i32 {
        self.uid
    }

    /// Returns the raw string table index for the user name.
    #[inline]
    pub fn raw_user_sid(&self) -> i32 {
        self.user_sid
    }

    /// Returns the user name.
    #[allow(clippy::cast_sign_loss)]
    pub fn user(&self) -> Result<&'a str> {
        if self.user_sid < 0 {
            return Err(crate::error::new_error(
                crate::error::ErrorKind::StringtableIndexOutOfBounds { index: 0 },
            ));
        }
        str_from_stringtable(self.block, self.user_sid as usize)
    }

    /// Returns the time stamp in milliseconds since the epoch.
    #[inline]
    pub fn milli_timestamp(&self) -> i64 {
        self.timestamp * self.date_granularity
    }

    /// Returns the visibility status of an element.
    // wontfix(name-is-has-bool): inherited from osmpbf public API
    #[inline]
    pub fn visible(&self) -> bool {
        self.visible
    }

    /// Returns true if the element was deleted.
    // wontfix(name-is-has-bool): inherited from osmpbf public API
    #[inline]
    pub fn deleted(&self) -> bool {
        !self.visible
    }
}

/// An iterator over dense nodes info. It decodes the delta encoded values.
pub struct DenseNodeInfoIter<'a> {
    block: &'a WireBlock<'static>,
    versions: PackedInt32Iter<'a>,
    dtimestamps: PackedSint64Iter<'a>,
    ctimestamp: i64,
    dchangesets: PackedSint64Iter<'a>,
    cchangeset: i64,
    duids: PackedSint32Iter<'a>,
    cuid: i32,
    duser_sids: PackedSint32Iter<'a>,
    cuser_sid: i32,
    visible: PackedBoolIter<'a>,
    date_granularity: i64,
}

impl<'a> DenseNodeInfoIter<'a> {
    fn new(block: &'a WireBlock<'static>, info: WireDenseInfo<'a>) -> DenseNodeInfoIter<'a> {
        DenseNodeInfoIter {
            block,
            versions: PackedInt32Iter::new(info.version_data),
            dtimestamps: PackedSint64Iter::new(info.timestamp_data),
            ctimestamp: 0,
            dchangesets: PackedSint64Iter::new(info.changeset_data),
            cchangeset: 0,
            duids: PackedSint32Iter::new(info.uid_data),
            cuid: 0,
            duser_sids: PackedSint32Iter::new(info.user_sid_data),
            cuser_sid: 0,
            visible: PackedBoolIter::new(info.visible_data),
            date_granularity: i64::from(block.date_granularity),
        }
    }
}

impl<'a> Iterator for DenseNodeInfoIter<'a> {
    type Item = DenseNodeInfo<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let version = self.versions.next()?;
        let dtimestamp = self.dtimestamps.next()?;
        let dchangeset = self.dchangesets.next()?;
        let duid = self.duids.next()?;
        let duser_sid = self.duser_sids.next()?;
        let visible_opt = self.visible.next();

        self.ctimestamp += dtimestamp;
        self.cchangeset += dchangeset;
        self.cuid += duid;
        self.cuser_sid += duser_sid;

        Some(DenseNodeInfo {
            block: self.block,
            version,
            timestamp: self.ctimestamp,
            changeset: self.cchangeset,
            uid: self.cuid,
            user_sid: self.cuser_sid,
            visible: visible_opt.unwrap_or(true),
            date_granularity: self.date_granularity,
        })
    }
}

/// An iterator over the tags in a dense node.
#[derive(Clone)]
pub struct DenseTagIter<'a> {
    block: &'a WireBlock<'static>,
    cursor: Cursor<'a>,
}

#[allow(clippy::cast_possible_truncation)]
impl<'a> Iterator for DenseTagIter<'a> {
    type Item = (&'a str, &'a str);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.cursor.is_empty() {
            return None;
        }
        // wontfix(type-result-fallible): Iterator trait constrains return type;
        // .ok() stops iteration on corrupt data rather than propagating errors.
        let key = self.cursor.read_varint().ok()? as usize;
        let val = self.cursor.read_varint().ok()? as usize;
        get_stringtable_key_value(self.block, Some(key), Some(val))
    }
}

/// An iterator over the tags of a dense node as raw index pairs.
#[derive(Clone)]
pub struct DenseRawTagIter<'a> {
    cursor: Cursor<'a>,
}

impl Iterator for DenseRawTagIter<'_> {
    type Item = (i32, i32);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.cursor.is_empty() {
            return None;
        }
        // wontfix(type-result-fallible): same as DenseTagIter - Iterator constrains return type
        #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
        let key = self.cursor.read_varint().ok()? as i32;
        #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
        let val = self.cursor.read_varint().ok()? as i32;
        Some((key, val))
    }
}