geo-polygonize-core 0.51.2

A native Rust port of the JTS/GEOS polygonization algorithm. Reconstruct valid polygons from a set of lines.
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
use serde::{Deserialize, Serialize};
use ts_rs::TS;

use crate::error::{PolygonizeError, Result};

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[serde(default, deny_unknown_fields)]
#[ts(export)]
/// The canonical configuration object for the `geo-polygonize` engine.
///
/// This struct controls every aspect of the polygonization pipeline, including
/// topological robustness, feature output, containment policies, noding, and determinism.
pub struct PolygonizerOptions {
    /// Whether to robustly node the input before polygonization.
    ///
    /// Enable this for real-world linework where segment intersections may not
    /// already exist as explicit vertices. This is slower than the fast path but
    /// avoids missing faces and unresolved crossings.
    ///
    /// Default: `false`
    pub node_input: bool,

    /// Coordinate precision used for topology and noding.
    ///
    /// Default: `PrecisionModel::Floating`
    pub precision_model: PrecisionModel,

    /// Snap input segments to nearby vertices from exact-noded input linework before grid noding.
    ///
    /// A value of `0.0` disables pre-snap. This mirrors the CFB/Shapely
    /// `snap(line, unary_union(all_lines), tolerance)` step closely enough to
    /// close small CAD gaps before polygonization.
    ///
    /// Default: `0.0`
    #[serde(default)]
    pub pre_snap_tolerance: f64,

    /// If `true`, only pure, outermost polygonal shells are returned.
    ///
    /// Floating dangles, internal cut-lines, or invalid rings will be discarded.
    ///
    /// Default: `false`
    pub extract_only_polygonal: bool,

    /// Controls robust snap noding and output coordinate handling.
    ///
    /// See `SnapStrategy` for differences between strict `Grid` snapping and
    /// Shapely/GEOS `GeosCompat` strategies.
    ///
    /// Default: `SnapStrategy::Grid`
    pub snap_strategy: SnapStrategy,

    /// Configures the noding engine backend and behavior.
    pub noding: NodingOptions,

    /// Configures how topological relationships (containment) are calculated
    /// during face formation.
    pub containment: ContainmentOptions,

    /// Configuration for enforcing exact topological determinism.
    pub determinism: DeterminismOptions,

    /// Options for capturing diagnostic topology failures.
    pub diagnostics: DiagnosticsOptions,

    /// Options for mapping final faces back to original input geometry IDs.
    pub provenance: ProvenanceOptions,

    /// Controls Z reconstruction and same-XY conflict handling.
    pub z: ZOptions,

    /// Optional application-level filtering applied after topology is established.
    pub output_filter: OutputFilterOptions,

    /// An optional identifier for the input dataset.
    #[ts(optional)]
    pub input_profile_id: Option<String>,
}

impl Default for PolygonizerOptions {
    fn default() -> Self {
        Self {
            node_input: false,
            precision_model: PrecisionModel::Floating,
            pre_snap_tolerance: 0.0,
            extract_only_polygonal: false,
            snap_strategy: SnapStrategy::Grid,
            noding: NodingOptions::default(),
            containment: ContainmentOptions::default(),
            determinism: DeterminismOptions::default(),
            diagnostics: DiagnosticsOptions::default(),
            provenance: ProvenanceOptions::default(),
            z: ZOptions::default(),
            output_filter: OutputFilterOptions::default(),
            input_profile_id: None,
        }
    }
}

impl PolygonizerOptions {
    pub fn cfb_robust_v1() -> Self {
        Self {
            node_input: true,
            precision_model: PrecisionModel::FixedGrid { grid_size: 0.1 },
            pre_snap_tolerance: 0.5,
            extract_only_polygonal: false,
            snap_strategy: SnapStrategy::GeosCompat,
            noding: NodingOptions {
                backend: NodingBackend::Snap,
                guarantee: NodingGuarantee::Unchecked,
            },
            containment: ContainmentOptions {
                touch_policy: TouchPolicy::AllowPointTouchDisallowEdgeShare,
            },
            determinism: DeterminismOptions {
                canonical_sort: true,
                canonical_ring_rotation: true,
                stable_tie_breaks: true,
            },
            diagnostics: DiagnosticsOptions {
                enabled: true,
                report_mode: true,
                timings: false,
            },
            provenance: ProvenanceOptions {
                enabled: true,
                include_boundary_line_ids: true,
            },
            z: ZOptions::default(),
            output_filter: OutputFilterOptions::default(),
            input_profile_id: Some("cfb_robust_v1".to_string()),
        }
    }

    pub fn validate(&self) -> Result<()> {
        for (field, value) in [("pre_snap_tolerance", self.pre_snap_tolerance)] {
            if !value.is_finite() || value < 0.0 {
                return Err(PolygonizeError::InvalidArgumentType {
                    field: field.to_string(),
                    expected: "a finite non-negative number".to_string(),
                    actual: value.to_string(),
                });
            }
        }

        if let PrecisionModel::FixedGrid { grid_size } = self.precision_model {
            if !grid_size.is_finite() || grid_size <= 0.0 {
                return Err(PolygonizeError::InvalidArgumentType {
                    field: "precision_model.grid_size".to_string(),
                    expected: "a finite positive number".to_string(),
                    actual: grid_size.to_string(),
                });
            }
            if self.node_input && matches!(self.noding.backend, NodingBackend::Advanced) {
                return Err(PolygonizeError::UnsupportedOptionCombination {
                    reason: "the Advanced compatibility noder supports floating precision only"
                        .to_string(),
                });
            }
        }

        if self.pre_snap_tolerance > 0.0 && !self.node_input {
            return Err(PolygonizeError::UnsupportedOptionCombination {
                reason: "pre_snap_tolerance requires node_input=true".to_string(),
            });
        }

        if matches!(
            self.noding.guarantee,
            NodingGuarantee::CertifiedFixedPrecision
        ) && (!self.node_input
            || !matches!(self.precision_model, PrecisionModel::FixedGrid { .. })
            || !matches!(self.noding.backend, NodingBackend::Snap)
            || !matches!(self.snap_strategy, SnapStrategy::Grid))
        {
            return Err(PolygonizeError::UnsupportedOptionCombination {
                reason: "CertifiedFixedPrecision requires node_input=true, FixedGrid precision, the Snap backend, and the Grid snap strategy".to_string(),
            });
        }

        if let Some(value) = self.output_filter.minimum_face_area {
            if !value.is_finite() || value < 0.0 {
                return Err(PolygonizeError::InvalidArgumentType {
                    field: "output_filter.minimum_face_area".to_string(),
                    expected: "a finite non-negative number".to_string(),
                    actual: value.to_string(),
                });
            }
        }

        if !self.z.conflict_tolerance.is_finite() || self.z.conflict_tolerance < 0.0 {
            return Err(PolygonizeError::InvalidArgumentType {
                field: "z.conflict_tolerance".to_string(),
                expected: "a finite non-negative number".to_string(),
                actual: self.z.conflict_tolerance.to_string(),
            });
        }

        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, TS)]
#[serde(tag = "type", rename_all = "snake_case")]
#[ts(export)]
/// Coordinate precision used by the topology pipeline.
pub enum PrecisionModel {
    /// Preserve input coordinates and compute intersections in floating point.
    #[default]
    Floating,
    /// Round topology coordinates to a fixed grid of positive cell size.
    FixedGrid { grid_size: f64 },
}

impl PrecisionModel {
    pub fn grid_size(self) -> f64 {
        match self {
            Self::Floating => 0.0,
            Self::FixedGrid { grid_size } => grid_size,
        }
    }

    pub fn from_grid_size(grid_size: f64) -> Self {
        if grid_size == 0.0 {
            Self::Floating
        } else {
            Self::FixedGrid { grid_size }
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[ts(export)]
/// Strategy for robust snap noding and output coordinates.
///
/// `Grid` uses the precision grid for both topology and output coordinates.
/// `GeosCompat` uses the grid for topology, then restores one deterministic nearest source XY
/// coordinate per snapped node. This targets Shapely-style `snap` followed by full-precision
/// noding and polygonization; it does not emulate `set_precision` output.
///
/// **Scale Guidance:**
/// Use `Grid` when an explicit precision model is the desired output contract.
/// Use `GeosCompat` when the grid is a robustness aid but source-coordinate fidelity matters.
/// Both strategies are deterministic; exact GEOS/Shapely parity is not guaranteed for
/// degenerate or many-to-one snaps.
pub enum SnapStrategy {
    Grid,
    GeosCompat,
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum TouchPolicy {
    AllowPointTouchDisallowEdgeShare,
    TreatAnyTouchAsDisjoint,
    AllowEdgeShare,
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum TileOwnershipPolicy {
    /// Fast ownership using the polygon centroid, which may lie outside a concave polygon.
    Centroid,
    /// Ownership using a point guaranteed to intersect the polygon interior when one exists.
    RepresentativePointInsidePolygon,
    /// Ownership using the smallest boundary vertex in XY order.
    LexicographicMinVertex,
    /// Legacy deterministic ownership policy; now uses a safe interior point.
    CanonicalBoundaryHash,
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum NodingBackend {
    /// Snap-rounding noder using the configured precision grid.
    Snap,
    /// Deprecated compatibility alias for exact (`grid_size = 0`) snap noding.
    ///
    /// The experimental sweep-line implementation was retired because it did not
    /// maintain the invariants required for complete intersection enumeration.
    Advanced,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum NodingGuarantee {
    /// Trust the selected noder without checking its output.
    #[default]
    Unchecked,
    /// Verify that the resulting segments are fully noded and normalized.
    Validate,
    /// Use hot-pixel snap rounding and verify the fixed-grid result.
    CertifiedFixedPrecision,
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct NodingOptions {
    pub backend: NodingBackend,
    pub guarantee: NodingGuarantee,
}

impl Default for NodingOptions {
    fn default() -> Self {
        Self {
            backend: NodingBackend::Snap,
            guarantee: NodingGuarantee::Unchecked,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct ContainmentOptions {
    pub touch_policy: TouchPolicy,
}

impl Default for ContainmentOptions {
    fn default() -> Self {
        Self {
            touch_policy: TouchPolicy::AllowPointTouchDisallowEdgeShare,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct DeterminismOptions {
    pub canonical_sort: bool,
    pub canonical_ring_rotation: bool,
    pub stable_tie_breaks: bool,
}

impl Default for DeterminismOptions {
    fn default() -> Self {
        Self {
            canonical_sort: true,
            canonical_ring_rotation: true,
            stable_tie_breaks: true,
        }
    }
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct DiagnosticsOptions {
    pub enabled: bool,
    pub report_mode: bool,
    // Collect phase timings without enabling the more expensive work counters.
    #[serde(default)]
    pub timings: bool,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct ProvenanceOptions {
    pub enabled: bool,
    pub include_boundary_line_ids: bool,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum ZPolicy {
    /// Ignore input Z and emit `0.0`.
    Ignore,
    /// Interpolate split-vertex Z along each source edge before graph reconciliation.
    #[default]
    InterpolateAlongEdge,
    /// Use the nearest source endpoint's Z for each split vertex.
    PreferNearestEndpoint,
    /// Interpolate Z, then fail if one XY node receives conflicting values.
    ErrorOnConflict,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct ZOptions {
    pub policy: ZPolicy,
    /// Z values at one XY node conflict when their difference exceeds this value.
    pub conflict_tolerance: f64,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, TS)]
#[serde(default)]
#[ts(export)]
pub struct OutputFilterOptions {
    /// Keep faces whose area is greater than or equal to this value.
    #[ts(optional)]
    pub minimum_face_area: Option<f64>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum DedupPolicy {
    #[default]
    KeepAll,
    CanonicalRingHash,
}

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

    #[test]
    fn partial_json_uses_defaults() {
        let options: PolygonizerOptions = serde_json::from_str(
            r#"{"diagnostics":{"enabled":true},"output_filter":{"minimum_face_area":2.0}}"#,
        )
        .unwrap();

        assert!(options.diagnostics.enabled);
        assert!(!options.diagnostics.report_mode);
        assert_eq!(options.precision_model, PrecisionModel::Floating);
        assert_eq!(options.output_filter.minimum_face_area, Some(2.0));
        assert_eq!(options.z, ZOptions::default());

        let fixed: PolygonizerOptions =
            serde_json::from_str(r#"{"precision_model":{"type":"fixed_grid","grid_size":0.25}}"#)
                .unwrap();
        assert_eq!(
            fixed.precision_model,
            PrecisionModel::FixedGrid { grid_size: 0.25 }
        );

        let validated: PolygonizerOptions =
            serde_json::from_str(r#"{"noding":{"guarantee":"Validate"}}"#).unwrap();
        assert_eq!(validated.noding.guarantee, NodingGuarantee::Validate);

        let z: PolygonizerOptions =
            serde_json::from_str(r#"{"z":{"policy":"ErrorOnConflict"}}"#).unwrap();
        assert_eq!(z.z.policy, ZPolicy::ErrorOnConflict);
        assert_eq!(z.z.conflict_tolerance, 0.0);
    }

    #[test]
    fn legacy_snap_grid_size_is_rejected_instead_of_ignored() {
        let error =
            serde_json::from_str::<PolygonizerOptions>(r#"{"snap_grid_size":0.1}"#).unwrap_err();
        assert!(error.to_string().contains("unknown field `snap_grid_size`"));
    }

    #[test]
    fn validation_rejects_invalid_options() {
        for value in [-1.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            let options = PolygonizerOptions {
                node_input: true,
                pre_snap_tolerance: value,
                ..Default::default()
            };
            assert!(options.validate().is_err());

            let options = PolygonizerOptions {
                output_filter: OutputFilterOptions {
                    minimum_face_area: Some(value),
                },
                ..Default::default()
            };
            assert!(options.validate().is_err());

            let options = PolygonizerOptions {
                z: ZOptions {
                    conflict_tolerance: value,
                    ..Default::default()
                },
                ..Default::default()
            };
            assert!(options.validate().is_err());
        }

        for grid_size in [-1.0, 0.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            let options = PolygonizerOptions {
                precision_model: PrecisionModel::FixedGrid { grid_size },
                ..Default::default()
            };
            assert!(options.validate().is_err());
        }

        let options = PolygonizerOptions {
            pre_snap_tolerance: 1.0,
            ..Default::default()
        };
        assert!(matches!(
            options.validate(),
            Err(PolygonizeError::UnsupportedOptionCombination { .. })
        ));

        let options = PolygonizerOptions {
            node_input: true,
            precision_model: PrecisionModel::FixedGrid { grid_size: 1.0 },
            noding: NodingOptions {
                backend: NodingBackend::Advanced,
                guarantee: NodingGuarantee::Unchecked,
            },
            ..Default::default()
        };
        assert!(matches!(
            options.validate(),
            Err(PolygonizeError::UnsupportedOptionCombination { .. })
        ));

        let certified = PolygonizerOptions {
            node_input: true,
            precision_model: PrecisionModel::FixedGrid { grid_size: 1.0 },
            noding: NodingOptions {
                guarantee: NodingGuarantee::CertifiedFixedPrecision,
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(certified.validate().is_ok());
        assert!(PolygonizerOptions {
            node_input: false,
            ..certified.clone()
        }
        .validate()
        .is_err());
        assert!(PolygonizerOptions {
            precision_model: PrecisionModel::Floating,
            ..certified
        }
        .validate()
        .is_err());

        assert!(PolygonizerOptions::default().validate().is_ok());
    }
}