gis-tools 1.13.1

A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
Documentation
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use crate::geometry::orient2d;
use alloc::vec::Vec;
use s2json::{
    Feature, Geometry, GetXY, MultiLineString, MultiLineString3D, MultiLineString3DGeometry,
    MultiLineStringGeometry, MultiPoint, MultiPoint3D, MultiPoint3DGeometry, MultiPointGeometry,
    MultiPolygon, MultiPolygon3D, MultiPolygon3DGeometry, MultiPolygonGeometry, Point, Point3D,
    Point3DGeometry, PointGeometry, VectorFeature, VectorGeometry, VectorMultiLineString,
    VectorMultiLineStringGeometry, VectorMultiPoint, VectorMultiPointGeometry, VectorMultiPolygon,
    VectorMultiPolygonGeometry, VectorPoint, VectorPointGeometry,
};

/// The result of a point being inside, outside, or on the boundary
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InsideResult {
    /// The point is inside
    Inside,
    /// The point is outside
    Outside,
    /// The point is on the boundary
    Boundary,
}
impl InsideResult {
    /// Get the dominant result. Inside > Boundary > Outside
    ///
    /// Inside takes precedence over Boundary and Outside
    ///
    /// Boundary takes precedence over Outside
    pub fn get_dominant(&self, other: InsideResult) -> InsideResult {
        if *self == other {
            *self
        } else {
            match (self, other) {
                (InsideResult::Inside, _) => InsideResult::Inside,
                (_, InsideResult::Inside) => InsideResult::Inside,
                (InsideResult::Boundary, _) => InsideResult::Boundary,
                (_, InsideResult::Boundary) => InsideResult::Boundary,
                _ => InsideResult::Outside,
            }
        }
    }
}

/// Check if a point is inside a geometry.
///
/// If the geoemtry we check against is a Point, check if the points are equal
///
/// If the geometry we check against is a MultiPoint or LineString, treat as a polygon with no holes
///
/// If the geometry we check against is a Polygon or MultiPolygon, check if the point is inside
///
/// This trait is implemented for:
/// - [`Feature`]
/// - [`Geometry`]
/// - [`PointGeometry`]
/// - [`MultiPointGeometry`]
/// - [`MultiLineStringGeometry`]
/// - [`MultiPolygonGeometry`]
/// - [`Point3DGeometry`]
/// - [`MultiPoint3DGeometry`]
/// - [`MultiLineString3DGeometry`]
/// - [`MultiPolygon3DGeometry`]
/// - [`VectorFeature`]
/// - [`VectorGeometry`]
/// - [`VectorPointGeometry`]
/// - [`VectorMultiPointGeometry`]
/// - [`VectorMultiLineStringGeometry`]
/// - [`VectorMultiPolygonGeometry`]
/// - [`VectorMultiPoint`]
/// - [`VectorMultiLineString`]
/// - [`VectorMultiPolygon`]
/// - `&Vec<Vec<P>>` or `&[Vec<P>]` where P implements [`GetXY`]
///
/// And all specific geometries of the above enums
pub trait Inside {
    /// Check if a point is inside a geometry.
    ///
    /// If the geoemtry we check against is a Point, check if the points are equal
    ///
    /// If the geometry we check against is a MultiPoint or LineString, treat as a polygon with no holes
    ///
    /// If the geometry we check against is a Polygon or MultiPolygon, check if the point is inside
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult;
}

// Feature and below

impl<P: GetXY> Inside for &Vec<Vec<P>> {
    fn inside<B: GetXY>(&self, b: &B) -> InsideResult {
        point_in_polygon(b, self)
    }
}
impl<P: GetXY> Inside for &[Vec<P>] {
    fn inside<B: GetXY>(&self, b: &B) -> InsideResult {
        point_in_polygon(b, self)
    }
}

impl<M, P: Clone + Default, D: Clone + Default> Inside for Feature<M, P, D> {
    fn inside<B: GetXY>(&self, b: &B) -> InsideResult {
        self.geometry.inside(b)
    }
}
impl<M: Clone + Default> Inside for Geometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        match self {
            Geometry::Point(g) => g.inside(b),
            Geometry::MultiPoint(g) => g.inside(b),
            Geometry::LineString(g) => g.inside(b),
            Geometry::MultiLineString(g) => g.inside(b),
            Geometry::Polygon(g) => g.inside(b),
            Geometry::MultiPolygon(g) => g.inside(b),
            Geometry::Point3D(g) => g.inside(b),
            Geometry::MultiPoint3D(g) => g.inside(b),
            Geometry::LineString3D(g) => g.inside(b),
            Geometry::MultiLineString3D(g) => g.inside(b),
            Geometry::Polygon3D(g) => g.inside(b),
            Geometry::MultiPolygon3D(g) => g.inside(b),
        }
    }
}
impl<M: Clone + Default> Inside for PointGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for MultiPointGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for MultiLineStringGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for MultiPolygonGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for Point3DGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for MultiPoint3DGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for MultiLineString3DGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for MultiPolygon3DGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}

// Feature Point types

impl Inside for Point {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        if self.x() == b.x() && self.y() == b.y() {
            InsideResult::Inside
        } else {
            InsideResult::Outside
        }
    }
}
impl Inside for MultiPoint {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        point_in_polyline(b, self)
    }
}
impl Inside for MultiLineString {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        point_in_polygon(b, self)
    }
}
impl Inside for MultiPolygon {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        let mut res = InsideResult::Outside;
        for poly in self {
            res = res.get_dominant(poly.inside(b));
        }
        res
    }
}
impl Inside for Point3D {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        if self.x() == b.x() && self.y() == b.y() {
            InsideResult::Inside
        } else {
            InsideResult::Outside
        }
    }
}
impl Inside for MultiPoint3D {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        point_in_polyline(b, self)
    }
}
impl Inside for MultiLineString3D {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        point_in_polygon(b, self)
    }
}
impl Inside for MultiPolygon3D {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        let mut res = InsideResult::Outside;
        for poly in self {
            res = res.get_dominant(poly.inside(b));
        }
        res
    }
}

// Vector Feature and below

impl<M, P: Clone + Default, D: Clone + Default> Inside for VectorFeature<M, P, D> {
    fn inside<B: GetXY>(&self, b: &B) -> InsideResult {
        self.geometry.inside(b)
    }
}
impl<M: Clone + Default> Inside for VectorGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        match self {
            VectorGeometry::Point(g) => g.inside(b),
            VectorGeometry::MultiPoint(g) => g.inside(b),
            VectorGeometry::LineString(g) => g.inside(b),
            VectorGeometry::MultiLineString(g) => g.inside(b),
            VectorGeometry::Polygon(g) => g.inside(b),
            VectorGeometry::MultiPolygon(g) => g.inside(b),
        }
    }
}
impl<M: Clone + Default> Inside for VectorPointGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for VectorMultiPointGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for VectorMultiLineStringGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}
impl<M: Clone + Default> Inside for VectorMultiPolygonGeometry<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        self.coordinates.inside(b)
    }
}

// Vector Point Types

impl<M: Clone + Default> Inside for VectorPoint<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        if self.x() == b.x() && self.y() == b.y() {
            InsideResult::Inside
        } else {
            InsideResult::Outside
        }
    }
}
impl<M: Clone + Default> Inside for VectorMultiPoint<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        point_in_polyline(b, self)
    }
}
impl<M: Clone + Default> Inside for VectorMultiLineString<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        point_in_polygon(b, self)
    }
}
impl<M: Clone + Default> Inside for VectorMultiPolygon<M> {
    fn inside<P: GetXY>(&self, b: &P) -> InsideResult {
        let mut res = InsideResult::Outside;
        for poly in self {
            res = res.get_dominant(poly.inside(b));
        }
        res
    }
}

/// A Robust point in polygon test
///
/// ## Parameters
/// - `point`: the point
/// - `polygon`: the polygon
///
/// ## Returns
/// true if the point is in the polygon, 0 if on the boundary, false otherwise
pub fn point_in_polygon<P1: GetXY, P2: GetXY>(point: &P1, polygon: &[Vec<P2>]) -> InsideResult {
    let mut k = 0;
    let mut f;
    let mut u1;
    let mut v1;
    let mut u2;
    let mut v2;
    let mut current_p: &P2;
    let mut next_p: &P2;

    let x = point.x();
    let y = point.y();
    for contour in polygon {
        let contour_len = contour.len() - 1;

        current_p = &contour[0];
        if current_p.x() != contour[contour_len].x() && current_p.y() != contour[contour_len].y() {
            // since the first and last coordinates in a ring are not the same, assume it's not a polygon and return false
            return InsideResult::Outside;
        }

        u1 = current_p.x() - x;
        v1 = current_p.y() - y;

        for ii in 0..contour_len {
            next_p = &contour[ii + 1];

            u2 = next_p.x() - x;
            v2 = next_p.y() - y;

            if v1 == 0. && v2 == 0. {
                if (u2 <= 0. && u1 >= 0.) || (u1 <= 0. && u2 >= 0.) {
                    return InsideResult::Boundary;
                }
            } else if (v2 >= 0. && v1 <= 0.) || (v2 <= 0. && v1 >= 0.) {
                f = orient2d(u1, u2, v1, v2, 0., 0.);
                if f == 0. {
                    return InsideResult::Boundary;
                }
                if (f > 0. && v2 > 0. && v1 <= 0.) || (f < 0. && v2 <= 0. && v1 > 0.) {
                    k += 1;
                }
            }
            // current_p = next_p;
            v1 = v2;
            u1 = u2;
        }
    }

    if k % 2 == 0 { InsideResult::Outside } else { InsideResult::Inside }
}

/// A Robust point in polygon test
///
/// ## Parameters
/// - `point`: the point
/// - `polygon`: the polygon
///
/// ## Returns
/// true if the point is in the polygon, 0 if on the boundary, false otherwise
pub fn point_in_polyline<P1: GetXY, P2: GetXY>(point: &P1, contour: &[P2]) -> InsideResult {
    let mut k = 0;
    let mut f;
    let mut u1;
    let mut v1;
    let mut u2;
    let mut v2;
    let mut next_p: &P2;

    let x = point.x();
    let y = point.y();
    let contour_len = contour.len() - 1;

    let current_p = &contour[0];
    if current_p.x() != contour[contour_len].x() && current_p.y() != contour[contour_len].y() {
        // since the first and last coordinates in a ring are not the same, assume it's not a polygon and return false
        return InsideResult::Outside;
    }

    u1 = current_p.x() - x;
    v1 = current_p.y() - y;

    for ii in 0..contour_len {
        next_p = &contour[ii + 1];

        u2 = next_p.x() - x;
        v2 = next_p.y() - y;

        if v1 == 0. && v2 == 0. {
            if (u2 <= 0. && u1 >= 0.) || (u1 <= 0. && u2 >= 0.) {
                return InsideResult::Boundary;
            }
        } else if (v2 >= 0. && v1 <= 0.) || (v2 <= 0. && v1 >= 0.) {
            f = orient2d(u1, u2, v1, v2, 0., 0.);
            if f == 0. {
                return InsideResult::Boundary;
            }
            if (f > 0. && v2 > 0. && v1 <= 0.) || (f < 0. && v2 <= 0. && v1 > 0.) {
                k += 1;
            }
        }
        // current_p = next_p;
        v1 = v2;
        u1 = u2;
    }

    if k % 2 == 0 { InsideResult::Outside } else { InsideResult::Inside }
}

/// Check if a polyline/hole is inside another polyline/outer ring
///
/// ## Parameters
/// - `hole`: the hole to test if inside the outer
/// - `outer`: the outer to test against
///
/// ## Returns
/// true if the hole is inside the outer
pub fn polyline_in_polyline<P: GetXY>(hole: &[P], outer: &[P]) -> bool {
    for point in hole {
        match point_in_polyline(point, outer) {
            InsideResult::Inside => return true,
            InsideResult::Outside => return false,
            InsideResult::Boundary => continue,
        }
    }
    // If we make it means all points of the hole were on the boundary therefore its inside the outer
    true
}