-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlib.rs
85 lines (70 loc) · 2.17 KB
/
lib.rs
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
use geo::{Area, Centroid};
#[derive(Debug, Clone, PartialEq)]
pub struct MultiPoint(geo::MultiPoint);
#[derive(Debug, Clone, PartialEq)]
pub struct MultiLineString(geo::MultiLineString);
#[derive(Debug, Clone, PartialEq)]
pub struct MultiPolygon(geo::MultiPolygon);
/// Implement `From` and `Into` for a geometry type.
macro_rules! impl_geo_from_to {
( $geom_type:ty, $geo_multi_type:ty, $geo_single_type:ty ) => {
impl From<$geo_multi_type> for $geom_type {
fn from(geom: $geo_multi_type) -> Self {
Self(geom)
}
}
impl From<$geo_single_type> for $geom_type {
fn from(geom: $geo_single_type) -> Self {
Self(<$geo_multi_type>::new(vec![geom]))
}
}
impl From<$geom_type> for $geo_multi_type {
fn from(geom: $geom_type) -> Self {
geom.0
}
}
};
}
impl_geo_from_to!(MultiPoint, geo::MultiPoint, geo::Point);
impl_geo_from_to!(MultiLineString, geo::MultiLineString, geo::LineString);
impl_geo_from_to!(MultiPolygon, geo::MultiPolygon, geo::Polygon);
/// Common operations for all geometry types.
pub trait GeoOptionOperations {
fn area(&self) -> Option<f64>;
fn centroid(&self) -> Option<MultiPoint>;
}
impl GeoOptionOperations for MultiPoint {
fn area(&self) -> Option<f64> {
Some(self.0.unsigned_area())
}
fn centroid(&self) -> Option<MultiPoint> {
Some(MultiPoint(self.0.centroid()?.into()))
}
}
impl GeoOptionOperations for MultiLineString {
fn area(&self) -> Option<f64> {
Some(self.0.unsigned_area())
}
fn centroid(&self) -> Option<MultiPoint> {
Some(MultiPoint(self.0.centroid()?.into()))
}
}
impl GeoOptionOperations for MultiPolygon {
fn area(&self) -> Option<f64> {
Some(self.0.unsigned_area())
}
fn centroid(&self) -> Option<MultiPoint> {
Some(MultiPoint(self.0.centroid()?.into()))
}
}
impl<T> GeoOptionOperations for Option<T>
where
T: GeoOptionOperations,
{
fn area(&self) -> Option<f64> {
self.as_ref()?.area()
}
fn centroid(&self) -> Option<MultiPoint> {
self.as_ref()?.centroid()
}
}