geo/algorithm/
euclidean_length.rs

1use std::iter::Sum;
2
3use crate::{CoordFloat, Euclidean, Length, Line, LineString, MultiLineString};
4
5/// Calculation of the length
6
7#[deprecated(
8    since = "0.29.0",
9    note = "Please use the `line.length::<Euclidean>()` via the `Length` trait instead."
10)]
11pub trait EuclideanLength<T, RHS = Self> {
12    /// Calculation of the length of a Line
13    ///
14    /// # Examples
15    ///
16    /// ```
17    /// use geo::EuclideanLength;
18    /// use geo::line_string;
19    ///
20    /// let line_string = line_string![
21    ///     (x: 40.02f64, y: 116.34),
22    ///     (x: 42.02f64, y: 116.34),
23    /// ];
24    ///
25    /// assert_eq!(
26    ///     2.,
27    ///     line_string.euclidean_length(),
28    /// )
29    /// ```
30    fn euclidean_length(&self) -> T;
31}
32
33#[allow(deprecated)]
34impl<T> EuclideanLength<T> for Line<T>
35where
36    T: CoordFloat,
37{
38    fn euclidean_length(&self) -> T {
39        self.length::<Euclidean>()
40    }
41}
42
43#[allow(deprecated)]
44impl<T> EuclideanLength<T> for LineString<T>
45where
46    T: CoordFloat + Sum,
47{
48    fn euclidean_length(&self) -> T {
49        self.length::<Euclidean>()
50    }
51}
52
53#[allow(deprecated)]
54impl<T> EuclideanLength<T> for MultiLineString<T>
55where
56    T: CoordFloat + Sum,
57{
58    fn euclidean_length(&self) -> T {
59        self.length::<Euclidean>()
60    }
61}
62
63#[cfg(test)]
64mod test {
65    use crate::line_string;
66    #[allow(deprecated)]
67    use crate::EuclideanLength;
68    use crate::{coord, Line, MultiLineString};
69
70    #[allow(deprecated)]
71    #[test]
72    fn empty_linestring_test() {
73        let linestring = line_string![];
74        assert_relative_eq!(0.0_f64, linestring.euclidean_length());
75    }
76    #[allow(deprecated)]
77    #[test]
78    fn linestring_one_point_test() {
79        let linestring = line_string![(x: 0., y: 0.)];
80        assert_relative_eq!(0.0_f64, linestring.euclidean_length());
81    }
82    #[allow(deprecated)]
83    #[test]
84    fn linestring_test() {
85        let linestring = line_string![
86            (x: 1., y: 1.),
87            (x: 7., y: 1.),
88            (x: 8., y: 1.),
89            (x: 9., y: 1.),
90            (x: 10., y: 1.),
91            (x: 11., y: 1.)
92        ];
93        assert_relative_eq!(10.0_f64, linestring.euclidean_length());
94    }
95    #[allow(deprecated)]
96    #[test]
97    fn multilinestring_test() {
98        let mline = MultiLineString::new(vec![
99            line_string![
100                (x: 1., y: 0.),
101                (x: 7., y: 0.),
102                (x: 8., y: 0.),
103                (x: 9., y: 0.),
104                (x: 10., y: 0.),
105                (x: 11., y: 0.)
106            ],
107            line_string![
108                (x: 0., y: 0.),
109                (x: 0., y: 5.)
110            ],
111        ]);
112        assert_relative_eq!(15.0_f64, mline.euclidean_length());
113    }
114    #[allow(deprecated)]
115    #[test]
116    fn line_test() {
117        let line0 = Line::new(coord! { x: 0., y: 0. }, coord! { x: 0., y: 1. });
118        let line1 = Line::new(coord! { x: 0., y: 0. }, coord! { x: 3., y: 4. });
119        assert_relative_eq!(line0.euclidean_length(), 1.);
120        assert_relative_eq!(line1.euclidean_length(), 5.);
121    }
122}