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
use super::{CoordIndex, RingRole, Validation, utils};
use crate::coordinate_position::CoordPos;
use crate::dimensions::Dimensions;
use crate::{GeoFloat, HasDimensions, Polygon, PreparedGeometry, Relate};
use std::fmt;
/// A [`Polygon`] must follow these rules to be valid:
/// - [x] the polygon boundary rings (the exterior shell ring and interior hole rings) are simple (do not cross or self-touch). Because of this a polygon cannnot have cut lines, spikes or loops. This implies that polygon holes must be represented as interior rings, rather than by the exterior ring self-touching (a so-called "inverted hole").
/// - [x] boundary rings do not cross
/// - [x] boundary rings may touch at points but only as a tangent (i.e. not in a line)
/// - [x] interior rings are contained in the exterior ring
/// - [ ] the polygon interior is simply connected (i.e. the rings must not touch in a way that splits the polygon into more than one part)
///
/// Note: the simple connectivity of the interior is not checked by this implementation.
#[derive(Debug, Clone, PartialEq)]
pub enum InvalidPolygon {
/// A ring must have at least 4 points to be valid. Note that, in order to close the ring, the first and final points will be identical.
TooFewPointsInRing(RingRole),
/// A ring has a self-intersection.
SelfIntersection(RingRole),
/// One of the Polygon's coordinates is non-finite.
NonFiniteCoord(RingRole, CoordIndex),
/// A polygon's interiors must be completely within its exterior.
InteriorRingNotContainedInExteriorRing(RingRole),
/// A valid polygon's rings must not intersect one another. In this case, the intersection is 1-dimensional.
IntersectingRingsOnALine(RingRole, RingRole),
/// A valid polygon's rings must not intersect one another. In this case, the intersection is 2-dimensional.
IntersectingRingsOnAnArea(RingRole, RingRole),
}
impl fmt::Display for InvalidPolygon {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InvalidPolygon::TooFewPointsInRing(ring) => {
write!(f, "{ring} must have at least 3 distinct points")
}
InvalidPolygon::SelfIntersection(ring) => {
write!(f, "{ring} has a self-intersection")
}
InvalidPolygon::NonFiniteCoord(ring, idx) => {
write!(f, "{ring} has a non-finite coordinate at index {}", idx.0)
}
InvalidPolygon::InteriorRingNotContainedInExteriorRing(ring) => {
write!(f, "{ring} is not contained within the polygon's exterior")
}
InvalidPolygon::IntersectingRingsOnALine(ring_1, ring_2) => {
write!(f, "{ring_1} and {ring_2} intersect on a line")
}
InvalidPolygon::IntersectingRingsOnAnArea(ring_1, ring_2) => {
write!(f, "{ring_1} and {ring_2} intersect on an area")
}
}
}
}
impl std::error::Error for InvalidPolygon {}
impl<F: GeoFloat> Validation for Polygon<F> {
type Error = InvalidPolygon;
fn visit_validation<T>(
&self,
mut handle_validation_error: Box<dyn FnMut(Self::Error) -> Result<(), T> + '_>,
) -> Result<(), T> {
if self.is_empty() {
return Ok(());
}
for (ring_idx, ring) in std::iter::once(self.exterior())
.chain(self.interiors().iter())
.enumerate()
{
if ring.is_empty() {
continue;
}
let ring_role = if ring_idx == 0 {
RingRole::Exterior
} else {
RingRole::Interior(ring_idx - 1)
};
// Perform the various checks
if utils::check_too_few_points(ring, true) {
handle_validation_error(InvalidPolygon::TooFewPointsInRing(ring_role))?;
}
if utils::linestring_has_self_intersection(ring) {
handle_validation_error(InvalidPolygon::SelfIntersection(ring_role))?;
}
for (coord_idx, coord) in ring.0.iter().enumerate() {
if utils::check_coord_is_not_finite(coord) {
handle_validation_error(InvalidPolygon::NonFiniteCoord(
ring_role,
CoordIndex(coord_idx),
))?;
}
}
}
// Skip interior checks if there are no non-empty interiors
let has_interiors = self.interiors().iter().any(|ring| !ring.is_empty());
if !has_interiors {
return Ok(());
}
// Use PreparedGeometry for the exterior to cache its R-tree, avoiding
// graph reconstruction for each interior containment check.
let polygon_exterior = Polygon::new(self.exterior().clone(), vec![]);
let prepared_exterior = PreparedGeometry::from(&polygon_exterior);
for (interior_1_idx, interior_1) in self.interiors().iter().enumerate() {
let ring_role_1 = RingRole::Interior(interior_1_idx);
if interior_1.is_empty() {
continue;
}
let interior_1_as_poly = Polygon::new(interior_1.clone(), vec![]);
let prepared_interior_1 = PreparedGeometry::from(&interior_1_as_poly);
let exterior_vs_interior = prepared_exterior.relate(&prepared_interior_1);
if !exterior_vs_interior.is_contains() {
handle_validation_error(InvalidPolygon::InteriorRingNotContainedInExteriorRing(
ring_role_1,
))?;
}
// Interior ring and exterior ring may only touch at point (not as a line)
if exterior_vs_interior.get(CoordPos::OnBoundary, CoordPos::OnBoundary)
== Dimensions::OneDimensional
{
handle_validation_error(InvalidPolygon::IntersectingRingsOnALine(
RingRole::Exterior,
ring_role_1,
))?;
}
for (interior_2_idx, interior_2) in
self.interiors().iter().enumerate().skip(interior_1_idx + 1)
{
let ring_role_2 = RingRole::Interior(interior_2_idx);
if interior_2.is_empty() {
continue;
}
let interior_2_as_poly = Polygon::new(interior_2.clone(), vec![]);
let intersection_matrix = prepared_interior_1.relate(&interior_2_as_poly);
if intersection_matrix.get(CoordPos::Inside, CoordPos::Inside)
== Dimensions::TwoDimensional
{
handle_validation_error(InvalidPolygon::IntersectingRingsOnAnArea(
ring_role_1,
ring_role_2,
))?;
}
if intersection_matrix.get(CoordPos::OnBoundary, CoordPos::OnBoundary)
== Dimensions::OneDimensional
{
handle_validation_error(InvalidPolygon::IntersectingRingsOnALine(
ring_role_1,
ring_role_2,
))?;
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::algorithm::validation::{assert_valid, assert_validation_errors};
use crate::wkt;
#[test]
fn test_polygon_valid() {
// Unclosed rings are automatically closed by geo_types
// so the following should be valid
let polygon = wkt!(
POLYGON((0. 0., 1. 1., 0. 1.))
);
assert_valid!(&polygon);
}
#[test]
fn test_polygon_valid_interior_ring_touches_exterior_ring() {
// The following polygon contains an interior ring that touches
// the exterior ring on one point.
// This is valid according to the OGC spec.
let polygon = wkt!(
POLYGON(
(0. 0., 4. 1., 4. 4.,0. 4.,0. 0.),
(0. 2., 2. 1., 3. 2., 2. 3., 0. 2.)
)
);
assert_valid!(&polygon);
}
#[test]
fn test_polygon_valid_interior_rings_touch_at_point() {
// The following polygon contains two interior rings that touch
// at one point.
let polygon = wkt!(
POLYGON(
(0. 0., 4. 0., 4. 4.,0. 4.,0. 0.),
(1. 2., 2. 1., 3. 2., 2. 3., 1. 2.),
(3. 2., 3.5 1., 3.75 2., 3.5 3., 3. 2.)
)
);
assert_valid!(&polygon);
}
#[test]
fn test_polygon_invalid_interior_rings_touch_at_line() {
// The following polygon contains two interior rings that touch
// on a line, this is not valid.
let polygon = wkt!(
POLYGON(
(0. 0., 4. 0., 4. 4.,0. 4.,0. 0.),
(1. 2., 2. 1., 3. 2., 2. 3., 1. 2.),
(3. 2., 2. 1., 3.5 1., 3.75 2., 3.5 3., 3. 2.)
)
);
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::IntersectingRingsOnALine(
RingRole::Interior(0),
RingRole::Interior(1)
)]
);
}
#[test]
fn test_polygon_invalid_interior_rings_crosses() {
// The following polygon contains two interior rings that cross
// each other (they share some common area), this is not valid.
let polygon = wkt!(
POLYGON(
(0. 0., 4. 0., 4. 4., 0. 4., 0. 0.),
(1. 2., 2. 1., 3. 2., 2. 3., 1. 2.),
(2. 2., 2. 1., 3.5 1., 3.75 2., 3.5 3., 3. 2.)
)
);
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::IntersectingRingsOnAnArea(
RingRole::Interior(0),
RingRole::Interior(1)
)]
);
}
#[test]
fn test_polygon_invalid_interior_ring_touches_exterior_ring_as_line() {
// The following polygon contains an interior ring that touches
// the exterior ring on one point.
// This is valid according to the OGC spec.
let polygon = wkt!(
POLYGON(
(0. 0., 4. 0., 4. 4., 0. 4., 0. 0.),
// First two points are on the exterior ring
(0. 2., 0. 1., 2. 1., 3. 2., 2. 3., 0. 2.)
)
);
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::IntersectingRingsOnALine(
RingRole::Exterior,
RingRole::Interior(0)
)]
);
}
#[test]
fn test_polygon_invalid_too_few_point_exterior_ring() {
// Unclosed rings are automatically closed by geo_types
// but there is still two few points in this ring
// to be a non-empty polygon
let polygon = wkt!( POLYGON((0. 0., 1. 1.)) );
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::TooFewPointsInRing(RingRole::Exterior)]
);
}
#[test]
fn test_polygon_invalid_spike() {
// The following polygon contains a spike
let polygon = wkt!(
POLYGON(
(0. 0., 4. 0., 4. 4., 2. 4., 2. 6., 2. 4., 0. 4., 0. 0.)
)
);
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::SelfIntersection(RingRole::Exterior)]
);
}
#[test]
fn test_polygon_invalid_exterior_is_not_simple() {
// The exterior ring of this polygon is not simple (i.e. it has a self-intersection)
let polygon = wkt!(
POLYGON((0. 0., 4. 0., 0. 2., 4. 2., 0. 0.))
);
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::SelfIntersection(RingRole::Exterior)]
);
}
#[test]
fn test_polygon_invalid_interior_not_fully_contained_in_exterior() {
let polygon = wkt!(
POLYGON (
(0.5 0.5, 3.0 0.5, 3.0 2.5, 0.5 2.5, 0.5 0.5),
(1.0 1.0, 1.0 2.0, 2.5 2.0, 3.5 1.0, 1.0 1.0)
)
);
assert_validation_errors!(
&polygon,
vec![InvalidPolygon::InteriorRingNotContainedInExteriorRing(
RingRole::Interior(0)
),]
);
}
#[test]
fn test_polygon_invalid_interior_ring_contained_in_interior_ring() {
// The following polygon contains an interior ring that is contained
// in another interior ring.
let polygon_1 = wkt!(
POLYGON(
(0. 0., 10. 0., 10. 10., 0. 10., 0. 0.),
(1. 1., 1. 9., 9. 9., 9. 1., 1. 1.),
(2. 2., 2. 8., 8. 8., 8. 2., 2. 2.)
)
);
assert_validation_errors!(
polygon_1,
vec![InvalidPolygon::IntersectingRingsOnAnArea(
RingRole::Interior(0),
RingRole::Interior(1)
)]
);
// Let see if we switch the order of the interior rings
// (this is still invalid)
let polygon_2 = wkt!(
POLYGON(
(0. 0., 10. 0., 10. 10., 0. 10., 0. 0.),
(2. 2., 2. 8., 8. 8., 8. 2., 2. 2.),
(1. 1., 1. 9., 9. 9., 9. 1., 1. 1.)
)
);
assert_validation_errors!(
polygon_2,
vec![InvalidPolygon::IntersectingRingsOnAnArea(
RingRole::Interior(0),
RingRole::Interior(1)
)]
);
}
}