Skip to content

Commit ef1b17c

Browse files
authored
Merge pull request #1298 from georust/mkirk/customizable-metric-space
Customizable metric space for line measures
2 parents 6881359 + ce3f8d9 commit ef1b17c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+862
-564
lines changed

geo/CHANGES.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,34 @@
22

33
## Unreleased
44

5+
- BREAKING: `Densify` and `Length` are now defined on the metric space, rather than a generic method on the geometry.
6+
- <https://github.com/georust/geo/pull/1298>
7+
```rust
8+
// before
9+
line_string.length::<Euclidean>()
10+
line_string.densify::<Euclidean>()
11+
line_string.densify::<Haversine>()
12+
13+
// after
14+
Euclidean.length(&line_string)
15+
Euclidean.densify(&line_string)
16+
Haversine.densify(&line_string)
17+
```
18+
- Add configurable `HaversineMeasure` for doing calculations on a custom sphere. Use `Haversine` for the default earth radius.
19+
- <https://github.com/georust/geo/pull/1298>
20+
```rust
21+
// before
22+
Haversine::distance(point1, point2)
23+
24+
// after
25+
Haversine.distance(point1, point2)
26+
27+
// For custom earth (or non-earth!) radius
28+
HaversineMeasure::new(3_389_500.0).distance(point1, point2)
29+
```
530
- Rename `triangulate_spade` and `TriangulateSpade` to `triangulate_delaunay` and `TriangulateDelaunay`
631
- Docs: Fix page location of citation for mean earth radius used in Haversine calculations
32+
- <https://github.com/georust/geo/pull/1297>
733
- Docs: Add top-level doc link for `InteriorPoint`
834
- Add Unary Union algorithm for fast union ops on adjacent / overlapping geometries
935
- <https://github.com/georust/geo/pull/1246>

geo/benches/euclidean_distance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn criterion_benchmark(c: &mut criterion::Criterion) {
3737
(x: -6.064453, y: 68.49604),
3838
];
3939
bencher.iter(|| {
40-
criterion::black_box(Euclidean::distance(&poly1, &poly2));
40+
criterion::black_box(Euclidean.distance(&poly1, &poly2));
4141
});
4242
});
4343

@@ -79,7 +79,7 @@ fn criterion_benchmark(c: &mut criterion::Criterion) {
7979
]
8080
.convex_hull();
8181
bencher.iter(|| {
82-
criterion::black_box(Euclidean::distance(&poly1, &poly2));
82+
criterion::black_box(Euclidean.distance(&poly1, &poly2));
8383
});
8484
},
8585
);

geo/benches/geodesic_distance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn criterion_benchmark(c: &mut criterion::Criterion) {
77
let b = geo::Point::new(16.372477, 48.208810);
88

99
bencher.iter(|| {
10-
criterion::black_box(criterion::black_box(Geodesic::distance(a, b)));
10+
criterion::black_box(criterion::black_box(Geodesic.distance(a, b)));
1111
});
1212
});
1313
}

geo/parcels.wkt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

geo/src/algorithm/centroid.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,9 @@ impl<T: GeoFloat> CentroidOperation<T> {
465465
fn add_line(&mut self, line: &Line<T>) {
466466
match line.dimensions() {
467467
ZeroDimensional => self.add_coord(line.start),
468-
OneDimensional => self.add_centroid(
469-
OneDimensional,
470-
line.centroid().0,
471-
line.length::<Euclidean>(),
472-
),
468+
OneDimensional => {
469+
self.add_centroid(OneDimensional, line.centroid().0, Euclidean.length(line))
470+
}
473471
_ => unreachable!("Line must be zero or one dimensional"),
474472
}
475473
}

geo/src/algorithm/closest_point.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<F: GeoFloat> ClosestPoint<F> for Point<F> {
5252
#[allow(clippy::many_single_char_names)]
5353
impl<F: GeoFloat> ClosestPoint<F> for Line<F> {
5454
fn closest_point(&self, p: &Point<F>) -> Closest<F> {
55-
let line_length = self.length::<Euclidean>();
55+
let line_length = Euclidean.length(self);
5656
if line_length == F::zero() {
5757
// if we've got a zero length line, technically the entire line
5858
// is the closest point...

geo/src/algorithm/concave_hull.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ where
116116
T: GeoFloat + RTreeNum,
117117
{
118118
let h = max_dist + max_dist;
119-
let w = line.length::<Euclidean>() + h;
119+
let w = Euclidean.length(&line) + h;
120120
let two = T::add(T::one(), T::one());
121121
let search_dist = T::div(T::sqrt(T::powi(w, 2) + T::powi(h, 2)), two);
122122
let centroid = line.centroid();
@@ -134,8 +134,8 @@ where
134134
let closest_point =
135135
candidates.fold(Point::new(point.x, point.y), |acc_point, candidate| {
136136
let candidate_point = Point::new(candidate.x, candidate.y);
137-
if Euclidean::distance(&line, &acc_point)
138-
> Euclidean::distance(&line, &candidate_point)
137+
if Euclidean.distance(&line, &acc_point)
138+
> Euclidean.distance(&line, &candidate_point)
139139
{
140140
candidate_point
141141
} else {
@@ -154,8 +154,8 @@ where
154154
let closest_edge_option = match peeked_edge {
155155
None => None,
156156
Some(&edge) => Some(edges_nearby_point.fold(*edge, |acc, candidate| {
157-
if Euclidean::distance(&closest_point, &acc)
158-
> Euclidean::distance(&closest_point, candidate)
157+
if Euclidean.distance(&closest_point, &acc)
158+
> Euclidean.distance(&closest_point, candidate)
159159
{
160160
*candidate
161161
} else {
@@ -164,8 +164,8 @@ where
164164
})),
165165
};
166166
let decision_distance = partial_min(
167-
Euclidean::distance(&closest_point, &line.start_point()),
168-
Euclidean::distance(&closest_point, &line.end_point()),
167+
Euclidean.distance(&closest_point, &line.start_point()),
168+
Euclidean.distance(&closest_point, &line.end_point()),
169169
);
170170
if let Some(closest_edge) = closest_edge_option {
171171
let far_enough = edge_length / decision_distance > concavity;
@@ -217,7 +217,7 @@ where
217217
line_tree.insert(line);
218218
}
219219
while let Some(line) = line_queue.pop_front() {
220-
let edge_length = line.length::<Euclidean>();
220+
let edge_length = Euclidean.length(&line);
221221
let dist = edge_length / concavity;
222222
let possible_closest_point = find_point_closest_to_line(
223223
&interior_points_tree,

geo/src/algorithm/cross_track_distance.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ where
4343
{
4444
fn cross_track_distance(&self, line_point_a: &Point<T>, line_point_b: &Point<T>) -> T {
4545
let mean_earth_radius = T::from(MEAN_EARTH_RADIUS).unwrap();
46-
let l_delta_13: T = Haversine::distance(*line_point_a, *self) / mean_earth_radius;
47-
let theta_13: T = Haversine::bearing(*line_point_a, *self).to_radians();
48-
let theta_12: T = Haversine::bearing(*line_point_a, *line_point_b).to_radians();
46+
let l_delta_13: T = Haversine.distance(*line_point_a, *self) / mean_earth_radius;
47+
let theta_13: T = Haversine.bearing(*line_point_a, *self).to_radians();
48+
let theta_12: T = Haversine.bearing(*line_point_a, *line_point_b).to_radians();
4949
let l_delta_xt: T = (l_delta_13.sin() * (theta_12 - theta_13).sin()).asin();
5050
mean_earth_radius * l_delta_xt.abs()
5151
}
@@ -90,13 +90,13 @@ mod test {
9090

9191
assert_relative_eq!(
9292
p.cross_track_distance(&line_point_a, &line_point_b),
93-
Haversine::distance(p, Point::new(1., 0.)),
93+
Haversine.distance(p, Point::new(1., 0.)),
9494
epsilon = 1.0e-6
9595
);
9696

9797
assert_relative_eq!(
9898
p.cross_track_distance(&line_point_b, &line_point_a),
99-
Haversine::distance(p, Point::new(1., 0.)),
99+
Haversine.distance(p, Point::new(1., 0.)),
100100
epsilon = 1.0e-6
101101
);
102102
}

geo/src/algorithm/densify_haversine.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010

1111
#[deprecated(
1212
since = "0.29.0",
13-
note = "Please use the `line.densify::<Haversine>()` via the `Densify` trait instead."
13+
note = "Please use the `Haversine.densify(&line)` via the `Densify` trait instead."
1414
)]
1515
/// Returns a new spherical geometry containing both existing and new interpolated coordinates with
1616
/// a maximum distance of `max_distance` between them.
@@ -50,7 +50,7 @@ where
5050
type Output = MultiPolygon<T>;
5151

5252
fn densify_haversine(&self, max_distance: T) -> Self::Output {
53-
self.densify::<Haversine>(max_distance)
53+
Haversine.densify(self, max_distance)
5454
}
5555
}
5656

@@ -64,7 +64,7 @@ where
6464
type Output = Polygon<T>;
6565

6666
fn densify_haversine(&self, max_distance: T) -> Self::Output {
67-
self.densify::<Haversine>(max_distance)
67+
Haversine.densify(self, max_distance)
6868
}
6969
}
7070

@@ -78,7 +78,7 @@ where
7878
type Output = MultiLineString<T>;
7979

8080
fn densify_haversine(&self, max_distance: T) -> Self::Output {
81-
self.densify::<Haversine>(max_distance)
81+
Haversine.densify(self, max_distance)
8282
}
8383
}
8484

@@ -92,7 +92,7 @@ where
9292
type Output = LineString<T>;
9393

9494
fn densify_haversine(&self, max_distance: T) -> Self::Output {
95-
self.densify::<Haversine>(max_distance)
95+
Haversine.densify(self, max_distance)
9696
}
9797
}
9898

@@ -106,7 +106,7 @@ where
106106
type Output = LineString<T>;
107107

108108
fn densify_haversine(&self, max_distance: T) -> Self::Output {
109-
self.densify::<Haversine>(max_distance)
109+
Haversine.densify(self, max_distance)
110110
}
111111
}
112112

@@ -120,7 +120,7 @@ where
120120
type Output = Polygon<T>;
121121

122122
fn densify_haversine(&self, max_distance: T) -> Self::Output {
123-
self.densify::<Haversine>(max_distance)
123+
Haversine.densify(self, max_distance)
124124
}
125125
}
126126

@@ -134,7 +134,7 @@ where
134134
type Output = Polygon<T>;
135135

136136
fn densify_haversine(&self, max_distance: T) -> Self::Output {
137-
self.densify::<Haversine>(max_distance)
137+
Haversine.densify(self, max_distance)
138138
}
139139
}
140140

0 commit comments

Comments
 (0)