Skip to content

Commit

Permalink
n-dimensional geo trait (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Jul 21, 2024
1 parent 066a999 commit 3e2a79c
Show file tree
Hide file tree
Showing 54 changed files with 773 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/algorithm/native/bounding_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ impl RectTrait for BoundingRect {
type T = f64;
type ItemType<'a> = Coord;

fn dim(&self) -> usize {
2
}

fn lower(&self) -> Self::ItemType<'_> {
Coord {
x: self.minx,
Expand Down
79 changes: 79 additions & 0 deletions src/geo_traits/coord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ use geo::{Coord, CoordNum, Point};
pub trait CoordTrait {
type T: CoordNum;

/// Access the n'th (0-based) element of the CoordinateTuple.
/// May panic if n >= DIMENSION.
/// See also [`nth()`](Self::nth).
fn nth_unchecked(&self, n: usize) -> Self::T;

/// Native dimension of the coordinate tuple
fn dim(&self) -> usize;

/// Access the n'th (0-based) element of the CoordinateTuple.
/// Returns NaN if `n >= DIMENSION`.
/// See also [`nth()`](Self::nth_unchecked).
fn nth(&self, n: usize) -> Option<Self::T> {
if n < self.dim() {
Some(self.nth_unchecked(n))
} else {
None
}
}

/// x component of this coord
fn x(&self) -> Self::T;

Expand All @@ -19,6 +38,18 @@ pub trait CoordTrait {
impl<T: CoordNum> CoordTrait for Point<T> {
type T = T;

fn nth_unchecked(&self, n: usize) -> Self::T {
match n {
0 => self.x(),
1 => self.y(),
_ => panic!(),
}
}

fn dim(&self) -> usize {
2
}

fn x(&self) -> Self::T {
self.0.x
}
Expand All @@ -31,6 +62,18 @@ impl<T: CoordNum> CoordTrait for Point<T> {
impl<T: CoordNum> CoordTrait for &Point<T> {
type T = T;

fn nth_unchecked(&self, n: usize) -> Self::T {
match n {
0 => self.x(),
1 => self.y(),
_ => panic!(),
}
}

fn dim(&self) -> usize {
2
}

fn x(&self) -> Self::T {
self.0.x
}
Expand All @@ -43,6 +86,18 @@ impl<T: CoordNum> CoordTrait for &Point<T> {
impl<T: CoordNum> CoordTrait for Coord<T> {
type T = T;

fn nth_unchecked(&self, n: usize) -> Self::T {
match n {
0 => self.x(),
1 => self.y(),
_ => panic!(),
}
}

fn dim(&self) -> usize {
2
}

fn x(&self) -> Self::T {
self.x
}
Expand All @@ -55,6 +110,18 @@ impl<T: CoordNum> CoordTrait for Coord<T> {
impl<T: CoordNum> CoordTrait for &Coord<T> {
type T = T;

fn nth_unchecked(&self, n: usize) -> Self::T {
match n {
0 => self.x(),
1 => self.y(),
_ => panic!(),
}
}

fn dim(&self) -> usize {
2
}

fn x(&self) -> Self::T {
self.x
}
Expand All @@ -67,6 +134,18 @@ impl<T: CoordNum> CoordTrait for &Coord<T> {
impl<T: CoordNum> CoordTrait for (T, T) {
type T = T;

fn nth_unchecked(&self, n: usize) -> Self::T {
match n {
0 => self.x(),
1 => self.y(),
_ => panic!(),
}
}

fn dim(&self) -> usize {
2
}

fn x(&self) -> Self::T {
self.0
}
Expand Down
10 changes: 10 additions & 0 deletions src/geo_traits/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub trait GeometryTrait {
where
Self: 'a;

fn dim(&self) -> usize;

fn as_type(
&self,
) -> GeometryType<
Expand Down Expand Up @@ -87,6 +89,10 @@ impl<'a, T: CoordNum + 'a> GeometryTrait for Geometry<T> {
type GeometryCollection<'b> = GeometryCollection<Self::T> where Self: 'b;
type Rect<'b> = Rect<Self::T> where Self: 'b;

fn dim(&self) -> usize {
2
}

fn as_type(
&self,
) -> GeometryType<
Expand Down Expand Up @@ -125,6 +131,10 @@ impl<'a, T: CoordNum + 'a> GeometryTrait for &'a Geometry<T> {
type GeometryCollection<'b> = GeometryCollection<Self::T> where Self: 'b;
type Rect<'b> = Rect<Self::T> where Self: 'b;

fn dim(&self) -> usize {
2
}

fn as_type(
&self,
) -> GeometryType<
Expand Down
10 changes: 10 additions & 0 deletions src/geo_traits/geometry_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub trait GeometryCollectionTrait: Sized {
where
Self: 'a;

fn dim(&self) -> usize;

/// An iterator over the geometries in this GeometryCollection
fn geometries(&self) -> GeometryCollectionIterator<'_, Self::T, Self::ItemType<'_>, Self> {
GeometryCollectionIterator::new(self, 0, self.num_geometries())
Expand Down Expand Up @@ -41,6 +43,10 @@ impl<T: CoordNum> GeometryCollectionTrait for GeometryCollection<T> {
where
Self: 'a;

fn dim(&self) -> usize {
2
}

fn num_geometries(&self) -> usize {
self.0.len()
}
Expand All @@ -55,6 +61,10 @@ impl<'a, T: CoordNum> GeometryCollectionTrait for &'a GeometryCollection<T> {
type ItemType<'b> = &'a Geometry<Self::T> where
Self: 'b;

fn dim(&self) -> usize {
2
}

fn num_geometries(&self) -> usize {
self.0.len()
}
Expand Down
10 changes: 10 additions & 0 deletions src/geo_traits/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub trait LineStringTrait: Sized {
where
Self: 'a;

fn dim(&self) -> usize;

/// An iterator over the coords in this LineString
fn coords(&self) -> LineStringIterator<'_, Self::T, Self::ItemType<'_>, Self> {
LineStringIterator::new(self, 0, self.num_coords())
Expand Down Expand Up @@ -40,6 +42,10 @@ impl<T: CoordNum> LineStringTrait for LineString<T> {
type T = T;
type ItemType<'a> = &'a Coord<Self::T> where Self: 'a;

fn dim(&self) -> usize {
2
}

fn num_coords(&self) -> usize {
self.0.len()
}
Expand All @@ -53,6 +59,10 @@ impl<'a, T: CoordNum> LineStringTrait for &'a LineString<T> {
type T = T;
type ItemType<'b> = &'a Coord<Self::T> where Self: 'b;

fn dim(&self) -> usize {
2
}

fn num_coords(&self) -> usize {
self.0.len()
}
Expand Down
10 changes: 10 additions & 0 deletions src/geo_traits/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub trait MultiLineStringTrait: Sized {
where
Self: 'a;

fn dim(&self) -> usize;

/// An iterator over the LineStrings in this MultiLineString
fn lines(&self) -> MultiLineStringIterator<'_, Self::T, Self::ItemType<'_>, Self> {
MultiLineStringIterator::new(self, 0, self.num_lines())
Expand Down Expand Up @@ -39,6 +41,10 @@ impl<T: CoordNum> MultiLineStringTrait for MultiLineString<T> {
type T = T;
type ItemType<'a> = &'a LineString<Self::T> where Self: 'a;

fn dim(&self) -> usize {
2
}

fn num_lines(&self) -> usize {
self.0.len()
}
Expand All @@ -52,6 +58,10 @@ impl<'a, T: CoordNum> MultiLineStringTrait for &'a MultiLineString<T> {
type T = T;
type ItemType<'b> = &'a LineString<Self::T> where Self: 'b;

fn dim(&self) -> usize {
2
}

fn num_lines(&self) -> usize {
self.0.len()
}
Expand Down
10 changes: 10 additions & 0 deletions src/geo_traits/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub trait MultiPointTrait: Sized {
where
Self: 'a;

fn dim(&self) -> usize;

/// An iterator over the points in this MultiPoint
fn points(&self) -> MultiPointIterator<'_, Self::T, Self::ItemType<'_>, Self> {
MultiPointIterator::new(self, 0, self.num_points())
Expand Down Expand Up @@ -39,6 +41,10 @@ impl<T: CoordNum> MultiPointTrait for MultiPoint<T> {
type T = T;
type ItemType<'a> = &'a Point<Self::T> where Self: 'a;

fn dim(&self) -> usize {
2
}

fn num_points(&self) -> usize {
self.0.len()
}
Expand All @@ -52,6 +58,10 @@ impl<'a, T: CoordNum> MultiPointTrait for &'a MultiPoint<T> {
type T = T;
type ItemType<'b> = &'a Point<Self::T> where Self: 'b;

fn dim(&self) -> usize {
2
}

fn num_points(&self) -> usize {
self.0.len()
}
Expand Down
10 changes: 10 additions & 0 deletions src/geo_traits/multi_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub trait MultiPolygonTrait: Sized {
where
Self: 'a;

fn dim(&self) -> usize;

/// An iterator over the Polygons in this MultiPolygon
fn polygons(&self) -> MultiPolygonIterator<'_, Self::T, Self::ItemType<'_>, Self> {
MultiPolygonIterator::new(self, 0, self.num_polygons())
Expand Down Expand Up @@ -39,6 +41,10 @@ impl<T: CoordNum> MultiPolygonTrait for MultiPolygon<T> {
type T = T;
type ItemType<'a> = &'a Polygon<Self::T> where Self: 'a;

fn dim(&self) -> usize {
2
}

fn num_polygons(&self) -> usize {
self.0.len()
}
Expand All @@ -52,6 +58,10 @@ impl<'a, T: CoordNum> MultiPolygonTrait for &'a MultiPolygon<T> {
type T = T;
type ItemType<'b> = &'a Polygon<Self::T> where Self: 'b;

fn dim(&self) -> usize {
2
}

fn num_polygons(&self) -> usize {
self.0.len()
}
Expand Down
Loading

0 comments on commit 3e2a79c

Please sign in to comment.