From 7e3aa0963254a87dc2ad214fcc73343e60bd72b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9E=97=E4=BC=9F?= Date: Fri, 17 Nov 2023 11:43:10 +0800 Subject: [PATCH] Implement Within for geo scalars (#249) * Within geo scalars * Rename --- src/algorithm/geo/within.rs | 164 +++++++++++++++++++++++++++--------- 1 file changed, 125 insertions(+), 39 deletions(-) diff --git a/src/algorithm/geo/within.rs b/src/algorithm/geo/within.rs index dce635ad..2169fa35 100644 --- a/src/algorithm/geo/within.rs +++ b/src/algorithm/geo/within.rs @@ -137,9 +137,9 @@ iter_geo_impl!(MultiPolygonArray, MultiPointArray); iter_geo_impl!(MultiPolygonArray, MultiLineStringArray); iter_geo_impl!(MultiPolygonArray, MultiPolygonArray); -// ┌─────────────────────────────────┐ -// │ Implementations for RHS scalars │ -// └─────────────────────────────────┘ +// ┌──────────────────────────────────────────┐ +// │ Implementations for RHS geoarrow scalars │ +// └──────────────────────────────────────────┘ // Note: this implementation is outside the macro because it is not generic over O impl<'a> Within> for PointArray { @@ -156,7 +156,7 @@ impl<'a> Within> for PointArray { } /// Implementation that iterates over geo objects -macro_rules! iter_geo_impl_scalar { +macro_rules! iter_geo_impl_geoarrow_scalar { ($first:ty, $second:ty) => { impl<'a, O: OffsetSizeTrait> Within<$second> for $first { fn is_within(&self, rhs: &$second) -> BooleanArray { @@ -175,48 +175,134 @@ macro_rules! iter_geo_impl_scalar { } // Implementations on PointArray -iter_geo_impl_scalar!(PointArray, LineString<'a, O>); -iter_geo_impl_scalar!(PointArray, Polygon<'a, O>); -iter_geo_impl_scalar!(PointArray, MultiPoint<'a, O>); -iter_geo_impl_scalar!(PointArray, MultiLineString<'a, O>); -iter_geo_impl_scalar!(PointArray, MultiPolygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(PointArray, LineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(PointArray, Polygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(PointArray, MultiPoint<'a, O>); +iter_geo_impl_geoarrow_scalar!(PointArray, MultiLineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(PointArray, MultiPolygon<'a, O>); // Implementations on LineStringArray -iter_geo_impl_scalar!(LineStringArray, Point<'a>); -iter_geo_impl_scalar!(LineStringArray, LineString<'a, O>); -iter_geo_impl_scalar!(LineStringArray, Polygon<'a, O>); -iter_geo_impl_scalar!(LineStringArray, MultiPoint<'a, O>); -iter_geo_impl_scalar!(LineStringArray, MultiLineString<'a, O>); -iter_geo_impl_scalar!(LineStringArray, MultiPolygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(LineStringArray, Point<'a>); +iter_geo_impl_geoarrow_scalar!(LineStringArray, LineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(LineStringArray, Polygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(LineStringArray, MultiPoint<'a, O>); +iter_geo_impl_geoarrow_scalar!(LineStringArray, MultiLineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(LineStringArray, MultiPolygon<'a, O>); // Implementations on PolygonArray -iter_geo_impl_scalar!(PolygonArray, Point<'a>); -iter_geo_impl_scalar!(PolygonArray, LineString<'a, O>); -iter_geo_impl_scalar!(PolygonArray, Polygon<'a, O>); -iter_geo_impl_scalar!(PolygonArray, MultiPoint<'a, O>); -iter_geo_impl_scalar!(PolygonArray, MultiLineString<'a, O>); -iter_geo_impl_scalar!(PolygonArray, MultiPolygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(PolygonArray, Point<'a>); +iter_geo_impl_geoarrow_scalar!(PolygonArray, LineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(PolygonArray, Polygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(PolygonArray, MultiPoint<'a, O>); +iter_geo_impl_geoarrow_scalar!(PolygonArray, MultiLineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(PolygonArray, MultiPolygon<'a, O>); // Implementations on MultiPointArray -iter_geo_impl_scalar!(MultiPointArray, Point<'a>); -iter_geo_impl_scalar!(MultiPointArray, LineString<'a, O>); -iter_geo_impl_scalar!(MultiPointArray, Polygon<'a, O>); -iter_geo_impl_scalar!(MultiPointArray, MultiPoint<'a, O>); -iter_geo_impl_scalar!(MultiPointArray, MultiLineString<'a, O>); -iter_geo_impl_scalar!(MultiPointArray, MultiPolygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPointArray, Point<'a>); +iter_geo_impl_geoarrow_scalar!(MultiPointArray, LineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPointArray, Polygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPointArray, MultiPoint<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPointArray, MultiLineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPointArray, MultiPolygon<'a, O>); // Implementations on MultiLineStringArray -iter_geo_impl_scalar!(MultiLineStringArray, Point<'a>); -iter_geo_impl_scalar!(MultiLineStringArray, LineString<'a, O>); -iter_geo_impl_scalar!(MultiLineStringArray, Polygon<'a, O>); -iter_geo_impl_scalar!(MultiLineStringArray, MultiPoint<'a, O>); -iter_geo_impl_scalar!(MultiLineStringArray, MultiLineString<'a, O>); -iter_geo_impl_scalar!(MultiLineStringArray, MultiPolygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiLineStringArray, Point<'a>); +iter_geo_impl_geoarrow_scalar!(MultiLineStringArray, LineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiLineStringArray, Polygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiLineStringArray, MultiPoint<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiLineStringArray, MultiLineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiLineStringArray, MultiPolygon<'a, O>); // Implementations on MultiPolygonArray -iter_geo_impl_scalar!(MultiPolygonArray, Point<'a>); -iter_geo_impl_scalar!(MultiPolygonArray, LineString<'a, O>); -iter_geo_impl_scalar!(MultiPolygonArray, Polygon<'a, O>); -iter_geo_impl_scalar!(MultiPolygonArray, MultiPoint<'a, O>); -iter_geo_impl_scalar!(MultiPolygonArray, MultiLineString<'a, O>); -iter_geo_impl_scalar!(MultiPolygonArray, MultiPolygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPolygonArray, Point<'a>); +iter_geo_impl_geoarrow_scalar!(MultiPolygonArray, LineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPolygonArray, Polygon<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPolygonArray, MultiPoint<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPolygonArray, MultiLineString<'a, O>); +iter_geo_impl_geoarrow_scalar!(MultiPolygonArray, MultiPolygon<'a, O>); + +// ┌─────────────────────────────────────┐ +// │ Implementations for RHS geo scalars │ +// └─────────────────────────────────────┘ + +macro_rules! non_generic_iter_geo_impl_geo_scalar { + ($first:ty, $second:ty) => { + impl<'a> Within<$second> for $first { + fn is_within(&self, rhs: &$second) -> BooleanArray { + let mut output_array = BooleanBuilder::with_capacity(self.len()); + + self.iter_geo().for_each(|maybe_geom| { + let output = maybe_geom.map(|geom| geom.is_within(rhs)); + output_array.append_option(output) + }); + + output_array.finish() + } + } + }; +} + +// Implementations on PointArray +non_generic_iter_geo_impl_geo_scalar!(PointArray, geo::Point); +non_generic_iter_geo_impl_geo_scalar!(PointArray, geo::LineString); +non_generic_iter_geo_impl_geo_scalar!(PointArray, geo::Polygon); +non_generic_iter_geo_impl_geo_scalar!(PointArray, geo::MultiPoint); +non_generic_iter_geo_impl_geo_scalar!(PointArray, geo::MultiLineString); +non_generic_iter_geo_impl_geo_scalar!(PointArray, geo::MultiPolygon); + +macro_rules! iter_geo_impl_geo_scalar { + ($first:ty, $second:ty) => { + impl<'a, O: OffsetSizeTrait> Within<$second> for $first { + fn is_within(&self, rhs: &$second) -> BooleanArray { + let mut output_array = BooleanBuilder::with_capacity(self.len()); + + self.iter_geo().for_each(|maybe_geom| { + let output = maybe_geom.map(|geom| geom.is_within(rhs)); + output_array.append_option(output) + }); + + output_array.finish() + } + } + }; +} + +// Implementations on LineStringArray +iter_geo_impl_geo_scalar!(LineStringArray, geo::Point); +iter_geo_impl_geo_scalar!(LineStringArray, geo::LineString); +iter_geo_impl_geo_scalar!(LineStringArray, geo::Polygon); +iter_geo_impl_geo_scalar!(LineStringArray, geo::MultiPoint); +iter_geo_impl_geo_scalar!(LineStringArray, geo::MultiLineString); +iter_geo_impl_geo_scalar!(LineStringArray, geo::MultiPolygon); + +// Implementations on PolygonArray +iter_geo_impl_geo_scalar!(PolygonArray, geo::Point); +iter_geo_impl_geo_scalar!(PolygonArray, geo::LineString); +iter_geo_impl_geo_scalar!(PolygonArray, geo::Polygon); +iter_geo_impl_geo_scalar!(PolygonArray, geo::MultiPoint); +iter_geo_impl_geo_scalar!(PolygonArray, geo::MultiLineString); +iter_geo_impl_geo_scalar!(PolygonArray, geo::MultiPolygon); + +// Implementations on MultiPointArray +iter_geo_impl_geo_scalar!(MultiPointArray, geo::Point); +iter_geo_impl_geo_scalar!(MultiPointArray, geo::LineString); +iter_geo_impl_geo_scalar!(MultiPointArray, geo::Polygon); +iter_geo_impl_geo_scalar!(MultiPointArray, geo::MultiPoint); +iter_geo_impl_geo_scalar!(MultiPointArray, geo::MultiLineString); +iter_geo_impl_geo_scalar!(MultiPointArray, geo::MultiPolygon); + +// Implementations on MultiLineStringArray +iter_geo_impl_geo_scalar!(MultiLineStringArray, geo::Point); +iter_geo_impl_geo_scalar!(MultiLineStringArray, geo::LineString); +iter_geo_impl_geo_scalar!(MultiLineStringArray, geo::Polygon); +iter_geo_impl_geo_scalar!(MultiLineStringArray, geo::MultiPoint); +iter_geo_impl_geo_scalar!(MultiLineStringArray, geo::MultiLineString); +iter_geo_impl_geo_scalar!(MultiLineStringArray, geo::MultiPolygon); + +// Implementations on MultiPolygonArray +iter_geo_impl_geo_scalar!(MultiPolygonArray, geo::Point); +iter_geo_impl_geo_scalar!(MultiPolygonArray, geo::LineString); +iter_geo_impl_geo_scalar!(MultiPolygonArray, geo::Polygon); +iter_geo_impl_geo_scalar!(MultiPolygonArray, geo::MultiPoint); +iter_geo_impl_geo_scalar!(MultiPolygonArray, geo::MultiLineString); +iter_geo_impl_geo_scalar!(MultiPolygonArray, geo::MultiPolygon);