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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use num_traits::Float;
use std::iter::Sum;
use crate::{Line, LineString, MultiLineString};
pub trait EuclideanLength<T, RHS = Self> {
fn euclidean_length(&self) -> T;
}
impl<T> EuclideanLength<T> for Line<T>
where
T: Float,
{
fn euclidean_length(&self) -> T {
::geo_types::private_utils::line_euclidean_length(*self)
}
}
impl<T> EuclideanLength<T> for LineString<T>
where
T: Float + Sum,
{
fn euclidean_length(&self) -> T {
self.lines().map(|line| line.euclidean_length()).sum()
}
}
impl<T> EuclideanLength<T> for MultiLineString<T>
where
T: Float + Sum,
{
fn euclidean_length(&self) -> T {
self.0
.iter()
.fold(T::zero(), |total, line| total + line.euclidean_length())
}
}
#[cfg(test)]
mod test {
use crate::algorithm::euclidean_length::EuclideanLength;
use crate::line_string;
use crate::{Coordinate, Line, MultiLineString};
#[test]
fn empty_linestring_test() {
let linestring = line_string![];
assert_relative_eq!(0.0_f64, linestring.euclidean_length());
}
#[test]
fn linestring_one_point_test() {
let linestring = line_string![(x: 0., y: 0.)];
assert_relative_eq!(0.0_f64, linestring.euclidean_length());
}
#[test]
fn linestring_test() {
let linestring = line_string![
(x: 1., y: 1.),
(x: 7., y: 1.),
(x: 8., y: 1.),
(x: 9., y: 1.),
(x: 10., y: 1.),
(x: 11., y: 1.)
];
assert_relative_eq!(10.0_f64, linestring.euclidean_length());
}
#[test]
fn multilinestring_test() {
let mline = MultiLineString(vec![
line_string![
(x: 1., y: 0.),
(x: 7., y: 0.),
(x: 8., y: 0.),
(x: 9., y: 0.),
(x: 10., y: 0.),
(x: 11., y: 0.)
],
line_string![
(x: 0., y: 0.),
(x: 0., y: 5.)
],
]);
assert_relative_eq!(15.0_f64, mline.euclidean_length());
}
#[test]
fn line_test() {
let line0 = Line::new(Coordinate { x: 0., y: 0. }, Coordinate { x: 0., y: 1. });
let line1 = Line::new(Coordinate { x: 0., y: 0. }, Coordinate { x: 3., y: 4. });
assert_relative_eq!(line0.euclidean_length(), 1.);
assert_relative_eq!(line1.euclidean_length(), 5.);
}
}