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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
extern crate geo;

use self::geo::{LineString, MultiPolygon, Polygon, Point};
use ffi::{CoordSeq, GGeom};
use error::Error;

// define our own TryInto while the std trait is not stable
pub trait TryInto<T> {
    type Err;
    fn try_into(self) -> Result<T, Self::Err>;
}

fn create_coord_seq<'a>(points: &'a Vec<Point<f64>>) -> Result<CoordSeq, Error> {
    let nb_pts = points.len();
    let coord_seq = CoordSeq::new(nb_pts as u32, 2);
    for i in 0..nb_pts {
        let j = i as u32;
        coord_seq.set_x(j, points[i].x())?;
        coord_seq.set_y(j, points[i].y())?;
    }
    Ok(coord_seq)
}

impl<'a> TryInto<GGeom> for &'a LineString<f64> {
    type Err = Error;

    fn try_into(self) -> Result<GGeom, Self::Err> {
        let coord_seq = create_coord_seq(&self.0)?;

        GGeom::create_line_string(coord_seq)
    }
}

// rust geo does not have the distinction LineString/LineRing, so we create a wrapper 

struct LineRing<'a>(&'a LineString<f64>);

impl<'a> TryInto<GGeom> for &'a LineRing<'a> {
    type Err = Error;

    fn try_into(self) -> Result<GGeom, Self::Err> {
        let points = &(self.0).0;
        let coord_seq = create_coord_seq(&points)?;

        if points.len() == 1 {
            Err(Error::InvalidGeometry("impossible to create a linering from one point".into()))
        } else if points.len() > 1 && points.first() != points.last() {
            // the linestring need to be closed else geos will crash
            Err(Error::InvalidGeometry("impossible to create a linering with an unclosed geometry".into()))
        } else {
            GGeom::create_linear_ring(coord_seq)
        }
    }
}

impl<'a> TryInto<GGeom> for &'a Polygon<f64> {
    type Err = Error;

    fn try_into(self) -> Result<GGeom, Self::Err> {
        let geom_exterior: GGeom = LineRing(&self.exterior).try_into()?;

        let interiors: Vec<_> = self.interiors
            .iter()
            .map(|i| LineRing(i).try_into())
            .collect::<Result<Vec<_>, _>>()?;

        GGeom::create_polygon(geom_exterior, interiors)
    }
}

impl<'a> TryInto<GGeom> for &'a MultiPolygon<f64> {
    type Err = Error;

    fn try_into(self) -> Result<GGeom, Self::Err> {
        let polygons: Vec<_> = self.0
            .iter()
            .map(|p| p.try_into())
            .collect::<Result<Vec<_>, _>>()?;

        GGeom::create_multipolygon(polygons)
    }
}

#[cfg(test)]
mod test {
    use from_geo::geo::{LineString, MultiPolygon, Point, Polygon};
    use ffi::GGeom;
    use from_geo::TryInto;

    #[test]
    fn polygon_contains_test() {
        let exterior = LineString(vec![
            Point::new(0., 0.),
            Point::new(0., 1.),
            Point::new(1., 1.),
            Point::new(1., 0.),
            Point::new(0., 0.),
        ]);
        let interiors = vec![
            LineString(vec![
                Point::new(0.1, 0.1),
                Point::new(0.1, 0.9),
                Point::new(0.9, 0.9),
                Point::new(0.9, 0.1),
                Point::new(0.1, 0.1),
            ]),
        ];
        let p = Polygon::new(exterior.clone(), interiors.clone());

        assert_eq!(p.exterior, exterior);
        assert_eq!(p.interiors, interiors);

        let geom: GGeom = (&p).try_into().unwrap();

        assert!(geom.contains(&geom));
        assert!(!geom.contains(&(&exterior).try_into().unwrap()));

        assert!(geom.covers(&(&exterior).try_into().unwrap()));
        assert!(geom.touches(&(&exterior).try_into().unwrap()));
    }

    #[test]
    fn multipolygon_contains_test() {
        let exterior = LineString(vec![
            Point::new(0., 0.),
            Point::new(0., 1.),
            Point::new(1., 1.),
            Point::new(1., 0.),
            Point::new(0., 0.),
        ]);
        let interiors = vec![
            LineString(vec![
                Point::new(0.1, 0.1),
                Point::new(0.1, 0.9),
                Point::new(0.9, 0.9),
                Point::new(0.9, 0.1),
                Point::new(0.1, 0.1),
            ]),
        ];
        let p = Polygon::new(exterior, interiors);
        let mp = MultiPolygon(vec![p.clone()]);

        let geom: GGeom = (&mp).try_into().unwrap();

        assert!(geom.contains(&geom));
        assert!(geom.contains(&(&p).try_into().unwrap()));
    }

    #[test]
    fn incorrect_multipolygon_test() {
        let exterior = LineString(vec![
            Point::new(0., 0.)
        ]);
        let interiors = vec![];
        let p = Polygon::new(exterior, interiors);
        let mp = MultiPolygon(vec![p.clone()]);

        let geom = (&mp).try_into();

        assert!(geom.is_err());
    }    
    
    #[test]
    fn incorrect_polygon_not_closed() {
        let exterior = LineString(vec![
            Point::new(0., 0.),
            Point::new(0., 2.),
            Point::new(2., 2.),
            Point::new(2., 0.),
            Point::new(0., 0.),
        ]);
        let interiors = vec![
            LineString(vec![
            Point::new(0., 0.),
            Point::new(0., 1.),
            Point::new(1., 1.),
            Point::new(1., 0.),
            Point::new(0., 10.),
            ]),
        ];
        let p = Polygon::new(exterior, interiors);
        let mp = MultiPolygon(vec![p]);

        let geom = (&mp).try_into();
        let error = geom.err().unwrap();

        assert_eq!(format!("{}", error), "Invalid geometry, impossible to create a linering with an unclosed geometry".to_string());
    }
}