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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
extern crate num_traits;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
#[cfg(feature = "rstar")]
extern crate rstar;
use num_traits::{Num, NumCast};
pub trait CoordinateType: Num + Copy + NumCast + PartialOrd {}
impl<T: Num + Copy + NumCast + PartialOrd> CoordinateType for T {}
mod coordinate;
pub use crate::coordinate::Coordinate;
mod point;
pub use crate::point::Point;
mod multi_point;
pub use crate::multi_point::MultiPoint;
mod line;
pub use crate::line::Line;
pub mod line_string;
pub use crate::line_string::LineString;
mod multi_line_string;
pub use crate::multi_line_string::MultiLineString;
mod polygon;
pub use crate::polygon::Polygon;
mod multi_polygon;
pub use crate::multi_polygon::MultiPolygon;
mod geometry;
pub use crate::geometry::Geometry;
mod geometry_collection;
pub use crate::geometry_collection::GeometryCollection;
mod triangle;
pub use crate::triangle::Triangle;
mod rect;
pub use crate::rect::Rect;
#[doc(hidden)]
pub mod private_utils;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn type_test() {
let c = Coordinate {
x: 40.02f64,
y: 116.34,
};
let p = Point(c);
let Point(c2) = p;
assert_eq!(c, c2);
assert_eq!(c.x, c2.x);
assert_eq!(c.y, c2.y);
let p: Point<f32> = (0f32, 1f32).into();
assert_eq!(p.x(), 0.);
assert_eq!(p.y(), 1.);
}
#[test]
fn convert_types() {
let p: Point<f32> = Point::new(0., 0.);
let p1 = p.clone();
let g: Geometry<f32> = p.into();
let p2 = g.into_point().unwrap();
assert_eq!(p1, p2);
}
#[test]
fn polygon_new_test() {
let exterior = LineString(vec![
Coordinate { x: 0., y: 0. },
Coordinate { x: 1., y: 1. },
Coordinate { x: 1., y: 0. },
Coordinate { x: 0., y: 0. },
]);
let interiors = vec![LineString(vec![
Coordinate { x: 0.1, y: 0.1 },
Coordinate { x: 0.9, y: 0.9 },
Coordinate { x: 0.9, y: 0.1 },
Coordinate { x: 0.1, y: 0.1 },
])];
let p = Polygon::new(exterior.clone(), interiors.clone());
assert_eq!(p.exterior(), &exterior);
assert_eq!(p.interiors(), &interiors[..]);
}
#[test]
fn iters() {
let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into();
let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();
let mut l1: LineString<_> = vec![(0., 0.), (1., 2.)].into();
assert_eq!(l1[1], Coordinate { x: 1., y: 2. });
let _: LineString<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();
l1[0] = Coordinate { x: 1., y: 1. };
assert_eq!(l1, vec![(1., 1.), (1., 2.)].into());
}
#[test]
fn test_coordinate_types() {
let p: Point<u8> = Point::new(0, 0);
assert_eq!(p.x(), 0u8);
let p: Point<i64> = Point::new(1_000_000, 0);
assert_eq!(p.x(), 1_000_000i64);
}
#[cfg(feature = "rstar")]
#[test]
fn line_test() {
use rstar::primitives::Line as RStarLine;
use rstar::{PointDistance, RTreeObject};
let rl = RStarLine::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
let l = Line::new(Coordinate { x: 0.0, y: 0.0 }, Coordinate { x: 5., y: 5. });
assert_eq!(rl.envelope(), l.envelope());
assert_eq!(26.0, rl.distance_2(&Point::new(4.0, 10.0)));
assert_eq!(25.999999999999996, l.distance_2(&Point::new(4.0, 10.0)));
}
#[test]
fn test_rects() {
let r = Rect {
min: Coordinate { x: -1., y: -1. },
max: Coordinate { x: 1., y: 1. },
};
let p: Polygon<_> = r.into();
assert_eq!(
p,
Polygon::new(
vec![(-1., -1.), (1., -1.), (1., 1.), (-1., 1.), (-1., -1.)].into(),
vec![]
)
);
}
}