From f02ad8275571866bda700b6345077ff34c7a5102 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Mon, 4 Nov 2024 23:25:10 -0500 Subject: [PATCH] Add geometry trait specializations (#19) * Add geometry trait specializations * update readme --- README.md | 1 - src/reader/geometry.rs | 86 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 121e29a..a9af095 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,4 @@ This is being refactored out of https://github.com/geoarrow/geoarrow-rs in the h Notes: -- Implement `GeometryTrait` for each scalar type. - Implement M and ZM variants. diff --git a/src/reader/geometry.rs b/src/reader/geometry.rs index 5b2f07b..e193a8f 100644 --- a/src/reader/geometry.rs +++ b/src/reader/geometry.rs @@ -176,3 +176,89 @@ impl<'a> GeometryTrait for &'a Wkb<'a> { } } } + +// Specialized implementations on each WKT concrete type. + +macro_rules! impl_specialization { + ($geometry_type:ident) => { + impl GeometryTrait for $geometry_type<'_> { + type T = f64; + type PointType<'b> = Point<'b> where Self: 'b; + type LineStringType<'b> = LineString<'b> where Self: 'b; + type PolygonType<'b> = Polygon<'b> where Self: 'b; + type MultiPointType<'b> = MultiPoint<'b> where Self: 'b; + type MultiLineStringType<'b> = MultiLineString<'b> where Self: 'b; + type MultiPolygonType<'b> = MultiPolygon<'b> where Self: 'b; + type GeometryCollectionType<'b> = GeometryCollection<'b> where Self: 'b; + type RectType<'b> = geo_traits::UnimplementedRect where Self: 'b; + type LineType<'b> = geo_traits::UnimplementedLine where Self: 'b; + type TriangleType<'b> = geo_traits::UnimplementedTriangle where Self: 'b; + + fn dim(&self) -> geo_traits::Dimensions { + self.dimension() + } + + fn as_type( + &self, + ) -> geo_traits::GeometryType< + '_, + Point, + LineString, + Polygon, + MultiPoint, + MultiLineString, + MultiPolygon, + GeometryCollection, + Self::RectType<'_>, + Self::TriangleType<'_>, + Self::LineType<'_>, + > { + geo_traits::GeometryType::$geometry_type(self) + } + } + + impl<'a> GeometryTrait for &'a $geometry_type<'_> { + type T = f64; + type PointType<'b> = Point<'b> where Self: 'b; + type LineStringType<'b> = LineString<'b> where Self: 'b; + type PolygonType<'b> = Polygon<'b> where Self: 'b; + type MultiPointType<'b> = MultiPoint<'b> where Self: 'b; + type MultiLineStringType<'b> = MultiLineString<'b> where Self: 'b; + type MultiPolygonType<'b> = MultiPolygon<'b> where Self: 'b; + type GeometryCollectionType<'b> = GeometryCollection<'b> where Self: 'b; + type RectType<'b> = geo_traits::UnimplementedRect where Self: 'b; + type LineType<'b> = geo_traits::UnimplementedLine where Self: 'b; + type TriangleType<'b> = geo_traits::UnimplementedTriangle where Self: 'b; + + fn dim(&self) -> geo_traits::Dimensions { + self.dimension() + } + + fn as_type( + &self, + ) -> geo_traits::GeometryType< + '_, + Point, + LineString, + Polygon, + MultiPoint, + MultiLineString, + MultiPolygon, + GeometryCollection, + Self::RectType<'_>, + Self::TriangleType<'_>, + Self::LineType<'_>, + > { + geo_traits::GeometryType::$geometry_type(self) + } + } + }; +} + +impl_specialization!(Point); +impl_specialization!(LineString); +impl_specialization!(Polygon); +impl_specialization!(MultiPoint); +impl_specialization!(MultiLineString); +impl_specialization!(MultiPolygon); +impl_specialization!(GeometryCollection);