v2rmp 0.4.3

rmpca — Route Optimization TUI & Agent Engine
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
use anyhow::Result;
use geo::algorithm::bool_ops::BooleanOps;
use geo::algorithm::orient::{Direction, Orient};
use geo::algorithm::remove_repeated_points::RemoveRepeatedPoints;
use geo::algorithm::simplify::Simplify;
use geo::{LineString, Polygon};
use geojson::{Feature, Geometry, Value};
use serde_json::Map;

use super::super::clean::{CleanOptions, CleanStats};

/// Repair and validate GeoJSON features.
/// Returns repaired LineString features and separately collected Point features.
pub fn repair_features(
    features: &[Feature],
    options: &CleanOptions,
    stats: &mut CleanStats,
) -> Result<(Vec<Feature>, Vec<Feature>)> {
    let mut repaired = Vec::new();
    let mut point_features: Vec<Feature> = Vec::new();

    for feature in features {
        let geom = match &feature.geometry {
            Some(g) => g,
            None => {
                stats.invalid_dropped += 1;
                continue;
            }
        };

        let geom_type = match &geom.value {
            Value::LineString(_) => "LineString",
            Value::MultiLineString(_) => "MultiLineString",
            Value::Polygon(_) if options.include_polygons => "Polygon",
            Value::MultiPolygon(_) if options.include_polygons => "MultiPolygon",
            Value::Point(_) if options.include_points => "Point",
            _ => {
                stats.invalid_dropped += 1;
                continue;
            }
        };

        match geom_type {
            "LineString" => {
                if let Some(line) = extract_linestring(&geom.value) {
                    if let Some(valid_line) = make_valid_and_validate_linestring(line, options) {
                        repaired.push(create_feature(valid_line, feature.properties.clone()));
                    } else {
                        stats.invalid_dropped += 1;
                    }
                } else {
                    stats.invalid_dropped += 1;
                }
            }
            "MultiLineString" => {
                if let Value::MultiLineString(ref coords) = geom.value {
                    for line_coords in coords {
                        if let Some(line) = coords_to_linestring(line_coords) {
                            if let Some(valid_line) =
                                make_valid_and_validate_linestring(line, options)
                            {
                                repaired
                                    .push(create_feature(valid_line, feature.properties.clone()));
                            }
                        }
                    }
                }
            }
            "Polygon" => {
                if let Some(poly) = extract_polygon(&geom.value) {
                    let lines = make_valid_polygon_to_linestrings(poly, options);
                    for line in lines {
                        if let Some(valid_line) = make_valid_and_validate_linestring(line, options)
                        {
                            repaired.push(create_feature(valid_line, feature.properties.clone()));
                        }
                    }
                }
            }
            "MultiPolygon" => {
                if let Value::MultiPolygon(ref coords) = geom.value {
                    for poly_coords in coords {
                        if let Some(poly) = polygon_coords_to_polygon(poly_coords) {
                            let lines = make_valid_polygon_to_linestrings(poly, options);
                            for line in lines {
                                if let Some(valid_line) =
                                    make_valid_and_validate_linestring(line, options)
                                {
                                    repaired.push(create_feature(
                                        valid_line,
                                        feature.properties.clone(),
                                    ));
                                }
                            }
                        }
                    }
                }
            }
            "Point" => {
                // Points are passed through unchanged, collected separately
                point_features.push(feature.clone());
            }
            _ => {}
        }
    }

    Ok((repaired, point_features))
}

fn extract_linestring(value: &Value) -> Option<LineString<f64>> {
    if let Value::LineString(ref coords) = value {
        coords_to_linestring(coords)
    } else {
        None
    }
}

fn extract_polygon(value: &Value) -> Option<Polygon<f64>> {
    if let Value::Polygon(ref coords) = value {
        polygon_coords_to_polygon(coords)
    } else {
        None
    }
}

fn coords_to_linestring(coords: &[Vec<f64>]) -> Option<LineString<f64>> {
    if coords.len() < 2 {
        return None;
    }

    let points: Vec<_> = coords
        .iter()
        .filter(|c| c.len() >= 2)
        .map(|c| (c[0], c[1]))
        .collect();

    if points.len() < 2 {
        return None;
    }

    Some(LineString::from(points))
}

fn polygon_coords_to_polygon(coords: &[Vec<Vec<f64>>]) -> Option<Polygon<f64>> {
    if coords.is_empty() {
        return None;
    }

    let exterior = coords_to_linestring(&coords[0])?;
    let interiors: Vec<_> = coords[1..]
        .iter()
        .filter_map(|ring| coords_to_linestring(ring))
        .collect();

    Some(Polygon::new(exterior, interiors))
}

/// Make-valid for LineStrings: remove repeated points, ensure >= 2 coords.
fn make_valid_linestring(mut line: LineString<f64>) -> Option<LineString<f64>> {
    line = line.remove_repeated_points();
    if line.0.len() < 2 {
        return None;
    }
    Some(line)
}

/// Make-valid for Polygons: fix ring orientation and self-intersections.
/// Uses `BooleanOps::union` with an empty polygon to repair self-intersections
/// (the geo crate docs note that union with an empty geom removes degeneracies).
/// Then orients rings to standard winding order and extracts LineStrings.
fn make_valid_polygon_to_linestrings(
    poly: Polygon<f64>,
    options: &CleanOptions,
) -> Vec<LineString<f64>> {
    if !options.make_valid {
        return polygon_to_linestrings(&poly);
    }

    // Remove repeated points first
    let poly = poly.remove_repeated_points();

    // Check if polygon has enough points for a valid exterior ring
    if poly.exterior().0.len() < 4 {
        // A valid polygon ring needs at least 4 points (3 unique + closing).
        // Try to treat it as a degenerate line.
        let coords: Vec<_> = poly.exterior().0.iter().map(|c| (c.x, c.y)).collect();
        if coords.len() >= 2 {
            let mut deduped = coords;
            deduped.dedup();
            if deduped.len() >= 2 {
                return vec![LineString::from(deduped)];
            }
        }
        return vec![];
    }

    // Fix self-intersections via union with empty polygon.
    // This handles bowtie/self-intersecting polygons and may split
    // them into a MultiPolygon.
    let empty = Polygon::new(LineString::new(vec![]), vec![]);
    let repaired = poly.union(&empty);

    // Convert each polygon in the result to linestrings
    let mut all_lines = Vec::new();
    for repaired_poly in &repaired.0 {
        // Orient to standard winding (CCW exterior, CW interior)
        let oriented = repaired_poly.orient(Direction::Default);
        all_lines.extend(polygon_to_linestrings(&oriented));
    }

    all_lines
}

/// Validate and optionally simplify a LineString.
fn validate_linestring(
    mut line: LineString<f64>,
    options: &CleanOptions,
) -> Option<LineString<f64>> {
    // Remove duplicate consecutive points
    let mut coords: Vec<_> = line.0.to_vec();
    coords.dedup();

    if coords.len() < 2 {
        return None;
    }

    line = LineString::from(coords);

    // Simplify if requested
    if options.simplify_tolerance_m > 0.0 {
        let tolerance_deg = options.simplify_tolerance_m / 111_320.0;
        line = line.simplify(&tolerance_deg);
    }

    if line.0.len() < 2 {
        return None;
    }

    Some(line)
}

/// Combined make-valid + validate for a LineString.
fn make_valid_and_validate_linestring(
    line: LineString<f64>,
    options: &CleanOptions,
) -> Option<LineString<f64>> {
    let line = if options.make_valid {
        make_valid_linestring(line)?
    } else {
        line
    };
    validate_linestring(line, options)
}

fn polygon_to_linestrings(poly: &Polygon<f64>) -> Vec<LineString<f64>> {
    let mut lines = Vec::new();

    // Exterior ring
    let exterior = poly.exterior();
    if !exterior.0.is_empty() {
        let mut coords = exterior.0.to_vec();
        // Remove closing point if present
        if coords.len() >= 2 && coords[0] == coords[coords.len() - 1] {
            coords.pop();
        }
        if coords.len() >= 2 {
            lines.push(LineString::from(coords));
        }
    }

    // Interior rings
    for interior in poly.interiors() {
        if !interior.0.is_empty() {
            let mut coords = interior.0.to_vec();
            if coords.len() >= 2 && coords[0] == coords[coords.len() - 1] {
                coords.pop();
            }
            if coords.len() >= 2 {
                lines.push(LineString::from(coords));
            }
        }
    }

    lines
}

fn create_feature(
    line: LineString<f64>,
    properties: Option<Map<String, serde_json::Value>>,
) -> Feature {
    let coords: Vec<Vec<f64>> = line.0.iter().map(|coord| vec![coord.x, coord.y]).collect();

    Feature {
        bbox: None,
        geometry: Some(Geometry {
            bbox: None,
            value: Value::LineString(coords),
            foreign_members: None,
        }),
        id: None,
        properties,
        foreign_members: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::clean::{CleanOptions, CleanStats};
    use geojson::{Feature, Geometry, Value};

    fn default_options() -> CleanOptions {
        CleanOptions::default()
    }

    fn make_linestring_feature(coords: Vec<Vec<f64>>) -> Feature {
        Feature {
            bbox: None,
            geometry: Some(Geometry {
                bbox: None,
                value: Value::LineString(coords),
                foreign_members: None,
            }),
            id: None,
            properties: None,
            foreign_members: None,
        }
    }

    fn make_point_feature(lon: f64, lat: f64) -> Feature {
        Feature {
            bbox: None,
            geometry: Some(Geometry {
                bbox: None,
                value: Value::Point(vec![lon, lat]),
                foreign_members: None,
            }),
            id: None,
            properties: None,
            foreign_members: None,
        }
    }

    #[test]
    fn test_repair_valid_linestring() {
        let feature = make_linestring_feature(vec![vec![0.0, 0.0], vec![1.0, 0.0], vec![2.0, 0.0]]);
        let mut stats = CleanStats::default();
        let (repaired, _) = repair_features(&[feature], &default_options(), &mut stats).unwrap();
        assert_eq!(repaired.len(), 1);
    }

    #[test]
    fn test_repair_short_linestring_dropped() {
        let feature = make_linestring_feature(vec![vec![0.0, 0.0]]);
        let mut stats = CleanStats::default();
        let (repaired, _) = repair_features(&[feature], &default_options(), &mut stats).unwrap();
        assert_eq!(repaired.len(), 0);
        assert_eq!(stats.invalid_dropped, 1);
    }

    #[test]
    fn test_repair_duplicate_points_removed() {
        let feature = make_linestring_feature(vec![
            vec![0.0, 0.0],
            vec![0.0, 0.0],
            vec![0.0, 0.0],
            vec![1.0, 0.0],
        ]);
        let mut stats = CleanStats::default();
        let (repaired, _) = repair_features(&[feature], &default_options(), &mut stats).unwrap();
        assert_eq!(repaired.len(), 1);
    }

    #[test]
    fn test_repair_point_passthrough() {
        let mut opts = default_options();
        opts.include_points = true;
        let feature = make_point_feature(1.0, 2.0);
        let mut stats = CleanStats::default();
        let (repaired, points) = repair_features(&[feature], &opts, &mut stats).unwrap();
        assert_eq!(repaired.len(), 0);
        assert_eq!(points.len(), 1);
    }

    #[test]
    fn test_repair_no_geometry_dropped() {
        let feature = Feature {
            bbox: None,
            geometry: None,
            id: None,
            properties: None,
            foreign_members: None,
        };
        let mut stats = CleanStats::default();
        let (repaired, _) = repair_features(&[feature], &default_options(), &mut stats).unwrap();
        assert_eq!(repaired.len(), 0);
        assert_eq!(stats.invalid_dropped, 1);
    }

    #[test]
    fn test_repair_multilinestring() {
        let feature = Feature {
            bbox: None,
            geometry: Some(Geometry {
                bbox: None,
                value: Value::MultiLineString(vec![
                    vec![vec![0.0, 0.0], vec![1.0, 0.0]],
                    vec![vec![2.0, 0.0], vec![3.0, 0.0]],
                ]),
                foreign_members: None,
            }),
            id: None,
            properties: None,
            foreign_members: None,
        };
        let mut stats = CleanStats::default();
        let (repaired, _) = repair_features(&[feature], &default_options(), &mut stats).unwrap();
        assert_eq!(repaired.len(), 2);
    }

    #[test]
    fn test_make_valid_disabled() {
        let mut opts = default_options();
        opts.make_valid = false;
        // Even with make_valid=false, duplicate consecutive points
        // should still be removed by validate_linestring's dedup step
        let feature = make_linestring_feature(vec![vec![0.0, 0.0], vec![1.0, 0.0], vec![2.0, 0.0]]);
        let mut stats = CleanStats::default();
        let (repaired, _) = repair_features(&[feature], &opts, &mut stats).unwrap();
        assert_eq!(repaired.len(), 1);
    }
}