elivagar 0.1.0

Shortbread vector tile generator - reads OSM PBF files and produces PMTiles v3 archives
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! MVT tile decoder producing structural detail for out-of-crate adjudication.
//!
//! elivagar is the only decoder of a PMTiles archive in this system; brokkr
//! links the crate and decodes tiles through this module. Decoding stops at
//! structure: a [`DetailTile`] carries its layers, features, geometry
//! components and rings in **wire order**, with no canonicalization and no
//! hashing. Deciding what counts as "the same tile" - ordering, digests, the
//! comparison - is the caller's, per the corpus redesign contract. The one
//! knob here is [`Strictness`]: the gate decodes strict so foreign structure
//! can never silently skip past it; a cross-producer comparison decodes
//! tolerant.

use std::sync::Arc;

use protohoggr::{Cursor, WIRE_32BIT, WIRE_64BIT, WIRE_LEN, WIRE_VARINT};

/// Unknown-wire-field policy.
///
/// `Strict` rejects any field the MVT schema does not name - the gate's
/// requirement, so a competitor's extra wire fields cannot pass unnoticed.
/// `Tolerant` skips unknown fields, for comparing archives from producers
/// that carry structure elivagar never emits.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Strictness {
    Strict,
    Tolerant,
}

/// Ring role recovered from geometry: point and line features carry a single
/// synthetic ring; polygon rings are classified outer/hole by signed area.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CanonRingRole {
    Point = 0,
    Path = 1,
    Outer = 2,
    Hole = 3,
}

/// A decoded MVT attribute value.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DetailAttr {
    String(Arc<str>),
    Float(u32),
    Double(u64),
    Int(i64),
    UInt(u64),
    SInt(i64),
    Bool(bool),
}

/// One geometry ring: its role plus its absolute integer vertices.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct DetailRing {
    pub role: CanonRingRole,
    pub points: Vec<(i32, i32)>,
}

/// One geometry component: an outer ring plus any following holes (polygons),
/// or a single ring (points, lines).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct DetailComponent {
    pub rings: Vec<DetailRing>,
}

/// One MVT feature, geometry decoded into components, tags resolved against
/// the layer key/value tables. Attributes and components are in wire order.
#[derive(Clone, Debug)]
pub struct DetailFeature {
    pub id: Option<u64>,
    pub geom_type: u8,
    pub attrs: Vec<(Arc<str>, DetailAttr)>,
    pub components: Vec<DetailComponent>,
}

/// One MVT layer with its features in wire order.
#[derive(Clone, Debug)]
pub struct DetailLayer {
    pub name: Arc<str>,
    pub extent: u32,
    pub version: u32,
    pub features: Vec<DetailFeature>,
}

/// A decoded MVT tile: its layers in wire order.
#[derive(Clone, Debug)]
pub struct DetailTile {
    pub layers: Vec<DetailLayer>,
}

/// Decode a decompressed MVT tile into structural detail.
pub fn decode_detail_tile(data: &[u8], strictness: Strictness) -> Result<DetailTile, String> {
    let mut layers = Vec::new();
    let mut cursor = Cursor::new(data);
    while let Some((field, wire_type)) = cursor
        .read_tag()
        .map_err(|error| format!("read tile tag: {error}"))?
    {
        if field == 3 && wire_type == WIRE_LEN {
            let bytes = cursor
                .read_len_delimited()
                .map_err(|error| format!("read tile layer: {error}"))?;
            layers.push(decode_detail_layer(bytes, strictness)?);
        } else if strictness == Strictness::Strict {
            return Err(format!("unknown tile field {field}"));
        } else {
            cursor
                .skip_field(wire_type)
                .map_err(|error| format!("skip tile field {field}: {error}"))?;
        }
    }
    Ok(DetailTile { layers })
}

fn decode_detail_layer(data: &[u8], strictness: Strictness) -> Result<DetailLayer, String> {
    let mut name: Arc<str> = Arc::from("");
    let mut extent = 4096u32;
    let mut version = 1u32;
    let mut keys = Vec::new();
    let mut values = Vec::new();
    let mut feature_bytes = Vec::new();
    let mut cursor = Cursor::new(data);
    while let Some((field, wire_type)) = cursor
        .read_tag()
        .map_err(|error| format!("read layer tag: {error}"))?
    {
        match (field, wire_type) {
            (1, WIRE_LEN) => {
                let bytes = cursor
                    .read_len_delimited()
                    .map_err(|error| format!("read layer name: {error}"))?;
                name = Arc::from(
                    std::str::from_utf8(bytes)
                        .map_err(|error| format!("layer name is not UTF-8: {error}"))?,
                );
            }
            (2, WIRE_LEN) => feature_bytes.push(
                cursor
                    .read_len_delimited()
                    .map_err(|error| format!("read feature message: {error}"))?,
            ),
            (3, WIRE_LEN) => {
                let bytes = cursor
                    .read_len_delimited()
                    .map_err(|error| format!("read layer key: {error}"))?;
                keys.push(Arc::from(
                    std::str::from_utf8(bytes)
                        .map_err(|error| format!("layer key is not UTF-8: {error}"))?,
                ));
            }
            (4, WIRE_LEN) => values.push(decode_detail_attr(
                cursor
                    .read_len_delimited()
                    .map_err(|error| format!("read layer value: {error}"))?,
                strictness,
            )?),
            (5, WIRE_VARINT) => {
                let raw = cursor
                    .read_varint()
                    .map_err(|error| format!("read layer extent: {error}"))?;
                extent = u32::try_from(raw).map_err(|_| format!("extent out of range: {raw}"))?;
            }
            (15, WIRE_VARINT) => {
                let raw = cursor
                    .read_varint()
                    .map_err(|error| format!("read layer version: {error}"))?;
                version = u32::try_from(raw).map_err(|_| format!("version out of range: {raw}"))?;
            }
            _ => {
                if strictness == Strictness::Strict {
                    return Err(format!("unknown layer field {field}"));
                }
                cursor
                    .skip_field(wire_type)
                    .map_err(|error| format!("skip layer field {field}: {error}"))?;
            }
        }
    }
    let mut features = Vec::with_capacity(feature_bytes.len());
    for bytes in feature_bytes {
        features.push(decode_detail_feature(bytes, &keys, &values, strictness)?);
    }
    Ok(DetailLayer {
        name,
        extent,
        version,
        features,
    })
}

/// Decode one MVT value message (the layer value table entry).
pub fn decode_detail_attr(data: &[u8], strictness: Strictness) -> Result<DetailAttr, String> {
    let mut out = None;
    let mut cursor = Cursor::new(data);
    while let Some((field, wire_type)) = cursor
        .read_tag()
        .map_err(|error| format!("read value tag: {error}"))?
    {
        let value = match (field, wire_type) {
            (1, WIRE_LEN) => Some(DetailAttr::String(Arc::from(
                std::str::from_utf8(
                    cursor
                        .read_len_delimited()
                        .map_err(|error| format!("read string value: {error}"))?,
                )
                .map_err(|error| format!("string value is not UTF-8: {error}"))?,
            ))),
            (2, WIRE_32BIT) => Some(DetailAttr::Float(
                cursor
                    .read_fixed32()
                    .map_err(|error| format!("read float value: {error}"))?,
            )),
            (3, WIRE_64BIT) => Some(DetailAttr::Double(
                cursor
                    .read_fixed64()
                    .map_err(|error| format!("read double value: {error}"))?,
            )),
            (4, WIRE_VARINT) => {
                let raw = cursor
                    .read_varint()
                    .map_err(|error| format!("read int value: {error}"))?;
                #[allow(clippy::cast_possible_wrap)]
                Some(DetailAttr::Int(raw as i64))
            }
            (5, WIRE_VARINT) => Some(DetailAttr::UInt(
                cursor
                    .read_varint()
                    .map_err(|error| format!("read uint value: {error}"))?,
            )),
            (6, WIRE_VARINT) => Some(DetailAttr::SInt(unzigzag64(
                cursor
                    .read_varint()
                    .map_err(|error| format!("read sint value: {error}"))?,
            ))),
            (7, WIRE_VARINT) => Some(DetailAttr::Bool(
                cursor
                    .read_varint()
                    .map_err(|error| format!("read bool value: {error}"))?
                    != 0,
            )),
            _ => {
                if strictness == Strictness::Strict {
                    return Err(format!("unknown value field {field}"));
                }
                cursor
                    .skip_field(wire_type)
                    .map_err(|error| format!("skip value field {field}: {error}"))?;
                None
            }
        };
        if value.is_some() {
            out = value;
        }
    }
    out.ok_or_else(|| "empty MVT value".to_string())
}

/// Decode one MVT feature message against the layer key/value tables.
pub fn decode_detail_feature(
    data: &[u8],
    keys: &[Arc<str>],
    values: &[DetailAttr],
    strictness: Strictness,
) -> Result<DetailFeature, String> {
    let mut id = None;
    let mut tag_bytes = Vec::new();
    let mut geom_type = 0u8;
    let mut geometry = None;
    let mut cursor = Cursor::new(data);
    while let Some((field, wire_type)) = cursor
        .read_tag()
        .map_err(|error| format!("read feature tag: {error}"))?
    {
        match (field, wire_type) {
            (1, WIRE_VARINT) => {
                id = Some(
                    cursor
                        .read_varint()
                        .map_err(|error| format!("read feature id: {error}"))?,
                );
            }
            (2, WIRE_LEN) => {
                tag_bytes.extend_from_slice(
                    cursor
                        .read_len_delimited()
                        .map_err(|error| format!("read feature tags: {error}"))?,
                );
            }
            (3, WIRE_VARINT) => {
                let raw = cursor
                    .read_varint()
                    .map_err(|error| format!("read feature type: {error}"))?;
                geom_type =
                    u8::try_from(raw).map_err(|_| format!("geometry type out of range: {raw}"))?;
            }
            (4, WIRE_LEN) => {
                let bytes = cursor
                    .read_len_delimited()
                    .map_err(|error| format!("read feature geometry: {error}"))?;
                geometry
                    .get_or_insert_with(Vec::new)
                    .extend_from_slice(bytes);
            }
            _ => {
                if strictness == Strictness::Strict {
                    return Err(format!("unknown feature field {field}"));
                }
                cursor
                    .skip_field(wire_type)
                    .map_err(|error| format!("skip feature field {field}: {error}"))?;
            }
        }
    }
    let attrs = decode_detail_attrs(&tag_bytes, keys, values)?;
    let components = match geometry {
        Some(geometry) => decode_detail_geometry(geom_type, &geometry)?,
        None => Vec::new(),
    };
    Ok(DetailFeature {
        id,
        geom_type,
        attrs,
        components,
    })
}

fn decode_detail_attrs(
    data: &[u8],
    keys: &[Arc<str>],
    values: &[DetailAttr],
) -> Result<Vec<(Arc<str>, DetailAttr)>, String> {
    let mut cursor = Cursor::new(data);
    let mut attrs = Vec::with_capacity(data.len() / 2);
    while !cursor.is_empty() {
        let key_idx = usize::try_from(
            cursor
                .read_varint()
                .map_err(|error| format!("read feature tag key: {error}"))?,
        )
        .map_err(|_| "feature tag key index overflow".to_string())?;
        let value_idx = usize::try_from(
            cursor
                .read_varint()
                .map_err(|error| format!("read feature tag value: {error}"))?,
        )
        .map_err(|_| "feature tag value index overflow".to_string())?;
        attrs.push((
            Arc::clone(
                keys.get(key_idx)
                    .ok_or_else(|| format!("key index out of range: {key_idx}"))?,
            ),
            values
                .get(value_idx)
                .ok_or_else(|| format!("value index out of range: {value_idx}"))?
                .clone(),
        ));
    }
    Ok(attrs)
}

fn decode_detail_geometry(geom_type: u8, data: &[u8]) -> Result<Vec<DetailComponent>, String> {
    match geom_type {
        1 => decode_detail_points(data),
        2 => decode_detail_lines(data),
        3 => decode_detail_polygons(data),
        _ => Ok(Vec::new()),
    }
}

fn decode_detail_points(data: &[u8]) -> Result<Vec<DetailComponent>, String> {
    let mut cursor = Cursor::new(data);
    let mut points = Vec::new();
    let (mut x, mut y) = (0i32, 0i32);
    while !cursor.is_empty() {
        let command = read_geometry_varint(&mut cursor, "point command")?;
        if command & 0x7 != 1 {
            return Err(format!("point geometry contains command {}", command & 0x7));
        }
        for _ in 0..(command >> 3) {
            x = x
                .checked_add(unzigzag(read_geometry_varint(&mut cursor, "point x")?))
                .ok_or_else(|| "point x overflows i32".to_string())?;
            y = y
                .checked_add(unzigzag(read_geometry_varint(&mut cursor, "point y")?))
                .ok_or_else(|| "point y overflows i32".to_string())?;
            points.push((x, y));
        }
    }
    if points.is_empty() {
        Ok(Vec::new())
    } else {
        Ok(vec![make_detail_component(vec![make_detail_ring(
            CanonRingRole::Point,
            points,
        )])])
    }
}

fn decode_detail_lines(data: &[u8]) -> Result<Vec<DetailComponent>, String> {
    let mut cursor = Cursor::new(data);
    let mut components = Vec::new();
    let mut path: Option<Vec<(i32, i32)>> = None;
    let (mut x, mut y) = (0i32, 0i32);
    while !cursor.is_empty() {
        let command = read_geometry_varint(&mut cursor, "line command")?;
        let id = command & 0x7;
        let count = command >> 3;
        match id {
            1 => {
                if let Some(path) = path.take() {
                    push_detail_line(&mut components, path);
                }
                for n in 0..count {
                    x = x
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "line MoveTo x",
                        )?))
                        .ok_or_else(|| "line x overflows i32".to_string())?;
                    y = y
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "line MoveTo y",
                        )?))
                        .ok_or_else(|| "line y overflows i32".to_string())?;
                    if n == 0 {
                        path = Some(vec![(x, y)]);
                    } else {
                        push_detail_line(&mut components, vec![(x, y)]);
                    }
                }
            }
            2 => {
                let path = path
                    .as_mut()
                    .ok_or_else(|| "line LineTo without MoveTo".to_string())?;
                for _ in 0..count {
                    x = x
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "line LineTo x",
                        )?))
                        .ok_or_else(|| "line x overflows i32".to_string())?;
                    y = y
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "line LineTo y",
                        )?))
                        .ok_or_else(|| "line y overflows i32".to_string())?;
                    path.push((x, y));
                }
            }
            7 => {}
            _ => return Err(format!("unknown line command {id}")),
        }
    }
    if let Some(path) = path {
        push_detail_line(&mut components, path);
    }
    Ok(components)
}

fn push_detail_line(components: &mut Vec<DetailComponent>, path: Vec<(i32, i32)>) {
    if !path.is_empty() {
        components.push(make_detail_component(vec![make_detail_ring(
            CanonRingRole::Path,
            path,
        )]));
    }
}

fn decode_detail_polygons(data: &[u8]) -> Result<Vec<DetailComponent>, String> {
    let mut cursor = Cursor::new(data);
    let mut rings: Vec<Vec<(i32, i32)>> = Vec::new();
    let (mut x, mut y) = (0i32, 0i32);
    while !cursor.is_empty() {
        let command = read_geometry_varint(&mut cursor, "polygon command")?;
        let id = command & 0x7;
        let count = command >> 3;
        match id {
            1 => {
                for _ in 0..count {
                    x = x
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "polygon MoveTo x",
                        )?))
                        .ok_or_else(|| "polygon x overflows i32".to_string())?;
                    y = y
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "polygon MoveTo y",
                        )?))
                        .ok_or_else(|| "polygon y overflows i32".to_string())?;
                    rings.push(vec![(x, y)]);
                }
            }
            2 => {
                let ring = rings
                    .last_mut()
                    .ok_or_else(|| "polygon LineTo without MoveTo".to_string())?;
                for _ in 0..count {
                    x = x
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "polygon LineTo x",
                        )?))
                        .ok_or_else(|| "polygon x overflows i32".to_string())?;
                    y = y
                        .checked_add(unzigzag(read_geometry_varint(
                            &mut cursor,
                            "polygon LineTo y",
                        )?))
                        .ok_or_else(|| "polygon y overflows i32".to_string())?;
                    ring.push((x, y));
                }
            }
            7 => {
                if let Some(ring) = rings.last_mut()
                    && let Some(&first) = ring.first()
                {
                    // ClosePath does not alter x/y. This is required by MVT 4.3.3.3.
                    ring.push(first);
                }
            }
            _ => return Err(format!("unknown polygon command {id}")),
        }
    }

    // Group rings into components: an outer ring opens a component, following
    // holes attach to it. Winding sign classifies the role.
    let mut grouped: Vec<Vec<DetailRing>> = Vec::new();
    for ring in rings {
        let role = if signed_area(&ring) > 0 {
            CanonRingRole::Outer
        } else {
            CanonRingRole::Hole
        };
        let ring = make_detail_ring(role, ring);
        if role == CanonRingRole::Outer || grouped.is_empty() {
            grouped.push(vec![ring]);
        } else if let Some(component) = grouped.last_mut() {
            component.push(ring);
        }
    }
    Ok(grouped.into_iter().map(make_detail_component).collect())
}

fn read_geometry_varint(cursor: &mut Cursor<'_>, context: &str) -> Result<u32, String> {
    let raw = cursor
        .read_varint()
        .map_err(|error| format!("read {context}: {error}"))?;
    u32::try_from(raw).map_err(|_| format!("{context} out of range: {raw}"))
}

fn make_detail_ring(role: CanonRingRole, points: Vec<(i32, i32)>) -> DetailRing {
    DetailRing { role, points }
}

fn make_detail_component(rings: Vec<DetailRing>) -> DetailComponent {
    DetailComponent { rings }
}

#[inline]
fn unzigzag(value: u32) -> i32 {
    #[allow(clippy::cast_possible_wrap)]
    {
        ((value >> 1) as i32) ^ (-((value & 1) as i32))
    }
}

#[inline]
fn unzigzag64(value: u64) -> i64 {
    #[allow(clippy::cast_possible_wrap)]
    {
        ((value >> 1) as i64) ^ (-((value & 1) as i64))
    }
}

fn signed_area(ring: &[(i32, i32)]) -> i128 {
    ring.windows(2).fold(0i128, |area, pair| {
        area + i128::from(pair[0].0) * i128::from(pair[1].1)
            - i128::from(pair[1].0) * i128::from(pair[0].1)
    })
}