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
#![allow(clippy::float_cmp)]

use byteorder::{ByteOrder, NativeEndian};
use geo::algorithm::contains::Contains;
use geo::algorithm::intersects::Intersects;
use geo_types::{Coordinate, Line, LineString, MultiPolygon, Point, Polygon};
use linked_hash_map::LinkedHashMap;
use num_traits;
use std::hash::{Hash, Hasher};

/// Violations of the OGC rules for polygon validity
/// This also includes usage of NaN or Infinite floating point values
///
pub struct ValidationErrors<T>
where
    T: num_traits::Float,
{
    /// Whether the polygon is valid or not
    pub valid: bool,
    /// Whether the polygon has less than three points
    pub has_less_than_three_points: bool,
    /// Whether the polygon is actually a multipolygon
    pub is_multi_polygon: bool,
    /// NaN/Infinite floating point values
    pub unsupported_floating_point_values: Vec<T>,
    /// Rings of the polygon that have not been closed
    pub open_rings: Vec<LineString<T>>,
    /// Holes in the polygon that intersect the outer ring of another inner ring
    /// (they may, however, share points)
    pub ring_intersects_other_ring: Vec<Coordinate<T>>,
    /// Coordinates where self intersection occurs
    pub self_intersections: Vec<Coordinate<T>>,
    /// Points that touch a line
    pub point_touching_line: Vec<Coordinate<T>>,
    /// Points repeated in a single ring
    pub repeated_points: Vec<Coordinate<T>>,
}

pub trait Validate<T>
where
    T: num_traits::Float,
{
    /// Validate a Multipolygon/Polygon Geometry according to the OGC rules
    ///
    /// This function is non-trivial from a computational perspective.
    /// It returns `false` at the first point it hits a violation of
    /// the OCG rules for a polygon.
    ///
    /// * A polygon may not have less than three points
    /// * A polygon may not have any unclosed rings
    /// * A polygon may not be a multi-polygon (all inner rings must be contained by the outer ring)
    /// * No ring in the polygon may intersect another ring
    /// * No ring in the polygon may intersect itself
    /// * No point on a ring may be touching a line in it or any other ring
    /// * No points may be repeated in a ring
    /// * All points must have valid floating point values
    ///
    /// # Examples
    ///
    /// ```
    /// use geo_types::polygon;
    /// use geo_validator::Validate;
    ///
    /// let poly = polygon!(
    ///             exterior: [
    ///                 (x: 0., y: 0.),
    ///                 (x: 0., y: 200.),
    ///                 (x: 200., y: 0.),
    ///                 (x: 200., y: 200.),
    ///             ],
    ///             interiors: [
    ///                 [
    ///                     (x: 10., y: 20.),
    ///                     (x: 50., y: 20.),
    ///                     (x: 20., y: 50.),
    ///                     (x: 50., y: 50.),
    ///                 ],
    ///             ],
    ///         );
    ///
    ///         let valid = poly.validate();
    ///         assert_eq!(valid, false);
    /// ```
    ///
    fn validate(&self) -> bool;

    /// Validate a Multipolygon/Polygon Geometry according to the OGC rules
    /// with a detailed report of errors
    ///
    /// This function is non-trivial from a computational perspective.
    /// It returns a large struct detailing all errors found in the submitted Geometry.
    ///
    /// * A polygon may not have less than three points
    /// * A polygon may not have any unclosed rings
    /// * No ring in the polygon may intersect another ring
    /// * No ring in the polygon may intersect itself
    /// * No point on a ring may be touching a line in it or any other ring
    /// * No points may be repeated in a ring
    /// * All points must have valid floating point values
    ///
    /// # Examples
    ///
    /// ```
    /// use geo_types::polygon;
    /// use geo_validator::Validate;
    ///
    /// let poly = polygon!(
    ///             exterior: [
    ///                 (x: 0., y: 0.),
    ///                 (x: 0., y: 200.),
    ///                 (x: 200., y: 0.),
    ///                 (x: 200., y: 200.),
    ///             ],
    ///             interiors: [
    ///                 [
    ///                     (x: 10., y: 20.),
    ///                     (x: 50., y: 20.),
    ///                     (x: 20., y: 50.),
    ///                     (x: 50., y: 50.),
    ///                 ],
    ///             ],
    ///         );
    ///
    ///         let valid = poly.validate_detailed();
    ///         assert_eq!(valid.valid, false);
    ///         assert_eq!(valid.ring_intersects_other_ring.len(), 3);
    ///         assert_eq!(valid.self_intersections.len(), 2);
    ///         assert_eq!(valid.point_touching_line.len(), 1);
    ///
    ///         assert_eq!(valid.ring_intersects_other_ring[0].x, 20_f64);
    ///         assert_eq!(valid.ring_intersects_other_ring[0].y, 20_f64);
    ///         assert_eq!(valid.ring_intersects_other_ring[1].x, 35_f64);
    ///         assert_eq!(valid.ring_intersects_other_ring[1].y, 35_f64);
    ///         assert_eq!(valid.ring_intersects_other_ring[2].x, 50_f64);
    ///         assert_eq!(valid.ring_intersects_other_ring[2].y, 50_f64);
    ///
    ///         assert_eq!(valid.self_intersections[0].x, 100_f64);
    ///         assert_eq!(valid.self_intersections[0].y, 100_f64);
    ///         assert_eq!(valid.self_intersections[1].x, 32.857142857142854_f64);
    ///         assert_eq!(valid.self_intersections[1].y, 37.142857142857146_f64);
    ///
    ///         assert_eq!(valid.point_touching_line[0].x, 50_f64);
    ///         assert_eq!(valid.point_touching_line[0].y, 50_f64);
    /// ```
    ///
    fn validate_detailed(&self) -> ValidationErrors<T>;
}

/** Polygons */

impl<T> Validate<T> for MultiPolygon<T>
where
    T: num_traits::Float,
{
    fn validate(&self) -> bool {
        validate_multi_polygon(self, true).valid
    }

    fn validate_detailed(&self) -> ValidationErrors<T> {
        validate_multi_polygon(self, false)
    }
}

fn validate_multi_polygon<T: num_traits::Float>(
    mp: &MultiPolygon<T>,
    quick: bool,
) -> ValidationErrors<T> {
    let mut validation_errors = ValidationErrors::<T> {
        valid: true,
        has_less_than_three_points: false,
        is_multi_polygon: false,
        unsupported_floating_point_values: vec![] as Vec<T>,
        open_rings: vec![] as Vec<LineString<T>>,
        ring_intersects_other_ring: vec![] as Vec<Coordinate<T>>,
        self_intersections: vec![] as Vec<Coordinate<T>>,
        point_touching_line: vec![] as Vec<Coordinate<T>>,
        repeated_points: vec![] as Vec<Coordinate<T>>,
    };
    for poly in mp.0.iter() {
        if quick {
            let valid = poly.validate();
            if !valid {
                validation_errors.valid = false;
                return validation_errors; // Early return on first error
            }
        } else {
            let err = poly.validate_detailed();
            if !err.valid && validation_errors.valid {
                validation_errors.valid = err.valid
            }
            if err.has_less_than_three_points && !validation_errors.has_less_than_three_points {
                validation_errors.has_less_than_three_points = err.has_less_than_three_points
            }
            validation_errors
                .unsupported_floating_point_values
                .extend(err.unsupported_floating_point_values);
            validation_errors.open_rings.extend(err.open_rings);
            validation_errors
                .ring_intersects_other_ring
                .extend(err.ring_intersects_other_ring);
            validation_errors
                .self_intersections
                .extend(err.self_intersections);
            validation_errors
                .point_touching_line
                .extend(err.point_touching_line);
            validation_errors
                .repeated_points
                .extend(err.repeated_points);
        }
    }
    validation_errors
}

impl<T> Validate<T> for Polygon<T>
where
    T: num_traits::Float,
{
    fn validate(&self) -> bool {
        validate_polygon(self, true).valid
    }

    fn validate_detailed(&self) -> ValidationErrors<T> {
        validate_polygon(self, false)
    }
}

/// Check a polygon for validity.
/// This function is rather long, because it tries to be as quick as possible
/// and to waste as few resources as possible. Setting the second parameter `quick`
/// to true will cause the function to bail out at the very first error (without)
/// providing any detailed information about the error.
fn validate_polygon<T>(poly: &Polygon<T>, quick: bool) -> ValidationErrors<T>
where
    T: num_traits::Float,
{
    let mut validation_errors = ValidationErrors::<T> {
        valid: true,
        has_less_than_three_points: false,
        is_multi_polygon: false,
        unsupported_floating_point_values: vec![] as Vec<T>,
        open_rings: vec![] as Vec<LineString<T>>,
        ring_intersects_other_ring: vec![] as Vec<Coordinate<T>>,
        self_intersections: vec![] as Vec<Coordinate<T>>,
        point_touching_line: vec![] as Vec<Coordinate<T>>,
        repeated_points: vec![] as Vec<Coordinate<T>>,
    };

    // First check if polygon is actually a MultiPolygon
    let exterior_ring = Polygon::new(poly.exterior().clone(), vec![]);
    for inner_ring in poly.interiors() {
        if !exterior_ring.contains(inner_ring) {
            validation_errors.valid = false;
            if quick {
                return validation_errors;
            }
            validation_errors.is_multi_polygon = true;
        }
    }

    let mut poly_lines = vec![] as Vec<Line<T>>;
    let mut rings = vec![poly.exterior()];
    rings.extend(poly.interiors());
    let mut ring_start_idx = 0; // This is used together with poly_lines to determine if intersection is with self
    for ring in rings.into_iter() {
        // Check for poly with less than 3 points
        let ring_points_count = ring.0.len();
        // The geo libs always close the poly so first and last point are the same and we need 4 point
        // to describe a triangle.
        if ring_points_count < 4 {
            validation_errors.valid = false;
            if quick {
                return validation_errors;
            }
            validation_errors.has_less_than_three_points = true;
        }

        // Check for open ring
        // Note: this check is pointless with the current geo crate, since it automatically closes
        // any open rings. It is computationally cheap though, so keep it in case of future design
        // changes.
        if !ring.0[0].x.eq(&ring.0[ring_points_count - 1].x)
            && !ring.0[0].y.eq(&ring.0[ring_points_count - 1].y)
        {
            validation_errors.valid = false;
            if quick {
                return validation_errors;
            }
            validation_errors.open_rings.push(ring.clone());
        }

        // Check for unsupported floating point value
        let mut prev_point = ring.0[0];
        if !prev_point.x.is_finite() {
            validation_errors.valid = false;
            if quick {
                return validation_errors;
            }
            validation_errors
                .unsupported_floating_point_values
                .push(prev_point.x);
        }
        if !prev_point.y.is_finite() {
            validation_errors.valid = false;
            if quick {
                return validation_errors;
            }
            validation_errors
                .unsupported_floating_point_values
                .push(prev_point.y);
        }

        let mut ring_points_map = LinkedHashMap::<CompCoord<T>, Coordinate<T>>::new();
        for i in 1..(ring_points_count) {
            let point = ring.0[i];

            // Check for unsupported floating point value
            if !point.x.is_finite() {
                validation_errors.valid = false;
                if quick {
                    return validation_errors;
                }
                validation_errors
                    .unsupported_floating_point_values
                    .push(point.x);
            }
            if !point.y.is_finite() {
                validation_errors.valid = false;
                if quick {
                    return validation_errors;
                }
                validation_errors
                    .unsupported_floating_point_values
                    .push(point.y);
            }

            // Check for repeated points (don't check the last point, since that should == first point)
            let pp_comp = CompCoord {
                0: Coordinate {
                    x: prev_point.x,
                    y: prev_point.y,
                },
            };
            if ring_points_map.contains_key(&pp_comp) {
                validation_errors.valid = false;
                if quick {
                    return validation_errors;
                }
                validation_errors.repeated_points.push(prev_point);
            }
            // Check for intersections
            let current_line = Line::<T>::new(prev_point, point);
            for (line_idx, line) in poly_lines.iter().enumerate() {
                if !line.end.eq(&current_line.start) && !line.start.eq(&current_line.end) {
                    // Check if any points intersect any lines
                    let start_point: Point<T> = current_line.start.into();
                    if line.intersects(&start_point) {
                        validation_errors.valid = false;
                        if quick {
                            return validation_errors;
                        }
                        validation_errors
                            .point_touching_line
                            .push(current_line.start);
                    } else if line.intersects(&current_line) {
                        validation_errors.valid = false;
                        if quick {
                            return validation_errors;
                        }
                        if line_idx > ring_start_idx {
                            validation_errors
                                .self_intersections
                                .push(line.intersection_point(&current_line));
                        } else {
                            validation_errors
                                .ring_intersects_other_ring
                                .push(line.intersection_point(&current_line));
                        }
                    }
                }
            }
            poly_lines.push(current_line);
            prev_point = point;
            ring_points_map.insert(pp_comp, point);
        }
        ring_start_idx = poly_lines.len();
    }

    validation_errors
}

struct CompCoord<T: num_traits::Float>(Coordinate<T>);

impl<T: num_traits::Float> PartialEq for CompCoord<T> {
    fn eq(&self, other: &CompCoord<T>) -> bool {
        // Note this function has no idea about the history of the float coordinates
        // only the current state.  This is a strict byte-equality check and does not
        // try to account in any way for the deviation of a float from its expected
        // value due to imprecision caused by floating point operations.
        transform_coord_to_array_of_u8(self) == transform_coord_to_array_of_u8(other)
    }
}

impl<T: num_traits::Float> Eq for CompCoord<T> {}

impl<T: num_traits::Float> Hash for CompCoord<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        transform_coord_to_array_of_u8(self).hash(state);
    }
}
/// Transform a coordinate into a 128byte array by concatenating the
/// byte representation of its position on the 2 axes (as f64)
///
fn transform_coord_to_array_of_u8<T>(coord: &CompCoord<T>) -> [u8; 16]
where
    T: num_traits::Float,
{
    let mut buf1 = [0; 8];
    NativeEndian::write_f64(&mut buf1, T::to_f64(&coord.0.x).unwrap());
    let mut buf2 = [0; 8];
    NativeEndian::write_f64(&mut buf2, T::to_f64(&coord.0.y).unwrap());

    [
        buf1[0], buf1[1], buf1[2], buf1[3], buf1[4], buf1[5], buf1[6], buf1[7], buf2[0], buf2[1],
        buf2[2], buf2[3], buf2[4], buf2[5], buf2[6], buf2[7],
    ]
}

/// Returns the coordinate at which two geometries intersect
pub trait IntersectionPoint<T>
where
    T: num_traits::Float,
{
    fn intersection_point(&self, line: &Line<T>) -> Coordinate<T>;
}

impl<T> IntersectionPoint<T> for Line<T>
where
    T: num_traits::Float,
{
    // See https://www.geeksforgeeks.org/program-for-point-of-intersection-of-two-lines/
    fn intersection_point(&self, line: &Line<T>) -> Coordinate<T> {
        // Line AB represented as a1x + b1y = c1
        let a1 = self.end.y - self.start.y;
        let b1 = self.start.x - self.end.x;
        let c1 = a1 * (self.start.x) + b1 * (self.start.y);

        // Line CD represented as a2x + b2y = c2
        let a2 = line.end.y - line.start.y;
        let b2 = line.start.x - line.end.x;
        let c2 = a2 * (line.start.x) + b2 * (line.start.y);

        let determinant = a1 * b2 - a2 * b1;
        if determinant.is_normal()
        // == T::from(0).unwrap() // Will this be problematic in cases where determinant is subnormal?
        {
            let x = (b2 * c1 - b1 * c2) / determinant;
            let y = (a1 * c2 - a2 * c1) / determinant;
            Coordinate { x, y }
        } else {
            // Parallel lines never intersect (hence infinity)
            Coordinate {
                x: T::infinity(),
                y: T::infinity(),
            }
        }
    }
}

/** Tests */

#[cfg(test)]
mod tests {
    use super::*;
    use geo_types::{line_string, point, polygon};

    #[test]
    fn can_validate_polygon() {
        let poly = polygon![
            (x: 1.0, y: 1.0),
            (x: 4.000000007, y: 1.0),
            (x: 4.0, y: 4.0),
            (x: 1.0, y: 4.0),
            (x: 1.0, y: 1.0),
        ];

        let valid = validate_polygon(&poly, false);
        assert_eq!(valid.valid, true);
    }

    #[test]
    fn can_validate_complex_polygon() {
        let poly = polygon!(
            exterior: [
                (x: 0., y: 0.),
                (x: 0., y: 20.),
                (x: 20., y: 20.),
                (x: 20., y: 0.),
            ],
            interiors: [
                [
                    (x: 10., y: 10.),
                    (x: 15., y: 10.),
                    (x: 15., y: 15.),
                    (x: 10., y: 15.),
                ],
            ],
        );

        let valid = validate_polygon(&poly, true);
        assert_eq!(valid.valid, true);
    }

    #[test]
    fn can_find_multiple_errors_in_complex_polygon() {
        let poly = polygon!(
            exterior: [
                (x: 0., y: 0.),
                (x: 0., y: 200.),
                (x: 200., y: 0.),
                (x: 200., y: 200.),
            ],
            interiors: [
                [
                    (x: 10., y: 20.),
                    (x: 50., y: 20.),
                    (x: 20., y: 50.),
                    (x: 50., y: 50.),
                ],
            ],
        );

        let valid = validate_polygon(&poly, false);
        assert_eq!(valid.valid, false);
        assert_eq!(valid.ring_intersects_other_ring.len(), 3);
        assert_eq!(valid.self_intersections.len(), 2);
        assert_eq!(valid.point_touching_line.len(), 1);

        assert_eq!(valid.ring_intersects_other_ring[0].x, 20_f64);
        assert_eq!(valid.ring_intersects_other_ring[0].y, 20_f64);
        assert_eq!(valid.ring_intersects_other_ring[1].x, 35_f64);
        assert_eq!(valid.ring_intersects_other_ring[1].y, 35_f64);
        assert_eq!(valid.ring_intersects_other_ring[2].x, 50_f64);
        assert_eq!(valid.ring_intersects_other_ring[2].y, 50_f64);

        assert_eq!(valid.self_intersections[0].x, 100_f64);
        assert_eq!(valid.self_intersections[0].y, 100_f64);
        assert_eq!(valid.self_intersections[1].x, 32.857142857142854_f64);
        assert_eq!(valid.self_intersections[1].y, 37.142857142857146_f64);

        assert_eq!(valid.point_touching_line[0].x, 50_f64);
        assert_eq!(valid.point_touching_line[0].y, 50_f64);
    }

    #[test]
    fn can_recognize_self_intersecting_polygon() {
        let poly = polygon![
            (x: 1.0_f64, y: 1.0),
            (x: 4.0, y: 1.0),
            (x: 1.0, y: 4.0),
            (x: 4.0, y: 4.0),
            (x: 1.0, y: 1.0),
        ];

        let valid = validate_polygon(&poly, false);
        assert_eq!(valid.valid, false);
        assert_eq!(valid.self_intersections.len(), 1);
        assert_eq!(valid.self_intersections[0].x, 2.5);
        assert_eq!(valid.self_intersections[0].y, 2.5);
    }

    #[test]
    fn can_recognize_multi_polygon() {
        let poly = polygon!(
            exterior: [
                (x: 0., y: 0.),
                (x: 0., y: 20.),
                (x: 20., y: 20.),
                (x: 0., y: 20.),
                (x: 0., y: 0.),
            ],
            interiors: [
                [
                    (x: 25., y: 25.),
                    (x: 25., y: 30.),
                    (x: 30., y: 30.),
                    (x: 30., y: 25.),
                    (x: 25., y: 25.),
                ],
            ],
        );

        let valid = validate_polygon(&poly, false);
        assert!(!valid.valid);
        assert!(valid.is_multi_polygon);
    }

    #[test]
    fn rejects_polygon_with_too_few_points() {
        let poly = polygon![
            (x: 1.0_f64, y: 1.0),
            (x: 4.0, y: 1.0),
            (x: 1.0, y: 1.0),
        ];

        let valid = validate_polygon(&poly, false);
        assert!(!valid.valid);
        assert!(valid.has_less_than_three_points);
    }

    #[test]
    fn rejects_polygon_with_nan_and_infinity() {
        let poly = polygon![
            (x: 1.0_f64, y: 1.0),
            (x: 1.0, y: 4.0),
            (x: 4.0, y: 4.0),
            (x: 4.0, y: 1.0),
            (x: std::f64::INFINITY, y: std::f64::NAN),
        ];

        let valid = validate_polygon(&poly, false);
        assert!(!valid.valid);
        assert_eq!(valid.unsupported_floating_point_values.len(), 2);
        assert!(valid.unsupported_floating_point_values[0].is_infinite());
        assert!(valid.unsupported_floating_point_values[1].is_nan());
    }

    #[test]
    fn rejects_polygon_with_repeated_points() {
        let poly = polygon![
            (x: 1.0_f64, y: 1.0),
            (x: 1.0, y: 4.0),
            (x: 4.0, y: 4.0),
            (x: 4.0, y: 1.0),
            (x: 1.0, y: 1.0),
            (x: 1.0, y: 1.0),
            (x: 1.0, y: 1.0),
        ];

        let valid = validate_polygon(&poly, false);
        assert!(!valid.valid);
        assert_eq!(valid.repeated_points.len(), 2);
        assert!(valid.repeated_points[0].eq(&Coordinate { x: 1.0_f64, y: 1.0 }));
        assert!(valid.repeated_points[1].eq(&Coordinate { x: 1.0_f64, y: 1.0 }));

        let poly2 = polygon![
            (x: 1.0_f64, y: 1.0),
            (x: 1.0, y: 4.0),
            (x: 4.0, y: 4.0),
            (x: 4.0, y: 1.0),
            (x: 4.0, y: 1.0),
            (x: 4.0, y: 1.0),
            (x: 1.0, y: 1.0),
        ];

        let valid2 = validate_polygon(&poly2, false);
        assert!(!valid2.valid);
        assert_eq!(valid2.repeated_points.len(), 2);
        assert!(valid2.repeated_points[0].eq(&Coordinate { x: 4.0_f64, y: 1.0 }));
        assert!(valid2.repeated_points[1].eq(&Coordinate { x: 4.0_f64, y: 1.0 }));
    }
}