-
Notifications
You must be signed in to change notification settings - Fork 204
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
Add GeodesicIntermediate algorithm #608
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use crate::{CoordFloat, Point}; | ||
use geographiclib_rs::{DirectGeodesic, Geodesic, InverseGeodesic}; | ||
|
||
/// Returns a new Point along a route between two existing points on an ellipsoidal model of the earth | ||
|
||
pub trait GeodesicIntermediate<T: CoordFloat> { | ||
/// Returns a new Point along a route between two existing points on an ellipsoidal model of the earth | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #[macro_use] extern crate approx; | ||
/// # | ||
/// use geo::algorithm::geodesic_intermediate::GeodesicIntermediate; | ||
/// use geo::Point; | ||
/// | ||
/// let p1 = Point::<f64>::new(10.0, 20.0); | ||
/// let p2 = Point::<f64>::new(125.0, 25.0); | ||
/// let i20 = p1.geodesic_intermediate(&p2, 0.2); | ||
/// let i50 = p1.geodesic_intermediate(&p2, 0.5); | ||
/// let i80 = p1.geodesic_intermediate(&p2, 0.8); | ||
/// let i20_should = Point::new(29.842907, 29.951445); | ||
/// let i50_should = Point::new(65.879360, 37.722253); | ||
/// let i80_should = Point::new(103.556796, 33.506196); | ||
/// assert_relative_eq!(i20, i20_should, epsilon = 1.0e-6); | ||
/// assert_relative_eq!(i50, i50_should, epsilon = 1.0e-6); | ||
/// assert_relative_eq!(i80, i80_should, epsilon = 1.0e-6); | ||
/// ``` | ||
|
||
fn geodesic_intermediate(&self, other: &Point<T>, f: T) -> Point<T>; | ||
fn geodesic_intermediate_fill( | ||
&self, | ||
other: &Point<T>, | ||
max_dist: T, | ||
include_ends: bool, | ||
) -> Vec<Point<T>>; | ||
} | ||
|
||
impl GeodesicIntermediate<f64> for Point<f64> { | ||
fn geodesic_intermediate(&self, other: &Point<f64>, f: f64) -> Point<f64> { | ||
let g = Geodesic::wgs84(); | ||
let (total_distance, azi1, _azi2, _a12) = | ||
g.inverse(self.lat(), self.lng(), other.lat(), other.lng()); | ||
let distance = total_distance * f; | ||
let (lat2, lon2) = g.direct(self.lat(), self.lng(), azi1, distance); | ||
|
||
Point::new(lon2, lat2) | ||
} | ||
|
||
fn geodesic_intermediate_fill( | ||
&self, | ||
other: &Point<f64>, | ||
max_dist: f64, | ||
include_ends: bool, | ||
) -> Vec<Point<f64>> { | ||
let g = Geodesic::wgs84(); | ||
let (total_distance, azi1, _azi2, _a12) = | ||
g.inverse(self.lat(), self.lng(), other.lat(), other.lng()); | ||
|
||
if total_distance <= max_dist { | ||
if include_ends { | ||
return vec![*self, *other]; | ||
} else { | ||
return vec![]; | ||
} | ||
} | ||
|
||
let number_of_points = (total_distance / max_dist).ceil(); | ||
let interval = 1.0 / number_of_points; | ||
|
||
let mut current_step = interval; | ||
let mut points = if include_ends { vec![*self] } else { vec![] }; | ||
|
||
while current_step < 1.0 { | ||
let (lat2, lon2) = | ||
g.direct(self.lat(), self.lng(), azi1, total_distance * current_step); | ||
let point = Point::new(lon2, lat2); | ||
points.push(point); | ||
current_step = current_step + interval; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have recently removed all warnings generated by running 'cargo clippy' on the project. Its a really minor point but this PR introduced a single clippy warning. The solution is the change this line : - into
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course! TIL about clippy |
||
} | ||
|
||
if include_ends { | ||
points.push(*other); | ||
} | ||
|
||
points | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use approx::assert_relative_eq; | ||
|
||
#[test] | ||
fn f_is_zero_or_one_test() { | ||
let p1 = Point::<f64>::new(10.0, 20.0); | ||
let p2 = Point::<f64>::new(15.0, 25.0); | ||
let i0 = p1.geodesic_intermediate(&p2, 0.0); | ||
let i100 = p1.geodesic_intermediate(&p2, 1.0); | ||
assert_relative_eq!(i0, p1, epsilon = 1.0e-6); | ||
assert_relative_eq!(i100, p2, epsilon = 1.0e-6); | ||
} | ||
|
||
#[test] | ||
fn various_f_values_test() { | ||
let p1 = Point::<f64>::new(10.0, 20.0); | ||
let p2 = Point::<f64>::new(125.0, 25.0); | ||
let i20 = p1.geodesic_intermediate(&p2, 0.2); | ||
let i50 = p1.geodesic_intermediate(&p2, 0.5); | ||
let i80 = p1.geodesic_intermediate(&p2, 0.8); | ||
let i20_should = Point::new(29.842907, 29.951445); | ||
let i50_should = Point::new(65.879360, 37.722253); | ||
let i80_should = Point::new(103.556796, 33.506196); | ||
assert_relative_eq!(i20, i20_should, epsilon = 1.0e-6); | ||
assert_relative_eq!(i50, i50_should, epsilon = 1.0e-6); | ||
assert_relative_eq!(i80, i80_should, epsilon = 1.0e-6); | ||
} | ||
|
||
#[test] | ||
fn should_add_i50_test() { | ||
let p1 = Point::<f64>::new(30.0, 40.0); | ||
let p2 = Point::<f64>::new(40.0, 50.0); | ||
let max_dist = 1000000.0; // meters | ||
let include_ends = true; | ||
let i50 = p1.geodesic_intermediate(&p2, 0.5); | ||
let route = p1.geodesic_intermediate_fill(&p2, max_dist, include_ends); | ||
assert_eq!(route, vec![p1, i50, p2]); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not justify these line
when I ran cargo doc and looked at the generated HTML
geo/target/doc/geo/algorithm/geodesic_intermediate/trait.GeodesicIntermediate.html
It did not appear in the output... perhaps we can just simplify by removing them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within a doc comment code block, lines starting with a
#
denote that the line should not be output into the docs, but they are executed (and necessary) when running the doc-test.It's commonly used to keep the rendered docs concise and focused on the usage, rather than implementation details like what testing library we use.