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

[wip] First attempts at implementing Haversine distance #48

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ pub trait Centroid<T: Float> {
}

impl<T> Centroid<T> for LineString<T>
where T: Float
where T: Float + FromPrimitive
{
///
/// Centroid on a LineString is the mean of the middle of the segment
67 changes: 50 additions & 17 deletions src/algorithm/distance.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use num::Float;
use num::{Float, FromPrimitive};
use types::{Point};

/// Returns the distance between two geometries.
trait Distance<T, Rhs = Self> {
fn distance<A: DistanceAlgorithm>(&self, rhs: &Rhs) -> T;
}

pub trait Distance<T, Rhs = Self>
{
/// Returns the distance between two points:
///
/// ```
/// use geo::{COORD_PRECISION, Point};
/// use geo::algorithm::distance::Distance;
///
/// let p = Point::new(-72.1235, 42.3521);
/// let dist = p.distance(&Point::new(-72.1260, 42.45));
/// assert!(dist < COORD_PRECISION)
/// ```
fn distance(&self, rhs: &Rhs) -> T;
trait EuclideanAlgorithm {
}

impl<T> Distance<T, Point<T>> for Point<T>
where T: Float
{
struct Euclidean;

impl DistanceAlgorithm for Euclidean {
fn distance(&self, p: &Point<T>) -> T {
let (dx, dy) = (self.x() - p.x(), self.y() - p.y());
dx.hypot(dy)
@@ -42,3 +33,45 @@ mod test {
assert!(dist < 147. && dist > 146.);
}
}


/// Returns the distance between two geometries.

// pub trait HaversineDistance<T, Rhs = Self>
// {
// /// Returns the distance between two points:
// ///
// /// ```
// /// use geo::{COORD_PRECISION, Point};
// /// use geo::algorithm::haversine_distance::HaversineDistance;
// ///
// /// let p = Point::new(-72.1235, 42.3521);
// /// let dist = p.haversine_distance(&Point::new(-72.1260, 42.45));
// /// assert_eq!(dist as i32, 10900)
// /// ```
// fn haversine_distance(&self, rhs: &Rhs) -> T;
// }
//
// impl<T> HaversineDistance<T, Point<T>> for Point<T>
// where T: Float + FromPrimitive
// {
// // currently gives answer in meters
// fn haversine_distance(&self, p: &Point<T>) -> T {
// let a = (self.y().to_radians().sin() * p.y().to_radians().sin()) +
// (self.y().to_radians().cos() * p.y().to_radians().cos()) *
// (p.x() - self.x()).to_radians().cos();
// T::from_i32(6378137).unwrap() * a.acos().min(T::one())
// }
// }
//
// #[cfg(test)]
// mod test {
// use types::Point;
// use algorithm::haversine_distance::HaversineDistance;
// #[test]
// fn distance3_test() {
// let dist = Point::new(-101.60, 37.43)
// .haversine_distance(&Point::new(-78.75, 40.97));
// assert_eq!(dist, 2004106.44144124);
// }
// }
1 change: 1 addition & 0 deletions src/algorithm/intersects.rs
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ impl<T> Intersects<LineString<T>> for LineString<T>
}
}


impl<T> Intersects<LineString<T>> for Polygon<T>
where T: Float
{
2 changes: 2 additions & 0 deletions src/algorithm/mod.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ pub mod area;
pub mod length;
/// Returns the distance between two geometries.
pub mod distance;
/// Returns the distance on a sphere between two geometries.
pub mod haversine_distance;
/// Returns the Bbox of a geometry.
pub mod boundingbox;
/// Simplifies a `LineString`.