|
| 1 | +use types::{MultiPoint, Line, Point, LineString, MultiLineString, MultiPolygon, Polygon, Geometry, GeometryCollection}; |
| 2 | +use postgis::ewkb; |
| 3 | + |
| 4 | +/// Converts geometry to a PostGIS type. |
| 5 | +/// |
| 6 | +/// Note that PostGIS databases can include a SRID (spatial reference |
| 7 | +/// system identifier) for geometry stored in them. You should specify |
| 8 | +/// the SRID of your geometry when converting, using `to_postgis_with_srid()`, |
| 9 | +/// or use `to_postgis_wgs84()` if your data is standard WGS84. |
| 10 | +pub trait ToPostgis<T> { |
| 11 | + /// Converts this geometry to a PostGIS type, using the supplied SRID. |
| 12 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> T; |
| 13 | + /// Converts this WGS84 geometry to a PostGIS type. |
| 14 | + fn to_postgis_wgs84(&self) -> T { |
| 15 | + self.to_postgis_with_srid(Some(4326)) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl ToPostgis<ewkb::Point> for Point<f64> { |
| 20 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::Point { |
| 21 | + ewkb::Point::new(self.x(), self.y(), srid) |
| 22 | + } |
| 23 | +} |
| 24 | +impl ToPostgis<ewkb::LineString> for Line<f64> { |
| 25 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::LineString { |
| 26 | + let points = vec![ |
| 27 | + self.start.to_postgis_with_srid(srid), |
| 28 | + self.end.to_postgis_with_srid(srid) |
| 29 | + ]; |
| 30 | + ewkb::LineString { points, srid } |
| 31 | + } |
| 32 | +} |
| 33 | +impl ToPostgis<ewkb::Polygon> for Polygon<f64> { |
| 34 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::Polygon { |
| 35 | + let rings = ::std::iter::once(&self.exterior) |
| 36 | + .chain(self.interiors.iter()) |
| 37 | + .map(|x| x.to_postgis_with_srid(srid)) |
| 38 | + .collect(); |
| 39 | + ewkb::Polygon { rings, srid } |
| 40 | + } |
| 41 | +} |
| 42 | +macro_rules! to_postgis_impl { |
| 43 | + ($from:ident, $to:path, $name:ident, srid) => { |
| 44 | + impl ToPostgis<$to> for $from<f64> { |
| 45 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> $to { |
| 46 | + let $name = self.0.iter() |
| 47 | + .map(|x| x.to_postgis_with_srid(srid)) |
| 48 | + .collect(); |
| 49 | + $to { $name, srid } |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + ($from:ident, $to:path, $name:ident) => { |
| 54 | + impl ToPostgis<$to> for $from<f64> { |
| 55 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> $to { |
| 56 | + let $name = self.0.iter() |
| 57 | + .map(|x| x.to_postgis_with_srid(srid)) |
| 58 | + .collect(); |
| 59 | + $to { $name } |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | +} |
| 64 | +to_postgis_impl!(GeometryCollection, ewkb::GeometryCollection, geometries); |
| 65 | +to_postgis_impl!(MultiPolygon, ewkb::MultiPolygon, polygons, srid); |
| 66 | +to_postgis_impl!(MultiLineString, ewkb::MultiLineString, lines, srid); |
| 67 | +to_postgis_impl!(MultiPoint, ewkb::MultiPoint, points, srid); |
| 68 | +to_postgis_impl!(LineString, ewkb::LineString, points, srid); |
| 69 | +impl ToPostgis<ewkb::Geometry> for Geometry<f64> { |
| 70 | + fn to_postgis_with_srid(&self, srid: Option<i32>) -> ewkb::Geometry { |
| 71 | + match *self { |
| 72 | + Geometry::Point(ref p) => ewkb::GeometryT::Point(p.to_postgis_with_srid(srid)), |
| 73 | + Geometry::Line(ref p) => ewkb::GeometryT::LineString(p.to_postgis_with_srid(srid)), |
| 74 | + Geometry::LineString(ref p) => ewkb::GeometryT::LineString(p.to_postgis_with_srid(srid)), |
| 75 | + Geometry::Polygon(ref p) => ewkb::GeometryT::Polygon(p.to_postgis_with_srid(srid)), |
| 76 | + Geometry::MultiPoint(ref p) => ewkb::GeometryT::MultiPoint(p.to_postgis_with_srid(srid)), |
| 77 | + Geometry::MultiLineString(ref p) => ewkb::GeometryT::MultiLineString(p.to_postgis_with_srid(srid)), |
| 78 | + Geometry::MultiPolygon(ref p) => ewkb::GeometryT::MultiPolygon(p.to_postgis_with_srid(srid)), |
| 79 | + Geometry::GeometryCollection(ref p) => ewkb::GeometryT::GeometryCollection(p.to_postgis_with_srid(srid)), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments