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