Skip to content

Commit

Permalink
implement area for multipolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Jan 16, 2017
1 parent 7496f15 commit 43105b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/algorithm/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ pub fn polygon<'a, G, T>(polygon: &'a G) -> T
})
}

pub fn multi_polygon<'a, G, T>(multi_polygon: &'a G) -> T
where T: 'a + Float,
G: 'a + MultiPolygonTrait<'a, T> + ?Sized
{
multi_polygon.polygons().map(polygon).fold(T::zero(), |acc, n| acc + n)
}

#[cfg(test)]
mod test {
use num::Float;
use types::{Coordinate, Point, LineString, Polygon, MultiPolygon, Bbox};
use types::{Coordinate, Point, LineString, Polygon, MultiPolygon};
use test_helpers::within_epsilon;
use ::PolygonTrait;
// Area of the polygon
use ::{PolygonTrait, MultiPolygonTrait};

#[test]
fn area_empty_polygon_test() {
let poly = Polygon::<f64>::new(LineString(Vec::new()), Vec::new());
Expand All @@ -56,14 +63,6 @@ mod test {
assert!(within_epsilon(poly.area(), 30., Float::epsilon()));
}

/*
#[test]
fn bbox_test() {
let bbox = Bbox {xmin: 10., xmax: 20., ymin: 30., ymax: 40.};
assert!(within_epsilon(bbox.area(), 100., Float::epsilon()));
}
*/

#[test]
fn area_polygon_inner_test() {
let p = |x, y| Point(Coordinate { x: x, y: y });
Expand All @@ -74,7 +73,6 @@ mod test {
assert!(within_epsilon(poly.area(), 98., Float::epsilon()));
}

/*
#[test]
fn area_multipolygon_test() {
let p = |x, y| Point(Coordinate { x: x, y: y });
Expand All @@ -91,5 +89,4 @@ mod test {
assert_eq!(mpoly.area(), 102.);
assert!(within_epsilon(mpoly.area(), 102., Float::epsilon()));
}
*/
}
4 changes: 4 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ pub trait MultiPolygonTrait<'a, T>
type Iter: Iterator<Item=&'a Self::ItemType>;

fn polygons(&'a self) -> Self::Iter;

fn area(&'a self) -> T {
::algorithm::area::multi_polygon(self)
}
}

0 comments on commit 43105b8

Please sign in to comment.