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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use types::{CoordinateType, Bbox, Point, MultiPoint, Line, LineString, MultiLineString, Polygon, MultiPolygon};

/// Calculation of the bounding box of a geometry.

pub trait BoundingBox<T: CoordinateType> {
    type Output;

    /// Return the Bounding Box of a geometry
    ///
    /// ```
    /// use geo::{Point, LineString};
    /// use geo::algorithm::boundingbox::BoundingBox;
    ///
    /// let mut vec = Vec::new();
    /// vec.push(Point::new(40.02f64, 116.34));
    /// vec.push(Point::new(42.02f64, 116.34));
    /// vec.push(Point::new(42.02f64, 118.34));
    /// let linestring = LineString(vec);
    /// let bbox = linestring.bbox().unwrap();
    ///
    /// assert_eq!(40.02f64, bbox.xmin);
    /// assert_eq!(42.02f64, bbox.xmax);
    /// assert_eq!(116.34, bbox.ymin);
    /// assert_eq!(118.34, bbox.ymax);
    /// ```
    ///
    fn bbox(&self) -> Self::Output;
}

fn get_min_max<T>(p: T, min: T, max: T) -> (T, T)
where
    T: CoordinateType,
{
    if p > max {
        (min, p)
    } else if p < min {
        (p, max)
    } else {
        (min, max)
    }
}

fn get_bbox<'a, I, T>(collection: I) -> Option<Bbox<T>>
where
    T: 'a + CoordinateType,
    I: 'a + IntoIterator<Item = &'a Point<T>>
{
    let mut iter = collection.into_iter();
    if let Some(pnt) = iter.next() {
        let mut xrange = (pnt.x(), pnt.x());
        let mut yrange = (pnt.y(), pnt.y());
        for pnt in iter {
            let (px, py) = (pnt.x(), pnt.y());
            xrange = get_min_max(px, xrange.0, xrange.1);
            yrange = get_min_max(py, yrange.0, yrange.1);
        }
        return Some(Bbox {
            xmin: xrange.0,
            xmax: xrange.1,
            ymin: yrange.0,
            ymax: yrange.1,
        });
    }
    None
}

impl<T> BoundingBox<T> for MultiPoint<T>
where
    T: CoordinateType,
{
    type Output = Option<Bbox<T>>;

    ///
    /// Return the BoundingBox for a MultiPoint
    ///
    fn bbox(&self) -> Self::Output {
        get_bbox(&self.0)
    }
}

impl<T> BoundingBox<T> for Line<T>
where
    T: CoordinateType,
{
    type Output = Bbox<T>;

    fn bbox(&self) -> Self::Output {
        let a = self.start;
        let b = self.end;
        let (xmin, xmax) = if a.x() <= b.x() {
            (a.x(), b.x())
        } else {
            (b.x(), a.x())
        };
        let (ymin, ymax) = if a.y() <= b.y() {
            (a.y(), b.y())
        } else {
            (b.y(), a.y())
        };
        Bbox {
            xmin: xmin,
            xmax: xmax,
            ymin: ymin,
            ymax: ymax,
        }
    }
}

impl<T> BoundingBox<T> for LineString<T>
where
    T: CoordinateType,
{
    type Output = Option<Bbox<T>>;

    ///
    /// Return the BoundingBox for a LineString
    ///
    fn bbox(&self) -> Self::Output {
        get_bbox(&self.0)
    }
}

impl<T> BoundingBox<T> for MultiLineString<T>
where
    T: CoordinateType,
{
    type Output = Option<Bbox<T>>;

    ///
    /// Return the BoundingBox for a MultiLineString
    ///
    fn bbox(&self) -> Self::Output {
        get_bbox(self.0.iter().flat_map(|line| line.0.iter()))
    }
}

impl<T> BoundingBox<T> for Polygon<T>
where
    T: CoordinateType,
{
    type Output = Option<Bbox<T>>;

    ///
    /// Return the BoundingBox for a Polygon
    ///
    fn bbox(&self) -> Self::Output {
        let line = &self.exterior;
        get_bbox(&line.0)
    }
}

impl<T> BoundingBox<T> for MultiPolygon<T>
where
    T: CoordinateType,
{
    type Output = Option<Bbox<T>>;

    ///
    /// Return the BoundingBox for a MultiPolygon
    ///
    fn bbox(&self) -> Self::Output {
        get_bbox(self.0.iter().flat_map(|poly| (poly.exterior).0.iter()))
    }
}

#[cfg(test)]
mod test {
    use types::{Bbox, Coordinate, Line, LineString, MultiLineString, MultiPoint, MultiPolygon, Point,
                Polygon};
    use algorithm::boundingbox::BoundingBox;

    #[test]
    fn empty_linestring_test() {
        let vect = Vec::<Point<f64>>::new();
        let linestring = LineString(vect);
        let bbox = linestring.bbox();
        assert!(bbox.is_none());
    }
    #[test]
    fn linestring_one_point_test() {
        let p = Point::new(40.02f64, 116.34);
        let mut vect = Vec::<Point<f64>>::new();
        vect.push(p);
        let linestring = LineString(vect);
        let bbox = Bbox {
            xmin: 40.02f64,
            ymax: 116.34,
            xmax: 40.02,
            ymin: 116.34,
        };
        assert_eq!(bbox, linestring.bbox().unwrap());
    }
    #[test]
    fn linestring_test() {
        let p = |x, y| Point(Coordinate { x: x, y: y });
        let linestring = LineString(vec![p(1., 1.), p(2., -2.), p(-3., -3.), p(-4., 4.)]);
        let bbox = Bbox {
            xmin: -4.,
            ymax: 4.,
            xmax: 2.,
            ymin: -3.,
        };
        assert_eq!(bbox, linestring.bbox().unwrap());
    }
    #[test]
    fn multilinestring_test() {
        let p = |x, y| Point(Coordinate { x: x, y: y });
        let multiline = MultiLineString(vec![
            LineString(vec![p(1., 1.), p(-40., 1.)]),
            LineString(vec![p(1., 1.), p(50., 1.)]),
            LineString(vec![p(1., 1.), p(1., -60.)]),
            LineString(vec![p(1., 1.), p(1., 70.)]),
        ]);
        let bbox = Bbox {
            xmin: -40.,
            ymax: 70.,
            xmax: 50.,
            ymin: -60.,
        };
        assert_eq!(bbox, multiline.bbox().unwrap());
    }
    #[test]
    fn multipoint_test() {
        let p = |x, y| Point(Coordinate { x: x, y: y });
        let multipoint = MultiPoint(vec![p(1., 1.), p(2., -2.), p(-3., -3.), p(-4., 4.)]);
        let bbox = Bbox {
            xmin: -4.,
            ymax: 4.,
            xmax: 2.,
            ymin: -3.,
        };
        assert_eq!(bbox, multipoint.bbox().unwrap());
    }
    #[test]
    fn polygon_test() {
        let p = |x, y| Point(Coordinate { x: x, y: y });
        let linestring = LineString(vec![p(0., 0.), p(5., 0.), p(5., 6.), p(0., 6.), p(0., 0.)]);
        let line_bbox = linestring.bbox().unwrap();
        let poly = Polygon::new(linestring, Vec::new());
        assert_eq!(line_bbox, poly.bbox().unwrap());
    }
    #[test]
    fn multipolygon_test() {
        let p = |x, y| Point(Coordinate { x: x, y: y });
        let mpoly = MultiPolygon(vec![
            Polygon::new(
                LineString(vec![p(0., 0.), p(50., 0.), p(0., -70.), p(0., 0.)]),
                Vec::new(),
            ),
            Polygon::new(
                LineString(vec![p(0., 0.), p(5., 0.), p(0., 80.), p(0., 0.)]),
                Vec::new(),
            ),
            Polygon::new(
                LineString(vec![p(0., 0.), p(-60., 0.), p(0., 6.), p(0., 0.)]),
                Vec::new(),
            ),
        ]);
        let bbox = Bbox {
            xmin: -60.,
            ymax: 80.,
            xmax: 50.,
            ymin: -70.,
        };
        assert_eq!(bbox, mpoly.bbox().unwrap());
    }
    #[test]
    fn line_test() {
        let p = |x, y| Point(Coordinate { x: x, y: y });
        let line1 = Line::new(p(0., 1.), p(2., 3.));
        let line2 = Line::new(p(2., 3.), p(0., 1.));
        assert_eq!(
            line1.bbox(),
            Bbox {
                xmin: 0.,
                xmax: 2.,
                ymin: 1.,
                ymax: 3.,
            }
        );
        assert_eq!(
            line2.bbox(),
            Bbox {
                xmin: 0.,
                xmax: 2.,
                ymin: 1.,
                ymax: 3.,
            }
        );
    }
}