osmflow 0.15.0

Fast, parallel reading of OpenStreetMap PBF files, and node location stores, from a city extract to the planet
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
//! Ways as geometries: node locations loaded into a store, and looked up for each way.

use std::collections::HashMap;
use std::iter;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};

use geo::Validation as _;
use geo_types::{Geometry, LineString, MultiPolygon};
use osmflow::nodes::{MemoryStore, NodeRead as _, NodeStore as _};
use osmflow::{AreaRules, GeometryError, Location, Locator, PbfReader, Select};

fn fixture(name: &str) -> PbfReader {
    PbfReader::open(
        Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("fixtures")
            .join(name),
    )
    .unwrap()
}

/// The file's node locations in 1e-7 degrees, by ID.
fn locations(file: &PbfReader) -> HashMap<i64, (i32, i32)> {
    let mut out = HashMap::new();
    for block in file.blocks(Select::node_locations()).unwrap() {
        for n in block.unwrap().nodes() {
            out.insert(n.id(), (n.decimicro_lat(), n.decimicro_lon()));
        }
    }
    out
}

fn loaded(file: &PbfReader) -> MemoryStore {
    let mut store = MemoryStore::new();
    let count = file.load_nodes(&mut store).unwrap();
    assert_eq!(count, store.len() as u64);
    store
}

#[test]
fn ways_get_their_nodes_locations() {
    for name in [
        "planetiler/monaco-latest.osm.pbf",
        "go-osm/monaco-20150428.osm.pbf",
        "r5/missing-nodes.pbf",
    ] {
        let file = fixture(name);
        let expected = locations(&file);
        let store = loaded(&file);
        assert_eq!(
            store.len(),
            expected.keys().filter(|&&id| id >= 0).count(),
            "{name}"
        );
        let mut locator = Locator::new(store.reader());
        let (mut lines, mut areas) = (0, 0);
        for block in file.blocks(Select::all()).unwrap() {
            for way in block.unwrap().ways() {
                let refs: Vec<i64> = way.refs().collect();
                match locator.line_string(&way) {
                    Ok(line) => {
                        lines += 1;
                        assert_eq!(line.0.len(), refs.len());
                        for (c, id) in line.coords().zip(&refs) {
                            let (lat, lon) = expected[id];
                            let loc = Location::new(lat, lon);
                            assert_eq!(
                                (c.y, c.x),
                                (loc.lat_degrees(), loc.lon_degrees()),
                                "{name} {id}"
                            );
                        }
                        let closed = refs.len() >= 4 && refs.first() == refs.last();
                        match locator.polygon(&way) {
                            Ok(_) => areas += 1,
                            Err(e) => assert!(
                                !closed && e == GeometryError::NotClosed,
                                "{name} way {}",
                                way.id()
                            ),
                        }
                    }
                    Err(GeometryError::MissingNode(id)) => {
                        assert!(!expected.contains_key(&id), "{name} {id}");
                    }
                    Err(e) => assert!(
                        refs.len() < 2 && e == GeometryError::TooFewNodes,
                        "{name} {e}"
                    ),
                }
            }
        }
        assert!(lines > 0, "{name}");
        if name.starts_with("planetiler") {
            assert!(areas > 0);
        }
    }
}

#[test]
fn a_way_leaving_the_file_names_its_missing_node() {
    let file = fixture("r5/missing-nodes.pbf");
    let store = loaded(&file);
    let mut locator = Locator::new(store.reader());
    let missing = file
        .blocks(Select::all())
        .unwrap()
        .flat_map(|b| {
            let block = b.unwrap();
            block
                .ways()
                .map(|w| locator.line_string(&w).err())
                .collect::<Vec<_>>()
        })
        .flatten()
        .collect::<Vec<_>>();
    assert!(
        matches!(missing.as_slice(), [GeometryError::MissingNode(_), ..]),
        "{missing:?}"
    );
}

#[test]
fn locations_on_ways_need_no_store() {
    let file = fixture("osmpbf/loc_on_ways.osm.pbf");
    let empty = MemoryStore::new();
    let mut locator = Locator::new(empty.reader());
    let mut lines = 0;
    for block in file.blocks(Select::all()).unwrap() {
        for way in block.unwrap().ways() {
            let line = locator.line_string(&way).unwrap();
            let expected: Vec<_> = way.locations().unwrap().collect();
            assert_eq!(line.0.len(), expected.len());
            lines += 1;
        }
    }
    assert!(lines > 0);
}

#[test]
fn par_resolve_gives_each_thread_a_lookup() {
    let file = fixture("planetiler/monaco-latest.osm.pbf");
    let store = loaded(&file);
    let mut locator = Locator::new(store.reader());
    let mut expected = 0;
    for block in file.blocks(Select::all()).unwrap() {
        expected += block
            .unwrap()
            .ways()
            .filter(|w| locator.line_string(w).is_ok())
            .count();
    }
    let lines = AtomicUsize::new(0);
    file.par_resolve(Select::all(), &store, |block, locator| {
        let n = block
            .ways()
            .filter(|w| locator.line_string(w).is_ok())
            .count();
        lines.fetch_add(n, Ordering::Relaxed);
        Ok(())
    })
    .unwrap();
    assert_eq!(lines.into_inner(), expected);
}

#[test]
fn closed_ways_are_areas_by_their_tags() {
    let file = fixture("planetiler/monaco-latest.osm.pbf");
    let store = loaded(&file);
    let mut locator = Locator::new(store.reader());
    let (rules, none) = (AreaRules::default(), AreaRules::none());
    let (mut areas, mut closed_lines, mut open) = (0, 0, 0);
    for block in file.blocks(Select::all()).unwrap() {
        let block = block.unwrap();
        for way in block.ways() {
            let refs: Vec<i64> = way.refs().collect();
            let closed = refs.len() >= 4 && refs.first() == refs.last();
            match locator.area_or_line(&way, &rules) {
                Ok(Geometry::Polygon(_)) => {
                    assert!(closed && rules.is_area(way.tags()), "way {}", way.id());
                    areas += 1;
                }
                Ok(Geometry::LineString(_)) if closed => {
                    assert!(!rules.is_area(way.tags()), "way {}", way.id());
                    closed_lines += 1;
                }
                Ok(Geometry::LineString(_)) => open += 1,
                Ok(other) => panic!("way {}: {other:?}", way.id()),
                Err(e) => assert!(matches!(e, GeometryError::MissingNode(_)), "{e}"),
            }
            // With no rules, only `area=yes` makes an area.
            if let Ok(Geometry::Polygon(_)) = locator.area_or_line(&way, &none) {
                assert!(way.tags().any(|t| t == ("area", "yes")), "way {}", way.id());
            }
        }
    }
    assert!(
        areas > 1000 && closed_lines > 0 && open > 1000,
        "{areas} {closed_lines} {open}"
    );
}

/// A degree in 1e-7 degrees, the unit of a [`Location`].
const DEGREE: i32 = 10_000_000;

/// The area of ways through these (x, y) points, in multiples of `unit` 1e-7 degrees; points at the same place are the
/// same node.
fn multi_polygon_in(unit: i32, ways: &[&[(i32, i32)]]) -> Result<MultiPolygon<f64>, GeometryError> {
    let mut store = MemoryStore::new();
    let mut ids = HashMap::new();
    let ways: Vec<Vec<i64>> = ways
        .iter()
        .map(|way| {
            way.iter()
                .map(|&(x, y)| {
                    let next = ids.len() as u64 + 1;
                    *ids.entry((x, y)).or_insert_with(|| {
                        store.set(next, Location::new(y * unit, x * unit)).unwrap();
                        i64::try_from(next).unwrap()
                    })
                })
                .collect()
        })
        .collect();
    Locator::new(store.reader()).multi_polygon(ways.iter().map(Vec::as_slice))
}

/// The polygons of the area, which must be valid, as rings of (x, y) in multiples of `unit` 1e-7 degrees, outer ring
/// first; each ring starts at its smallest point, keeping its direction, and the polygons are in the order of their
/// first points.
fn area_in(unit: i32, ways: &[&[(i32, i32)]]) -> Vec<Vec<Vec<(i32, i32)>>> {
    let area = multi_polygon_in(unit, ways).unwrap();
    assert!(area.is_valid(), "{area:?}");
    let ring = |r: &LineString<f64>| {
        let mut points: Vec<(i32, i32)> = r
            .coords()
            .map(|c| {
                let loc = Location::from_degrees(c.y, c.x);
                (loc.lon / unit, loc.lat / unit)
            })
            .collect();
        points.pop();
        let first = (0..points.len()).min_by_key(|&i| points[i]).unwrap();
        points.rotate_left(first);
        points.push(points[0]);
        points
    };
    let mut polygons: Vec<Vec<Vec<(i32, i32)>>> = area
        .iter()
        .map(|p| {
            iter::once(ring(p.exterior()))
                .chain(p.interiors().iter().map(ring))
                .collect()
        })
        .collect();
    polygons.sort();
    polygons
}

/// [`area_in`] whole degrees.
fn area(ways: &[&[(i32, i32)]]) -> Vec<Vec<Vec<(i32, i32)>>> {
    area_in(DEGREE, ways)
}

#[test]
fn split_and_reversed_ways_join_into_an_outer_ring_with_a_hole() {
    let area = area(&[
        &[(10, 10), (10, 0), (0, 0)],
        &[(4, 4), (4, 6), (6, 6), (6, 4), (4, 4)],
        &[(10, 10), (0, 10), (0, 0)],
    ]);
    assert_eq!(
        area,
        [[
            vec![(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)],
            vec![(4, 4), (4, 6), (6, 6), (6, 4), (4, 4)],
        ]]
    );
}

#[test]
fn an_island_in_a_hole_is_a_polygon_of_its_own() {
    let area = area(&[
        &[(4, 4), (6, 4), (6, 6), (4, 6), (4, 4)],
        &[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)],
        &[(2, 2), (2, 8), (8, 8), (8, 2), (2, 2)],
    ]);
    assert_eq!(
        area,
        [
            vec![
                vec![(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)],
                vec![(2, 2), (2, 8), (8, 8), (8, 2), (2, 2)],
            ],
            vec![vec![(4, 4), (6, 4), (6, 6), (4, 6), (4, 4)]],
        ]
    );
}

#[test]
fn rings_touching_at_a_node_are_two_polygons() {
    // two squares that share (5, 5), each of two ways: four way ends meet there
    let area = area(&[
        &[(0, 0), (5, 0), (5, 5)],
        &[(5, 5), (10, 5), (10, 10)],
        &[(10, 10), (5, 10), (5, 5)],
        &[(5, 5), (0, 5), (0, 0)],
    ]);
    assert_eq!(
        area,
        [
            [[(0, 0), (5, 0), (5, 5), (0, 5), (0, 0)]],
            [[(5, 5), (10, 5), (10, 10), (5, 10), (5, 5)]],
        ]
    );
}

#[test]
fn a_hole_touching_its_outer_ring_at_a_node_stays_a_hole() {
    // a triangle hole with a corner at (10, 10), a corner of the outer ring, both split into two ways there
    let area = area(&[
        &[(0, 0), (10, 0), (10, 10)],
        &[(10, 10), (8, 5), (5, 8)],
        &[(5, 8), (10, 10)],
        &[(10, 10), (0, 10), (0, 0)],
    ]);
    assert_eq!(
        area,
        [[
            vec![(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)],
            vec![(5, 8), (10, 10), (8, 5), (5, 8)],
        ]]
    );
}

#[test]
fn an_edge_two_rings_share_cancels_out() {
    // a hole on the outer ring's right edge, from (10, 3) to (10, 7), notches the outer ring
    let area = area(&[
        &[(0, 0), (10, 0), (10, 3), (10, 7), (10, 10), (0, 10), (0, 0)],
        &[(10, 3), (10, 7), (6, 7), (6, 3), (10, 3)],
    ]);
    assert_eq!(
        area,
        [[[
            (0, 0),
            (10, 0),
            (10, 3),
            (6, 3),
            (6, 7),
            (10, 7),
            (10, 10),
            (0, 10),
            (0, 0)
        ]]]
    );
}

#[test]
fn outer_rings_that_overlap_leave_the_overlap_out() {
    let area = area(&[
        &[(0, 0), (6, 0), (6, 6), (0, 6), (0, 0)],
        &[(3, 3), (9, 3), (9, 9), (3, 9), (3, 3)],
    ]);
    assert_eq!(
        area,
        [
            [[(0, 0), (6, 0), (6, 3), (3, 3), (3, 6), (0, 6), (0, 0)]],
            [[(3, 6), (6, 6), (6, 3), (9, 3), (9, 9), (3, 9), (3, 6)]],
        ]
    );
}

#[test]
fn a_way_listed_twice_counts_once() {
    let area = area(&[
        &[(0, 0), (10, 0), (10, 10)],
        &[(10, 10), (0, 10), (0, 0)],
        &[(10, 10), (0, 10), (0, 0)],
    ]);
    assert_eq!(area, [[[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]]]);
}

#[test]
fn a_chain_that_does_not_close_is_left_out() {
    let area = area(&[
        &[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)],
        &[(4, 4), (4, 6), (6, 6)],
    ]);
    assert_eq!(area, [[[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]]]);
}

#[test]
fn a_dangling_way_at_a_ring_node_does_not_break_the_ring() {
    let area = area(&[
        &[(10, 0), (10, 10)],
        // from a corner of the ring to nowhere
        &[(0, 0), (4, 4)],
        &[(10, 10), (0, 10), (0, 0)],
        &[(0, 0), (10, 0)],
    ]);
    assert_eq!(area, [[[(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]]]);
}

#[test]
fn ways_that_close_no_ring_are_an_error() {
    let area = multi_polygon_in(
        DEGREE,
        &[&[(0, 0), (10, 0), (10, 10)], &[(10, 10), (0, 10)]],
    );
    assert_eq!(area, Err(GeometryError::NoRing));
}

#[test]
fn a_ring_node_without_a_location_is_an_error() {
    let mut store = MemoryStore::new();
    store.set(1, Location::new(0, 0)).unwrap();
    store.set(2, Location::new(0, 10 * DEGREE)).unwrap();
    store.set(4, Location::new(10 * DEGREE, 0)).unwrap();
    // node 3, a corner of the square, is not in the store
    let square: &[i64] = &[1, 2, 3, 4, 1];
    let area = Locator::new(store.reader()).multi_polygon([square]);
    assert_eq!(area, Err(GeometryError::MissingNode(3)));
}

#[test]
fn locations_are_kept_exactly() {
    // 100 degrees across, at odd units of 1e-7 degrees
    let (west, east, south, north) = (-500_000_001, 499_999_999, -3, 123_456_789);
    let square = [
        (west, south),
        (east, south),
        (east, north),
        (west, north),
        (west, south),
    ];
    assert_eq!(area_in(1, &[&square]), [[square]]);
}

#[test]
fn locations_are_kept_exactly_around_the_world() {
    let (west, east, south, north) = (-1_799_999_999, 1_799_999_999, -3, 123_456_789);
    let square = [
        (west, south),
        (east, south),
        (east, north),
        (west, north),
        (west, south),
    ];
    assert_eq!(area_in(1, &[&square]), [[square]]);
}

/// What became of each multipolygon relation.
#[derive(Debug, Default, PartialEq)]
struct Outcomes {
    built: usize,
    polygons: usize,
    missing_node: usize,
    no_ring: usize,
}

#[test]
fn monaco_multipolygons_assemble() {
    let file = fixture("planetiler/monaco-latest.osm.pbf");
    let store = loaded(&file);
    let mut ways: HashMap<i64, Vec<i64>> = HashMap::new();
    let mut relations = Vec::new();
    for block in file.blocks(Select::all()).unwrap() {
        let block = block.unwrap();
        for way in block.ways() {
            ways.insert(way.id(), way.refs().collect());
        }
        for relation in block.relations() {
            if relation
                .tags()
                .any(|tag| tag == ("type", "multipolygon") || tag == ("type", "boundary"))
            {
                let members: Vec<i64> = relation
                    .members()
                    .filter(|m| m.kind == osmflow::MemberType::Way)
                    .map(|m| m.id)
                    .collect();
                relations.push(members);
            }
        }
    }
    assert_eq!(relations.len(), 72);
    let mut locator = Locator::new(store.reader());
    let mut outcomes = Outcomes::default();
    for members in &relations {
        // members outside the extract are missing, and leave rings that do not close
        let refs = members
            .iter()
            .filter_map(|id| ways.get(id).map(Vec::as_slice));
        match locator.multi_polygon(refs) {
            Ok(area) => {
                assert!(area.is_valid(), "{members:?}");
                outcomes.built += 1;
                outcomes.polygons += area.0.len();
            }
            Err(GeometryError::MissingNode(_)) => outcomes.missing_node += 1,
            Err(GeometryError::NoRing) => outcomes.no_ring += 1,
            Err(e) => panic!("{members:?}: {e}"),
        }
    }
    assert_eq!(
        outcomes,
        Outcomes {
            built: 42,
            polygons: 43,
            missing_node: 0,
            no_ring: 30,
        }
    );
}