kenro 0.4.0

SpatiaLite-style spatial SQL for SQLite in pure Rust — PostGIS-compatible ST_ functions, GeoPackage R-tree, CRS transform, H3, MVT. Use via rusqlite, loadable extension, or WASM
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Mapbox Vector Tile functions: `ST_AsMVTGeom` (world → tile-space
//! transform with optional clipping) and the `ST_AsMVT` aggregate (features
//! → encoded tile layer).
//!
//! `ST_AsMVT`'s signature deliberately diverges from PostGIS: SQLite has no
//! record type, so instead of `ST_AsMVT(row, name, extent, geom_column)` the
//! aggregate takes `(geom [, name [, extent [, props_json]]])` where
//! `props_json` is a JSON object (build it with `json_object(...)`). A
//! PostGIS-style call fails loudly at the type level — never silently.

use geo_types::{Coord, Geometry, LineString, MultiLineString, MultiPolygon, Point, Polygon, Rect};

use crate::error::{Error, Result};
use crate::geom::{self, Geom};
use crate::mvt;

use super::classify::{
    Class, classify, ensure_finite, normalize_lines, normalize_points, normalize_polygons,
    points_of, to_multi_line, to_multi_polygon,
};

// ---- axis-aligned rectangle clipping ----
//
// The clip window is always an axis-aligned rectangle, so the general
// overlay engine (i_overlay, the `overlay` feature) is deliberately NOT
// used here: Liang–Barsky handles segments and Sutherland–Hodgman handles
// rings, both exact for rectangular windows. One behavioral difference vs
// a boolean intersection: a concave polygon crossing the window several
// times stays a single ring connected by zero-width edges along the
// window boundary instead of splitting into parts — identical when
// rendered, and the golden vectors bound the coordinate difference vs
// PostGIS at ±1 tile pixel.

/// Liang–Barsky: the sub-segment of `a→b` inside `rect`, if any.
fn clip_segment(
    a: Coord<f64>,
    b: Coord<f64>,
    rect: &Rect<f64>,
) -> Option<(Coord<f64>, Coord<f64>)> {
    let (dx, dy) = (b.x - a.x, b.y - a.y);
    let mut t0 = 0.0f64;
    let mut t1 = 1.0f64;
    for (p, q) in [
        (-dx, a.x - rect.min().x),
        (dx, rect.max().x - a.x),
        (-dy, a.y - rect.min().y),
        (dy, rect.max().y - a.y),
    ] {
        if p == 0.0 {
            if q < 0.0 {
                return None; // parallel and outside
            }
        } else {
            let t = q / p;
            if p < 0.0 {
                t0 = t0.max(t);
            } else {
                t1 = t1.min(t);
            }
            if t0 > t1 {
                return None;
            }
        }
    }
    let at = |t: f64| Coord {
        x: a.x + t * dx,
        y: a.y + t * dy,
    };
    Some((at(t0), at(t1)))
}

/// Clip every member polyline, splitting where it leaves the window and
/// merging consecutive in-window segments back into polylines.
fn clip_lines(lines: &MultiLineString<f64>, rect: &Rect<f64>) -> MultiLineString<f64> {
    let mut out: Vec<LineString<f64>> = Vec::new();
    for line in &lines.0 {
        let mut current: Vec<Coord<f64>> = Vec::new();
        for w in line.0.windows(2) {
            match clip_segment(w[0], w[1], rect) {
                Some((p, q)) if p != q => {
                    if current.last() != Some(&p) {
                        if current.len() >= 2 {
                            out.push(LineString::new(std::mem::take(&mut current)));
                        } else {
                            current.clear();
                        }
                        current.push(p);
                    }
                    current.push(q);
                }
                _ => {
                    if current.len() >= 2 {
                        out.push(LineString::new(std::mem::take(&mut current)));
                    } else {
                        current.clear();
                    }
                }
            }
        }
        if current.len() >= 2 {
            out.push(LineString::new(current));
        }
    }
    MultiLineString(out)
}

#[cfg(not(feature = "overlay"))]
/// Sutherland–Hodgman: clip a closed ring against one half-plane.
fn clip_ring_edge(
    ring: &[Coord<f64>],
    inside: impl Fn(&Coord<f64>) -> bool,
    intersect: impl Fn(&Coord<f64>, &Coord<f64>) -> Coord<f64>,
) -> Vec<Coord<f64>> {
    let mut out = Vec::with_capacity(ring.len() + 4);
    for i in 0..ring.len() {
        let (cur, next) = (&ring[i], &ring[(i + 1) % ring.len()]);
        match (inside(cur), inside(next)) {
            (true, true) => out.push(*next),
            (true, false) => out.push(intersect(cur, next)),
            (false, true) => {
                out.push(intersect(cur, next));
                out.push(*next);
            }
            (false, false) => {}
        }
    }
    out
}

#[cfg(not(feature = "overlay"))]
/// Sutherland–Hodgman a ring (closed or open input) against `rect`;
/// returns a closed ring, or `None` when nothing remains.
fn clip_ring(ring: &LineString<f64>, rect: &Rect<f64>) -> Option<LineString<f64>> {
    let mut pts: Vec<Coord<f64>> = ring.0.clone();
    if pts.len() > 1 && pts.first() == pts.last() {
        pts.pop();
    }
    let (min, max) = (rect.min(), rect.max());
    let lerp_x = |x0: f64| {
        move |a: &Coord<f64>, b: &Coord<f64>| {
            let t = (x0 - a.x) / (b.x - a.x);
            Coord {
                x: x0,
                y: a.y + t * (b.y - a.y),
            }
        }
    };
    let lerp_y = |y0: f64| {
        move |a: &Coord<f64>, b: &Coord<f64>| {
            let t = (y0 - a.y) / (b.y - a.y);
            Coord {
                x: a.x + t * (b.x - a.x),
                y: y0,
            }
        }
    };
    pts = clip_ring_edge(&pts, |c| c.x >= min.x, lerp_x(min.x));
    if pts.is_empty() {
        return None;
    }
    pts = clip_ring_edge(&pts, |c| c.x <= max.x, lerp_x(max.x));
    if pts.is_empty() {
        return None;
    }
    pts = clip_ring_edge(&pts, |c| c.y >= min.y, lerp_y(min.y));
    if pts.is_empty() {
        return None;
    }
    pts = clip_ring_edge(&pts, |c| c.y <= max.y, lerp_y(max.y));
    pts.dedup();
    if pts.len() < 3 {
        return None;
    }
    let first = pts[0];
    pts.push(first);
    Some(LineString::new(pts))
}

#[cfg(not(feature = "overlay"))]
/// Clip a polygon against `rect`: exterior and holes ring-by-ring (holes of
/// a valid polygon stay inside the clipped exterior by construction).
fn clip_polygon(p: &Polygon<f64>, rect: &Rect<f64>) -> Option<Polygon<f64>> {
    let exterior = clip_ring(p.exterior(), rect)?;
    let interiors = p
        .interiors()
        .iter()
        .filter_map(|r| clip_ring(r, rect))
        .collect();
    Some(Polygon::new(exterior, interiors))
}

/// Areal clip, standard tier: ring-by-ring Sutherland–Hodgman (exact for
/// the rectangular window; self-intersecting input stays self-intersecting,
/// as documented).
#[cfg(not(feature = "overlay"))]
fn clip_areal(g: &Geometry<f64>, rect: &Rect<f64>) -> MultiPolygon<f64> {
    MultiPolygon(
        to_multi_polygon(g)
            .0
            .iter()
            .filter_map(|p| clip_polygon(p, rect))
            .collect(),
    )
}

/// Areal clip, `full` tier: PostGIS-grade — invalid input is repaired first
/// (ST_AsMVTGeom in PostGIS runs make-valid internally), then a boolean
/// intersection clips AND splits window-crossing concave polygons into
/// proper parts instead of bridged rings.
#[cfg(feature = "overlay")]
fn clip_areal(g: &Geometry<f64>, rect: &Rect<f64>) -> MultiPolygon<f64> {
    use geo::BooleanOps;
    use geo::algorithm::Validation;
    let mut mp = to_multi_polygon(g);
    if !mp.is_valid() {
        mp = super::overlay::repair_multi_polygon(mp);
    }
    mp.intersection(&MultiPolygon(vec![rect.to_polygon()]))
}

/// Post-snap repair, `full` tier only: rounding onto the integer grid can
/// make even valid input self-intersect (the classic MVT failure mode);
/// PostGIS re-validates after gridding and so does kenro when the overlay
/// engine is present. One pass: repair, re-round, drop re-degenerated
/// rings.
#[cfg(feature = "overlay")]
fn snap_repair(mp: MultiPolygon<f64>) -> MultiPolygon<f64> {
    use geo::algorithm::Validation;
    if mp.is_valid() {
        return mp;
    }
    let repaired = super::overlay::repair_multi_polygon(mp);
    MultiPolygon(repaired.0.iter().filter_map(clean_polygon).collect())
}

// ---- degenerate cleanup on tile-space (integer) coordinates ----

/// Collapse consecutive duplicate coordinates after rounding.
fn dedup_rounded(coords: &[Coord<f64>]) -> Vec<Coord<f64>> {
    let mut out: Vec<Coord<f64>> = Vec::with_capacity(coords.len());
    for c in coords {
        let r = Coord {
            x: c.x.round(),
            y: c.y.round(),
        };
        if out.last() != Some(&r) {
            out.push(r);
        }
    }
    out
}

fn clean_line(ls: &LineString<f64>) -> Option<LineString<f64>> {
    let coords = dedup_rounded(&ls.0);
    (coords.len() >= 2).then(|| LineString::new(coords))
}

fn clean_ring(ring: &LineString<f64>) -> Option<LineString<f64>> {
    let mut coords = dedup_rounded(&ring.0);
    // Re-close after dedup (rounding can merge the closing point away).
    if coords.first() != coords.last() {
        if let Some(&first) = coords.first() {
            coords.push(first);
        }
    }
    if coords.len() < 4 {
        return None;
    }
    let ls = LineString::new(coords);
    use geo::Area;
    let area = Polygon::new(ls.clone(), vec![]).unsigned_area();
    (area > 0.0).then_some(ls)
}

fn clean_polygon(p: &Polygon<f64>) -> Option<Polygon<f64>> {
    let exterior = clean_ring(p.exterior())?;
    let interiors = p.interiors().iter().filter_map(clean_ring).collect();
    Some(Polygon::new(exterior, interiors))
}

/// Round a tile-space geometry to the integer grid and drop pieces the
/// rounding degenerated (zero-length lines, zero-area rings). `None` means
/// nothing survived — SQL NULL.
fn clean_tile_geometry(g: &Geometry<f64>) -> Option<Geometry<f64>> {
    let cleaned = match g {
        Geometry::Point(_) | Geometry::MultiPoint(_) => {
            let points: Vec<Point<f64>> = points_of(g)
                .into_iter()
                .map(|p| Point::new(p.x().round(), p.y().round()))
                .collect();
            if points.is_empty() {
                return None;
            }
            normalize_points(points)
        }
        Geometry::Line(_) | Geometry::LineString(_) | Geometry::MultiLineString(_) => {
            let lines: Vec<LineString<f64>> =
                to_multi_line(g).0.iter().filter_map(clean_line).collect();
            if lines.is_empty() {
                return None;
            }
            normalize_lines(geo_types::MultiLineString(lines))
        }
        _ => {
            let polys: Vec<Polygon<f64>> = to_multi_polygon(g)
                .0
                .iter()
                .filter_map(clean_polygon)
                .collect();
            if polys.is_empty() {
                return None;
            }
            #[cfg(feature = "overlay")]
            let polys = snap_repair(MultiPolygon(polys)).0;
            if polys.is_empty() {
                return None;
            }
            normalize_polygons(MultiPolygon(polys))
        }
    };
    Some(cleaned)
}

// ---- ST_AsMVTGeom ----

/// `ST_AsMVTGeom(geom, bounds [, extent [, buffer [, clip]]])` — transform a
/// world-space geometry into integer tile coordinates (Y down). `bounds` is
/// any geometry whose envelope defines the tile. Returns `None` (SQL NULL)
/// when nothing remains inside the tile, like PostGIS.
pub fn st_as_mvt_geom(
    bytes: &[u8],
    bounds: &[u8],
    extent: Option<i32>,
    buffer: Option<i32>,
    clip: Option<i32>,
) -> Result<Option<Vec<u8>>> {
    const FUNC: &str = "ST_AsMVTGeom";
    let extent = extent.unwrap_or(4096);
    if extent <= 0 {
        return Err(Error::Unsupported {
            func: FUNC,
            reason: format!("extent must be positive, got {extent}"),
        });
    }
    let buffer = buffer.unwrap_or(256);
    if buffer < 0 {
        return Err(Error::Unsupported {
            func: FUNC,
            reason: format!("buffer must be non-negative, got {buffer}"),
        });
    }
    let clip = clip.map(|v| v != 0).unwrap_or(true);

    let gb = geom::decode_auto(bounds)?;
    let env = geom::envelope(&gb.geometry).ok_or_else(|| Error::Unsupported {
        func: FUNC,
        reason: "bounds must be a non-empty geometry".into(),
    })?;
    let (width, height) = (env.max_x - env.min_x, env.max_y - env.min_y);
    if !(width.is_finite() && height.is_finite()) || width <= 0.0 || height <= 0.0 {
        return Err(Error::Unsupported {
            func: FUNC,
            reason: "bounds envelope is degenerate (zero width or height)".into(),
        });
    }

    let g = geom::decode_auto(bytes)?;
    if geom::is_empty(&g.geometry) {
        return Ok(None);
    }
    let class = classify(FUNC, &g.geometry)?;
    ensure_finite(FUNC, &g.geometry)?;

    let mut geometry = g.geometry;
    if clip {
        let bx = buffer as f64 / extent as f64 * width;
        let by = buffer as f64 / extent as f64 * height;
        let rect = Rect::new(
            Coord {
                x: env.min_x - bx,
                y: env.min_y - by,
            },
            Coord {
                x: env.max_x + bx,
                y: env.max_y + by,
            },
        );
        geometry = match class {
            Class::Puntal => {
                let inside: Vec<Point<f64>> = points_of(&geometry)
                    .into_iter()
                    .filter(|p| {
                        p.x() >= rect.min().x
                            && p.x() <= rect.max().x
                            && p.y() >= rect.min().y
                            && p.y() <= rect.max().y
                    })
                    .collect();
                if inside.is_empty() {
                    return Ok(None);
                }
                normalize_points(inside)
            }
            Class::Lineal => {
                let result = normalize_lines(clip_lines(&to_multi_line(&geometry), &rect));
                if geom::is_empty(&result) {
                    return Ok(None);
                }
                result
            }
            Class::Areal => {
                let result = normalize_polygons(clip_areal(&geometry, &rect));
                if geom::is_empty(&result) {
                    return Ok(None);
                }
                result
            }
        };
    }

    use geo::MapCoords;
    let (fx, fy) = (f64::from(extent) / width, f64::from(extent) / height);
    let tile = geometry.map_coords(|c| Coord {
        x: ((c.x - env.min_x) * fx).round(),
        y: ((env.max_y - c.y) * fy).round(),
    });

    match clean_tile_geometry(&tile) {
        None => Ok(None),
        Some(geometry) => geom::encode_canonical_gpb(
            &Geom {
                geometry,
                srid: 0,
                has_zm: false,
            },
            FUNC,
        )
        .map(Some),
    }
}

// ---- ST_AsMVT aggregate ----

fn parse_props(json: &str) -> Result<Vec<(String, mvt::Value)>> {
    const FUNC: &str = "ST_AsMVT";
    let parsed: serde_json::Value = serde_json::from_str(json).map_err(|e| Error::Unsupported {
        func: FUNC,
        reason: format!("properties must be valid JSON (use json_object(...)): {e}"),
    })?;
    let object = parsed.as_object().ok_or_else(|| Error::Unsupported {
        func: FUNC,
        reason: "properties must be a JSON object (use json_object(...))".into(),
    })?;
    let mut props = Vec::with_capacity(object.len());
    for (key, value) in object {
        let value = match value {
            serde_json::Value::Null => continue, // NULL property → omitted, like PostGIS
            serde_json::Value::Bool(b) => mvt::Value::Bool(*b),
            serde_json::Value::Number(n) => match n.as_i64() {
                Some(i) => mvt::Value::Int(i),
                None => mvt::Value::Double(n.as_f64().unwrap_or(f64::NAN)),
            },
            serde_json::Value::String(s) => mvt::Value::Str(s.clone()),
            serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
                return Err(Error::Unsupported {
                    func: FUNC,
                    reason: format!(
                        "property {key:?} has a nested JSON value; MVT properties \
                                     are scalar (string, number, boolean)"
                    ),
                });
            }
        };
        props.push((key.clone(), value));
    }
    Ok(props)
}

/// Accumulator behind the `ST_AsMVT(geom [, name [, extent [, props_json]]])`
/// aggregate. Layer name and extent must be constant within one aggregation
/// group; geometries must already be in tile space (`ST_AsMVTGeom` output).
pub struct MvtAggregate {
    layer_name: Option<String>,
    extent: Option<i32>,
    features: Vec<mvt::Feature>,
}

impl MvtAggregate {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        MvtAggregate {
            layer_name: None,
            extent: None,
            features: Vec::new(),
        }
    }

    pub fn step(
        &mut self,
        geom: &[u8],
        name: Option<&str>,
        extent: Option<i32>,
        props: Option<&str>,
    ) -> Result<()> {
        const FUNC: &str = "ST_AsMVT";
        let name = name.unwrap_or("default");
        let extent = extent.unwrap_or(4096);
        if extent <= 0 {
            return Err(Error::Unsupported {
                func: FUNC,
                reason: format!("extent must be positive, got {extent}"),
            });
        }
        match &self.layer_name {
            None => {
                self.layer_name = Some(name.to_string());
                self.extent = Some(extent);
            }
            Some(existing) => {
                if existing != name || self.extent != Some(extent) {
                    return Err(Error::Unsupported {
                        func: FUNC,
                        reason: "layer name and extent must be constant within one \
                                 aggregation group"
                            .into(),
                    });
                }
            }
        }
        let g = geom::decode_auto(geom)?;
        if geom::is_empty(&g.geometry) {
            return Ok(()); // empty geometries contribute no feature
        }
        classify(FUNC, &g.geometry)?; // reject GeometryCollection loudly
        ensure_finite(FUNC, &g.geometry)?;
        // Snap to the integer grid and drop rounding degenerates, so hostile
        // (non-ST_AsMVTGeom) input cannot produce malformed command streams.
        let Some(geometry) = clean_tile_geometry(&g.geometry) else {
            return Ok(());
        };
        let props = match props {
            None => Vec::new(),
            Some(json) => parse_props(json)?,
        };
        self.features.push(mvt::Feature { geometry, props });
        Ok(())
    }

    /// `None` = SQL NULL (zero input rows). Rows whose geometries all
    /// degenerated still produce a valid empty layer.
    pub fn finish(self) -> Result<Option<Vec<u8>>> {
        let Some(name) = self.layer_name else {
            return Ok(None);
        };
        let extent = self.extent.unwrap_or(4096) as u32;
        mvt::encode_tile("ST_AsMVT", &name, extent, &self.features).map(Some)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geom::decode_wkt;

    fn gpb(wkt: &str) -> Vec<u8> {
        crate::geom::encode_canonical_gpb(&decode_wkt(wkt, 0).unwrap(), "test").unwrap()
    }

    fn as_wkt(bytes: &[u8]) -> String {
        crate::geom::encode_wkt(&crate::geom::decode_auto(bytes).unwrap(), "test").unwrap()
    }

    #[test]
    fn point_transforms_into_tile_space() {
        // Tile bounds (0,0)-(100,100), extent 10: point (50, 90) → (5, 1)
        // after the Y flip.
        let out = st_as_mvt_geom(
            &gpb("POINT(50 90)"),
            &gpb("POLYGON((0 0,100 0,100 100,0 100,0 0))"),
            Some(10),
            Some(0),
            None,
        )
        .unwrap()
        .unwrap();
        assert_eq!(as_wkt(&out), "POINT(5 1)");
    }

    #[test]
    fn outside_geometry_becomes_null() {
        let out = st_as_mvt_geom(
            &gpb("POINT(500 500)"),
            &gpb("POLYGON((0 0,100 0,100 100,0 100,0 0))"),
            None,
            None,
            None,
        )
        .unwrap();
        assert!(out.is_none());
    }

    #[test]
    fn clip_false_keeps_outside_geometry() {
        let out = st_as_mvt_geom(
            &gpb("POINT(200 0)"),
            &gpb("POLYGON((0 0,100 0,100 100,0 100,0 0))"),
            Some(10),
            Some(0),
            Some(0),
        )
        .unwrap()
        .unwrap();
        assert_eq!(as_wkt(&out), "POINT(20 10)");
    }

    #[test]
    fn polygon_is_clipped_to_buffered_bounds() {
        let out = st_as_mvt_geom(
            &gpb("POLYGON((50 50,150 50,150 60,50 60,50 50))"),
            &gpb("POLYGON((0 0,100 0,100 100,0 100,0 0))"),
            Some(100),
            Some(0),
            None,
        )
        .unwrap()
        .unwrap();
        let decoded = crate::geom::decode_auto(&out).unwrap();
        use geo::Area;
        // Clipped to x ∈ [50,100] world = 50×10 world units = 50×10 tile
        // units at extent 100 over a 100-wide tile.
        assert_eq!(decoded.geometry.unsigned_area(), 500.0);
    }

    #[test]
    fn degenerate_after_rounding_is_null() {
        // A sliver far thinner than one tile pixel collapses to zero area.
        let out = st_as_mvt_geom(
            &gpb("POLYGON((10 10,20 10,20 10.001,10 10.001,10 10))"),
            &gpb("POLYGON((0 0,100 0,100 100,0 100,0 0))"),
            Some(100),
            Some(0),
            None,
        )
        .unwrap();
        assert!(out.is_none());
    }

    #[cfg(feature = "overlay")]
    #[test]
    fn full_tier_repairs_invalid_input_like_postgis() {
        use crate::functions::accessors::st_is_valid;
        let out = st_as_mvt_geom(
            &gpb("POLYGON((10 10,60 60,60 10,10 60,10 10))"),
            &gpb("POLYGON((0 0,100 0,100 100,0 100,0 0))"),
            Some(100),
            Some(0),
            None,
        )
        .unwrap()
        .unwrap();
        assert!(st_is_valid(&out).unwrap());
        assert!(as_wkt(&out).starts_with("MULTIPOLYGON"), "{}", as_wkt(&out));
    }

    #[test]
    fn aggregate_builds_a_decodable_layer() {
        let mut acc = MvtAggregate::new();
        acc.step(
            &gpb("POINT(10 10)"),
            Some("parks"),
            Some(4096),
            Some(r#"{"name":"yoyogi","area":54.3,"open":true,"count":7,"skip":null}"#),
        )
        .unwrap();
        acc.step(
            &gpb("LINESTRING(0 0,10 10)"),
            Some("parks"),
            Some(4096),
            None,
        )
        .unwrap();
        let tile = acc.finish().unwrap().unwrap();
        // Tile → layers field (3, len-delim).
        assert_eq!(tile[0], 0x1a);
        // The layer must contain the name and the deduped keys.
        let hay = String::from_utf8_lossy(&tile);
        assert!(hay.contains("parks"));
        assert!(hay.contains("yoyogi"));
        assert!(hay.contains("count"));
        assert!(!hay.contains("skip"), "null props must be omitted");
    }

    #[test]
    fn aggregate_rejects_changing_layer_name() {
        let mut acc = MvtAggregate::new();
        acc.step(&gpb("POINT(1 1)"), Some("a"), None, None).unwrap();
        let err = acc.step(&gpb("POINT(2 2)"), Some("b"), None, None);
        assert!(err.is_err());
    }

    #[test]
    fn aggregate_zero_rows_is_null() {
        assert!(MvtAggregate::new().finish().unwrap().is_none());
    }

    #[test]
    fn aggregate_rejects_nested_props() {
        let mut acc = MvtAggregate::new();
        let err = acc.step(&gpb("POINT(1 1)"), None, None, Some(r#"{"a":[1,2]}"#));
        assert!(err.is_err());
    }
}