Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement simplify using idx versions #424

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/algorithm/geo/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use crate::array::*;
use crate::chunked_array::ChunkedGeometryArray;
use crate::datatypes::GeoDataType;
use crate::error::{GeoArrowError, Result};
use crate::scalar::linestring::scalar::TakeLineString;
use crate::trait_::GeometryScalarTrait;
use crate::GeometryArrayTrait;
use arrow_array::OffsetSizeTrait;
use geo::Simplify as _Simplify;
use geo::{Simplify as _Simplify, SimplifyIdx};

/// Simplifies a geometry.
///
Expand Down Expand Up @@ -96,7 +98,26 @@ macro_rules! iter_geo_impl {
};
}

iter_geo_impl!(LineStringArray<O>, geo::LineString);
impl<O: OffsetSizeTrait> Simplify for LineStringArray<O> {
type Output = Self;

fn simplify(&self, epsilon: &f64) -> Self {
let geoms = self
.iter()
.map(|maybe_g| {
if let Some(geom) = maybe_g {
let idxs = geom.to_geo().simplify_idx(epsilon);
Some(TakeLineString::new(geom, idxs))
} else {
None
}
})
.collect::<Vec<_>>();
LineStringBuilder::from_nullable_line_strings(geoms.as_slice(), Default::default()).finish()
}
}

// 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);
Expand Down
15 changes: 14 additions & 1 deletion src/array/linestring/capacity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Add;
use std::ops::{Add, AddAssign};

use arrow_array::OffsetSizeTrait;

Expand Down Expand Up @@ -52,6 +52,12 @@ impl LineStringCapacity {
};
Ok(())
}

#[inline]
pub fn add_null(&mut self) {
self.geom_capacity += 1;
}

pub fn coord_capacity(&self) -> usize {
self.coord_capacity
}
Expand Down Expand Up @@ -94,3 +100,10 @@ impl Add for LineStringCapacity {
Self::new(coord_capacity, geom_capacity)
}
}

impl AddAssign for LineStringCapacity {
fn add_assign(&mut self, rhs: Self) {
self.coord_capacity += rhs.coord_capacity;
self.geom_capacity += rhs.geom_capacity;
}
}
2 changes: 1 addition & 1 deletion src/scalar/linestring/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod iterator;
mod owned;
mod scalar;
pub(crate) mod scalar;

pub use iterator::LineStringIterator;
pub use owned::OwnedLineString;
Expand Down
36 changes: 36 additions & 0 deletions src/scalar/linestring/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ impl<O: OffsetSizeTrait> PartialEq for LineString<'_, O> {
}
}

/// A LineString where specific indices of the geometry are selected
///
/// Primarily used in the Simplify trait
pub(crate) struct TakeLineString<'a, O: OffsetSizeTrait> {
line_string: LineString<'a, O>,
idxs: Vec<usize>,
}

impl<'a, O: OffsetSizeTrait> TakeLineString<'a, O> {
pub fn new(line_string: LineString<'a, O>, idxs: Vec<usize>) -> Self {
Self { line_string, idxs }
}
}

impl<'a, O: OffsetSizeTrait> LineStringTrait for TakeLineString<'a, O> {
type T = f64;
type ItemType<'b> = Point<'a> where Self: 'b;
type Iter<'b> = LineStringIterator<'a, O> where Self: 'b;

fn coords(&self) -> Self::Iter<'_> {
todo!()
}

fn num_coords(&self) -> usize {
self.idxs.len()
}

fn coord(&self, i: usize) -> Option<Self::ItemType<'_>> {
if i > self.num_coords() {
return None;
}

self.line_string.coord(self.idxs[i])
}
}

#[cfg(test)]
mod test {
use crate::array::LineStringArray;
Expand Down
4 changes: 2 additions & 2 deletions src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use binary::WKB;
pub use coord::{Coord, InterleavedCoord, SeparatedCoord};
pub use geometry::Geometry;
pub use geometrycollection::GeometryCollection;
pub use linestring::{LineString, OwnedLineString};
pub use linestring::{LineString, LineStringIterator, OwnedLineString};
pub use multilinestring::{MultiLineString, OwnedMultiLineString};
pub use multipoint::{MultiPoint, OwnedMultiPoint};
pub use multipolygon::{MultiPolygon, OwnedMultiPolygon};
Expand All @@ -17,7 +17,7 @@ mod binary;
mod coord;
mod geometry;
mod geometrycollection;
mod linestring;
pub(crate) mod linestring;
mod multilinestring;
mod multipoint;
mod multipolygon;
Expand Down
Loading