#polygon #merge #repair #multipolygon #graphics

geo-repair-polygon

A trait that attempts repairing a geo-types (Multi)Polygon that is invalid according to geo-validator, and a trait to merge MultiPolygon into a valid Polygon

3 releases

0.1.2 Jun 5, 2020
0.1.1 Apr 21, 2020
0.1.0 Apr 17, 2020

#214 in Geospatial

Download history 29/week @ 2024-07-19 28/week @ 2024-07-26 59/week @ 2024-08-02 27/week @ 2024-08-09 13/week @ 2024-08-16 14/week @ 2024-08-23 51/week @ 2024-08-30 65/week @ 2024-09-06 43/week @ 2024-09-13 36/week @ 2024-09-20 20/week @ 2024-09-27 98/week @ 2024-10-04 156/week @ 2024-10-11 108/week @ 2024-10-18 132/week @ 2024-10-25 55/week @ 2024-11-01

468 downloads per month

MIT license

64KB
833 lines

rust-geo-repair-polygon

A package containing:

  • a trait for (Multi)Polygon that attempts repairing a geo-types (Multi)Polygon that is invalid according to geo-validator.
  • a trait for MultiPolygon that merges all of its Polygons into a single valid Polygon

Repair (MultiPolygon/Polygon)

When running repair, it will try its best to produce a (Multi)Polygon that meets OGC standards. Some very invalid polygons may still fail, but most come through as valid with very little change.

Example

use geo_repair_polygon::repair::Repair;
use geo_types::polygon;

let bowtie = polygon![
    (x: 0_f64, y: 0.),
    (x: 0., y: 20.),
    (x: 20., y: 0.),
    (x: 20., y: 20.),
    (x: 0., y: 0.)];

let expected = polygon![
    (x: 0_f64, y: 0.),
    (x: 9.999999999999996, y: 9.999999999999996),
    (x: 10.000000000000004, y: 9.999999999999996),
    (x: 20., y: 0.),
    (x: 20., y: 20.),
    (x: 10.000000000000004, y: 10.000000000000004),
    (x: 9.999999999999996, y: 10.000000000000004),
    (x: 0., y: 20.),
    (x: 0., y: 0.)];

let repaired_bowtie = bowtie.repair();
assert!(repaired_bowtie.is_some());
assert_eq!(repaired_bowtie.unwrap(), expected);

Join (MultiPolygon)

The join trait for MultiPolygon will merge all of its Polygons into a single valid Polygon. This may involve a union or the creation of a small bridge between the closest points of non-overlapping Polygons.

Example

use geo_repair_polygon::join::Join;
use geo_types::{polygon, MultiPolygon};

let separate_polygons: MultiPolygon<f64> = vec![
    polygon![
        (x: 0_f64, y: 0.),
        (x: 10., y: 0.),
        (x: 10., y: 10.),
        (x: 0., y: 10.),
        (x: 0., y: 0.)],
    polygon![
        (x: 11_f64, y: 11.),
        (x: 20., y: 11.),
        (x: 20., y: 20.),
        (x: 11., y: 20.),
        (x: 11., y: 11.)],
    ].into();

let expected = polygon![
    (x: 10_f64, y: 10.),
    (x: 11., y: 11.),
    (x: 20., y: 11.),
    (x: 20., y: 20.),
    (x: 11., y: 20.),
    (x: 11., y: 11.000000000000004),
    (x: 9.999999999999996, y: 10.),
    (x: 0., y: 10.),
    (x: 0., y: 0.),
    (x: 10., y: 0.),
    (x: 10., y: 10.)];

let merged = separate_polygons.join();
assert_eq!(merged, expected);

Dependencies

~3MB
~54K SLoC