1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use num_traits::{Float, FromPrimitive};

use crate::algorithm::vincenty_distance::{FailedToConvergeError, VincentyDistance};
use crate::{Line, LineString, MultiLineString};

/// Determine the length of a geometries using [Vincenty’s formulae].
///
/// [Vincenty’s formulae]: https://en.wikipedia.org/wiki/Vincenty%27s_formulae
pub trait VincentyLength<T, RHS = Self> {
    /// Determine the length of a geometries using [Vincenty’s formulae].
    ///
    /// # Units
    ///
    /// - return value: meters
    ///
    /// # Examples
    ///
    /// ```
    /// use geo::LineString;
    /// use geo::prelude::*;
    ///
    /// let linestring = LineString::<f64>::from(vec![
    ///   // New York City
    ///   (-74.006, 40.7128),
    ///   // London
    ///   (-0.1278, 51.5074),
    /// ]);
    ///
    /// let length = linestring.vincenty_length().unwrap();
    ///
    /// assert_eq!(
    ///   5_585_234., // meters
    ///   length.round()
    /// );
    /// ```
    ///
    /// [Vincenty’s formulae]: https://en.wikipedia.org/wiki/Vincenty%27s_formulae
    fn vincenty_length(&self) -> Result<T, FailedToConvergeError>;
}

impl<T> VincentyLength<T> for Line<T>
where
    T: Float + FromPrimitive,
{
    /// The units of the returned value is meters.
    fn vincenty_length(&self) -> Result<T, FailedToConvergeError> {
        let (start, end) = self.points();
        start.vincenty_distance(&end)
    }
}

impl<T> VincentyLength<T> for LineString<T>
where
    T: Float + FromPrimitive,
{
    fn vincenty_length(&self) -> Result<T, FailedToConvergeError> {
        let mut length = T::zero();
        for line in self.lines() {
            length = length + line.vincenty_length()?;
        }
        Ok(length)
    }
}

impl<T> VincentyLength<T> for MultiLineString<T>
where
    T: Float + FromPrimitive,
{
    fn vincenty_length(&self) -> Result<T, FailedToConvergeError> {
        let mut length = T::zero();
        for line_string in &self.0 {
            length = length + line_string.vincenty_length()?;
        }
        Ok(length)
    }
}