-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
183: Add Haversine length algorithm r=frewsxcv a=eeeeeta This PR adds a new algorithm, `haversine_length`, that computes the "Haversine length" of a line or linestring - i.e. the sum of the Haversine distances between every two points in the line. It also reexports this algorithm, and the PostGIS algorithms introduced in #180, in the prelude (because I realised I forgot to reexport them in #180...)
- Loading branch information
Showing
3 changed files
with
57 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,50 @@ | ||
|
||
use num_traits::{Float, FromPrimitive}; | ||
|
||
use types::{Line, LineString, MultiLineString}; | ||
use algorithm::haversine_distance::HaversineDistance; | ||
|
||
/// Calculation of the length | ||
pub trait HaversineLength<T, RHS = Self> { | ||
/// Calculation of the length of a Line | ||
/// | ||
/// ``` | ||
/// use geo::{Point, LineString, Coordinate}; | ||
/// use geo::algorithm::haversine_length::HaversineLength; | ||
/// | ||
/// let mut vec = Vec::new(); | ||
/// vec.push(Point::new(40.02f64, 116.34)); | ||
/// vec.push(Point::new(42.02f64, 116.34)); | ||
/// let linestring = LineString(vec); | ||
/// | ||
/// println!("HaversineLength {}", linestring.haversine_length()); | ||
/// ``` | ||
/// | ||
fn haversine_length(&self) -> T; | ||
} | ||
|
||
impl<T> HaversineLength<T> for Line<T> | ||
where T: Float + FromPrimitive | ||
{ | ||
fn haversine_length(&self) -> T { | ||
self.start.haversine_distance(&self.end) | ||
} | ||
} | ||
|
||
impl<T> HaversineLength<T> for LineString<T> | ||
where T: Float + FromPrimitive | ||
{ | ||
fn haversine_length(&self) -> T { | ||
self.0.windows(2) | ||
.fold(T::zero(), |total_length, p| total_length + p[0].haversine_distance(&p[1])) | ||
} | ||
} | ||
|
||
impl<T> HaversineLength<T> for MultiLineString<T> | ||
where T: Float + FromPrimitive | ||
{ | ||
fn haversine_length(&self) -> T { | ||
self.0.iter().fold(T::zero(), |total, line| total + line.haversine_length()) | ||
} | ||
} |
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
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