pbfhogg 0.3.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! CLI-driven invariant tests for `pbfhogg apply-changes`.
//!
//! Replaces `tests/apply_changes_invariants.rs` (split out of
//! priority 1; sibling to `cli_apply_changes.rs`). Pins specific
//! correctness invariants that the descriptor-first streaming
//! rewrite documented in `notes/apply-changes-opportunities.md`
//! must preserve byte-for-byte. These tests MUST pass on current
//! main before the rewrite starts and must still pass after.
//!
//! Each test pins down a specific invariant from the plan doc's
//! "Correctness invariants" section:
//!
//! - `cursor_rule_*`: Rewrite slots advance UpsertCursors past their blob's ID range; Passthrough/FalsePositive slots do NOT.
//! - `empty_base_pbf_*`: `last_type == None` forever; trailing creates must flush all three kinds.
//! - `trailing_creates_after_*`: Type-transition flush correctness.
//! - `modify_on_missing_id_*`, `delete_on_missing_id_*`, `create_on_existing_id_*`: Permissive missing-element semantics (reference/osmium-parity.md).
//! - `merge_jobs_parity_*`: Output is independent of worker count between `-j 2` and `-j 4`.
//!
//! No imports from `pbfhogg::apply_changes::*` or `pbfhogg::altw::*`.
//! The parity tests bootstrap their LocationsOnWays base via
//! `pbfhogg add-locations-to-ways` (CLI), and the merge runs are
//! `pbfhogg apply-changes` (CLI). A rewrite of either command cannot
//! break these tests by type changes alone.

#![allow(clippy::unwrap_used)]
#![allow(clippy::too_many_lines)]

mod common;

use std::fs::File;
use std::io::Write;
use std::path::Path;

use common::cli::{CliInvoker, CliOutput};
use common::{
    assert_elements_equivalent, generate_nodes, generate_ways,
    read_all_elements_with_coords as read_all_elements, write_multi_block_test_pbf,
    write_test_pbf_sorted, TestNode, TestWay,
};
use flate2::write::GzEncoder;
use tempfile::TempDir;

fn write_osc(path: &Path, xml: &str) {
    let file = File::create(path).expect("create osc file");
    let mut enc = GzEncoder::new(file, flate2::Compression::fast());
    enc.write_all(xml.as_bytes()).expect("write xml");
    enc.finish().expect("finish gz");
}

fn run_apply_changes(base: &Path, osc: &Path, output: &Path, jobs: Option<usize>, locations_on_ways: bool) -> CliOutput {
    let mut cli = CliInvoker::new()
        .arg("apply-changes")
        .arg(base)
        .arg(osc)
        .arg("-o")
        .arg(output);
    if locations_on_ways {
        cli = cli.arg("--locations-on-ways");
    }
    if let Some(j) = jobs {
        cli = cli.arg("-j").arg(j.to_string());
    }
    cli.arg("--force").assert_success()
}

fn run_apply_simple(base: &Path, osc: &Path, output: &Path) -> CliOutput {
    run_apply_changes(base, osc, output, None, false)
}

/// Bootstrap a LocationsOnWays-enriched base PBF via the
/// `add-locations-to-ways` CLI.
fn bootstrap_low_base(input: &Path, output: &Path) {
    CliInvoker::new()
        .arg("add-locations-to-ways")
        .arg(input)
        .arg("-o")
        .arg(output)
        .arg("--keep-untagged-nodes")
        .arg("--force")
        .assert_success();
}

#[allow(clippy::cast_possible_wrap)]
fn write_merge_jobs_fixture(base: &Path, osc: &Path) {
    let mut nodes = generate_nodes(24, 1);
    for (i, node) in nodes.iter_mut().enumerate() {
        if i % 4 == 0 {
            node.tags = vec![("name", "base")];
        }
    }

    let mut ways = generate_ways(10, 1_000, 3, 1);
    for (i, way) in ways.iter_mut().enumerate() {
        let start = 1 + i as i64 * 2;
        way.refs = vec![start, start + 1, start + 2];
        way.tags = if i % 2 == 0 {
            vec![("highway", "residential")]
        } else {
            vec![("highway", "service")]
        };
    }

    write_multi_block_test_pbf(base, &nodes, &ways, &[], 4);

    write_osc(
        osc,
        r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <node id="30" lat="0.3000000" lon="0.6000000" version="1">
      <tag k="created" v="yes"/>
    </node>
    <way id="2000" version="1">
      <nd ref="5"/>
      <nd ref="30"/>
      <nd ref="6"/>
      <tag k="highway" v="primary"/>
    </way>
  </create>
  <modify>
    <node id="5" lat="0.5555555" lon="0.4444444" version="2">
      <tag k="name" v="modified"/>
    </node>
    <way id="1003" version="2">
      <nd ref="7"/>
      <nd ref="5"/>
      <nd ref="30"/>
      <tag k="highway" v="secondary"/>
      <tag k="surface" v="gravel"/>
    </way>
  </modify>
  <delete>
    <node id="23" version="1"/>
    <way id="1007" version="1"/>
  </delete>
</osmChange>"#,
    );
}

/// Pull just the "Wrote/Base/Diff/Deleted/Blobs" lines out of a
/// stats summary stderr block. The Blobs line includes total-blob
/// count which is jobs-independent (both runs decode the same
/// number of blobs); the byte-rewrite-ratio and blob-size lines
/// are deliberately *not* compared - they vary with worker
/// scheduling on multi-blob inputs.
fn stats_block(stderr: &str) -> Vec<&str> {
    stderr
        .lines()
        .filter(|l| {
            l.starts_with("Merge complete:")
                || l.starts_with("  Base:")
                || l.starts_with("  Diff:")
                || l.starts_with("  Deleted:")
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Cursor rule: Passthrough / FalsePositive slots must NOT advance the cursor.
// ---------------------------------------------------------------------------

#[test]
fn cursor_rule_false_positive_blob_emits_create_after() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[
            TestNode { id: 1, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None },
            TestNode { id: 2, lat: 200_000_000, lon: 200_000_000, tags: vec![], meta: None },
            TestNode { id: 10, lat: 300_000_000, lon: 300_000_000, tags: vec![], meta: None },
        ],
        &[TestWay { id: 100, refs: vec![1, 2, 10], tags: vec![], meta: None }],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <node id="5" lat="40.0" lon="40.0" version="1"/>
  </create>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    let node_ids: Vec<i64> = c.nodes.iter().map(|n| n.0).collect();
    assert!(
        node_ids.contains(&5),
        "FalsePositive cursor rule: id=5 must be present. Got {node_ids:?}",
    );
    assert_eq!(
        node_ids,
        vec![1, 2, 10, 5],
        "FalsePositive blob is passed through verbatim, then id=5 emitted \
         on the type transition Node -> Way (blob-tail order, not OSM-sorted)."
    );

    assert_eq!(c.ways.iter().map(|w| w.0).collect::<Vec<_>>(), vec![100]);
}

#[test]
fn cursor_rule_false_positive_blob_emits_create_at_tail() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[
            TestNode { id: 1, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None },
            TestNode { id: 10, lat: 300_000_000, lon: 300_000_000, tags: vec![], meta: None },
        ],
        &[],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <node id="5" lat="40.0" lon="40.0" version="1"/>
  </create>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    let node_ids: Vec<i64> = c.nodes.iter().map(|n| n.0).collect();
    assert!(node_ids.contains(&5), "id=5 must be present (got {node_ids:?})");
    assert_eq!(
        node_ids,
        vec![1, 10, 5],
        "Trailing-create after FalsePositive blob: blob-tail order [1, 10, 5]"
    );
}

// ---------------------------------------------------------------------------
// jobs parity: output is independent of worker count.
// ---------------------------------------------------------------------------

#[test]
fn merge_jobs_parity_on_multiblob_input() {
    let dir = TempDir::new().expect("tempdir");
    let base_raw = dir.path().join("base_raw.osm.pbf");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let out_seq = dir.path().join("out_seq.osm.pbf");
    let out_par = dir.path().join("out_par.osm.pbf");

    write_merge_jobs_fixture(&base_raw, &osc);
    bootstrap_low_base(&base_raw, &base);

    // Parity baseline is jobs=2 (T01: jobs=1 rejected up front).
    let seq = run_apply_changes(&base, &osc, &out_seq, Some(2), true);
    let par = run_apply_changes(&base, &osc, &out_par, Some(4), true);

    // Stats parity: every counter that's part of the standard summary
    // block must match between worker counts.
    assert_eq!(
        stats_block(&seq.stderr_str()),
        stats_block(&par.stderr_str()),
        "stats summary diverges between -j 2 and -j 4",
    );

    assert_elements_equivalent(&out_seq, &out_par);
}

#[test]
fn merge_jobs_parity_without_locations_on_ways() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let out_seq = dir.path().join("out_seq.osm.pbf");
    let out_par = dir.path().join("out_par.osm.pbf");

    write_merge_jobs_fixture(&base, &osc);

    let seq = run_apply_changes(&base, &osc, &out_seq, Some(2), false);
    let par = run_apply_changes(&base, &osc, &out_par, Some(4), false);

    assert_eq!(
        stats_block(&seq.stderr_str()),
        stats_block(&par.stderr_str()),
        "stats summary diverges between -j 2 and -j 4",
    );

    assert_elements_equivalent(&out_seq, &out_par);
}

// ---------------------------------------------------------------------------
// Empty base PBF: last_type stays None; trailing creates flush all three.
// ---------------------------------------------------------------------------

#[test]
fn empty_base_pbf_flushes_all_three_kinds() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(&base, &[], &[], &[]);

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <node id="1" lat="10.0" lon="10.0" version="1"/>
    <node id="2" lat="20.0" lon="20.0" version="1"/>
    <way id="100" version="1">
      <nd ref="1"/>
      <nd ref="2"/>
    </way>
    <relation id="1000" version="1">
      <member type="way" ref="100" role="outer"/>
    </relation>
  </create>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    assert_eq!(c.nodes.iter().map(|n| n.0).collect::<Vec<_>>(), vec![1, 2]);
    assert_eq!(c.ways.iter().map(|w| w.0).collect::<Vec<_>>(), vec![100]);
    assert_eq!(c.relations.iter().map(|r| r.0).collect::<Vec<_>>(), vec![1000]);
}

#[test]
fn empty_base_pbf_noop_on_empty_diff() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(&base, &[], &[], &[]);
    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    assert!(c.nodes.is_empty());
    assert!(c.ways.is_empty());
    assert!(c.relations.is_empty());
}

// ---------------------------------------------------------------------------
// Trailing creates interleave correctly across kinds.
// ---------------------------------------------------------------------------

#[test]
fn trailing_creates_after_node_blob_flush_way_and_relation() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[TestNode { id: 1, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None }],
        &[],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <node id="2" lat="20.0" lon="20.0" version="1"/>
    <way id="100" version="1">
      <nd ref="1"/>
      <nd ref="2"/>
    </way>
    <relation id="1000" version="1">
      <member type="way" ref="100" role="outer"/>
    </relation>
  </create>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    assert_eq!(c.nodes.iter().map(|n| n.0).collect::<Vec<_>>(), vec![1, 2]);
    assert_eq!(c.ways.iter().map(|w| w.0).collect::<Vec<_>>(), vec![100]);
    assert_eq!(c.relations.iter().map(|r| r.0).collect::<Vec<_>>(), vec![1000]);
}

#[test]
fn trailing_creates_after_way_blob_flush_relation_only() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[TestNode { id: 1, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None }],
        &[TestWay { id: 10, refs: vec![1], tags: vec![], meta: None }],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <relation id="500" version="1">
      <member type="way" ref="10" role="outer"/>
    </relation>
  </create>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    assert_eq!(c.nodes.iter().map(|n| n.0).collect::<Vec<_>>(), vec![1]);
    assert_eq!(c.ways.iter().map(|w| w.0).collect::<Vec<_>>(), vec![10]);
    assert_eq!(c.relations.iter().map(|r| r.0).collect::<Vec<_>>(), vec![500]);
}

// ---------------------------------------------------------------------------
// Permissive missing-element semantics (reference/osmium-parity.md):
// modify on absent ID silently inserts; delete on absent ID is a no-op;
// create on existing ID silently overwrites.
// ---------------------------------------------------------------------------

#[test]
fn modify_on_missing_id_silently_inserts() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[TestNode { id: 1, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None }],
        &[],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <modify>
    <node id="42" lat="55.0" lon="12.0" version="3"/>
  </modify>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);
    let node_ids: Vec<i64> = c.nodes.iter().map(|n| n.0).collect();
    assert_eq!(
        node_ids,
        vec![1, 42],
        "modify on absent ID is treated as an insert (reference/osmium-parity.md)",
    );
}

#[test]
fn delete_on_missing_id_is_noop() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[TestNode { id: 1, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None }],
        &[],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <delete>
    <node id="42" version="1"/>
  </delete>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    assert_eq!(c.nodes.iter().map(|n| n.0).collect::<Vec<_>>(), vec![1]);
    assert!(c.ways.is_empty());
    assert!(c.relations.is_empty());
}

#[test]
fn create_on_existing_id_overwrites_base() {
    let dir = TempDir::new().expect("tempdir");
    let base = dir.path().join("base.osm.pbf");
    let osc = dir.path().join("diff.osc.gz");
    let output = dir.path().join("output.osm.pbf");

    write_test_pbf_sorted(
        &base,
        &[TestNode { id: 42, lat: 100_000_000, lon: 100_000_000, tags: vec![], meta: None }],
        &[],
        &[],
    );

    write_osc(&osc, r#"<?xml version="1.0" encoding="UTF-8"?>
<osmChange version="0.6">
  <create>
    <node id="42" lat="20.0" lon="20.0" version="2"/>
  </create>
</osmChange>"#);

    run_apply_simple(&base, &osc, &output);
    let c = read_all_elements(&output);

    assert_eq!(c.nodes.len(), 1, "create on existing ID must not duplicate");
    let n = &c.nodes[0];
    assert_eq!(n.0, 42);
    // OSC record wins: lat=20.0deg = 200_000_000 decimicrodegrees.
    assert_eq!((n.1, n.2), (200_000_000, 200_000_000));
}