Skip to content

Commit

Permalink
Merge branch 'main' into frewsxcv-F7EBFDB9-B0BD-4B79-9718-C3089E65BBD1
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv authored Nov 1, 2024
2 parents 5cbc9a0 + 5f3f912 commit 0dfb40c
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
uses: actions/checkout@v3
- run: cargo check --all-targets --no-default-features
# we don't want to test `proj-network` because it only enables the `proj` feature
- run: cargo test --features "use-proj use-serde"
- run: cargo test --features "use-proj use-serde earcutr multithreading"

geo_traits:
name: geo-traits
Expand Down
2 changes: 1 addition & 1 deletion geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- Allow configuring of the `i_overlay` Rayon transitive dependency with a new Cargo `multithreading` flag.
- <https://github.com/georust/geo/pull/1250>
- Handle some edge cases with `InterpolatePoint`
- Improve handling of InterploatePoint with collapsed Line
- <https://github.com/georust/geo/pull/1248>

## 0.29.0 - 2024.10.30
Expand Down
5 changes: 3 additions & 2 deletions geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ rust-version = "1.75"
categories = ["science::geo"]

[features]
default = ["earcutr", "spade"]
default = ["earcutr", "spade", "multithreading"]
use-proj = ["proj"]
proj-network = ["use-proj", "proj/network"]
use-serde = ["serde", "geo-types/serde"]
multithreading = ["i_overlay/allow_multithreading"]

[dependencies]
earcutr = { version = "0.4.2", optional = true }
Expand All @@ -30,7 +31,7 @@ proj = { version = "0.27.0", optional = true }
robust = "1.1.0"
rstar = "0.12.0"
serde = { version = "1.0", optional = true, features = ["derive"] }
i_overlay = "1.7.2"
i_overlay = { version = "1.7.2", default-features = false }

[dev-dependencies]
approx = ">= 0.4.0, < 0.6.0"
Expand Down
156 changes: 156 additions & 0 deletions geo/src/algorithm/line_measures/interpolate_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,159 @@ pub trait InterpolatePoint<F: CoordFloat> {
include_ends: bool,
) -> impl Iterator<Item = Point<F>>;
}

#[cfg(test)]
mod tests {
use crate::{Euclidean, Geodesic, Haversine, InterpolatePoint, Point, Rhumb};

#[test]
fn point_at_ratio_between_line_ends() {
let start = Point::new(0.0, 0.0);
let end = Point::new(1.0, 1.0);

let ratio = 0.0;
assert_eq!(Haversine::point_at_ratio_between(start, end, ratio), start);
assert_eq!(Euclidean::point_at_ratio_between(start, end, ratio), start);
assert_eq!(Geodesic::point_at_ratio_between(start, end, ratio), start);
assert_eq!(Rhumb::point_at_ratio_between(start, end, ratio), start);

let ratio = 1.0;
assert_eq!(Haversine::point_at_ratio_between(start, end, ratio), end);
assert_eq!(Euclidean::point_at_ratio_between(start, end, ratio), end);
assert_eq!(Geodesic::point_at_ratio_between(start, end, ratio), end);
assert_eq!(Rhumb::point_at_ratio_between(start, end, ratio), end);
}

mod degenerate {
use super::*;

#[test]
fn point_at_ratio_between_collapsed_line() {
let start = Point::new(1.0, 1.0);

let ratio = 0.0;
assert_eq!(
Haversine::point_at_ratio_between(start, start, ratio),
start
);
assert_eq!(
Euclidean::point_at_ratio_between(start, start, ratio),
start
);
assert_eq!(Geodesic::point_at_ratio_between(start, start, ratio), start);
assert_eq!(Rhumb::point_at_ratio_between(start, start, ratio), start);

let ratio = 0.5;
assert_eq!(
Haversine::point_at_ratio_between(start, start, ratio),
start
);
assert_eq!(
Euclidean::point_at_ratio_between(start, start, ratio),
start
);
assert_eq!(Geodesic::point_at_ratio_between(start, start, ratio), start);
assert_eq!(Rhumb::point_at_ratio_between(start, start, ratio), start);

let ratio = 1.0;
assert_eq!(
Haversine::point_at_ratio_between(start, start, ratio),
start
);
assert_eq!(
Euclidean::point_at_ratio_between(start, start, ratio),
start
);
assert_eq!(Geodesic::point_at_ratio_between(start, start, ratio), start);
assert_eq!(Rhumb::point_at_ratio_between(start, start, ratio), start);
}

#[test]
fn point_at_distance_between_collapsed_line() {
// This method just documents existing behavior. I don't think our current behavior
// is especially useful, but we might consider handling it uniformly one day.
let start: Point = Point::new(1.0, 1.0);

let distance = 0.0;
assert_eq!(
Haversine::point_at_distance_between(start, start, distance),
start
);

let euclidean_result = Euclidean::point_at_distance_between(start, start, distance);
assert!(euclidean_result.x().is_nan());
assert!(euclidean_result.y().is_nan());
assert_eq!(
Geodesic::point_at_distance_between(start, start, distance),
start
);
assert_eq!(
Rhumb::point_at_distance_between(start, start, distance),
start
);

let distance = 100000.0;
let due_north = Point::new(1.0, 1.9);
let due_south = Point::new(1.0, 0.1);
assert_relative_eq!(
Haversine::point_at_distance_between(start, start, distance),
due_north,
epsilon = 1.0e-1
);
let euclidean_result = Euclidean::point_at_distance_between(start, start, distance);
assert!(euclidean_result.x().is_nan());
assert!(euclidean_result.y().is_nan());
assert_relative_eq!(
Geodesic::point_at_distance_between(start, start, distance),
due_south,
epsilon = 1.0e-1
);
assert_relative_eq!(
Rhumb::point_at_distance_between(start, start, distance),
due_north,
epsilon = 1.0e-1
);
}

#[test]
fn points_along_collapsed_line() {
let start = Point::new(1.0, 1.0);

let max_distance = 1.0;

let include_ends = true;
let points: Vec<_> =
Haversine::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![start, start]);

let points: Vec<_> =
Euclidean::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![start, start]);

let points: Vec<_> =
Geodesic::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![start, start]);

let points: Vec<_> =
Rhumb::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![start, start]);

let include_ends = false;
let points: Vec<_> =
Haversine::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![]);

let points: Vec<_> =
Euclidean::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![]);

let points: Vec<_> =
Geodesic::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![]);

let points: Vec<_> =
Rhumb::points_along_line(start, start, max_distance, include_ends).collect();
assert_eq!(points, vec![]);
}
}
}
10 changes: 10 additions & 0 deletions geo/src/algorithm/line_measures/metric_spaces/geodesic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ impl InterpolatePoint<f64> for Geodesic {
end: Point<f64>,
meters_from_start: f64,
) -> Point<f64> {
if meters_from_start == 0.0 {
return start;
}
let bearing = Self::bearing(start, end);
Self::destination(start, bearing, meters_from_start)
}
Expand Down Expand Up @@ -205,6 +208,13 @@ impl InterpolatePoint<f64> for Geodesic {
end: Point<f64>,
ratio_from_start: f64,
) -> Point<f64> {
if start == end || ratio_from_start == 0.0 {
return start;
}
if ratio_from_start == 1.0 {
return end;
}

let g = geographiclib_rs::Geodesic::wgs84();
let (total_distance, azi1, _azi2, _a12) = g.inverse(start.y(), start.x(), end.y(), end.x());
let distance = total_distance * ratio_from_start;
Expand Down
6 changes: 6 additions & 0 deletions geo/src/algorithm/line_measures/metric_spaces/haversine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ impl<F: CoordFloat + FromPrimitive> InterpolatePoint<F> for Haversine {
///
/// [great circle]: https://en.wikipedia.org/wiki/Great_circle
fn point_at_ratio_between(start: Point<F>, end: Point<F>, ratio_from_start: F) -> Point<F> {
if start == end || ratio_from_start == F::zero() {
return start;
}
if ratio_from_start == F::one() {
return end;
}
let calculation = HaversineIntermediateFillCalculation::new(start, end);
calculation.point_at_ratio(ratio_from_start)
}
Expand Down
4 changes: 3 additions & 1 deletion geo/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use kernels::{Kernel, Orientation};
pub mod area;
pub use area::Area;

/// Boolean Ops such as union, xor, difference;
/// Boolean Ops such as union, xor, difference.
pub mod bool_ops;
pub use bool_ops::{BooleanOps, OpType};

Expand Down Expand Up @@ -254,6 +254,8 @@ pub mod translate;
pub use translate::Translate;

/// Triangulate polygons using an [ear-cutting algorithm](https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf).
///
/// Requires the `"earcutr"` feature.
#[cfg(feature = "earcutr")]
pub mod triangulate_earcut;
#[cfg(feature = "earcutr")]
Expand Down
2 changes: 2 additions & 0 deletions geo/src/algorithm/triangulate_earcut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{coord, CoordFloat, CoordsIter, Polygon, Triangle};

/// Triangulate polygons using an [ear-cutting algorithm](https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf).
///
/// Requires the `"earcutr"` feature, which is enabled by default.
pub trait TriangulateEarcut<T: CoordFloat> {
/// # Examples
///
Expand Down
23 changes: 18 additions & 5 deletions geo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
//!
//! ## Boolean Operations
//!
//! - **[`BooleanOps`]**: combine or split (Multi)Polygons using intersecton, union, xor, or difference operations
//! - **[`BooleanOps`]**: combine or split (Multi)Polygons using intersecton, union, xor, or difference operations.
//!
//! ## Outlier Detection
//!
Expand Down Expand Up @@ -112,7 +112,7 @@
//!
//! ## Triangulation
//!
//! - **[`TriangulateEarcut`](triangulate_earcut)**: Triangulate polygons using the earcut algorithm (requires the `earcutr` feature).
//! - **[`TriangulateEarcut`](triangulate_earcut)**: Triangulate polygons using the earcut algorithm. Requires the `"earcutr"` feature, which is enabled by default.
//!
//! ## Winding
//!
Expand Down Expand Up @@ -177,9 +177,22 @@
//!
//! The following optional [Cargo features] are available:
//!
//! - `proj-network`: Enables [network grid] support for the [`proj` crate]. After enabling this feature, [further configuration][proj crate file download] is required to use the network grid
//! - `use-proj`: Enables coordinate conversion and transformation of `Point` geometries using the [`proj` crate]
//! - `use-serde`: Allows geometry types to be serialized and deserialized with [Serde]
//! - `earcutr`:
//! - Enables the `earcutr` crate, which provides triangulation of polygons using the earcut algorithm.
//! - ☑ Enabled by default.
//! - `proj-network`:
//! - Enables [network grid] support for the [`proj` crate].
//! - After enabling this feature, [further configuration][proj crate file download] is required to use the network grid.
//! - ☐ Disabled by default.
//! - `use-proj`:
//! - Enables coordinate conversion and transformation of `Point` geometries using the [`proj` crate]
//! - ☐ Disabled by default.
//! - `use-serde`:
//! - Allows geometry types to be serialized and deserialized with [Serde].
//! - ☐ Disabled by default.
//! - `multithreading`:
//! - Enables multithreading support for the `i_overlay` crate.
//! - ☑ Enabled by default.
//!
//! # Ecosystem
//!
Expand Down

0 comments on commit 0dfb40c

Please sign in to comment.