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
use types::{MultiPoint, Point, LineString, MultiLineString, MultiPolygon, Polygon, Geometry, GeometryCollection};
use postgis;
use postgis::ewkb::{GeometryT, GeometryCollectionT};
pub trait FromPostgis<T> {
fn from_postgis(T) -> Self;
}
impl<'a, T> FromPostgis<&'a T> for Point<f64> where T: postgis::Point {
fn from_postgis(pt: &'a T) -> Self {
Point::new(pt.x(), pt.y())
}
}
impl<'a, T> FromPostgis<&'a T> for LineString<f64> where T: postgis::LineString<'a> {
fn from_postgis(ls: &'a T) -> Self {
let ret = ls.points()
.map(|x| Point::from_postgis(x))
.collect();
LineString(ret)
}
}
impl<'a, T> FromPostgis<&'a T> for Option<Polygon<f64>> where T: postgis::Polygon<'a> {
fn from_postgis(poly: &'a T) -> Self {
let mut rings = poly.rings()
.map(|x| LineString::from_postgis(x))
.collect::<Vec<_>>();
if rings.len() == 0 {
return None;
}
let exterior = rings.remove(0);
Some(Polygon {
exterior,
interiors: rings
})
}
}
impl<'a, T> FromPostgis<&'a T> for MultiPoint<f64> where T: postgis::MultiPoint<'a> {
fn from_postgis(mp: &'a T) -> Self {
let ret = mp.points()
.map(|x| Point::from_postgis(x))
.collect();
MultiPoint(ret)
}
}
impl<'a, T> FromPostgis<&'a T> for MultiLineString<f64> where T: postgis::MultiLineString<'a> {
fn from_postgis(mp: &'a T) -> Self {
let ret = mp.lines()
.map(|x| LineString::from_postgis(x))
.collect();
MultiLineString(ret)
}
}
impl<'a, T> FromPostgis<&'a T> for MultiPolygon<f64> where T: postgis::MultiPolygon<'a> {
fn from_postgis(mp: &'a T) -> Self {
let ret = mp.polygons()
.filter_map(|x| Option::from_postgis(x))
.collect();
MultiPolygon(ret)
}
}
impl<'a, T> FromPostgis<&'a GeometryCollectionT<T>> for GeometryCollection<f64> where T: postgis::Point + postgis::ewkb::EwkbRead {
fn from_postgis(gc: &'a GeometryCollectionT<T>) -> Self {
let geoms = gc.geometries.iter()
.filter_map(|x| Option::from_postgis(x))
.collect();
GeometryCollection(geoms)
}
}
impl<'a, T> FromPostgis<&'a GeometryT<T>> for Option<Geometry<f64>> where T: postgis::Point + postgis::ewkb::EwkbRead {
fn from_postgis(geo: &'a GeometryT<T>) -> Self {
Some(match *geo {
GeometryT::Point(ref p) => Geometry::Point(Point::from_postgis(p)),
GeometryT::LineString(ref ls) => Geometry::LineString(LineString::from_postgis(ls)),
GeometryT::Polygon(ref p) => match Option::from_postgis(p) {
Some(p) => Geometry::Polygon(p),
None => return None
},
GeometryT::MultiPoint(ref p) => Geometry::MultiPoint(MultiPoint::from_postgis(p)),
GeometryT::MultiLineString(ref p) => Geometry::MultiLineString(MultiLineString::from_postgis(p)),
GeometryT::MultiPolygon(ref p) => Geometry::MultiPolygon(MultiPolygon::from_postgis(p)),
GeometryT::GeometryCollection(ref p) => Geometry::GeometryCollection(GeometryCollection::from_postgis(p))
})
}
}