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
use crate::array::*;
use arrow_array::OffsetSizeTrait;
use geo::Simplify as _Simplify;
/// Simplifies a geometry.
///
/// The [Ramer–Douglas–Peucker
/// algorithm](https://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm) simplifies a
/// linestring. Polygons are simplified by running the RDP algorithm on all their constituent
/// rings. This may result in invalid Polygons, and has no guarantee of preserving topology.
///
/// Multi* objects are simplified by simplifying all their constituent geometries individually.
///
/// An epsilon less than or equal to zero will return an unaltered version of the geometry.
pub trait Simplify {
/// Returns the simplified representation of a geometry, using the [Ramer–Douglas–Peucker](https://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm) algorithm
///
/// # Examples
///
/// ```
/// use geoarrow2::algorithm::geo::Simplify;
/// use geoarrow2::array::LineStringArray;
/// use geoarrow2::trait_::GeoArrayAccessor;
/// use geo::line_string;
///
/// let line_string = line_string![
/// (x: 0.0, y: 0.0),
/// (x: 5.0, y: 4.0),
/// (x: 11.0, y: 5.5),
/// (x: 17.3, y: 3.2),
/// (x: 27.8, y: 0.1),
/// ];
/// let line_string_array: LineStringArray<i32> = vec![line_string].into();
///
/// let simplified_array = line_string_array.simplify(&1.0);
///
/// let expected = line_string![
/// (x: 0.0, y: 0.0),
/// (x: 5.0, y: 4.0),
/// (x: 11.0, y: 5.5),
/// (x: 27.8, y: 0.1),
/// ];
///
/// assert_eq!(expected, simplified_array.value_as_geo(0))
/// ```
fn simplify(&self, epsilon: &f64) -> Self;
}
// Note: this can't (easily) be parameterized in the macro because PointArray is not generic over O
impl Simplify for PointArray {
fn simplify(&self, _epsilon: &f64) -> Self {
self.clone()
}
}
/// Implementation that returns the identity
macro_rules! identity_impl {
($type:ty) => {
impl<O: OffsetSizeTrait> Simplify for $type {
fn simplify(&self, _epsilon: &f64) -> Self {
self.clone()
}
}
};
}
identity_impl!(MultiPointArray<O>);
/// Implementation that iterates over geo objects
macro_rules! iter_geo_impl {
($type:ty, $geo_type:ty) => {
impl<O: OffsetSizeTrait> Simplify for $type {
fn simplify(&self, epsilon: &f64) -> Self {
let output_geoms: Vec<Option<$geo_type>> = self
.iter_geo()
.map(|maybe_g| maybe_g.map(|geom| geom.simplify(epsilon)))
.collect();
output_geoms.into()
}
}
};
}
iter_geo_impl!(LineStringArray<O>, geo::LineString);
iter_geo_impl!(PolygonArray<O>, geo::Polygon);
iter_geo_impl!(MultiLineStringArray<O>, geo::MultiLineString);
iter_geo_impl!(MultiPolygonArray<O>, geo::MultiPolygon);
impl<O: OffsetSizeTrait> Simplify for GeometryArray<O> {
fn simplify(&self, epsilon: &f64) -> Self {
use GeometryArray::*;
match self {
Point(arr) => Point(arr.simplify(epsilon)),
LineString(arr) => LineString(arr.simplify(epsilon)),
Polygon(arr) => Polygon(arr.simplify(epsilon)),
MultiPoint(arr) => MultiPoint(arr.simplify(epsilon)),
MultiLineString(arr) => MultiLineString(arr.simplify(epsilon)),
MultiPolygon(arr) => MultiPolygon(arr.simplify(epsilon)),
Rect(arr) => Rect(arr.clone()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::array::{LineStringArray, PolygonArray};
use crate::trait_::GeoArrayAccessor;
use geo::{line_string, polygon};
#[test]
fn rdp_test() {
let input_geom = line_string![
(x: 0.0, y: 0.0 ),
(x: 5.0, y: 4.0 ),
(x: 11.0, y: 5.5 ),
(x: 17.3, y: 3.2 ),
(x: 27.8, y: 0.1 ),
];
let input_array: LineStringArray<i64> = vec![input_geom].into();
let result_array = input_array.simplify(&1.0);
let expected = line_string![
( x: 0.0, y: 0.0 ),
( x: 5.0, y: 4.0 ),
( x: 11.0, y: 5.5 ),
( x: 27.8, y: 0.1 ),
];
assert_eq!(expected, result_array.get_as_geo(0).unwrap());
}
#[test]
fn polygon() {
let input_geom = polygon![
(x: 0., y: 0.),
(x: 0., y: 10.),
(x: 5., y: 11.),
(x: 10., y: 10.),
(x: 10., y: 0.),
(x: 0., y: 0.),
];
let input_array: PolygonArray<i64> = vec![input_geom].into();
let result_array = input_array.simplify(&2.0);
let expected = polygon![
(x: 0., y: 0.),
(x: 0., y: 10.),
(x: 10., y: 10.),
(x: 10., y: 0.),
(x: 0., y: 0.),
];
assert_eq!(expected, result_array.get_as_geo(0).unwrap());
}
}