Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type Bbox and trait BoundingBox #41

Merged
merged 21 commits into from
Jul 21, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement BoundingBox for MultiPolygon
  • Loading branch information
Zambelli Pietro committed Jul 15, 2016
commit de27c9e836afae0b048849161a2fcdc374c01f20
41 changes: 37 additions & 4 deletions src/algorithm/boundingbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use num::{Float};

use types::{Bbox, Point, MultiPoint, LineString, MultiLineString, Polygon};
use types::{Bbox, Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon};

/// Calculation of the bounding box of a geometry.

Expand Down Expand Up @@ -99,8 +99,6 @@ impl<T> BoundingBox<T> for MultiLineString<T>
}
}
Some(bbox)
// vect[1..].iter()
// .fold(bbox, |bbfinal, (geo0, geo1)| bbfinal + geo0.bbox().unwrap() + geo1.bbox().unwrap());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated code with...

}
}
Expand All @@ -117,12 +115,37 @@ impl<T> BoundingBox<T> for Polygon<T>
}
}

impl<T> BoundingBox<T> for MultiPolygon<T>
where T: Float
{
///
/// Return the BoundingBox for a MultiLineString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad comment

///
fn bbox(&self) -> Option<Bbox<T>> {
let vect = &self.0;
if vect.is_empty() {
return None;
}
if vect.len() == 1 {
return vect[0].bbox()
} else {
let mut bbox = vect[0].bbox().unwrap();
for geo in vect[1..].iter() {
let gopt = geo.bbox();
if gopt.is_some() {
bbox += gopt.unwrap();
}
}
Some(bbox)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... that.

}
}



#[cfg(test)]
mod test {
use types::{Bbox, Coordinate, Point, MultiPoint, LineString, MultiLineString, Polygon};
use types::{Bbox, Coordinate, Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon};
use algorithm::boundingbox::BoundingBox;

#[test]
Expand Down Expand Up @@ -173,4 +196,14 @@ mod test {
let poly = Polygon(linestring, Vec::new());
assert_eq!(line_bbox, poly.bbox().unwrap());
}
#[test]
fn multipolygon_test(){
let p = |x, y| Point(Coordinate { x: x, y: y });
let mpoly = MultiPolygon(vec![Polygon(LineString(vec![p(0., 0.), p(50., 0.), p(0., -70.), p(0., 0.)]), Vec::new()),
Polygon(LineString(vec![p(0., 0.), p(5., 0.), p(0., 80.), p(0., 0.)]), Vec::new()),
Polygon(LineString(vec![p(0., 0.), p(-60., 0.), p(0., 6.), p(0., 0.)]), Vec::new()),
]);
let bbox = Bbox{xmin: -60., ymax: 80., xmax: 50., ymin: -70.};
assert_eq!(bbox, mpoly.bbox().unwrap());
}
}