Skip to content

Commit

Permalink
Implement specializations for geo-traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Oct 30, 2024
1 parent 6ac47b6 commit a0d29b1
Showing 1 changed file with 100 additions and 14 deletions.
114 changes: 100 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,13 @@ impl<T: WktNum> GeometryTrait for Wkt<T> {

fn dim(&self) -> geo_traits::Dimensions {
match self {
Wkt::Point(geom) => geom.dim(),
Wkt::LineString(geom) => geom.dim(),
Wkt::Polygon(geom) => geom.dim(),
Wkt::MultiPoint(geom) => geom.dim(),
Wkt::MultiLineString(geom) => geom.dim(),
Wkt::MultiPolygon(geom) => geom.dim(),
Wkt::GeometryCollection(geom) => geom.dim(),
Wkt::Point(geom) => PointTrait::dim(geom),
Wkt::LineString(geom) => LineStringTrait::dim(geom),
Wkt::Polygon(geom) => PolygonTrait::dim(geom),
Wkt::MultiPoint(geom) => MultiPointTrait::dim(geom),
Wkt::MultiLineString(geom) => MultiLineStringTrait::dim(geom),
Wkt::MultiPolygon(geom) => MultiPolygonTrait::dim(geom),
Wkt::GeometryCollection(geom) => GeometryCollectionTrait::dim(geom),
}
}

Expand Down Expand Up @@ -468,13 +468,13 @@ impl<T: WktNum> GeometryTrait for &Wkt<T> {

fn dim(&self) -> geo_traits::Dimensions {
match self {
Wkt::Point(geom) => geom.dim(),
Wkt::LineString(geom) => geom.dim(),
Wkt::Polygon(geom) => geom.dim(),
Wkt::MultiPoint(geom) => geom.dim(),
Wkt::MultiLineString(geom) => geom.dim(),
Wkt::MultiPolygon(geom) => geom.dim(),
Wkt::GeometryCollection(geom) => geom.dim(),
Wkt::Point(geom) => PointTrait::dim(geom),
Wkt::LineString(geom) => LineStringTrait::dim(geom),
Wkt::Polygon(geom) => PolygonTrait::dim(geom),
Wkt::MultiPoint(geom) => MultiPointTrait::dim(geom),
Wkt::MultiLineString(geom) => MultiLineStringTrait::dim(geom),
Wkt::MultiPolygon(geom) => MultiPolygonTrait::dim(geom),
Wkt::GeometryCollection(geom) => GeometryCollectionTrait::dim(geom),
}
}

Expand Down Expand Up @@ -505,6 +505,92 @@ impl<T: WktNum> GeometryTrait for &Wkt<T> {
}
}

// Specialized implementations on each WKT concrete type.

macro_rules! impl_specialization {
($geometry_type:ident) => {
impl<T: WktNum> GeometryTrait for $geometry_type<T> {
type T = T;
type PointType<'b> = Point<Self::T> where Self: 'b;
type LineStringType<'b> = LineString<Self::T> where Self: 'b;
type PolygonType<'b> = Polygon<Self::T> where Self: 'b;
type MultiPointType<'b> = MultiPoint<Self::T> where Self: 'b;
type MultiLineStringType<'b> = MultiLineString<Self::T> where Self: 'b;
type MultiPolygonType<'b> = MultiPolygon<Self::T> where Self: 'b;
type GeometryCollectionType<'b> = GeometryCollection<Self::T> where Self: 'b;
type RectType<'b> = geo_traits::UnimplementedRect<T> where Self: 'b;
type LineType<'b> = geo_traits::UnimplementedLine<T> where Self: 'b;
type TriangleType<'b> = geo_traits::UnimplementedTriangle<T> where Self: 'b;

fn dim(&self) -> geo_traits::Dimensions {
geo_traits::Dimensions::Xy
}

fn as_type(
&self,
) -> geo_traits::GeometryType<
'_,
Point<T>,
LineString<T>,
Polygon<T>,
MultiPoint<T>,
MultiLineString<T>,
MultiPolygon<T>,
GeometryCollection<T>,
Self::RectType<'_>,
Self::TriangleType<'_>,
Self::LineType<'_>,
> {
geo_traits::GeometryType::$geometry_type(self)
}
}

impl<'a, T: WktNum + 'a> GeometryTrait for &'a $geometry_type<T> {
type T = T;
type PointType<'b> = Point<Self::T> where Self: 'b;
type LineStringType<'b> = LineString<Self::T> where Self: 'b;
type PolygonType<'b> = Polygon<Self::T> where Self: 'b;
type MultiPointType<'b> = MultiPoint<Self::T> where Self: 'b;
type MultiLineStringType<'b> = MultiLineString<Self::T> where Self: 'b;
type MultiPolygonType<'b> = MultiPolygon<Self::T> where Self: 'b;
type GeometryCollectionType<'b> = GeometryCollection<Self::T> where Self: 'b;
type RectType<'b> = geo_traits::UnimplementedRect<T> where Self: 'b;
type LineType<'b> = geo_traits::UnimplementedLine<T> where Self: 'b;
type TriangleType<'b> = geo_traits::UnimplementedTriangle<T> where Self: 'b;

fn dim(&self) -> geo_traits::Dimensions {
geo_traits::Dimensions::Xy
}

fn as_type(
&self,
) -> geo_traits::GeometryType<
'_,
Point<T>,
LineString<T>,
Polygon<T>,
MultiPoint<T>,
MultiLineString<T>,
MultiPolygon<T>,
GeometryCollection<T>,
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);

fn infer_geom_dimension<T: WktNum + FromStr + Default>(
tokens: &mut PeekableTokens<T>,
) -> Result<Dimension, &'static str> {
Expand Down

0 comments on commit a0d29b1

Please sign in to comment.