Skip to content

Commit

Permalink
Add geometry trait specializations (#19)
Browse files Browse the repository at this point in the history
* Add geometry trait specializations

* update readme
  • Loading branch information
kylebarron authored Nov 5, 2024
1 parent cc6c552 commit f02ad82
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
86 changes: 86 additions & 0 deletions src/reader/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64> where Self: 'b;
type LineType<'b> = geo_traits::UnimplementedLine<f64> where Self: 'b;
type TriangleType<'b> = geo_traits::UnimplementedTriangle<f64> 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<f64> where Self: 'b;
type LineType<'b> = geo_traits::UnimplementedLine<f64> where Self: 'b;
type TriangleType<'b> = geo_traits::UnimplementedTriangle<f64> 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);

0 comments on commit f02ad82

Please sign in to comment.