-
Notifications
You must be signed in to change notification settings - Fork 200
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
Unify line measures #1216
Merged
Merged
Unify line measures #1216
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c54af80
Unify line measures
michaelkirk 0f184d1
bump MSRV to 1.75 to support "return-position impl Trait in trait"
michaelkirk 5ae5890
Doc fixups
michaelkirk 8446610
test tolerances
michaelkirk 6ecd4d3
fixup! Doc fixups
michaelkirk e28647e
fixup! Unify line measures
michaelkirk 01eb36c
fixup! Unify line measures
michaelkirk 61783e1
fixup! Doc fixups
michaelkirk 032b94d
remove TODO, add trait doc
michaelkirk 0b8c444
Alphabetize algorithm imports
michaelkirk 58d4e28
fixup: impl Interpolate for CoordFloat, not just f64
michaelkirk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use geo_types::{CoordFloat, Point}; | ||
|
||
/// Calculate the bearing between two points | ||
pub trait Bearing<F: CoordFloat> { | ||
/// Calculate the bearing from `origin` to `destination` in degrees. | ||
/// | ||
/// See [specific implementations](#implementors) for details. | ||
/// | ||
/// # Units | ||
/// - `origin`, `destination`: Point where the units of x/y depend on the [trait implementation](#implementors). | ||
/// - returns: degrees, where: North: 0°, East: 90°, South: 180°, West: 270° | ||
fn bearing(origin: Point<F>, destination: Point<F>) -> F; | ||
} |
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,19 @@ | ||
use geo_types::{CoordFloat, Point}; | ||
|
||
/// Calculate the destination point from an origin point, a bearing and a distance. | ||
pub trait Destination<F: CoordFloat> { | ||
/// Returns a new point having travelled the `distance` along a line | ||
/// from the `origin` point with the given `bearing`. | ||
/// | ||
/// See [specific implementations](#implementors) for details. | ||
/// | ||
/// # Units | ||
/// | ||
/// - `origin`: Point where the units of x/y depend on the [trait implementation](#implementors). | ||
/// - `bearing`: degrees, where: North: 0°, East: 90°, South: 180°, West: 270° | ||
/// - `distance`: depends on the [trait implementation](#implementors). | ||
/// - returns: Point where the units of x/y depend on the [trait implementation](#implementors). | ||
/// | ||
/// [`metric_spaces`]: super::metric_spaces | ||
fn destination(origin: Point<F>, bearing: F, distance: F) -> Point<F>; | ||
} |
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,12 @@ | ||
/// Calculate the distance between the `Origin` and `Destination` geometry. | ||
pub trait Distance<F, Origin, Destination> { | ||
/// Note that not all implementations support all geometry combinations, but at least `Point` to `Point` | ||
/// is supported. | ||
/// See [specific implementations](#implementors) for details. | ||
/// | ||
/// # Units | ||
/// | ||
/// - `origin`, `destination`: geometry where the units of x/y depend on the trait implementation. | ||
/// - returns: depends on the trait implementation. | ||
fn distance(origin: Origin, destination: Destination) -> F; | ||
} |
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,28 @@ | ||
use crate::{CoordFloat, Point}; | ||
|
||
/// Interpolate a `Point` along a line between two existing points | ||
pub trait InterpolatePoint<F: CoordFloat> { | ||
/// Returns a new Point along a line between two existing points | ||
/// | ||
/// See [specific implementations](#implementors) for details. | ||
fn point_at_ratio_between(start: Point<F>, end: Point<F>, ratio_from_start: F) -> Point<F>; | ||
|
||
// TODO: | ||
// fn point_at_distance_between(start: Point<F>, end: Point<F>, distance_from_start: F) -> Point<F>; | ||
|
||
/// Interpolates `Point`s along a line between `start` and `end`. | ||
/// | ||
/// See [specific implementations](#implementors) for details. | ||
/// | ||
/// As many points as necessary will be added such that the distance between points | ||
/// never exceeds `max_distance`. If the distance between start and end is less than | ||
/// `max_distance`, no additional points will be included in the output. | ||
/// | ||
/// `include_ends`: Should the start and end points be included in the output? | ||
fn points_along_line( | ||
start: Point<F>, | ||
end: Point<F>, | ||
max_distance: F, | ||
include_ends: bool, | ||
) -> impl Iterator<Item = Point<F>>; | ||
} |
78 changes: 78 additions & 0 deletions
78
geo/src/algorithm/line_measures/metric_spaces/euclidean.rs
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,78 @@ | ||
use super::super::Distance; | ||
use crate::{GeoFloat, Point}; | ||
|
||
/// Operations on the [Euclidean plane] measure distance with the pythagorean formula - | ||
/// what you'd measure with a ruler. | ||
/// | ||
/// If you have lon/lat points, use the [`Haversine`], [`Geodesic`], or other [metric spaces] - | ||
/// Euclidean methods will give nonsense results. | ||
/// | ||
/// If you wish to use Euclidean operations with lon/lat, the coordinates must first be transformed | ||
/// using the [`Transform::transform`](crate::Transform::transform) / [`Transform::transform_crs_to_crs`](crate::Transform::transform_crs_to_crs) methods or their | ||
/// immutable variants. Use of these requires the proj feature | ||
/// | ||
/// [Euclidean plane]: https://en.wikipedia.org/wiki/Euclidean_plane | ||
/// [`Transform`]: crate::Transform | ||
/// [`Haversine`]: super::Haversine | ||
/// [`Geodesic`]: super::Geodesic | ||
/// [metric spaces]: super | ||
pub struct Euclidean; | ||
|
||
/// Calculate the Euclidean distance (a.k.a. pythagorean distance) between two Points | ||
impl<F: GeoFloat> Distance<F, Point<F>, Point<F>> for Euclidean { | ||
/// Calculate the Euclidean distance (a.k.a. pythagorean distance) between two Points | ||
/// | ||
/// # Units | ||
/// - `origin`, `destination`: Point where the units of x/y represent non-angular units | ||
/// — e.g. meters or miles, not lon/lat. For lon/lat points, use the | ||
/// [`Haversine`] or [`Geodesic`] [metric spaces]. | ||
/// - returns: distance in the same units as the `origin` and `destination` points | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use geo::{Euclidean, Distance}; | ||
/// use geo::Point; | ||
/// // web mercator | ||
/// let new_york_city = Point::new(-8238310.24, 4942194.78); | ||
/// // web mercator | ||
/// let london = Point::new(-14226.63, 6678077.70); | ||
/// let distance: f64 = Euclidean::distance(new_york_city, london); | ||
/// | ||
/// assert_eq!( | ||
/// 8_405_286., // meters in web mercator | ||
/// distance.round() | ||
/// ); | ||
/// ``` | ||
/// | ||
/// [`Haversine`]: super::Haversine | ||
/// [`Geodesic`]: super::Geodesic | ||
/// [metric spaces]: super | ||
fn distance(origin: Point<F>, destination: Point<F>) -> F { | ||
crate::EuclideanDistance::euclidean_distance(&origin, &destination) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
type MetricSpace = Euclidean; | ||
|
||
mod distance { | ||
use super::*; | ||
|
||
#[test] | ||
fn new_york_to_london() { | ||
// web mercator | ||
let new_york_city = Point::new(-8238310.24, 4942194.78); | ||
// web mercator | ||
let london = Point::new(-14226.63, 6678077.70); | ||
let distance: f64 = MetricSpace::distance(new_york_city, london); | ||
|
||
assert_relative_eq!( | ||
8_405_286., // meters in web mercator | ||
distance.round() | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In a similar vein, I don't think we should be referring to "meters or miles" here: non-Euclidean distances are often measured in meters / miles, so this is confusing. "Not lon/lat" is OK, but I'd prefer that this is either implicit (you can't measure lon / lat distances in Euclidean space, and we are in Euclidean space, therefore…) or that we decline to explain with physical-world examples completely.
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 believe you, but I honestly didn't know that was true. Can you give me an example?
Oh but you can! And it's a common error that gives you a meaningless result. Look no further than our own example code on the legacy euclidean distance:
I think it's important to keep simple relatable language and examples to help mitigate that.
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.
Any physical measurement of distance on the earth's surface (or a simplified abstraction of it) is definitionally non-Euclidean because it's a curved surface.
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.
Ah I see, that makes sense. Thank you for bearing with me. I don't have a formal GIS background, so a lot of things that may seem obvious, I truly don't know.
I've included the link to [Euclidean Plane], which I hope addresses your ambiguity concern.
I've also linked to the
Transform
trait in reference to what we mean by "projection". While also not 100% precise, I think that it will be helpful to those who know a little, without confusing those who know a lot.I still think it's important that our documentation aim to help people avoid the most likely errors by saying something like:
But the negation:
Feels awkward and unnecessarily mysterious without an example. I now understand how Euclidean space is not the only space that could use meters as a unit, but as an example of what we mean by "not lng/lat", I think it is clarifying, and unlikely to confuse.
So if I've said something that is incorrect, let me know and I'll try again to fix it. But if it's more so that you think it reads like it was written for dummies, well then I'd please beg of you to have mercy on us dummies.