oxigdal-qc 0.1.6

Quality control and validation suite for OxiGDAL - Comprehensive data integrity checks for geospatial data
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
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
//! Vector topology validation.
//!
//! This module provides quality control checks for vector topology,
//! including error detection, invalid geometry identification, and repair suggestions.

use crate::error::{QcIssue, QcResult, Severity};
use crate::vector::violations::{TopologyOptions, TopologyViolation};
use oxigdal_algorithms::vector::{
    SegmentIntersection, intersect_polygons, intersect_segment_segment,
};
use oxigdal_core::vector::{
    Coordinate, FeatureCollection, FeatureId, Geometry, LineString, Polygon,
};
use oxigdal_index::{Bbox2D, RTree};
use std::collections::HashMap;

/// Helper function to convert FeatureId to String
fn feature_id_to_string(id: &FeatureId) -> String {
    match id {
        FeatureId::Integer(i) => i.to_string(),
        FeatureId::String(s) => s.clone(),
    }
}

/// Result of topology validation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TopologyResult {
    /// Total number of features checked.
    pub feature_count: usize,

    /// Number of valid geometries.
    pub valid_geometries: usize,

    /// Number of invalid geometries.
    pub invalid_geometries: usize,

    /// Topology errors detected.
    pub topology_errors: Vec<TopologyError>,

    /// Sliver polygons detected.
    pub slivers: Vec<SliverPolygon>,

    /// Duplicate geometries detected.
    pub duplicates: Vec<DuplicateGroup>,

    /// Topology rule violations.
    pub rule_violations: Vec<RuleViolation>,

    /// Quality control issues found.
    pub issues: Vec<QcIssue>,
}

/// Topology error information.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TopologyError {
    /// Feature ID where error was found.
    pub feature_id: Option<String>,

    /// Type of topology error.
    pub error_type: TopologyErrorType,

    /// Location of the error.
    pub location: Coordinate,

    /// Severity of the error.
    pub severity: Severity,

    /// Description of the error.
    pub description: String,

    /// Suggested fix.
    pub fix_suggestion: Option<String>,
}

/// Types of topology errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TopologyErrorType {
    /// Dangling edge (line that doesn't connect).
    Dangle,

    /// Overshoot (line extends past intersection).
    Overshoot,

    /// Undershoot (line doesn't reach intersection).
    Undershoot,

    /// Self-intersection.
    SelfIntersection,

    /// Invalid ring (not closed).
    InvalidRing,

    /// Invalid polygon (less than 3 points).
    InvalidPolygon,

    /// Duplicate vertex.
    DuplicateVertex,

    /// Spike (extremely sharp angle).
    Spike,

    /// Invalid coordinate (NaN or infinite).
    InvalidCoordinate,
}

/// Sliver polygon information.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SliverPolygon {
    /// Feature ID of the sliver.
    pub feature_id: Option<String>,

    /// Area of the polygon.
    pub area: f64,

    /// Perimeter of the polygon.
    pub perimeter: f64,

    /// Compactness ratio (area / perimeter^2).
    pub compactness: f64,

    /// Width of the sliver.
    pub width: f64,

    /// Severity based on size.
    pub severity: Severity,
}

/// Duplicate geometry group.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DuplicateGroup {
    /// Feature IDs in the duplicate group.
    pub feature_ids: Vec<String>,

    /// Number of duplicates.
    pub count: usize,

    /// Geometry type.
    pub geometry_type: String,
}

/// Topology rule violation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RuleViolation {
    /// Rule that was violated.
    pub rule: TopologyRule,

    /// Feature IDs involved.
    pub feature_ids: Vec<String>,

    /// Location of the violation.
    pub location: Option<Coordinate>,

    /// Severity of the violation.
    pub severity: Severity,

    /// Description of the violation.
    pub description: String,
}

/// Topology rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TopologyRule {
    /// Polygons must not overlap.
    MustNotOverlap,

    /// Polygons must not have gaps.
    MustNotHaveGaps,

    /// Lines must not cross.
    MustNotCross,

    /// Lines must not self-overlap.
    MustNotSelfOverlap,

    /// Polygons must be covered by feature class.
    MustBeCoveredBy,

    /// Boundary must be covered by.
    BoundaryMustBeCoveredBy,

    /// Must be inside.
    MustBeInside,

    /// Points must be covered by line.
    PointsMustBeCoveredByLine,
}

/// Configuration for topology checks.
#[derive(Debug, Clone)]
pub struct TopologyConfig {
    /// Minimum area threshold for sliver detection.
    pub sliver_area_threshold: f64,

    /// Maximum compactness for sliver detection (0.0 - 1.0).
    pub sliver_compactness_threshold: f64,

    /// Tolerance for coordinate comparison (degrees or meters).
    pub coordinate_tolerance: f64,

    /// Tolerance for dangle detection.
    pub dangle_tolerance: f64,

    /// Whether to check for self-intersections.
    pub check_self_intersections: bool,

    /// Whether to check for duplicates.
    pub check_duplicates: bool,

    /// Whether to check for slivers.
    pub check_slivers: bool,

    /// Topology rules to enforce.
    pub topology_rules: Vec<TopologyRule>,
}

impl Default for TopologyConfig {
    fn default() -> Self {
        Self {
            sliver_area_threshold: 1.0,
            sliver_compactness_threshold: 0.01,
            coordinate_tolerance: 1e-9,
            dangle_tolerance: 1e-6,
            check_self_intersections: true,
            check_duplicates: true,
            check_slivers: true,
            topology_rules: vec![TopologyRule::MustNotOverlap],
        }
    }
}

/// Topology checker.
pub struct TopologyChecker {
    config: TopologyConfig,
}

impl TopologyChecker {
    /// Creates a new topology checker with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self {
            config: TopologyConfig::default(),
        }
    }

    /// Creates a new topology checker with custom configuration.
    #[must_use]
    pub fn with_config(config: TopologyConfig) -> Self {
        Self { config }
    }

    /// Validates topology of a feature collection.
    ///
    /// # Errors
    ///
    /// Returns an error if validation fails.
    pub fn validate(&self, features: &FeatureCollection) -> QcResult<TopologyResult> {
        let mut issues = Vec::new();
        let mut topology_errors = Vec::new();
        let mut valid_geometries = 0;
        let mut invalid_geometries = 0;

        // Validate individual geometries
        for feature in &features.features {
            let feature_id_str = feature.id.as_ref().map(feature_id_to_string);
            if let Some(ref geometry) = feature.geometry {
                match self.validate_geometry(geometry, &feature_id_str) {
                    Ok(errors) => {
                        if errors.is_empty() {
                            valid_geometries += 1;
                        } else {
                            invalid_geometries += 1;
                            topology_errors.extend(errors);
                        }
                    }
                    Err(e) => {
                        invalid_geometries += 1;
                        issues.push(QcIssue::new(
                            Severity::Major,
                            "topology",
                            "Geometry validation error",
                            format!("Failed to validate geometry: {}", e),
                        ));
                    }
                }
            } else {
                // Feature has no geometry
                valid_geometries += 1;
            }
        }

        // Check for duplicates
        let duplicates = if self.config.check_duplicates {
            self.find_duplicates(features)?
        } else {
            Vec::new()
        };

        for dup in &duplicates {
            issues.push(
                QcIssue::new(
                    Severity::Warning,
                    "topology",
                    "Duplicate geometries detected",
                    format!(
                        "{} duplicate {} geometries found",
                        dup.count, dup.geometry_type
                    ),
                )
                .with_suggestion("Remove or merge duplicate features"),
            );
        }

        // Check for slivers
        let slivers = if self.config.check_slivers {
            self.find_slivers(features)?
        } else {
            Vec::new()
        };

        for sliver in &slivers {
            if sliver.severity >= Severity::Minor {
                issues.push(
                    QcIssue::new(
                        sliver.severity,
                        "topology",
                        "Sliver polygon detected",
                        format!(
                            "Sliver with area {} and compactness {}",
                            sliver.area, sliver.compactness
                        ),
                    )
                    .with_location(sliver.feature_id.clone().unwrap_or_default())
                    .with_suggestion("Remove or merge sliver polygon"),
                );
            }
        }

        // Check topology rules
        let rule_violations = self.check_topology_rules(features)?;

        for violation in &rule_violations {
            issues.push(
                QcIssue::new(
                    violation.severity,
                    "topology",
                    format!("Topology rule violation: {:?}", violation.rule),
                    violation.description.clone(),
                )
                .with_suggestion("Fix geometry to comply with topology rule"),
            );
        }

        // Add issues for topology errors
        for error in &topology_errors {
            if error.severity >= Severity::Minor {
                let mut issue = QcIssue::new(
                    error.severity,
                    "topology",
                    format!("Topology error: {:?}", error.error_type),
                    error.description.clone(),
                )
                .with_location(format!("({}, {})", error.location.x, error.location.y));

                if let Some(ref fix) = error.fix_suggestion {
                    issue = issue.with_suggestion(fix.clone());
                }

                issues.push(issue);
            }
        }

        Ok(TopologyResult {
            feature_count: features.features.len(),
            valid_geometries,
            invalid_geometries,
            topology_errors,
            slivers,
            duplicates,
            rule_violations,
            issues,
        })
    }

    /// Validates a single geometry.
    fn validate_geometry(
        &self,
        geometry: &Geometry,
        feature_id: &Option<String>,
    ) -> QcResult<Vec<TopologyError>> {
        let mut errors = Vec::new();

        match geometry {
            Geometry::Point(point) => {
                errors.extend(self.validate_point(&point.coord, feature_id)?);
            }
            Geometry::LineString(linestring) => {
                errors.extend(self.validate_linestring(linestring, feature_id)?);
            }
            Geometry::Polygon(polygon) => {
                errors.extend(self.validate_polygon(polygon, feature_id)?);
            }
            Geometry::MultiPolygon(multipolygon) => {
                for polygon in &multipolygon.polygons {
                    errors.extend(self.validate_polygon(polygon, feature_id)?);
                }
            }
            Geometry::MultiLineString(multilinestring) => {
                for linestring in &multilinestring.line_strings {
                    errors.extend(self.validate_linestring(linestring, feature_id)?);
                }
            }
            _ => {
                // Other geometry types
            }
        }

        Ok(errors)
    }

    /// Validates a point coordinate.
    fn validate_point(
        &self,
        coord: &Coordinate,
        feature_id: &Option<String>,
    ) -> QcResult<Vec<TopologyError>> {
        let mut errors = Vec::new();

        if coord.x.is_nan() || coord.y.is_nan() || coord.x.is_infinite() || coord.y.is_infinite() {
            errors.push(TopologyError {
                feature_id: feature_id.clone(),
                error_type: TopologyErrorType::InvalidCoordinate,
                location: *coord,
                severity: Severity::Critical,
                description: "Invalid coordinate (NaN or infinite)".to_string(),
                fix_suggestion: Some("Remove or fix invalid coordinate".to_string()),
            });
        }

        Ok(errors)
    }

    /// Validates a linestring.
    fn validate_linestring(
        &self,
        linestring: &LineString,
        feature_id: &Option<String>,
    ) -> QcResult<Vec<TopologyError>> {
        let mut errors = Vec::new();

        if linestring.coords.len() < 2 {
            errors.push(TopologyError {
                feature_id: feature_id.clone(),
                error_type: TopologyErrorType::InvalidRing,
                location: linestring
                    .coords
                    .first()
                    .copied()
                    .unwrap_or(Coordinate::new_2d(0.0, 0.0)),
                severity: Severity::Critical,
                description: "LineString has less than 2 points".to_string(),
                fix_suggestion: Some("Add more points or remove invalid linestring".to_string()),
            });
            return Ok(errors);
        }

        // Check for invalid coordinates
        for coord in &linestring.coords {
            errors.extend(self.validate_point(coord, feature_id)?);
        }

        // Check for duplicate consecutive vertices
        for i in 0..linestring.coords.len() - 1 {
            let c1 = &linestring.coords[i];
            let c2 = &linestring.coords[i + 1];

            if self.coords_equal(c1, c2) {
                errors.push(TopologyError {
                    feature_id: feature_id.clone(),
                    error_type: TopologyErrorType::DuplicateVertex,
                    location: *c1,
                    severity: Severity::Minor,
                    description: format!("Duplicate vertex at index {}", i),
                    fix_suggestion: Some("Remove duplicate vertex".to_string()),
                });
            }
        }

        // Check for self-intersections
        if self.config.check_self_intersections && self.has_self_intersection(linestring) {
            errors.push(TopologyError {
                feature_id: feature_id.clone(),
                error_type: TopologyErrorType::SelfIntersection,
                location: linestring.coords[0],
                severity: Severity::Major,
                description: "LineString has self-intersection".to_string(),
                fix_suggestion: Some("Remove or fix self-intersection".to_string()),
            });
        }

        Ok(errors)
    }

    /// Validates a polygon.
    fn validate_polygon(
        &self,
        polygon: &Polygon,
        feature_id: &Option<String>,
    ) -> QcResult<Vec<TopologyError>> {
        let mut errors = Vec::new();

        // Validate exterior ring
        if polygon.exterior.coords.len() < 4 {
            errors.push(TopologyError {
                feature_id: feature_id.clone(),
                error_type: TopologyErrorType::InvalidPolygon,
                location: polygon
                    .exterior
                    .coords
                    .first()
                    .copied()
                    .unwrap_or(Coordinate::new_2d(0.0, 0.0)),
                severity: Severity::Critical,
                description: "Polygon has less than 4 points".to_string(),
                fix_suggestion: Some("Add more points or remove invalid polygon".to_string()),
            });
            return Ok(errors);
        }

        // Check if ring is closed
        let first = polygon.exterior.coords.first();
        let last = polygon.exterior.coords.last();

        if let (Some(f), Some(l)) = (first, last) {
            if !self.coords_equal(f, l) {
                errors.push(TopologyError {
                    feature_id: feature_id.clone(),
                    error_type: TopologyErrorType::InvalidRing,
                    location: *f,
                    severity: Severity::Critical,
                    description: "Polygon ring is not closed".to_string(),
                    fix_suggestion: Some("Close the ring by adding first point at end".to_string()),
                });
            }
        }

        // Validate exterior ring as linestring
        errors.extend(self.validate_linestring(&polygon.exterior, feature_id)?);

        // Validate interior rings
        for interior in &polygon.interiors {
            errors.extend(self.validate_linestring(interior, feature_id)?);
        }

        Ok(errors)
    }

    /// Checks if two coordinates are equal within tolerance.
    fn coords_equal(&self, c1: &Coordinate, c2: &Coordinate) -> bool {
        (c1.x - c2.x).abs() < self.config.coordinate_tolerance
            && (c1.y - c2.y).abs() < self.config.coordinate_tolerance
    }

    /// Checks if a linestring has self-intersections.
    ///
    /// Uses a pairwise segment test, filtering out adjacent segment pairs that
    /// share exactly one endpoint (which is expected and valid).
    ///
    /// Returns `None` when no violations are found; `Some(pairs)` otherwise where
    /// each pair `(i, j)` identifies the non-adjacent segment indices that cross.
    fn has_self_intersection(&self, linestring: &LineString) -> bool {
        has_self_intersection(linestring).is_some()
    }

    /// Finds duplicate geometries.
    fn find_duplicates(&self, features: &FeatureCollection) -> QcResult<Vec<DuplicateGroup>> {
        let mut geometry_map: HashMap<String, Vec<String>> = HashMap::new();

        for feature in &features.features {
            if let Some(ref geometry) = feature.geometry {
                let geom_hash = self.hash_geometry(geometry)?;
                let feature_id = feature
                    .id
                    .as_ref()
                    .map_or_else(|| "unknown".to_string(), feature_id_to_string);

                geometry_map.entry(geom_hash).or_default().push(feature_id);
            }
        }

        let duplicates: Vec<DuplicateGroup> = geometry_map
            .into_iter()
            .filter(|(_, ids)| ids.len() > 1)
            .map(|(_, ids)| DuplicateGroup {
                count: ids.len(),
                feature_ids: ids,
                geometry_type: "Unknown".to_string(),
            })
            .collect();

        Ok(duplicates)
    }

    /// Creates a hash representation of a geometry (simplified).
    fn hash_geometry(&self, geometry: &Geometry) -> QcResult<String> {
        // Simplified implementation - real version would use proper geometry hashing
        Ok(format!("{:?}", geometry))
    }

    /// Finds sliver polygons.
    fn find_slivers(&self, features: &FeatureCollection) -> QcResult<Vec<SliverPolygon>> {
        let mut slivers = Vec::new();

        for feature in &features.features {
            let feature_id_str = feature.id.as_ref().map(feature_id_to_string);
            if let Some(ref geometry) = feature.geometry {
                match geometry {
                    Geometry::Polygon(polygon) => {
                        if let Some(sliver) = self.check_sliver(polygon, &feature_id_str)? {
                            slivers.push(sliver);
                        }
                    }
                    Geometry::MultiPolygon(multipolygon) => {
                        for polygon in &multipolygon.polygons {
                            if let Some(sliver) = self.check_sliver(polygon, &feature_id_str)? {
                                slivers.push(sliver);
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        Ok(slivers)
    }

    /// Checks if a polygon is a sliver.
    fn check_sliver(
        &self,
        polygon: &Polygon,
        feature_id: &Option<String>,
    ) -> QcResult<Option<SliverPolygon>> {
        let area = self.calculate_area(polygon);
        let perimeter = self.calculate_perimeter(polygon);

        if area < self.config.sliver_area_threshold {
            let compactness = if perimeter > 0.0 {
                area / (perimeter * perimeter)
            } else {
                0.0
            };

            if compactness < self.config.sliver_compactness_threshold {
                let width = if perimeter > 0.0 {
                    area / perimeter
                } else {
                    0.0
                };

                let severity = if area < 0.1 {
                    Severity::Major
                } else if area < 0.5 {
                    Severity::Minor
                } else {
                    Severity::Warning
                };

                return Ok(Some(SliverPolygon {
                    feature_id: feature_id.clone(),
                    area,
                    perimeter,
                    compactness,
                    width,
                    severity,
                }));
            }
        }

        Ok(None)
    }

    /// Calculates polygon area (simplified shoelace formula).
    fn calculate_area(&self, polygon: &Polygon) -> f64 {
        let coords = &polygon.exterior.coords;
        if coords.len() < 3 {
            return 0.0;
        }

        let mut area = 0.0;
        for i in 0..coords.len() - 1 {
            area += coords[i].x * coords[i + 1].y;
            area -= coords[i + 1].x * coords[i].y;
        }

        (area / 2.0).abs()
    }

    /// Calculates polygon perimeter.
    fn calculate_perimeter(&self, polygon: &Polygon) -> f64 {
        let coords = &polygon.exterior.coords;
        if coords.len() < 2 {
            return 0.0;
        }

        let mut perimeter = 0.0;
        for i in 0..coords.len() - 1 {
            let dx = coords[i + 1].x - coords[i].x;
            let dy = coords[i + 1].y - coords[i].y;
            perimeter += (dx * dx + dy * dy).sqrt();
        }

        perimeter
    }

    /// Checks topology rules.
    fn check_topology_rules(&self, features: &FeatureCollection) -> QcResult<Vec<RuleViolation>> {
        let options = TopologyOptions {
            tolerance: self.config.coordinate_tolerance,
            detect_gaps: self
                .config
                .topology_rules
                .contains(&TopologyRule::MustNotHaveGaps),
            detect_dangles: false,
        };

        let topology_violations = check_topology_rules(features, &options);
        let mut rule_violations = Vec::new();

        for viol in topology_violations {
            let rv = topology_violation_to_rule_violation(viol);
            rule_violations.push(rv);
        }

        Ok(rule_violations)
    }
}

impl Default for TopologyChecker {
    fn default() -> Self {
        Self::new()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Free-standing topology engine functions (public API)
// ─────────────────────────────────────────────────────────────────────────────

/// Computes the signed area of a closed ring using the shoelace formula.
///
/// Positive result = CCW winding (standard exterior ring).
/// Negative result = CW winding (standard hole ring).
fn signed_area(coords: &[Coordinate]) -> f64 {
    let n = coords.len();
    if n < 3 {
        return 0.0;
    }
    let mut sum = 0.0;
    for i in 0..n {
        let j = (i + 1) % n;
        sum += coords[i].x * coords[j].y - coords[j].x * coords[i].y;
    }
    sum / 2.0
}

/// Returns the bounding box of a polygon exterior as `Option<Bbox2D>`.
fn polygon_bbox(polygon: &Polygon) -> Option<Bbox2D> {
    let coords = &polygon.exterior.coords;
    if coords.is_empty() {
        return None;
    }
    let mut min_x = coords[0].x;
    let mut min_y = coords[0].y;
    let mut max_x = coords[0].x;
    let mut max_y = coords[0].y;
    for c in coords.iter().skip(1) {
        if c.x < min_x {
            min_x = c.x;
        }
        if c.y < min_y {
            min_y = c.y;
        }
        if c.x > max_x {
            max_x = c.x;
        }
        if c.y > max_y {
            max_y = c.y;
        }
    }
    Bbox2D::new(min_x, min_y, max_x, max_y)
}

/// Computes the intersection area of two bounding boxes, returning 0.0 if they
/// do not overlap.
fn bbox_intersection_area(a: &Bbox2D, b: &Bbox2D) -> f64 {
    let ix_min = a.min_x.max(b.min_x);
    let iy_min = a.min_y.max(b.min_y);
    let ix_max = a.max_x.min(b.max_x);
    let iy_max = a.max_y.min(b.max_y);
    if ix_max <= ix_min || iy_max <= iy_min {
        0.0
    } else {
        (ix_max - ix_min) * (iy_max - iy_min)
    }
}

/// Checks whether two non-adjacent line segments (i, j) of a `LineString` cross.
///
/// Adjacent segments share an endpoint; those are **not** reported.
///
/// For closed rings (first coord == last coord within f64 epsilon), the pair
/// `(0, seg_count-1)` is also skipped because the last segment connects back to
/// the first coordinate — they share one endpoint.  For open linestrings that guard
/// does NOT apply.
///
/// Returns `None` if no self-intersection is found, or `Some(pairs)` where each
/// tuple `(i, j)` identifies the crossing segment indices.
pub fn has_self_intersection(linestring: &LineString) -> Option<Vec<(usize, usize)>> {
    let coords = &linestring.coords;
    let n = coords.len();
    if n < 4 {
        // Need at least 4 points to have two non-adjacent segments
        return None;
    }

    // A linestring is a ring when its first and last coordinates coincide.
    let is_ring = {
        let first = &coords[0];
        let last = &coords[n - 1];
        (first.x - last.x).abs() < f64::EPSILON && (first.y - last.y).abs() < f64::EPSILON
    };

    let seg_count = n - 1;
    let mut crossings: Vec<(usize, usize)> = Vec::new();

    for i in 0..seg_count {
        let p1 = &coords[i];
        let p2 = &coords[i + 1];

        // j must be at least i+2 to be non-adjacent
        for j in (i + 2)..seg_count {
            // For a closed ring the last segment shares the start point of segment 0
            // (j = seg_count-1, i = 0).  Skip that pair for rings only.
            if is_ring && i == 0 && j == seg_count - 1 {
                continue;
            }

            let p3 = &coords[j];
            let p4 = &coords[j + 1];

            match intersect_segment_segment(p1, p2, p3, p4) {
                SegmentIntersection::None => {}
                _ => {
                    crossings.push((i, j));
                }
            }
        }
    }

    if crossings.is_empty() {
        None
    } else {
        Some(crossings)
    }
}

/// Runs the full suite of topology rules against a `FeatureCollection`.
///
/// Rules implemented:
/// - **R1** `LineString` self-intersection
/// - **R2** Polygon ring orientation (shoelace signed-area)
/// - **R3** Ring closure (first == last within tolerance)
/// - **R4** Polygon ring self-intersection
/// - **R6** Overlap between polygons (STRtree pre-filter + boundary crossing check)
/// - **R5** Gap detection (opt-in via `options.detect_gaps`)
pub fn check_topology_rules(
    features: &FeatureCollection,
    options: &TopologyOptions,
) -> Vec<TopologyViolation> {
    let mut violations: Vec<TopologyViolation> = Vec::new();

    // Collect all polygons with their feature indices for spatial operations.
    // We use the feature's position in the collection as its numeric id when the
    // feature has no explicit integer id.
    let mut polygons_with_ids: Vec<(u64, &Polygon)> = Vec::new();

    for (idx, feature) in features.features.iter().enumerate() {
        let feature_id = feature_id_as_u64(&feature.id, idx);

        let Some(ref geometry) = feature.geometry else {
            continue;
        };

        match geometry {
            Geometry::LineString(ls) => {
                // R1 — LineString self-intersection
                if let Some(segs) = has_self_intersection(ls) {
                    violations.push(TopologyViolation::SelfIntersection {
                        feature_id,
                        segments: segs,
                    });
                }
            }
            Geometry::Polygon(polygon) => {
                // R3 — exterior ring closure
                check_ring_closure(
                    &polygon.exterior.coords,
                    feature_id,
                    0,
                    options.tolerance,
                    &mut violations,
                );
                // R2 — exterior ring must be CCW (positive signed area)
                let area = signed_area(&polygon.exterior.coords);
                if area < 0.0 {
                    violations.push(TopologyViolation::RingOrientation {
                        feature_id,
                        ring_index: 0,
                        expected_ccw: true,
                    });
                }
                // R4 — exterior ring self-intersection
                if let Some(segs) = has_self_intersection(&polygon.exterior) {
                    violations.push(TopologyViolation::SelfIntersection {
                        feature_id,
                        segments: segs,
                    });
                }
                // Interior rings (holes)
                for (hole_idx, hole) in polygon.interiors.iter().enumerate() {
                    let ring_index = hole_idx + 1;
                    // R3 — hole closure
                    check_ring_closure(
                        &hole.coords,
                        feature_id,
                        ring_index,
                        options.tolerance,
                        &mut violations,
                    );
                    // R2 — hole must be CW (negative signed area)
                    let hole_area = signed_area(&hole.coords);
                    if hole_area > 0.0 {
                        violations.push(TopologyViolation::RingOrientation {
                            feature_id,
                            ring_index,
                            expected_ccw: false,
                        });
                    }
                    // R4 — hole self-intersection
                    if let Some(segs) = has_self_intersection(hole) {
                        violations.push(TopologyViolation::SelfIntersection {
                            feature_id,
                            segments: segs,
                        });
                    }
                }
                polygons_with_ids.push((feature_id, polygon));
            }
            Geometry::MultiPolygon(mp) => {
                for polygon in &mp.polygons {
                    // Same per-polygon checks
                    check_ring_closure(
                        &polygon.exterior.coords,
                        feature_id,
                        0,
                        options.tolerance,
                        &mut violations,
                    );
                    let area = signed_area(&polygon.exterior.coords);
                    if area < 0.0 {
                        violations.push(TopologyViolation::RingOrientation {
                            feature_id,
                            ring_index: 0,
                            expected_ccw: true,
                        });
                    }
                    if let Some(segs) = has_self_intersection(&polygon.exterior) {
                        violations.push(TopologyViolation::SelfIntersection {
                            feature_id,
                            segments: segs,
                        });
                    }
                    for (hole_idx, hole) in polygon.interiors.iter().enumerate() {
                        let ring_index = hole_idx + 1;
                        check_ring_closure(
                            &hole.coords,
                            feature_id,
                            ring_index,
                            options.tolerance,
                            &mut violations,
                        );
                        let hole_area = signed_area(&hole.coords);
                        if hole_area > 0.0 {
                            violations.push(TopologyViolation::RingOrientation {
                                feature_id,
                                ring_index,
                                expected_ccw: false,
                            });
                        }
                        if let Some(segs) = has_self_intersection(hole) {
                            violations.push(TopologyViolation::SelfIntersection {
                                feature_id,
                                segments: segs,
                            });
                        }
                    }
                    polygons_with_ids.push((feature_id, polygon));
                }
            }
            _ => {}
        }
    }

    // R6 — Overlap detection using STRtree spatial pre-filter
    violations.extend(detect_overlaps(&polygons_with_ids));

    // R5 — Gap detection (opt-in, expensive)
    if options.detect_gaps {
        violations.extend(detect_gaps(&polygons_with_ids, options.tolerance));
    }

    violations
}

/// Helper: check ring closure and push an `UnclosedRing` violation if needed.
fn check_ring_closure(
    coords: &[Coordinate],
    feature_id: u64,
    ring_index: usize,
    tolerance: f64,
    violations: &mut Vec<TopologyViolation>,
) {
    if coords.len() < 2 {
        violations.push(TopologyViolation::UnclosedRing {
            feature_id,
            ring_index,
        });
        return;
    }
    let first = &coords[0];
    let last = &coords[coords.len() - 1];
    if (first.x - last.x).abs() >= tolerance || (first.y - last.y).abs() >= tolerance {
        violations.push(TopologyViolation::UnclosedRing {
            feature_id,
            ring_index,
        });
    }
}

/// Converts a `FeatureId` to a stable `u64` using the feature's collection index
/// as a fallback.
fn feature_id_as_u64(id: &Option<FeatureId>, fallback_idx: usize) -> u64 {
    match id {
        Some(FeatureId::Integer(i)) => *i as u64,
        _ => fallback_idx as u64,
    }
}

/// Detect overlapping polygon pairs using an R-tree spatial pre-filter.
///
/// For each pair whose bboxes intersect we perform a more precise test:
/// 1. Check if any boundary segments cross.
/// 2. If boundaries don't cross, check if a vertex of A is strictly inside B (one
///    polygon contained in the other).
fn detect_overlaps(polygons: &[(u64, &Polygon)]) -> Vec<TopologyViolation> {
    let mut violations = Vec::new();
    if polygons.len() < 2 {
        return violations;
    }

    // Build an R-tree indexed by (bbox → index into `polygons`).
    let mut tree: RTree<usize> = RTree::new();
    for (i, (_, poly)) in polygons.iter().enumerate() {
        if let Some(bbox) = polygon_bbox(poly) {
            tree.insert(bbox, i);
        }
    }

    // For each polygon, query the tree for candidates and check each pair once
    // (use i < j to avoid duplicates).
    for (i, (id_a, poly_a)) in polygons.iter().enumerate() {
        let bbox_a = match polygon_bbox(poly_a) {
            Some(b) => b,
            None => continue,
        };

        let candidates = tree.search(&bbox_a);
        for j_ref in candidates {
            let j = *j_ref;
            if j <= i {
                continue; // already processed or same polygon
            }
            let (id_b, poly_b) = &polygons[j];

            // Precise overlap test: try polygon intersection
            let overlap_area = compute_overlap_area(poly_a, poly_b, &bbox_a);
            if overlap_area > 0.0 {
                violations.push(TopologyViolation::Overlap {
                    feature_a: *id_a,
                    feature_b: *id_b,
                    area: overlap_area,
                });
            }
        }
    }

    violations
}

/// Computes the overlap area between two polygons.
///
/// Uses `intersect_polygons` if both polygons have valid rings (≥4 coords), then
/// accumulates the absolute area of the result polygons.  Falls back to the
/// bbox-intersection area as a lower bound when `intersect_polygons` cannot be
/// called or returns an error.
fn compute_overlap_area(poly_a: &Polygon, poly_b: &Polygon, bbox_a: &Bbox2D) -> f64 {
    // Only attempt full intersection when both exteriors have the minimum 4 coords
    // required by `intersect_polygons`.
    if poly_a.exterior.coords.len() >= 4 && poly_b.exterior.coords.len() >= 4 {
        if let Ok(intersection_polys) = intersect_polygons(poly_a, poly_b) {
            let total: f64 = intersection_polys
                .iter()
                .map(|p| signed_area(&p.exterior.coords).abs())
                .sum();
            return total;
        }
    }

    // Fallback: use bbox-intersection area as an estimate.
    let bbox_b = match polygon_bbox(poly_b) {
        Some(b) => b,
        None => return 0.0,
    };
    bbox_intersection_area(bbox_a, &bbox_b)
}

/// Approximate gap detection.
///
/// For each pair of polygons whose bboxes are nearly-touching (within `tolerance`),
/// we check whether any boundary segment of one polygon's exterior is very close to
/// the other.  If their bboxes touch but no boundary points overlap, we assume a gap.
///
/// This is intentionally approximate — it emits `Gap { area: 0.0 }` for detected
/// candidates.  Full gap geometry is too expensive without a polygon clipping engine.
fn detect_gaps(polygons: &[(u64, &Polygon)], tolerance: f64) -> Vec<TopologyViolation> {
    let mut violations = Vec::new();
    if polygons.len() < 2 {
        return violations;
    }

    // Build R-tree with bboxes expanded by tolerance so near-touching bboxes
    // register as intersecting.
    let mut tree: RTree<usize> = RTree::new();
    for (i, (_, poly)) in polygons.iter().enumerate() {
        if let Some(bbox) = polygon_bbox(poly) {
            if let Some(expanded) = Bbox2D::new(
                bbox.min_x - tolerance,
                bbox.min_y - tolerance,
                bbox.max_x + tolerance,
                bbox.max_y + tolerance,
            ) {
                tree.insert(expanded, i);
            }
        }
    }

    for (i, (id_a, poly_a)) in polygons.iter().enumerate() {
        let bbox_a = match polygon_bbox(poly_a) {
            Some(b) => b,
            None => continue,
        };
        let query = match Bbox2D::new(
            bbox_a.min_x - tolerance,
            bbox_a.min_y - tolerance,
            bbox_a.max_x + tolerance,
            bbox_a.max_y + tolerance,
        ) {
            Some(b) => b,
            None => continue,
        };

        let candidates = tree.search(&query);
        for j_ref in candidates {
            let j = *j_ref;
            if j <= i {
                continue;
            }
            let (id_b, poly_b) = &polygons[j];
            let bbox_b = match polygon_bbox(poly_b) {
                Some(b) => b,
                None => continue,
            };

            // Compute actual overlap (not expanded).
            let actual_overlap = bbox_intersection_area(&bbox_a, &bbox_b);

            // Polygons are "near" but don't overlap — potential gap.
            if actual_overlap <= 0.0 {
                // Check if they are genuinely adjacent (bboxes close within tolerance).
                let gap_x = (bbox_a.min_x - bbox_b.max_x)
                    .abs()
                    .min((bbox_b.min_x - bbox_a.max_x).abs());
                let gap_y = (bbox_a.min_y - bbox_b.max_y)
                    .abs()
                    .min((bbox_b.min_y - bbox_a.max_y).abs());

                if gap_x <= tolerance * 100.0 || gap_y <= tolerance * 100.0 {
                    violations.push(TopologyViolation::Gap {
                        feature_a: *id_a,
                        feature_b: *id_b,
                        area: 0.0,
                    });
                }
            }
        }
    }

    violations
}

/// Converts a [`TopologyViolation`] into a [`RuleViolation`] for use in
/// `TopologyResult`.
fn topology_violation_to_rule_violation(viol: TopologyViolation) -> RuleViolation {
    match viol {
        TopologyViolation::SelfIntersection {
            feature_id,
            segments,
        } => RuleViolation {
            rule: TopologyRule::MustNotSelfOverlap,
            feature_ids: vec![feature_id.to_string()],
            location: None,
            severity: Severity::Major,
            description: format!(
                "Feature {} has {} self-intersecting segment pair(s)",
                feature_id,
                segments.len()
            ),
        },
        TopologyViolation::RingOrientation {
            feature_id,
            ring_index,
            expected_ccw,
        } => RuleViolation {
            rule: TopologyRule::MustNotSelfOverlap,
            feature_ids: vec![feature_id.to_string()],
            location: None,
            severity: Severity::Major,
            description: format!(
                "Feature {} ring {} has wrong orientation (expected {})",
                feature_id,
                ring_index,
                if expected_ccw { "CCW" } else { "CW" }
            ),
        },
        TopologyViolation::UnclosedRing {
            feature_id,
            ring_index,
        } => RuleViolation {
            rule: TopologyRule::MustNotSelfOverlap,
            feature_ids: vec![feature_id.to_string()],
            location: None,
            severity: Severity::Major,
            description: format!("Feature {} ring {} is not closed", feature_id, ring_index),
        },
        TopologyViolation::Overlap {
            feature_a,
            feature_b,
            area,
        } => RuleViolation {
            rule: TopologyRule::MustNotOverlap,
            feature_ids: vec![feature_a.to_string(), feature_b.to_string()],
            location: None,
            severity: Severity::Major,
            description: format!(
                "Features {} and {} overlap by approximately {:.6} area units",
                feature_a, feature_b, area
            ),
        },
        TopologyViolation::Gap {
            feature_a,
            feature_b,
            area,
        } => RuleViolation {
            rule: TopologyRule::MustNotHaveGaps,
            feature_ids: vec![feature_a.to_string(), feature_b.to_string()],
            location: None,
            severity: Severity::Minor,
            description: format!(
                "Possible gap between features {} and {} (area ≈ {:.6})",
                feature_a, feature_b, area
            ),
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxigdal_core::vector::{Feature, FeatureCollection, FeatureId};

    // ── helpers ────────────────────────────────────────────────────────────────

    fn ls(coords: &[(f64, f64)]) -> LineString {
        LineString {
            coords: coords
                .iter()
                .map(|(x, y)| Coordinate::new_2d(*x, *y))
                .collect(),
        }
    }

    /// Build a Polygon directly (bypasses Polygon::new validation, needed for
    /// constructing intentionally invalid geometries in tests).
    fn poly_raw(exterior_coords: &[(f64, f64)]) -> Polygon {
        Polygon {
            exterior: ls(exterior_coords),
            interiors: Vec::new(),
        }
    }

    /// Build a valid closed polygon (CCW square).
    fn ccw_square(x0: f64, y0: f64, x1: f64, y1: f64) -> Polygon {
        poly_raw(&[(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0, y0)])
    }

    fn fc_with_polygon(poly: Polygon) -> FeatureCollection {
        FeatureCollection::new(vec![Feature::new(Geometry::Polygon(poly))])
    }

    fn fc_with_linestring(ls_geom: LineString) -> FeatureCollection {
        FeatureCollection::new(vec![Feature::new(Geometry::LineString(ls_geom))])
    }

    // ── existing tests (unchanged) ─────────────────────────────────────────────

    #[test]
    fn test_topology_checker_creation() {
        let checker = TopologyChecker::new();
        assert!(checker.config.check_self_intersections);
    }

    #[test]
    fn test_invalid_coordinate_detection() {
        let checker = TopologyChecker::new();
        let coord = Coordinate::new_2d(f64::NAN, 0.0);
        let errors = checker.validate_point(&coord, &None);

        assert!(errors.is_ok());
        let errors = errors.ok().unwrap_or_default();
        assert!(!errors.is_empty());
        assert_eq!(errors[0].error_type, TopologyErrorType::InvalidCoordinate);
    }

    #[test]
    fn test_linestring_validation() {
        let checker = TopologyChecker::new();
        let linestring = LineString {
            coords: vec![Coordinate::new_2d(0.0, 0.0), Coordinate::new_2d(1.0, 1.0)],
        };

        let errors = checker.validate_linestring(&linestring, &None);
        assert!(errors.is_ok());
    }

    #[test]
    fn test_coords_equal() {
        let checker = TopologyChecker::new();
        let c1 = Coordinate::new_2d(0.0, 0.0);
        let c2 = Coordinate::new_2d(0.0, 0.0);
        let c3 = Coordinate::new_2d(1.0, 1.0);

        assert!(checker.coords_equal(&c1, &c2));
        assert!(!checker.coords_equal(&c1, &c3));
    }

    // ── new topology engine tests ──────────────────────────────────────────────

    /// X-shaped self-intersecting linestring: (0,0)→(2,2)→(0,2)→(2,0).
    /// Segment 0 (0,0)→(2,2) crosses segment 2 (0,2)→(2,0) at (1,1).
    #[test]
    fn test_self_intersect_simple_x() {
        let ls_geom = ls(&[(0.0, 0.0), (2.0, 2.0), (0.0, 2.0), (2.0, 0.0)]);
        let result = has_self_intersection(&ls_geom);
        assert!(
            result.is_some(),
            "Expected self-intersection to be detected"
        );
        let pairs = result.unwrap_or_default();
        assert!(
            pairs.contains(&(0, 2)),
            "Expected pair (0, 2) in crossings, got: {:?}",
            pairs
        );
    }

    /// Straight line with 10 collinear points — no self-intersection.
    #[test]
    fn test_self_intersect_no_intersection() {
        let pts: Vec<(f64, f64)> = (0..10).map(|i| (i as f64, 0.0)).collect();
        let ls_geom = ls(&pts);
        let result = has_self_intersection(&ls_geom);
        assert!(result.is_none(), "Straight line must not self-intersect");
    }

    /// 3-point L-bend: (0,0)→(1,0)→(1,1).  Adjacent segments share a point — not
    /// a self-intersection (only 2 segments, cannot be non-adjacent).
    #[test]
    fn test_self_intersect_endpoint_shared_only() {
        let ls_geom = ls(&[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)]);
        // 3 coords → 2 segments → no non-adjacent pairs
        let result = has_self_intersection(&ls_geom);
        assert!(result.is_none(), "L-bend must not be flagged");
    }

    /// Collinear overlap: (0,0)→(2,0)→(1,0)→(3,0).
    /// Segment 0 and segment 2 are collinear and overlap.
    #[test]
    fn test_self_intersect_collinear_overlap() {
        let ls_geom = ls(&[(0.0, 0.0), (2.0, 0.0), (1.0, 0.0), (3.0, 0.0)]);
        let result = has_self_intersection(&ls_geom);
        assert!(result.is_some(), "Collinear overlap should be detected");
    }

    /// Polygon with exterior ring in CW order — R2 violation expected.
    #[test]
    fn test_check_topology_rules_polygon_orientation_violation() {
        // CW square (reversed from CCW)
        let cw_poly = poly_raw(&[(0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)]);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc_with_polygon(cw_poly), &options);
        let has_orient = violations.iter().any(|v| {
            matches!(
                v,
                TopologyViolation::RingOrientation {
                    ring_index: 0,
                    expected_ccw: true,
                    ..
                }
            )
        });
        assert!(
            has_orient,
            "Expected RingOrientation violation, got: {:?}",
            violations
        );
    }

    /// Polygon where last coord ≠ first — R3 violation.
    #[test]
    fn test_check_topology_rules_unclosed_ring() {
        let unclosed = poly_raw(&[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0)]);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc_with_polygon(unclosed), &options);
        let has_unclosed = violations
            .iter()
            .any(|v| matches!(v, TopologyViolation::UnclosedRing { ring_index: 0, .. }));
        assert!(
            has_unclosed,
            "Expected UnclosedRing violation, got: {:?}",
            violations
        );
    }

    /// Bowtie ring (self-intersecting exterior): R4 violation.
    #[test]
    fn test_check_topology_rules_polygon_self_intersect_ring() {
        // Bowtie: (0,0)→(2,2)→(0,2)→(2,0)→(0,0)
        let bowtie = poly_raw(&[(0.0, 0.0), (2.0, 2.0), (0.0, 2.0), (2.0, 0.0), (0.0, 0.0)]);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc_with_polygon(bowtie), &options);
        let has_self_intersect = violations
            .iter()
            .any(|v| matches!(v, TopologyViolation::SelfIntersection { .. }));
        assert!(
            has_self_intersect,
            "Expected SelfIntersection on bowtie ring, got: {:?}",
            violations
        );
    }

    /// Two overlapping squares: A (0,0)-(2,2) and B (1,1)-(3,3).  R6 violation.
    #[test]
    fn test_check_topology_rules_overlap_detection() {
        let poly_a = ccw_square(0.0, 0.0, 2.0, 2.0);
        let poly_b = ccw_square(1.0, 1.0, 3.0, 3.0);
        let fc = FeatureCollection::new(vec![
            Feature::with_id(FeatureId::Integer(1), Geometry::Polygon(poly_a)),
            Feature::with_id(FeatureId::Integer(2), Geometry::Polygon(poly_b)),
        ]);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc, &options);
        let overlap = violations
            .iter()
            .find(|v| matches!(v, TopologyViolation::Overlap { .. }));
        assert!(
            overlap.is_some(),
            "Expected Overlap violation, got: {:?}",
            violations
        );
        if let Some(TopologyViolation::Overlap { area, .. }) = overlap {
            assert!(*area > 0.0, "Expected positive overlap area, got {}", area);
        }
    }

    /// Well-formed CCW polygon — no violations expected.
    #[test]
    fn test_check_topology_rules_clean_data_returns_empty() {
        let clean = ccw_square(0.0, 0.0, 2.0, 2.0);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc_with_polygon(clean), &options);
        assert!(
            violations.is_empty(),
            "Expected no violations for clean polygon, got: {:?}",
            violations
        );
    }

    /// R5 gap detection is opt-in.  With `detect_gaps: false`, no Gap violations.
    /// With `detect_gaps: true` and two side-by-side non-overlapping polygons, a Gap
    /// may be detected (proximity-based heuristic).
    #[test]
    fn test_check_topology_rules_gap_detection_optional() {
        let poly_a = ccw_square(0.0, 0.0, 1.0, 1.0);
        let poly_b = ccw_square(1.5, 0.0, 2.5, 1.0); // 0.5 gap on X axis
        let fc = FeatureCollection::new(vec![
            Feature::new(Geometry::Polygon(poly_a)),
            Feature::new(Geometry::Polygon(poly_b)),
        ]);

        // Default options (detect_gaps = false) — no R5 violations
        let options_off = TopologyOptions::default();
        let violations_off = check_topology_rules(&fc, &options_off);
        let has_gap_off = violations_off
            .iter()
            .any(|v| matches!(v, TopologyViolation::Gap { .. }));
        assert!(
            !has_gap_off,
            "Should not detect gaps when detect_gaps=false"
        );

        // With detect_gaps = true — proximity heuristic may fire
        let options_on = TopologyOptions {
            detect_gaps: true,
            ..TopologyOptions::default()
        };
        let violations_on = check_topology_rules(&fc, &options_on);
        // We don't assert it MUST find a gap (heuristic), but we verify the code runs
        let _ = violations_on;
    }

    /// 1000 non-overlapping polygons in a grid — no overlap violations expected.
    /// This is a performance smoke test: we only assert correctness, not timing.
    #[test]
    fn test_check_topology_rules_1000_polygons_perf_smoke() {
        let mut features = Vec::with_capacity(1000);
        for row in 0..25 {
            for col in 0..40 {
                let x0 = col as f64 * 2.0;
                let y0 = row as f64 * 2.0;
                let poly = ccw_square(x0, y0, x0 + 1.0, y0 + 1.0);
                features.push(Feature::new(Geometry::Polygon(poly)));
            }
        }
        let fc = FeatureCollection::new(features);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc, &options);
        // No overlaps expected among grid cells with 1-unit gaps between them
        let overlap_count = violations
            .iter()
            .filter(|v| matches!(v, TopologyViolation::Overlap { .. }))
            .count();
        assert!(
            overlap_count == 0,
            "Non-overlapping grid should produce 0 Overlap violations, got {}",
            overlap_count
        );
        // Total violations count is an upper bound sanity check (only orientation/closure
        // violations if any raw struct construction produced bad geometry, which it shouldn't)
        assert!(
            violations.len() < 10,
            "Expected < 10 violations for clean grid, got {}",
            violations.len()
        );
    }

    /// A self-intersecting LineString geometry inside a FeatureCollection — R1 violation.
    #[test]
    fn test_check_topology_rules_linestring_self_intersect() {
        let ls_geom = ls(&[(0.0, 0.0), (2.0, 2.0), (0.0, 2.0), (2.0, 0.0)]);
        let fc = fc_with_linestring(ls_geom);
        let options = TopologyOptions::default();
        let violations = check_topology_rules(&fc, &options);
        let has_si = violations
            .iter()
            .any(|v| matches!(v, TopologyViolation::SelfIntersection { .. }));
        assert!(
            has_si,
            "Expected SelfIntersection for X linestring, got: {:?}",
            violations
        );
    }
}