Skip to content

Commit e80bc65

Browse files
authored
Merge pull request #1305 from georust/mkirk/line-measure-docs-2
docs: add example to line_measure
2 parents 509d4d7 + f377cba commit e80bc65

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

geo/src/algorithm/line_measures/bearing.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,18 @@ pub trait Bearing<F: CoordFloat> {
99
/// # Units
1010
/// - `origin`, `destination`: Point where the units of x/y depend on the [trait implementation](#implementors).
1111
/// - returns: degrees, where: North: 0°, East: 90°, South: 180°, West: 270°
12+
///
13+
/// # Examples
14+
///
15+
/// ```
16+
/// use geo::{Point, Haversine, Bearing, Geodesic};
17+
///
18+
/// let point_1 = Point::new(0.0, 0.0);
19+
/// let point_2 = Point::new(0.0, 2.0);
20+
///
21+
/// // Due north
22+
/// assert_eq!(Haversine::bearing(point_1, point_2), 0.0);
23+
/// assert_eq!(Geodesic::bearing(point_1, point_2), 0.0);
24+
/// ```
1225
fn bearing(origin: Point<F>, destination: Point<F>) -> F;
1326
}

geo/src/algorithm/line_measures/densify.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use num_traits::FromPrimitive;
1212
/// - `max_segment_length` units depend on the implementing [metric space]. It must be greater than 0.
1313
///
1414
/// # Examples
15+
///
1516
/// ```
1617
/// # use approx::assert_relative_eq;
1718
/// use geo::{wkt, Densify};

geo/src/algorithm/line_measures/destination.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ pub trait Destination<F: CoordFloat> {
1414
/// - `distance`: depends on the [trait implementation](#implementors).
1515
/// - returns: Point where the units of x/y depend on the [trait implementation](#implementors).
1616
///
17+
/// # Examples
18+
///
19+
/// ```
20+
/// # use approx::assert_relative_eq;
21+
/// use geo::{Haversine, Rhumb, Geodesic, Destination, Point};
22+
///
23+
/// let point = Point::new(0.0, 0.0);
24+
///
25+
/// assert_relative_eq!(Haversine::destination(point, 45.0, 111_111.0), Point::new(0.706607921147679, 0.7065541919063233));
26+
/// assert_relative_eq!(Geodesic::destination(point, 45.0, 111_111.0), Point::new(0.7058183774535367, 0.7105205988658333));
27+
/// assert_relative_eq!(Rhumb::destination(point, 45.0, 111_111.0), Point::new(0.706590011673029, 0.7065721019258285));
28+
/// ```
29+
///
1730
/// [`metric_spaces`]: super::metric_spaces
1831
fn destination(origin: Point<F>, bearing: F, distance: F) -> Point<F>;
1932
}

geo/src/algorithm/line_measures/distance.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,20 @@ pub trait Distance<F, Origin, Destination> {
88
///
99
/// - `origin`, `destination`: geometry where the units of x/y depend on the trait implementation.
1010
/// - returns: depends on the trait implementation.
11+
///
12+
/// # Examples
13+
///
14+
/// ```
15+
/// use geo::{Haversine, Euclidean, Distance, Point};
16+
/// let p1: Point = Point::new(0.0, 0.0);
17+
/// let p2: Point = Point::new(0.0, 2.0);
18+
///
19+
/// assert_eq!(Euclidean::distance(p1, p2), 2.0);
20+
///
21+
/// // The units of the output depend on the metric space.
22+
/// // In the case of [`Haversine`], it's meters.
23+
/// // See the documentation for each metric space for details.
24+
/// assert_eq!(Haversine::distance(p1, p2).round(), 222_390.0);
25+
/// ```
1126
fn distance(origin: Origin, destination: Destination) -> F;
1227
}

geo/src/algorithm/line_measures/interpolate_point.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ pub trait InterpolatePoint<F: CoordFloat> {
55
/// Returns a new Point along a line between two existing points.
66
///
77
/// See [specific implementations](#implementors) for details.
8+
///
9+
/// # Examples
10+
///
11+
/// ```
12+
/// # use approx::assert_relative_eq;
13+
/// use geo::{Haversine, Euclidean, InterpolatePoint, Point};
14+
///
15+
/// let p1: Point = Point::new(0.0, 0.0);
16+
/// let p2: Point = Point::new(0.0, 2.0);
17+
///
18+
/// assert_relative_eq!(Euclidean::point_at_distance_between(p1, p2, 0.5), Point::new(0.0, 0.5));
19+
///
20+
/// // The units of the argument depend on the metric space.
21+
/// // In the case of [`Haversine`], it's meters.
22+
/// // See the documentation for each metric space for details.
23+
/// assert_relative_eq!(Haversine::point_at_distance_between(p1, p2, 111_111.0), Point::new(0.0, 0.9992438493379715));
24+
/// ```
825
fn point_at_distance_between(
926
start: Point<F>,
1027
end: Point<F>,
@@ -14,6 +31,18 @@ pub trait InterpolatePoint<F: CoordFloat> {
1431
/// Returns a new Point along a line between two existing points.
1532
///
1633
/// See [specific implementations](#implementors) for details.
34+
///
35+
/// # Examples
36+
///
37+
/// ```
38+
/// # use approx::assert_relative_eq;
39+
/// use geo::{Haversine, Euclidean, InterpolatePoint, Point};
40+
/// let p1: Point = Point::new(0.0, 0.0);
41+
/// let p2: Point = Point::new(20.0, 20.0);
42+
///
43+
/// assert_relative_eq!(Euclidean::point_at_ratio_between(p1, p2, 0.5), Point::new(10.0, 10.0));
44+
/// assert_relative_eq!(Haversine::point_at_ratio_between(p1, p2, 0.5), Point::new(9.685895184381804, 10.150932342575631));
45+
/// ```
1746
fn point_at_ratio_between(start: Point<F>, end: Point<F>, ratio_from_start: F) -> Point<F>;
1847

1948
/// Interpolates `Point`s along a line between `start` and `end`.
@@ -25,6 +54,26 @@ pub trait InterpolatePoint<F: CoordFloat> {
2554
/// `max_distance`, no additional points will be included in the output.
2655
///
2756
/// `include_ends`: Should the start and end points be included in the output?
57+
///
58+
/// # Examples
59+
///
60+
/// ```
61+
/// # use approx::assert_relative_eq;
62+
/// use geo::{Haversine, Euclidean, InterpolatePoint, Point, MultiPoint, LineString, wkt};
63+
/// let p1: Point = Point::new(0.0, 0.0);
64+
/// let p2: Point = Point::new(0.0, 2.0);
65+
///
66+
/// let intermediate_points: Vec<Point> = Euclidean::points_along_line(p1, p2, 0.5, false).collect();
67+
/// let multi_point = MultiPoint(intermediate_points);
68+
/// assert_relative_eq!(multi_point, wkt!(MULTIPOINT(0. 0.5,0. 1.,0. 1.5)));
69+
///
70+
/// // The units of the argument depend on the metric space.
71+
/// // In the case of [`Haversine`], it's meters.
72+
/// // See the documentation for each metric space for details.
73+
/// let intermediate_points: Vec<Point> = Haversine::points_along_line(p1, p2, 55_555.0, false).collect();
74+
/// let multi_point = MultiPoint(intermediate_points);
75+
/// assert_relative_eq!(multi_point, wkt!(MULTIPOINT(0. 0.4,0. 0.8,0. 1.2,0. 1.6)));
76+
/// ```
2877
fn points_along_line(
2978
start: Point<F>,
3079
end: Point<F>,

geo/src/algorithm/line_measures/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
//! Line measurements like [`Bearing`] and [`Distance`] for various metric spaces like [`Euclidean`], [`Haversine`], [`Geodesic`], and [`Rhumb`].
2+
//!
3+
//! ## Example
4+
//! ```
5+
//! use geo::{Haversine, Euclidean, Distance, Point, Bearing};
6+
//! let p1: Point = Point::new(0.0, 0.0);
7+
//! let p2: Point = Point::new(0.0, 2.0);
8+
//!
9+
//! assert_eq!(Euclidean::distance(p1, p2), 2.0);
10+
//!
11+
//! // The units of the output depend on the metric space.
12+
//! // In the case of [`Haversine`], it's meters.
13+
//! // See the documentation for each metric space for details.
14+
//! assert_eq!(Haversine::distance(p1, p2).round(), 222_390.0);
15+
//!
16+
//! // Due north
17+
//! assert_eq!(Haversine::bearing(p1, p2), 0.0);
18+
//! ```
19+
//!
20+
//! See the individual [`metric_spaces`] or algorithm [traits](#traits) for more details.
221
322
mod bearing;
423
pub use bearing::Bearing;

0 commit comments

Comments
 (0)