sqlitegis 0.2.0

SQLiteGIS: PostGIS-style spatial functions for SQLite in pure Rust.
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
//! Spatial operations
//!
//! ST_Union, ST_Intersection, ST_Difference, ST_SymDifference, ST_Buffer

use std::cmp::Ordering;

use geo::algorithm::bool_ops::BooleanOps;
use geo::algorithm::line_intersection::{line_intersection, LineIntersection};
use geo::algorithm::line_measures::metric_spaces::{Euclidean, Geodesic, Haversine};
use geo::algorithm::line_measures::Densify;
use geo::algorithm::Buffer;
use geo::algorithm::Intersects;
use geo::{
    Geometry, GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point,
    Polygon,
};

use crate::core::error::{Result, SqliteGisError};
use crate::core::ewkb::{
    concat_multipolygon_bodies, extract_mbr, extract_srid, parse_ewkb, parse_ewkb_pair, write_ewkb,
};
use crate::core::functions::emptiness::{is_empty_geometry, is_empty_point};

/// Extract a Polygon or MultiPolygon from a geometry, converting single
/// Polygons into MultiPolygon for uniform BooleanOps handling.
fn require_multi_polygon(geom: Geometry<f64>) -> Result<MultiPolygon<f64>> {
    match geom {
        Geometry::Polygon(p) => Ok(MultiPolygon::new(vec![p])),
        Geometry::MultiPolygon(mp) => Ok(mp),
        other => Err(SqliteGisError::wrong_type(
            "Polygon or MultiPolygon",
            &other,
        )),
    }
}

fn binary_polygon_op<F>(a: &[u8], b: &[u8], op: F) -> Result<Vec<u8>>
where
    F: FnOnce(&MultiPolygon<f64>, &MultiPolygon<f64>) -> MultiPolygon<f64>,
{
    let (ga, gb, srid) = parse_ewkb_pair(a, b)?;
    let ma = require_multi_polygon(ga)?;
    let mb = require_multi_polygon(gb)?;
    let result = op(&ma, &mb);
    write_ewkb(&Geometry::MultiPolygon(result), srid)
}

/// Bag of homogeneous-typed pieces extracted from a possibly-nested input.
///
/// `ST_Intersection` accepts any geometry on either side. We normalise the
/// inputs by decomposing them into points, line strings, and polygons, then
/// intersect the bags pair-wise, then pack the smallest matching variant on
/// the way out.
#[derive(Default)]
struct GeometryBag {
    points: Vec<Point<f64>>,
    lines: Vec<LineString<f64>>,
    polygons: Vec<Polygon<f64>>,
}

impl GeometryBag {
    fn new() -> Self {
        Self::default()
    }
}

fn decompose_into(geom: Geometry<f64>, bag: &mut GeometryBag) -> Result<()> {
    match geom {
        Geometry::Point(p) => {
            if !is_empty_point(&p) {
                bag.points.push(p);
            }
        }
        Geometry::MultiPoint(mp) => {
            for p in mp.0 {
                if !is_empty_point(&p) {
                    bag.points.push(p);
                }
            }
        }
        Geometry::LineString(ls) => {
            if !ls.0.is_empty() {
                bag.lines.push(ls);
            }
        }
        Geometry::MultiLineString(mls) => {
            for ls in mls.0 {
                if !ls.0.is_empty() {
                    bag.lines.push(ls);
                }
            }
        }
        Geometry::Polygon(p) => {
            if !p.exterior().0.is_empty() {
                bag.polygons.push(p);
            }
        }
        Geometry::MultiPolygon(mp) => {
            for p in mp.0 {
                if !p.exterior().0.is_empty() {
                    bag.polygons.push(p);
                }
            }
        }
        Geometry::GeometryCollection(gc) => {
            for g in gc.0 {
                decompose_into(g, bag)?;
            }
        }
        other => {
            return Err(SqliteGisError::wrong_type(
                "Point, LineString, Polygon, or a Multi/Collection of these",
                &other,
            ));
        }
    }
    Ok(())
}

fn intersect_bags(a: &GeometryBag, b: &GeometryBag) -> GeometryBag {
    let mut out = GeometryBag::new();

    if !a.polygons.is_empty() && !b.polygons.is_empty() {
        let ma = MultiPolygon::new(a.polygons.clone());
        let mb = MultiPolygon::new(b.polygons.clone());
        let result = ma.intersection(&mb);
        out.polygons.extend(result.0);
    }

    if !a.lines.is_empty() && !b.polygons.is_empty() {
        let mls = MultiLineString::new(a.lines.clone());
        let mb = MultiPolygon::new(b.polygons.clone());
        let clipped = mb.clip(&mls, false);
        out.lines.extend(clipped.0);
    }

    if !a.polygons.is_empty() && !b.lines.is_empty() {
        let ma = MultiPolygon::new(a.polygons.clone());
        let mls = MultiLineString::new(b.lines.clone());
        let clipped = ma.clip(&mls, false);
        out.lines.extend(clipped.0);
    }

    if !a.points.is_empty() && !b.polygons.is_empty() {
        let mb = MultiPolygon::new(b.polygons.clone());
        for p in &a.points {
            if mb.intersects(p) {
                out.points.push(*p);
            }
        }
    }

    if !a.polygons.is_empty() && !b.points.is_empty() {
        let ma = MultiPolygon::new(a.polygons.clone());
        for p in &b.points {
            if ma.intersects(p) {
                out.points.push(*p);
            }
        }
    }

    if !a.points.is_empty() && !b.lines.is_empty() {
        let mls = MultiLineString::new(b.lines.clone());
        for p in &a.points {
            if mls.intersects(p) {
                out.points.push(*p);
            }
        }
    }

    if !a.lines.is_empty() && !b.points.is_empty() {
        let mls = MultiLineString::new(a.lines.clone());
        for p in &b.points {
            if mls.intersects(p) {
                out.points.push(*p);
            }
        }
    }

    if !a.points.is_empty() && !b.points.is_empty() {
        for pa in &a.points {
            for pb in &b.points {
                if pa.x() == pb.x() && pa.y() == pb.y() {
                    out.points.push(*pa);
                    break;
                }
            }
        }
    }

    if !a.lines.is_empty() && !b.lines.is_empty() {
        intersect_lines_into(&a.lines, &b.lines, &mut out);
    }

    out
}

/// Naive O(n*m) pairwise segment-intersection sweep. Sufficient for typical
/// LineString sizes. A Bentley-Ottmann sweep would only pay off for very
/// long, very sparse-intersection inputs.
fn intersect_lines_into(a: &[LineString<f64>], b: &[LineString<f64>], out: &mut GeometryBag) {
    let mut collinear: Vec<LineString<f64>> = Vec::new();
    for la in a {
        for seg_a in la.lines() {
            for lb in b {
                for seg_b in lb.lines() {
                    match line_intersection(seg_a, seg_b) {
                        Some(LineIntersection::SinglePoint { intersection, .. }) => {
                            out.points.push(Point::new(intersection.x, intersection.y));
                        }
                        Some(LineIntersection::Collinear { intersection }) => {
                            collinear.push(LineString::from(vec![
                                (intersection.start.x, intersection.start.y),
                                (intersection.end.x, intersection.end.y),
                            ]));
                        }
                        None => {}
                    }
                }
            }
        }
    }
    out.lines.extend(collinear);
}

fn coord_cmp(a: &Point<f64>, b: &Point<f64>) -> Ordering {
    a.x()
        .partial_cmp(&b.x())
        .unwrap_or(Ordering::Equal)
        .then(a.y().partial_cmp(&b.y()).unwrap_or(Ordering::Equal))
}

fn pack(bag: GeometryBag) -> Geometry<f64> {
    let GeometryBag {
        mut points,
        lines,
        polygons,
    } = bag;

    points.sort_by(coord_cmp);
    points.dedup_by(|a, b| a.x() == b.x() && a.y() == b.y());

    let has_points = !points.is_empty();
    let has_lines = !lines.is_empty();
    let has_polygons = !polygons.is_empty();
    let kinds = (has_points as u8) + (has_lines as u8) + (has_polygons as u8);

    if kinds == 0 {
        return Geometry::GeometryCollection(GeometryCollection::new_from(vec![]));
    }

    if kinds > 1 {
        let mut parts: Vec<Geometry<f64>> = Vec::new();
        if points.len() == 1 {
            parts.push(Geometry::Point(points.into_iter().next().unwrap()));
        } else if !points.is_empty() {
            parts.push(Geometry::MultiPoint(MultiPoint::new(points)));
        }
        if lines.len() == 1 {
            parts.push(Geometry::LineString(lines.into_iter().next().unwrap()));
        } else if !lines.is_empty() {
            parts.push(Geometry::MultiLineString(MultiLineString::new(lines)));
        }
        if polygons.len() == 1 {
            parts.push(Geometry::Polygon(polygons.into_iter().next().unwrap()));
        } else if !polygons.is_empty() {
            parts.push(Geometry::MultiPolygon(MultiPolygon::new(polygons)));
        }
        return Geometry::GeometryCollection(GeometryCollection::new_from(parts));
    }

    if has_points {
        return if points.len() == 1 {
            Geometry::Point(points.into_iter().next().unwrap())
        } else {
            Geometry::MultiPoint(MultiPoint::new(points))
        };
    }
    if has_lines {
        return if lines.len() == 1 {
            Geometry::LineString(lines.into_iter().next().unwrap())
        } else {
            Geometry::MultiLineString(MultiLineString::new(lines))
        };
    }
    if polygons.len() == 1 {
        Geometry::Polygon(polygons.into_iter().next().unwrap())
    } else {
        Geometry::MultiPolygon(MultiPolygon::new(polygons))
    }
}

/// ST_Union: compute the geometric union of two polygon geometries.
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_union;
/// use sqlitegis::core::functions::io::geom_from_text;
/// use sqlitegis::core::functions::measurement::st_area;
///
/// let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
/// let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
/// let u = st_union(&a, &b).unwrap();
/// assert!((st_area(&u).unwrap() - 6.0).abs() < 1e-10);
/// ```
pub fn st_union(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
    super::catch_geo("ST_Union", || {
        // MBR-only fastpath. If both bboxes exist and are disjoint, the
        // union is simply the concatenation of both polygon lists. We splice
        // the input EWKB bytes directly without decoding, which is several
        // times faster than the decode + Vec + serialize path.
        if let (Ok(Some(ra)), Ok(Some(rb))) = (extract_mbr(a), extract_mbr(b)) {
            if !ra.intersects(&rb) {
                return concat_multipolygon_bodies(a, b);
            }
        }
        binary_polygon_op(a, b, |ma, mb| ma.union(mb))
    })
}

/// ST_Intersection: compute the geometric intersection of two geometries.
///
/// Accepts any combination of Point, LineString, Polygon, their Multi*
/// variants, and GeometryCollection on either side. The result is packed
/// into the smallest matching variant (single primitive, single Multi*,
/// or GeometryCollection for mixed-dimension results). Disjoint inputs
/// return an empty GeometryCollection.
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_intersection;
/// use sqlitegis::core::functions::io::geom_from_text;
/// use sqlitegis::core::functions::measurement::st_area;
///
/// let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
/// let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
/// let i = st_intersection(&a, &b).unwrap();
/// assert!((st_area(&i).unwrap() - 2.0).abs() < 1e-10);
/// ```
///
/// Point inside polygon returns the point:
///
/// ```
/// use sqlitegis::core::functions::operations::st_intersection;
/// use sqlitegis::core::functions::io::{as_text, geom_from_text};
///
/// let pt = geom_from_text("POINT(1 1)", None).unwrap();
/// let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
/// let r = st_intersection(&pt, &poly).unwrap();
/// assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
/// ```
///
/// Two crossing line strings yield the crossing point:
///
/// ```
/// use sqlitegis::core::functions::operations::st_intersection;
/// use sqlitegis::core::functions::io::{as_text, geom_from_text};
///
/// let a = geom_from_text("LINESTRING(0 0,2 2)", None).unwrap();
/// let b = geom_from_text("LINESTRING(0 2,2 0)", None).unwrap();
/// let r = st_intersection(&a, &b).unwrap();
/// assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
/// ```
pub fn st_intersection(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
    super::catch_geo("ST_Intersection", || {
        if let (Ok(Some(ra)), Ok(Some(rb))) = (extract_mbr(a), extract_mbr(b)) {
            if !ra.intersects(&rb) {
                let empty = Geometry::GeometryCollection(GeometryCollection::new_from(vec![]));
                return write_ewkb(&empty, extract_srid(a));
            }
        }
        let (ga, gb, srid) = parse_ewkb_pair(a, b)?;
        let mut bag_a = GeometryBag::new();
        let mut bag_b = GeometryBag::new();
        decompose_into(ga, &mut bag_a)?;
        decompose_into(gb, &mut bag_b)?;
        let result = intersect_bags(&bag_a, &bag_b);
        write_ewkb(&pack(result), srid)
    })
}

/// ST_Difference: compute the geometric difference (A minus B) of two polygon geometries.
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_difference;
/// use sqlitegis::core::functions::io::geom_from_text;
/// use sqlitegis::core::functions::measurement::st_area;
///
/// let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
/// let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
/// let d = st_difference(&a, &b).unwrap();
/// assert!((st_area(&d).unwrap() - 2.0).abs() < 1e-10);
/// ```
pub fn st_difference(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
    super::catch_geo("ST_Difference", || {
        binary_polygon_op(a, b, |ma, mb| ma.difference(mb))
    })
}

/// ST_SymDifference: compute the symmetric difference (XOR) of two polygon geometries.
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_sym_difference;
/// use sqlitegis::core::functions::io::geom_from_text;
/// use sqlitegis::core::functions::measurement::st_area;
///
/// let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
/// let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
/// let sd = st_sym_difference(&a, &b).unwrap();
/// assert!((st_area(&sd).unwrap() - 4.0).abs() < 1e-10);
/// ```
pub fn st_sym_difference(a: &[u8], b: &[u8]) -> Result<Vec<u8>> {
    super::catch_geo("ST_SymDifference", || {
        // MBR-only fastpath. Symmetric difference of disjoint geometries is
        // their union (XOR of non-overlapping sets is the full pair). Same
        // bytes-only splice as `st_union`.
        if let (Ok(Some(ra)), Ok(Some(rb))) = (extract_mbr(a), extract_mbr(b)) {
            if !ra.intersects(&rb) {
                return concat_multipolygon_bodies(a, b);
            }
        }
        binary_polygon_op(a, b, |ma, mb| ma.xor(mb))
    })
}

/// ST_Buffer: expand or shrink a geometry by a given distance.
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_buffer;
/// use sqlitegis::core::functions::constructors::st_point;
/// use sqlitegis::core::functions::measurement::st_area;
///
/// let pt = st_point(0.0, 0.0, None).unwrap();
/// let buffered = st_buffer(&pt, 1.0).unwrap();
/// let area = st_area(&buffered).unwrap();
/// // Area of a circle with radius 1 approximately  pi
/// assert!((area - std::f64::consts::PI).abs() < 0.1);
/// ```
pub fn st_buffer(blob: &[u8], distance: f64) -> Result<Vec<u8>> {
    super::catch_geo("ST_Buffer", || {
        let (geom, srid) = parse_ewkb(blob)?;
        if is_empty_geometry(&geom) {
            let empty = Geometry::Polygon(geo::Polygon::new(geo::LineString::new(vec![]), vec![]));
            return write_ewkb(&empty, srid);
        }
        let result = geom.buffer(distance);
        let mut polygons = result.0;
        let out_geom = match polygons.len() {
            0 => Geometry::Polygon(geo::Polygon::new(geo::LineString::new(vec![]), vec![])),
            1 => {
                let polygon = polygons.pop().ok_or_else(|| {
                    SqliteGisError::InvalidInput(
                        "buffer result unexpectedly missing single polygon".to_string(),
                    )
                })?;
                Geometry::Polygon(polygon)
            }
            _ => Geometry::MultiPolygon(MultiPolygon::new(polygons)),
        };
        write_ewkb(&out_geom, srid)
    })
}

/// Reject a segment length that is not a positive real number, the way
/// PostGIS does with "invalid max_distance 0 (must be >= 0)".
fn require_segment_length(value: f64, fn_name: &str) -> Result<f64> {
    if !value.is_finite() || value <= 0.0 {
        return Err(SqliteGisError::InvalidInput(format!(
            "{fn_name}: max_segment_length must be finite and greater than 0, got {value}"
        )));
    }
    Ok(value)
}

/// Split every segment of a geometry so none is longer than `max`, measured
/// in `metric`. `geo` densifies lineal and areal shapes, which is the same
/// set PostGIS actually changes.
fn densify_geometry<M: Densify<f64>>(
    geom: &Geometry<f64>,
    metric: &M,
    max: f64,
) -> Result<Geometry<f64>> {
    Ok(match geom {
        Geometry::LineString(ls) => Geometry::LineString(metric.densify(ls, max)),
        Geometry::MultiLineString(mls) => Geometry::MultiLineString(metric.densify(mls, max)),
        Geometry::Polygon(p) => Geometry::Polygon(metric.densify(p, max)),
        Geometry::MultiPolygon(mp) => Geometry::MultiPolygon(metric.densify(mp, max)),
        other => {
            return Err(SqliteGisError::wrong_type(
                "LineString, MultiLineString, Polygon or MultiPolygon",
                other,
            ))
        }
    })
}

/// ST_Segmentize: insert vertices so no segment is longer than
/// `max_segment_length`, in the units of the CRS.
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_segmentize;
/// use sqlitegis::core::functions::io::{as_text, geom_from_text};
///
/// let line = geom_from_text("LINESTRING(0 0, 4 0)", None).unwrap();
/// let dense = st_segmentize(&line, 2.0).unwrap();
/// assert_eq!(as_text(&dense).unwrap(), "LINESTRING(0 0,2 0,4 0)");
/// ```
pub fn st_segmentize(blob: &[u8], max_segment_length: f64) -> Result<Vec<u8>> {
    let max = require_segment_length(max_segment_length, "ST_Segmentize")?;
    let (geom, srid) = parse_ewkb(blob)?;
    write_ewkb(&densify_geometry(&geom, &Euclidean, max)?, srid)
}

/// ST_SegmentizeSphere: `ST_Segmentize` with the limit in metres along a
/// sphere of the mean earth radius (SRID 4326).
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_segmentize_sphere;
/// use sqlitegis::core::functions::io::geom_from_text;
///
/// let line = geom_from_text("LINESTRING(0 0, 90 60)", Some(4326)).unwrap();
/// assert!(st_segmentize_sphere(&line, 3_000_000.0).is_ok());
/// ```
pub fn st_segmentize_sphere(blob: &[u8], max_segment_length: f64) -> Result<Vec<u8>> {
    let max = require_segment_length(max_segment_length, "ST_SegmentizeSphere")?;
    let (geom, srid) = parse_geographic("ST_SegmentizeSphere", blob)?;
    write_ewkb(&densify_geometry(&geom, &Haversine, max)?, srid)
}

/// ST_SegmentizeSpheroid: `ST_Segmentize` with the limit in metres along the
/// WGS84 ellipsoid (Karney, SRID 4326).
///
/// # Example
///
/// ```
/// use sqlitegis::core::functions::operations::st_segmentize_spheroid;
/// use sqlitegis::core::functions::io::geom_from_text;
///
/// let line = geom_from_text("LINESTRING(0 0, 90 60)", Some(4326)).unwrap();
/// assert!(st_segmentize_spheroid(&line, 3_000_000.0).is_ok());
/// ```
pub fn st_segmentize_spheroid(blob: &[u8], max_segment_length: f64) -> Result<Vec<u8>> {
    let max = require_segment_length(max_segment_length, "ST_SegmentizeSpheroid")?;
    let (geom, srid) = parse_geographic("ST_SegmentizeSpheroid", blob)?;
    write_ewkb(&densify_geometry(&geom, &Geodesic, max)?, srid)
}

/// Parse for the curved segmentize forms: SRID 4326 and every vertex latitude
/// in range, matching the rest of the curved-earth surface.
fn parse_geographic(fn_name: &str, blob: &[u8]) -> Result<(Geometry<f64>, Option<i32>)> {
    use crate::core::functions::measurement::{
        ensure_geographic_srid, require_geographic_line_latitudes,
    };
    let (geom, srid) = parse_ewkb(blob)?;
    ensure_geographic_srid(srid, fn_name)?;
    match &geom {
        Geometry::LineString(ls) => require_geographic_line_latitudes(ls, fn_name)?,
        Geometry::MultiLineString(mls) => {
            for ls in &mls.0 {
                require_geographic_line_latitudes(ls, fn_name)?;
            }
        }
        Geometry::Polygon(p) => require_geographic_polygon_rings(p, fn_name)?,
        Geometry::MultiPolygon(mp) => {
            for p in &mp.0 {
                require_geographic_polygon_rings(p, fn_name)?;
            }
        }
        _ => {}
    }
    Ok((geom, srid))
}

fn require_geographic_polygon_rings(poly: &Polygon<f64>, fn_name: &str) -> Result<()> {
    use crate::core::functions::measurement::require_geographic_line_latitudes;
    require_geographic_line_latitudes(poly.exterior(), fn_name)?;
    for ring in poly.interiors() {
        require_geographic_line_latitudes(ring, fn_name)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::functions::accessors::{st_geometry_type, st_is_empty};
    use crate::core::functions::constructors::st_point;
    use crate::core::functions::io::geom_from_text;
    use crate::core::functions::measurement::{st_area, st_point_on_surface};

    /// Fuzzer-reduced degenerate-but-finite polygon that makes `i_overlay` and
    /// geo's sweeps `assert!` and abort. The guard must turn that into an error.
    const DEGENERATE_POLYGON: &[u8] = &[
        1, 3, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 229, 129, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 129, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 129, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 229, 229,
        229, 229, 229, 229, 229, 77, 229, 229, 28, 229, 229, 229, 229, 229, 229, 229, 229,
    ];

    /// Every op must return, never abort, on the degenerate polygon above.
    #[test]
    fn degenerate_geometry_never_panics() {
        let g = DEGENERATE_POLYGON;
        let _ = st_buffer(g, 1.0);
        let _ = st_union(g, g);
        let _ = st_intersection(g, g);
        let _ = st_difference(g, g);
        let _ = st_sym_difference(g, g);
        let _ = st_point_on_surface(g);
    }

    #[test]
    fn union_overlapping() {
        let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
        let u = st_union(&a, &b).unwrap();
        assert!((st_area(&u).unwrap() - 6.0).abs() < 1e-10);
    }

    #[test]
    fn intersection_overlapping() {
        let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
        let i = st_intersection(&a, &b).unwrap();
        assert!((st_area(&i).unwrap() - 2.0).abs() < 1e-10);
    }

    #[test]
    fn difference_overlapping() {
        let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
        let d = st_difference(&a, &b).unwrap();
        assert!((st_area(&d).unwrap() - 2.0).abs() < 1e-10);
    }

    #[test]
    fn sym_difference_overlapping() {
        let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
        let sd = st_sym_difference(&a, &b).unwrap();
        assert!((st_area(&sd).unwrap() - 4.0).abs() < 1e-10);
    }

    #[test]
    fn buffer_point() {
        let pt = st_point(0.0, 0.0, None).unwrap();
        let buffered = st_buffer(&pt, 1.0).unwrap();
        let area = st_area(&buffered).unwrap();
        assert!((area - std::f64::consts::PI).abs() < 0.1);
        assert_eq!(st_geometry_type(&buffered).unwrap(), "ST_Polygon");
    }

    #[test]
    fn buffer_multipoint_returns_multipolygon_for_disconnected_components() {
        let mp = geom_from_text("MULTIPOINT((0 0),(10 0))", None).unwrap();
        let buffered = st_buffer(&mp, 1.0).unwrap();
        assert_eq!(st_geometry_type(&buffered).unwrap(), "ST_MultiPolygon");
    }

    #[test]
    fn union_wrong_type() {
        let line = geom_from_text("LINESTRING(0 0,1 1)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,1 0,1 1,0 1,0 0))", None).unwrap();
        assert!(st_union(&line, &poly).is_err());
    }

    #[test]
    fn union_accepts_multipolygon_inputs() {
        let mp = geom_from_text("MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0)))", None).unwrap();
        let poly = geom_from_text("POLYGON((1 0,2 0,2 1,1 1,1 0))", None).unwrap();
        let u = st_union(&mp, &poly).unwrap();
        assert!(st_area(&u).unwrap() > 1.0);
    }

    #[test]
    fn buffer_negative_shrinks() {
        let poly = geom_from_text("POLYGON((0 0,10 0,10 10,0 10,0 0))", None).unwrap();
        let shrunk = st_buffer(&poly, -1.0).unwrap();
        let area = st_area(&shrunk).unwrap();
        assert!(area < 100.0 && area > 0.0);
    }

    #[test]
    fn buffer_empty_polygon_returns_empty_polygon() {
        let empty = geom_from_text("POLYGON EMPTY", None).unwrap();
        let buffered = st_buffer(&empty, 1.0).unwrap();
        assert_eq!(st_geometry_type(&buffered).unwrap(), "ST_Polygon");
        assert!(st_is_empty(&buffered).unwrap());
    }

    #[test]
    fn buffer_empty_point_returns_empty_polygon() {
        let empty = geom_from_text("POINT EMPTY", None).unwrap();
        let buffered = st_buffer(&empty, 1.0).unwrap();
        assert_eq!(st_geometry_type(&buffered).unwrap(), "ST_Polygon");
        assert!(st_is_empty(&buffered).unwrap());
    }

    use crate::core::functions::io::as_text;

    #[test]
    fn intersection_point_point_match() {
        let a = geom_from_text("POINT(1 2)", None).unwrap();
        let b = geom_from_text("POINT(1 2)", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 2)");
    }

    #[test]
    fn intersection_point_point_disjoint_is_empty() {
        let a = geom_from_text("POINT(1 2)", None).unwrap();
        let b = geom_from_text("POINT(3 4)", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert!(st_is_empty(&r).unwrap());
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_GeometryCollection");
    }

    #[test]
    fn intersection_point_in_polygon() {
        let pt = geom_from_text("POINT(1 1)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&pt, &poly).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_point_outside_polygon_is_empty() {
        let pt = geom_from_text("POINT(5 5)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&pt, &poly).unwrap();
        assert!(st_is_empty(&r).unwrap());
    }

    #[test]
    fn intersection_polygon_point_swapped() {
        let pt = geom_from_text("POINT(1 1)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&poly, &pt).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_point_on_linestring() {
        let pt = geom_from_text("POINT(1 1)", None).unwrap();
        let ls = geom_from_text("LINESTRING(0 0,2 2)", None).unwrap();
        let r = st_intersection(&pt, &ls).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_point_off_linestring_is_empty() {
        let pt = geom_from_text("POINT(1 0)", None).unwrap();
        let ls = geom_from_text("LINESTRING(0 0,2 2)", None).unwrap();
        let r = st_intersection(&pt, &ls).unwrap();
        assert!(st_is_empty(&r).unwrap());
    }

    #[test]
    fn intersection_linestring_polygon_clips() {
        let ls = geom_from_text("LINESTRING(-1 1,3 1)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&ls, &poly).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_LineString");
        assert_eq!(as_text(&r).unwrap(), "LINESTRING(0 1,2 1)");
    }

    #[test]
    fn intersection_polygon_linestring_swapped_clips() {
        let ls = geom_from_text("LINESTRING(-1 1,3 1)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&poly, &ls).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_LineString");
        assert_eq!(as_text(&r).unwrap(), "LINESTRING(0 1,2 1)");
    }

    #[test]
    fn intersection_linestring_disjoint_polygon_is_empty() {
        let ls = geom_from_text("LINESTRING(10 10,20 20)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&ls, &poly).unwrap();
        assert!(st_is_empty(&r).unwrap());
    }

    #[test]
    fn intersection_two_crossing_linestrings_point() {
        let a = geom_from_text("LINESTRING(0 0,2 2)", None).unwrap();
        let b = geom_from_text("LINESTRING(0 2,2 0)", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_collinear_linestrings_yield_overlap_linestring() {
        let a = geom_from_text("LINESTRING(0 0,4 0)", None).unwrap();
        let b = geom_from_text("LINESTRING(2 0,6 0)", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_LineString");
        assert_eq!(as_text(&r).unwrap(), "LINESTRING(2 0,4 0)");
    }

    #[test]
    fn intersection_parallel_linestrings_disjoint_is_empty() {
        let a = geom_from_text("LINESTRING(0 0,2 0)", None).unwrap();
        let b = geom_from_text("LINESTRING(0 1,2 1)", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert!(st_is_empty(&r).unwrap());
    }

    #[test]
    fn intersection_multipoint_polygon_keeps_inside_points() {
        let mp = geom_from_text("MULTIPOINT((1 1),(5 5),(0 0))", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&mp, &poly).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_MultiPoint");
        let text = as_text(&r).unwrap();
        assert!(text.contains("0 0"), "actual: {text}");
        assert!(text.contains("1 1"), "actual: {text}");
        assert!(!text.contains("5 5"), "actual: {text}");
    }

    #[test]
    fn intersection_geometrycollection_input_dispatches_per_part() {
        let gc =
            geom_from_text("GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(-1 1,3 1))", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&gc, &poly).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_GeometryCollection");
        let text = as_text(&r).unwrap();
        assert!(text.contains("POINT(1 1)"));
        assert!(text.contains("LINESTRING(0 1,2 1)"));
    }

    #[test]
    fn intersection_rejects_unsupported_type() {
        let pt = geom_from_text("POINT(0 0)", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let mut bag = GeometryBag::new();
        let rect = Geometry::Rect(geo::Rect::new(
            geo::Coord { x: 0.0, y: 0.0 },
            geo::Coord { x: 1.0, y: 1.0 },
        ));
        assert!(decompose_into(rect, &mut bag).is_err());
        let _ = (pt, poly);
    }

    #[test]
    fn intersection_polygon_multipoint_swapped_filters_outside() {
        let mp = geom_from_text("MULTIPOINT((1 1),(5 5),(0 0))", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&poly, &mp).unwrap();
        let text = as_text(&r).unwrap();
        assert!(text.contains("0 0") && text.contains("1 1"));
        assert!(!text.contains("5 5"));
    }

    #[test]
    fn intersection_multipoint_linestring_keeps_on_line_points() {
        let mp = geom_from_text("MULTIPOINT((1 1),(5 5))", None).unwrap();
        let ls = geom_from_text("LINESTRING(0 0,2 2)", None).unwrap();
        let r = st_intersection(&mp, &ls).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_linestring_multipoint_swapped_keeps_on_line_points() {
        let mp = geom_from_text("MULTIPOINT((1 1),(5 5))", None).unwrap();
        let ls = geom_from_text("LINESTRING(0 0,2 2)", None).unwrap();
        let r = st_intersection(&ls, &mp).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_disjoint_multipolygon_yields_multipolygon() {
        let a = geom_from_text(
            "MULTIPOLYGON(((0 0,2 0,2 2,0 2,0 0)),((10 0,12 0,12 2,10 2,10 0)))",
            None,
        )
        .unwrap();
        let b = geom_from_text(
            "MULTIPOLYGON(((1 0,3 0,3 2,1 2,1 0)),((11 0,13 0,13 2,11 2,11 0)))",
            None,
        )
        .unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_MultiPolygon");
    }

    #[test]
    fn intersection_multilinestring_polygon_yields_multilinestring() {
        let mls = geom_from_text("MULTILINESTRING((-1 1,3 1),(-1 0.5,3 0.5))", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&mls, &poly).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_MultiLineString");
    }

    #[test]
    fn intersection_geometrycollection_mixed_multi_parts() {
        let gc = geom_from_text(
            "GEOMETRYCOLLECTION(MULTIPOINT((1 1),(0 0)),MULTILINESTRING((-1 1,3 1),(-1 0.5,3 0.5)))",
            None,
        )
        .unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&gc, &poly).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_GeometryCollection");
        let text = as_text(&r).unwrap();
        assert!(text.contains("MULTIPOINT"), "actual: {text}");
        assert!(text.contains("MULTILINESTRING"), "actual: {text}");
    }

    #[test]
    fn intersection_nested_geometrycollection_recurses() {
        let gc =
            geom_from_text("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(1 1)))", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&gc, &poly).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_empty_inputs_decompose_cleanly() {
        let empty_mp = geom_from_text("MULTIPOINT EMPTY", None).unwrap();
        let poly = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let r = st_intersection(&empty_mp, &poly).unwrap();
        assert!(st_is_empty(&r).unwrap());

        let empty_mls = geom_from_text("MULTILINESTRING EMPTY", None).unwrap();
        let r2 = st_intersection(&empty_mls, &poly).unwrap();
        assert!(st_is_empty(&r2).unwrap());

        let empty_mpoly = geom_from_text("MULTIPOLYGON EMPTY", None).unwrap();
        let r3 = st_intersection(&empty_mpoly, &poly).unwrap();
        assert!(st_is_empty(&r3).unwrap());
    }

    #[test]
    fn sym_difference_disjoint_concatenates_polygons() {
        let a = geom_from_text("POLYGON((0 0,1 0,1 1,0 1,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((10 10,11 10,11 11,10 11,10 10))", None).unwrap();
        let sd = st_sym_difference(&a, &b).unwrap();
        assert!((st_area(&sd).unwrap() - 2.0).abs() < 1e-10);
        assert_eq!(st_geometry_type(&sd).unwrap(), "ST_MultiPolygon");
    }

    #[test]
    fn union_disjoint_concatenates_polygons() {
        let a = geom_from_text("POLYGON((0 0,1 0,1 1,0 1,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((10 10,11 10,11 11,10 11,10 10))", None).unwrap();
        let u = st_union(&a, &b).unwrap();
        // Two unit squares: total area 2.0.
        assert!((st_area(&u).unwrap() - 2.0).abs() < 1e-10);
        assert_eq!(st_geometry_type(&u).unwrap(), "ST_MultiPolygon");
    }

    #[test]
    fn intersection_mbr_disjoint_returns_empty_geometrycollection() {
        let a = geom_from_text("POLYGON((0 0,1 0,1 1,0 1,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((10 10,11 10,11 11,10 11,10 10))", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_GeometryCollection");
        assert!(st_is_empty(&r).unwrap());
    }

    #[test]
    fn intersection_disconnected_linestring_polygon_yields_multilinestring() {
        let ls = geom_from_text("LINESTRING(-1 1,3 1,3 5,5 5,5 1,7 1)", None).unwrap();
        let poly_a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let poly_b = geom_from_text("POLYGON((4 0,6 0,6 2,4 2,4 0))", None).unwrap();
        let mp = geom_from_text(
            "MULTIPOLYGON(((0 0,2 0,2 2,0 2,0 0)),((4 0,6 0,6 2,4 2,4 0)))",
            None,
        )
        .unwrap();
        let r = st_intersection(&ls, &mp).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_MultiLineString");
        let _ = (poly_a, poly_b);
    }
    #[test]
    fn intersection_multipoint_multipoint_shared_point() {
        let a = geom_from_text("MULTIPOINT((0 0),(1 1))", None).unwrap();
        let b = geom_from_text("MULTIPOINT((1 1),(2 2))", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert_eq!(as_text(&r).unwrap(), "POINT(1 1)");
    }

    #[test]
    fn intersection_parallel_linestrings_no_intersection() {
        let a = geom_from_text("LINESTRING(0 0,0 10)", None).unwrap();
        let b = geom_from_text("LINESTRING(1 0,1 10)", None).unwrap();
        let r = st_intersection(&a, &b).unwrap();
        assert!(st_is_empty(&r).unwrap());
    }

    #[test]
    fn union_overlapping_slow_path() {
        let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((1 1,3 1,3 3,1 3,1 1))", None).unwrap();
        let u = st_union(&a, &b).unwrap();
        assert!((st_area(&u).unwrap() - 7.0).abs() < 1e-10);
    }

    #[test]
    fn sym_difference_overlapping_slow_path() {
        let a = geom_from_text("POLYGON((0 0,2 0,2 2,0 2,0 0))", None).unwrap();
        let b = geom_from_text("POLYGON((1 0,3 0,3 2,1 2,1 0))", None).unwrap();
        let sd = st_sym_difference(&a, &b).unwrap();
        assert!((st_area(&sd).unwrap() - 4.0).abs() < 1e-10);
    }

    #[test]
    fn buffer_negative_large_distance_empty_result() {
        let poly = geom_from_text("POLYGON((0 0,1 0,1 1,0 1,0 0))", None).unwrap();
        let result = st_buffer(&poly, -10.0).unwrap();
        assert_eq!(st_geometry_type(&result).unwrap(), "ST_Polygon");
        assert!(st_is_empty(&result).unwrap());
    }

    #[test]
    fn intersection_geometrycollection_mixed_point_and_polygon() {
        let gc = geom_from_text(
            "GEOMETRYCOLLECTION(POINT(0.5 0.5), POLYGON((0 0,1 0,1 1,0 1,0 0)))",
            None,
        )
        .unwrap();
        let r = st_intersection(&gc, &gc).unwrap();
        assert_eq!(st_geometry_type(&r).unwrap(), "ST_GeometryCollection");
        let text = as_text(&r).unwrap();
        assert!(text.contains("POINT(0.5 0.5)"), "actual: {text}");
        assert!(text.contains("POLYGON"), "actual: {text}");
    }

    // -- Segmentize ---------------------------------------------------

    /// Reference values are PostGIS 3.5 readings. Its `geography` form
    /// measures on the sphere, so `ST_SegmentizeSpheroid` has no PostGIS
    /// counterpart to read and is pinned to the geodesic interpolation of
    /// the same line instead.
    fn assert_wkt_close(got: &str, want: &str) {
        let nums = |s: &str| -> Vec<f64> {
            s.split(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-' || c == 'e'))
                .filter(|t| !t.is_empty())
                .filter_map(|t| t.parse::<f64>().ok())
                .collect()
        };
        let (g, w) = (nums(got), nums(want));
        assert_eq!(g.len(), w.len(), "shape differs: got {got}, want {want}");
        for (a, b) in g.iter().zip(w.iter()) {
            assert!((a - b).abs() < 1e-9, "got {got}, want {want}");
        }
    }

    #[test]
    fn segmentize_planar_matches_postgis() {
        let line = geom_from_text("LINESTRING(0 0, 90 60)", None).unwrap();
        let out = st_segmentize(&line, 30.0).unwrap();
        assert_wkt_close(
            &as_text(&out).unwrap(),
            "LINESTRING(0 0,22.5 15,45 30,67.5 45,90 60)",
        );
    }

    #[test]
    fn segmentize_sphere_matches_postgis_geography() {
        let line = geom_from_text("LINESTRING(0 0, 90 60)", Some(4326)).unwrap();
        let out = st_segmentize_sphere(&line, 3_000_000.0).unwrap();
        assert_wkt_close(
            &as_text(&out).unwrap(),
            "LINESTRING(0 0,11.700919508154 19.35459615165,26.565051177078 37.761243907035,50.360727762244 53.139953123517,90 60)",
        );
    }

    #[test]
    fn segmentize_spheroid_lands_on_geodesic_interpolations() {
        let line = geom_from_text("LINESTRING(0 0, 90 60)", Some(4326)).unwrap();
        let out = st_segmentize_spheroid(&line, 3_000_000.0).unwrap();
        // PostGIS readings of ST_LineInterpolatePoint(geography, f, true).
        assert_wkt_close(
            &as_text(&out).unwrap(),
            "LINESTRING(0 0,11.716741571563 19.435698270019,26.606325079977 37.874403088301,50.432454297567 53.213438567481,90 60)",
        );
    }

    #[test]
    fn segmentize_leaves_a_short_enough_line_alone() {
        let line = geom_from_text("LINESTRING(0 0, 1 0)", None).unwrap();
        let out = st_segmentize(&line, 10.0).unwrap();
        assert_wkt_close(&as_text(&out).unwrap(), "LINESTRING(0 0,1 0)");
    }

    #[test]
    fn segmentize_densifies_polygon_rings() {
        let poly = geom_from_text("POLYGON((0 0,1 0,1 1,0 1,0 0))", None).unwrap();
        let out = st_segmentize(&poly, 0.6).unwrap();
        assert_wkt_close(
            &as_text(&out).unwrap(),
            "POLYGON((0 0,0.5 0,1 0,1 0.5,1 1,0.5 1,0 1,0 0.5,0 0))",
        );
    }

    #[test]
    fn segmentize_rejects_a_non_positive_length() {
        for bad in [0.0, -1.0, f64::NAN, f64::INFINITY] {
            let line = geom_from_text("LINESTRING(0 0, 1 1)", None).unwrap();
            let err = st_segmentize(&line, bad).expect_err("must reject");
            assert!(
                format!("{err}").contains("max_segment_length"),
                "unexpected error: {err}"
            );
        }
    }

    #[test]
    fn segmentize_rejects_shapes_without_segments() {
        let pt = geom_from_text("POINT(0 0)", None).unwrap();
        let geographic_pt = geom_from_text("POINT(0 0)", Some(4326)).unwrap();
        // The curved forms reach the same rejection past their SRID and
        // latitude guards, which have no ring to walk on a Point.
        for err in [
            st_segmentize(&pt, 1.0).expect_err("a point has no segments"),
            st_segmentize_sphere(&geographic_pt, 1000.0).expect_err("a point has no segments"),
            st_segmentize_spheroid(&geographic_pt, 1000.0).expect_err("a point has no segments"),
        ] {
            assert!(
                format!("{err}").contains("LineString"),
                "unexpected error: {err}"
            );
        }
    }

    #[test]
    fn curved_segmentize_requires_srid_4326() {
        let line = geom_from_text("LINESTRING(0 0, 1 1)", None).unwrap();
        for err in [
            st_segmentize_sphere(&line, 1000.0).expect_err("needs SRID"),
            st_segmentize_spheroid(&line, 1000.0).expect_err("needs SRID"),
        ] {
            assert!(
                format!("{err}").contains("requires SRID 4326"),
                "unexpected error: {err}"
            );
        }
    }

    #[test]
    fn curved_segmentize_rejects_out_of_range_latitude() {
        let bad = geom_from_text("LINESTRING(0 0, 10 95)", Some(4326)).unwrap();
        assert!(st_segmentize_sphere(&bad, 100_000.0).is_err());
        assert!(st_segmentize_spheroid(&bad, 100_000.0).is_err());
    }

    #[test]
    fn segmentize_planar_handles_multi_part_shapes() {
        let multiline = geom_from_text("MULTILINESTRING((0 0,4 0),(0 2,2 2))", None).unwrap();
        assert_wkt_close(
            &as_text(&st_segmentize(&multiline, 2.0).unwrap()).unwrap(),
            "MULTILINESTRING((0 0,2 0,4 0),(0 2,2 2))",
        );

        let multipoly = geom_from_text(
            "MULTIPOLYGON(((0 0,2 0,2 2,0 2,0 0)),((5 5,7 5,7 7,5 7,5 5)))",
            None,
        )
        .unwrap();
        assert_wkt_close(
            &as_text(&st_segmentize(&multipoly, 2.0).unwrap()).unwrap(),
            "MULTIPOLYGON(((0 0,2 0,2 2,0 2,0 0)),((5 5,7 5,7 7,5 7,5 5)))",
        );
    }

    #[test]
    fn segmentize_sphere_bulges_polygon_rings_along_great_circles() {
        // The inserted vertex on the northern edge sits at 10.0374 rather than
        // 10, because a great circle between two points at the same latitude
        // bows poleward. PostGIS reads the same value off `geography`.
        let poly = geom_from_text("POLYGON((0 0,10 0,10 10,0 10,0 0))", Some(4326)).unwrap();
        assert_wkt_close(
            &as_text(&st_segmentize_sphere(&poly, 700_000.0).unwrap()).unwrap(),
            "POLYGON((0 0,5 0,10 0,10 5,10 10,5 10.037423045911,0 10,0 5,0 0))",
        );
    }

    #[test]
    fn segmentize_sphere_handles_multilinestring() {
        let multiline =
            geom_from_text("MULTILINESTRING((0 0,10 0),(0 2,10 2))", Some(4326)).unwrap();
        assert_wkt_close(
            &as_text(&st_segmentize_sphere(&multiline, 700_000.0).unwrap()).unwrap(),
            "MULTILINESTRING((0 0,5 0,10 0),(0 2,5 2.007633435231,10 2))",
        );
    }

    #[test]
    fn segmentize_spheroid_handles_multipolygon() {
        let multipoly = geom_from_text(
            "MULTIPOLYGON(((0 0,2 0,2 2,0 2,0 0)),((5 5,7 5,7 7,5 7,5 5)))",
            Some(4326),
        )
        .unwrap();
        let out = st_segmentize_spheroid(&multipoly, 100_000.0).unwrap();
        let (geom, srid) = parse_ewkb(&out).unwrap();
        assert_eq!(srid, Some(4326));
        let Geometry::MultiPolygon(parts) = geom else {
            panic!("expected a MultiPolygon, got {geom:?}");
        };
        assert_eq!(parts.0.len(), 2);
        // Each 2-degree ring is roughly 222 km a side, so a 100 km limit has
        // to have split every side of both parts.
        for part in &parts.0 {
            assert!(
                part.exterior().0.len() > 5,
                "part was left undensified: {part:?}"
            );
        }
    }

    #[test]
    fn curved_segmentize_checks_every_ring_of_every_part() {
        // Exercises the polygon and multipolygon arms of the latitude guard,
        // including interior rings, which the LineString case never reaches.
        let bad_shell = geom_from_text("POLYGON((0 0,10 0,10 95,0 95,0 0))", Some(4326)).unwrap();
        let bad_hole = geom_from_text(
            "POLYGON((0 0,10 0,10 10,0 10,0 0),(1 1,2 1,2 95,1 95,1 1))",
            Some(4326),
        )
        .unwrap();
        let bad_part = geom_from_text(
            "MULTIPOLYGON(((0 0,2 0,2 2,0 2,0 0)),((20 20,21 20,21 95,20 95,20 20)))",
            Some(4326),
        )
        .unwrap();
        let bad_multiline =
            geom_from_text("MULTILINESTRING((0 0,1 1),(2 2,3 95))", Some(4326)).unwrap();

        for geom in [&bad_shell, &bad_hole, &bad_part, &bad_multiline] {
            for err in [
                st_segmentize_sphere(geom, 100_000.0).expect_err("out-of-range latitude"),
                st_segmentize_spheroid(geom, 100_000.0).expect_err("out-of-range latitude"),
            ] {
                assert!(
                    format!("{err}").contains("latitude"),
                    "unexpected error: {err}"
                );
            }
        }
    }
}