Skip to content

Commit

Permalink
Merge #183
Browse files Browse the repository at this point in the history
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
bors[bot] committed Feb 4, 2018
2 parents 0143a68 + a1b0e3a commit 50bf719
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/algorithm/haversine_length.rs
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())
}
}
2 changes: 2 additions & 0 deletions src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ pub mod from_postgis;
/// Converts geometry into PostGIS types.
#[cfg(feature = "postgis-integration")]
pub mod to_postgis;
/// Returns the Haversine length of a line.
pub mod haversine_length;
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ pub mod prelude {
pub use algorithm::simplifyvw::SimplifyVW;
pub use algorithm::translate::Translate;
pub use algorithm::closest_point::ClosestPoint;
pub use algorithm::haversine_length::HaversineLength;
#[cfg(feature = "postgis-integration")]
pub use algorithm::from_postgis::FromPostgis;
#[cfg(feature = "postgis-integration")]
pub use algorithm::to_postgis::ToPostgis;
}

0 comments on commit 50bf719

Please sign in to comment.