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

implement EuclideanDistance for MultiPolygon to Line and Line to MultiPolygon #227

Merged
merged 3 commits into from
May 16, 2018
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
Next Next commit
impl EuclideanDistance for MultiPolygon-Line and Line-MultiPolygon
  • Loading branch information
jidkano committed May 15, 2018
commit 74c927e28a3a7f562bf1bb360972957013824b73
68 changes: 68 additions & 0 deletions geo/src/algorithm/euclidean_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,30 @@ where
}
}

/// Line to MultiPolygon distance
impl<T> EuclideanDistance<T, MultiPolygon<T>> for Line<T>
where
T: Float + FloatConst + Signed + SpadeFloat,
{
fn euclidean_distance(&self, mpolygon: &MultiPolygon<T>) -> T {
mpolygon
.0
.iter()
.map(|p| self.euclidean_distance(p))
.fold(T::max_value(), |accum, val| accum.min(val))
}
}

/// MultiPolygon to Line distance
impl<T> EuclideanDistance<T, Line<T>> for MultiPolygon<T>
where
T: Float + FloatConst + Signed + SpadeFloat,
{
fn euclidean_distance(&self, other: &Line<T>) -> T {
other.euclidean_distance(self)
}
}

// Line to Polygon distance
impl<T> EuclideanDistance<T, Polygon<T>> for Line<T>
where
Expand Down Expand Up @@ -552,6 +576,50 @@ mod test {
// 0.41036467732879783 <-- Shapely
assert_relative_eq!(dist, 0.41036467732879767);
}

#[test]
fn line_distance_multipolygon_do_not_touch_test() {
// checks that the distance from the multipolygon
// is equal to the distance from the closest polygon
// taken in isolation, whatever that distance is
let ls1 = LineString(vec![
Point::new(0.0, 0.0),
Point::new(10.0, 0.0),
Point::new(10.0, 10.0),
Point::new(5.0, 15.0),
Point::new(0.0, 10.0),
Point::new(0.0, 0.0),
]);
let ls2 = LineString(vec![
Point::new(0.0, 30.0),
Point::new(0.0, 25.0),
Point::new(10.0, 25.0),
Point::new(10.0, 30.0),
Point::new(0.0, 30.0),
]);
let ls3 = LineString(vec![
Point::new(15.0, 30.0),
Point::new(15.0, 25.0),
Point::new(20.0, 25.0),
Point::new(20.0, 30.0),
Point::new(15.0, 30.0),
]);
let pol1 = Polygon::new(ls1, vec![]);
let pol2 = Polygon::new(ls2, vec![]);
let pol3 = Polygon::new(ls3, vec![]);
let mp = MultiPolygon(vec![
pol1.clone(),
pol2.clone(),
pol3.clone(),
]);
let pnt1 = Point::new(0.0, 15.0);
let pnt2 = Point::new(10.0, 20.0);
let ln = Line::new(pnt1, pnt2);
let dist_mp_ln = ln.euclidean_distance(&mp);
let dist_pol1_ln = ln.euclidean_distance(&pol1);
assert_relative_eq!(dist_mp_ln, dist_pol1_ln);
}

#[test]
fn point_distance_multipolygon_test() {
let ls1 = LineString(vec![
Expand Down