Skip to content

Commit

Permalink
Move num_coords functionality to CoordsIter
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Dec 18, 2020
1 parent 5d81ae1 commit f39d35f
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 70 deletions.
5 changes: 0 additions & 5 deletions geo-types/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# Changes

## Unreleased

* Introduce `num_coords` method on all geometry types.
* <https://github.com/georust/geo/pull/563>

## 0.6.2

* Add `into_iter`, `iter` and `iter_mut` methods for `MultiPolygon`, `MultiPoint`, and `MultiLineString`
Expand Down
16 changes: 0 additions & 16 deletions geo-types/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,6 @@ impl<T: CoordinateType> Geometry<T> {
None
}
}

/// Return the number of coordinates in the `Geometry`.
pub fn num_coords(&self) -> usize {
match self {
Geometry::Point(g) => g.num_coords(),
Geometry::Line(g) => g.num_coords(),
Geometry::LineString(g) => g.num_coords(),
Geometry::Polygon(g) => g.num_coords(),
Geometry::MultiPoint(g) => g.num_coords(),
Geometry::MultiLineString(g) => g.num_coords(),
Geometry::MultiPolygon(g) => g.num_coords(),
Geometry::GeometryCollection(g) => g.num_coords(),
Geometry::Rect(g) => g.num_coords(),
Geometry::Triangle(g) => g.num_coords(),
}
}
}

#[derive(Debug)]
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/geometry_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ impl<T: CoordinateType> GeometryCollection<T> {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Return the number of coordinates in the `GeometryCollection`.
pub fn num_coords(&self) -> usize {
self.0.iter().map(|geometry| geometry.num_coords()).sum()
}
}

/// Convert any Geometry (or anything that can be converted to a Geometry) into a
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ where
pub fn points(&self) -> (Point<T>, Point<T>) {
(self.start_point(), self.end_point())
}

/// Return the number of coordinates in the `Line`.
pub fn num_coords(self) -> usize {
2
}
}

impl<T: CoordinateType> From<[(T, T); 2]> for Line<T> {
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ impl<T: CoordinateType> MultiLineString<T> {
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut LineString<T>> {
self.0.iter_mut()
}

/// Return the number of coordinates in this `MultiLineString`.
pub fn num_coords(&self) -> usize {
self.0.iter().map(|line_string| line_string.num_coords()).sum()
}
}

#[cfg(test)]
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ impl<T: CoordinateType> MultiPoint<T> {
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point<T>> {
self.0.iter_mut()
}

/// Return the number of coordinates in the `MultiPoint`.
pub fn num_coords(&self) -> usize {
self.0.len()
}
}

#[cfg(test)]
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/multi_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ impl<T: CoordinateType> MultiPolygon<T> {
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Polygon<T>> {
self.0.iter_mut()
}

/// Return the number of coordinates in the `MultiPolygon`.
pub fn num_coords(&self) -> usize {
self.0.iter().map(|polygon| polygon.num_coords()).sum()
}
}

#[cfg(test)]
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,6 @@ where
pub fn set_lat(&mut self, lat: T) -> &mut Point<T> {
self.set_y(lat)
}

/// Return the number of coordinates in the `Point`.
pub fn num_coords(self) -> usize {
1
}
}

impl<T> Point<T>
Expand Down
6 changes: 0 additions & 6 deletions geo-types/src/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,6 @@ where
{
(current_vertex + (self.exterior.0.len() - 1) - 1) % (self.exterior.0.len() - 1)
}

/// Return the number of coordinates in the `Polygon`.
pub fn num_coords(&self) -> usize {
self.exterior().num_coords() +
self.interiors().iter().map(|i| i.num_coords()).sum::<usize>()
}
}

// used to check the sign of a vec of floats
Expand Down
8 changes: 0 additions & 8 deletions geo-types/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,6 @@ impl<T: CoordinateType> Rect<T> {
]
}

/// Return the number of coordinates in the `Rect`.
///
/// Note: Although a `Rect` is represented by two coordinates, it is
/// spatially represented by four, so this method returns `4`.
pub fn num_coords(self) -> usize {
4
}

fn assert_valid_bounds(&self) {
if !self.has_valid_bounds() {
panic!(RECT_INVALID_BOUNDS_ERROR);
Expand Down
5 changes: 0 additions & 5 deletions geo-types/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ impl<T: CoordinateType> Triangle<T> {
pub fn to_polygon(self) -> Polygon<T> {
polygon![self.0, self.1, self.2, self.0]
}

/// Number of coordinates in this `Triangle`.
pub fn num_coords(self) -> usize {
3
}
}

impl<IC: Into<Coordinate<T>> + Copy, T: CoordinateType> From<[IC; 3]> for Triangle<T> {
Expand Down
3 changes: 3 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Introduce `coords_count` method on `CoordsIter`.
* <https://github.com/georust/geo/pull/563>

## 0.16.0

* Fix panic when `simplify` is given a negative epsilon
Expand Down
72 changes: 72 additions & 0 deletions geo/src/algorithm/coords_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub trait CoordsIter<'a, T: CoordinateType> {
/// assert_eq!(None, iter.next());
/// ```
fn coords_iter(&'a self) -> Self::Iter;

fn coords_count(&'a self) -> usize;
}

// ┌──────────────────────────┐
Expand All @@ -42,6 +44,11 @@ impl<'a, T: CoordinateType> CoordsIter<'a, T> for Point<T> {
fn coords_iter(&'a self) -> Self::Iter {
iter::once(self.0)
}

/// Return the number of coordinates in the `Point`.
fn coords_count(&'a self) -> usize {
1
}
}

// ┌─────────────────────────┐
Expand All @@ -54,6 +61,11 @@ impl<'a, T: CoordinateType> CoordsIter<'a, T> for Line<T> {
fn coords_iter(&'a self) -> Self::Iter {
iter::once(self.start).chain(iter::once(self.end))
}

/// Return the number of coordinates in the `Line`.
fn coords_count(&'a self) -> usize {
2
}
}

// ┌───────────────────────────────┐
Expand All @@ -66,6 +78,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for LineString<T> {
fn coords_iter(&'a self) -> Self::Iter {
self.0.iter().copied()
}

/// Return the number of coordinates in the `LineString`.
fn coords_count(&'a self) -> usize {
self.0.len()
}
}

// ┌────────────────────────────┐
Expand All @@ -84,6 +101,12 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Polygon<T> {
.coords_iter()
.chain(MapCoordsIter(self.interiors().iter(), marker::PhantomData).flatten())
}

/// Return the number of coordinates in the `Polygon`.
fn coords_count(&'a self) -> usize {
self.exterior().num_coords() +
self.interiors().iter().map(|i| i.num_coords()).sum::<usize>()
}
}

// ┌───────────────────────────────┐
Expand All @@ -96,6 +119,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for MultiPoint<T> {
fn coords_iter(&'a self) -> Self::Iter {
MapCoordsIter(self.0.iter(), marker::PhantomData).flatten()
}

/// Return the number of coordinates in the `MultiPoint`.
fn coords_count(&'a self) -> usize {
self.0.len()
}
}

// ┌────────────────────────────────────┐
Expand All @@ -108,6 +136,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for MultiLineString<T> {
fn coords_iter(&'a self) -> Self::Iter {
MapCoordsIter(self.0.iter(), marker::PhantomData).flatten()
}

/// Return the number of coordinates in the `MultiLineString`.
fn coords_count(&'a self) -> usize {
self.0.iter().map(|line_string| line_string.num_coords()).sum()
}
}

// ┌─────────────────────────────────┐
Expand All @@ -120,6 +153,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for MultiPolygon<T> {
fn coords_iter(&'a self) -> Self::Iter {
MapCoordsIter(self.0.iter(), marker::PhantomData).flatten()
}

/// Return the number of coordinates in the `MultiPolygon`.
fn coords_count(&'a self) -> usize {
self.0.iter().map(|polygon| polygon.coords_count()).sum()
}
}

// ┌───────────────────────────────────────┐
Expand All @@ -132,6 +170,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for GeometryCollection<T> {
fn coords_iter(&'a self) -> Self::Iter {
Box::new(self.0.iter().flat_map(|geometry| geometry.coords_iter()))
}

/// Return the number of coordinates in the `GeometryCollection`.
fn coords_count(&'a self) -> usize {
self.0.iter().map(|geometry| geometry.coords_count()).sum()
}
}

// ┌─────────────────────────┐
Expand Down Expand Up @@ -164,6 +207,14 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Rect<T> {
y: self.min().y,
}))
}

/// Return the number of coordinates in the `Rect`.
///
/// Note: Although a `Rect` is represented by two coordinates, it is
/// spatially represented by four, so this method returns `4`.
fn coords_count(&'a self) -> usize {
4
}
}

// ┌─────────────────────────────┐
Expand All @@ -178,6 +229,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Triangle<T> {
.chain(iter::once(self.1))
.chain(iter::once(self.2))
}

/// Return the number of coordinates in the `Triangle`.
fn coords_count(&'a self) -> usize {
3
}
}

// ┌─────────────────────────────┐
Expand All @@ -203,6 +259,22 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Geometry<T> {
Geometry::Triangle(g) => GeometryCoordsIter::Triangle(g.coords_iter()),
}
}

/// Return the number of coordinates in the `Geometry`.
fn coords_count(&'a self) -> usize {
match self {
Geometry::Point(g) => g.coords_count(),
Geometry::Line(g) => g.coords_count(),
Geometry::LineString(g) => g.coords_count(),
Geometry::Polygon(g) => g.coords_count(),
Geometry::MultiPoint(g) => g.coords_count(),
Geometry::MultiLineString(g) => g.coords_count(),
Geometry::MultiPolygon(g) => g.coords_count(),
Geometry::GeometryCollection(g) => g.coords_count(),
Geometry::Rect(g) => g.coords_count(),
Geometry::Triangle(g) => g.coords_count(),
}
}
}

// ┌───────────┐
Expand Down

0 comments on commit f39d35f

Please sign in to comment.