forked from georust/geo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rory McCann
committed
Aug 6, 2017
1 parent
8f02157
commit b7a01f5
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use num_traits::{Float}; | ||
use types::{Point, Polygon, LineString, Line, MultiPoint, MultiPolygon, MultiLineString}; | ||
|
||
pub trait CoordChange<T> { | ||
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Self where T: Float; | ||
|
||
} | ||
|
||
impl<T: Float> CoordChange<T> for Point<T> { | ||
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Point<T> | ||
{ | ||
let new_point = func(&(self.0.x, self.0.y)); | ||
Point::new(new_point.0, new_point.1) | ||
} | ||
} | ||
|
||
impl<T: Float> CoordChange<T> for LineString<T> { | ||
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Self | ||
{ | ||
LineString(self.0.iter().map(|p| p.coord_change(func)).collect()) | ||
} | ||
} | ||
|
||
impl<T: Float> CoordChange<T> for Polygon<T> { | ||
fn coord_change(&self, func: &Fn(&(T, T)) -> (T, T)) -> Self | ||
{ | ||
Polygon::new(self.exterior.coord_change(func), self.interiors.iter().map(|l| l.coord_change(func)).collect()) | ||
} | ||
} | ||
|
||
|
||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn point() { | ||
let p = Point::new(10., 10.); | ||
let new_p = p.coord_change(&|&(x, y)| (x+10., y+100.)); | ||
assert_eq!(new_p.x(), 20.); | ||
assert_eq!(new_p.y(), 110.); | ||
} | ||
|
||
#[test] | ||
fn linestring() { | ||
let line1: LineString<f32> = LineString(vec![Point::new(0., 0.), Point::new(1., 2.).into()]); | ||
let line2 = line1.coord_change(&|&(x, y)| (x+10., y-100.)); | ||
assert_eq!(line2.0[0], Point::new(10., -100.)); | ||
assert_eq!(line2.0[1], Point::new(11., -98.)); | ||
|
||
} | ||
|
||
#[test] | ||
fn polygon() { | ||
let exterior = LineString(vec![Point::new(0., 0.), Point::new(1., 1.), | ||
Point::new(1., 0.), Point::new(0., 0.)]); | ||
let interiors = vec![LineString(vec![Point::new(0.1, 0.1), Point::new(0.9, 0.9), | ||
Point::new(0.9, 0.1), Point::new(0.1, 0.1)])]; | ||
let p = Polygon::new(exterior, interiors); | ||
|
||
let p2 = p.coord_change(&|&(x, y)| (x+10., y-100.)); | ||
|
||
let exterior2 = LineString(vec![Point::new(10., -100.), Point::new(11., -99.), | ||
Point::new(11., -100.), Point::new(10., -100.)]); | ||
let interiors2 = vec![LineString(vec![Point::new(10.1, -99.9), Point::new(10.9, -99.1), | ||
Point::new(10.9, -99.9), Point::new(10.1, -99.)])]; | ||
let expected_p2 = Polygon::new(exterior2, interiors2); | ||
|
||
assert_eq!(p2, expected_p2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters