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

deprecate EuclideanDistance #1232

Merged
merged 4 commits into from
Oct 25, 2024
Merged

Conversation

michaelkirk
Copy link
Member

@michaelkirk michaelkirk commented Oct 22, 2024

  • I agree to follow the project's code of conduct.
  • I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

Part of #1181

In #1222, I moved only the Point x Point distance calculations. EuclideanDistance is implemented for all other geometry types too. This PR moves the rest of those calculations to the new Euclidean line measure struct for consistency.

It's a lot of code movement, but no change in behavior (unless I messed up 😬).

As part of aligning all our line measure traits (see
#1181), all the Point x Point
distance calculations had been moved into `line_measures::metric_spaces`.

Euclidean space is the only space that implements distance calculations
on *non* point geometries. This commit moves all those calculations onto
the `line_measures::metric_spaces::Euclidean` so that we can fully
deprecate the existing EuclideanDistance trait.
@michaelkirk michaelkirk changed the title mkirk/deprecate euclidean distance deprecate EuclideanDistance Oct 22, 2024
@michaelkirk michaelkirk force-pushed the mkirk/deprecate-euclidean-distance branch from 3cb6732 to f09257e Compare October 22, 2024 23:52
@michaelkirk michaelkirk force-pushed the mkirk/deprecate-euclidean-distance branch from f09257e to 0e9c0a4 Compare October 22, 2024 23:53
impl<T> EuclideanDistance<T, Line<T>> for Coord<T>
where
T: GeoFloat,
{
/// Minimum distance from a `Coord` to a `Line`
fn euclidean_distance(&self, line: &Line<T>) -> T {
line.euclidean_distance(self)
Euclidean::distance(&Point(*self), line)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the methods in this file now delegate to the new Euclidean as Distance implementation.

if line_string.intersects(polygon) {
F::zero()
} else if !polygon.interiors().is_empty()
// FIXME: Explodes on empty line_string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: FIXME - this panic on the next line isn't new behavior in this PR, but I noticed it while moving the code over.

Note that currently nearest_neighbor_distance returns Float::max_value if the input geometries are empty.

So, since we're already returning Float::max_value for some degenerate input, I'd argue it'd be preferable to always return Float::max_value for any degenerate input in this algorithm, rather than sometimes panic'ing and sometimes returning Float::max_value.

A larger breaking change could be to make distance return an Option.

❤️ empty geometries ❤️

return F::zero();
}

// REVIEW: This impl changed slightly.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up that this impl changed slightly, though the behavior should be the same. I couldn't resist the simplification. 😇

All the other implementations were simply cut+paste+renamed.

The original impl is here: https://github.com/georust/geo/pull/1232/files#diff-23bef671023e471a3228d4ac73548395b9073026c10fb07f418ac3e81df8cdddL242

fn euclidean_distance(&self, other: &Polygon<T>) -> T {
  if self.intersects(other) {
    return T::zero();
  }
  // line-line distance between each exterior polygon segment and the line
  let exterior_min = other
    .exterior()
    .lines()
    .fold(<T as Bounded>::max_value(), |acc, point| {
      acc.min(self.euclidean_distance(&point))
    });
  // line-line distance between each interior ring segment and the line
  // if there are no rings this just evaluates to max_float
  let interior_min = other
    .interiors()
    .iter()
    .map(|ring| {
      ring.lines().fold(<T as Bounded>::max_value(), |acc, line| {
        acc.min(self.euclidean_distance(&line))
      })
    })
    .fold(<T as Bounded>::max_value(), |acc, ring_min| {
      acc.min(ring_min)
    });
  // return smaller of the two values
  exterior_min.min(interior_min)
}

}

#[cfg(test)]
mod test {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the tests copied from the legacy euclidean_distance trait, updated to use the new trait. I didn't review them very carefully, except to make sure that they still passed.

@@ -93,9 +93,6 @@ pub fn partial_min<T: PartialOrd>(a: T, b: T) -> T {
}
}

// Moved to their own module, but we re-export to avoid breaking the API.
pub use crate::coordinate_position::{coord_pos_relative_to_ring, CoordPos};
Copy link
Member Author

@michaelkirk michaelkirk Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The util module is not publicly accessible, so there's no need to worry about breaking any API's within it.

(I came across this because the only place using this call site was the internal implementation of the deprecated EuclideanDistance trait - which no longer uses it. The modern trait imports CoordPos from its "new" home.)

@frewsxcv frewsxcv added this pull request to the merge queue Oct 25, 2024
Merged via the queue into main with commit a513ac3 Oct 25, 2024
15 checks passed
@frewsxcv frewsxcv deleted the mkirk/deprecate-euclidean-distance branch October 25, 2024 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants