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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// src/ops/validate.rs
//! Validity enumeration: every reason a set of rings could not become a
//! `Polygon`, reported together rather than failing on the first.
//!
//! # What this is, and is not
//!
//! The constructors **reject**: `LinearRing::new` and `Polygon::new` return
//! the first defect they meet and nothing else. [`validate`] is the
//! complementary **non-rejecting** form for the same material: it runs every
//! per-ring check on every ring and returns all of the issues found, so a
//! caller fixing a geometry sees the whole list at once. An empty result
//! means the rings pass every check made here.
//!
//! The two are not duplicates. A `Polygon` that exists is already valid, so
//! `validate` cannot take one; it takes the raw material:
//!
//! - [`validate`] takes `&[CoordSeq]`, ring 0 the would-be exterior and
//! rings 1.. the would-be holes, and applies the orientation rule by
//! position.
//! - [`validate_line`] takes one `&CoordSeq` as a would-be `LineString`,
//! where the only conditions are finite coordinates, two or more vertices
//! and no consecutive repeat.
//!
//! `CoordSeq` is the one type in the crate that can hold anything a
//! constructor refuses (too few vertices, repeats, a self-crossing, either
//! winding, a non-finite coordinate) while still being coordinates and
//! nothing else: no record, schema, reference system or format.
//!
//! # The checks, per ring
//!
//! | Issue | The constructor's counterpart |
//! |---|---|
//! | [`ValidityIssue::NonFiniteCoordinate`] | `GeometryError::NonFiniteCoordinate` |
//! | [`ValidityIssue::TooFewPoints`] | `GeometryError::InsufficientVertices` |
//! | [`ValidityIssue::DuplicatePoint`] | `GeometryError::ConsecutiveDuplicateVertices` |
//! | [`ValidityIssue::NonFiniteArea`] | `GeometryError::NonFiniteResult` |
//! | [`ValidityIssue::DegenerateRing`] | `GeometryError::DegenerateRing` |
//! | [`ValidityIssue::RingOrientation`] | `GeometryError::IncorrectWindingOrder`, `IncorrectHoleWindingOrder` |
//! | [`ValidityIssue::SelfIntersection`] | none; the constructors do not test simplicity, and the overlay and simplify vouches do |
//!
//! # The checks, between rings
//!
//! | Issue | The constructor's counterpart |
//! |---|---|
//! | [`ValidityIssue::HoleOutsideExterior`] | `GeometryError::HoleNotInsideExterior` |
//! | [`ValidityIssue::RingsCross`] | `HoleNotInsideExterior` (against the exterior), `OverlappingHoles` (between holes) |
//! | [`ValidityIssue::DuplicateRings`] | `HoleNotInsideExterior`, `OverlappingHoles` |
//! | [`ValidityIssue::NestedHoles`] | `GeometryError::OverlappingHoles` |
//! | [`ValidityIssue::DisconnectedInterior`] | none; `Polygon::new` admits it (see below) |
//!
//! Each hole is placed against the exterior, then each pair of holes, in the
//! order `Polygon::new` checks them and by the same exact `place_ring`, so a
//! placement issue is reported if and only if `Polygon::new` refuses the
//! placement, and the first one names the hole or pair `Polygon::new` names.
//! Placement is skipped for a ring that is non-finite, has too few points,
//! has an area that is not a number, or is degenerate: it bounds no region
//! to place.
//!
//! **A disconnected interior is constructible.** `Polygon::new` accepts holes
//! that touch the exterior or each other at points, and a cycle of such
//! touches (a hole touching the exterior twice, or two holes touching each
//! other and each touching the exterior) cuts the interior in two. OGC calls
//! that polygon invalid; `Polygon::new` builds it, like a self-intersecting
//! ring, and [`validity_issues`] reports it. The test, its proof, and why it
//! counts touch points rather than ring pairs are in `ops/validate/between.rs`.
//! It runs only when every other check passes (an orientation issue aside):
//! its proof needs simple, correctly placed rings.
//!
//! # Multipolygons and constructed values
//!
//! [`validate_multipolygon`] runs [`validate`] on each part and adds the
//! issues between parts: [`ValidityIssue::PartsOverlap`] and
//! [`ValidityIssue::NestedParts`], the counterparts of
//! `GeometryError::OverlappingParts`.
//!
//! [`is_valid`] and [`validity_issues`] take a constructed geometry and
//! report what its constructor does not check: ring self-intersection and a
//! disconnected interior. Each issue carries the path of element and part
//! indices that leads to it ([`LocatedIssue`]).
//!
//! # Conventions, all the constructor's
//!
//! - A **closing vertex** (first repeated as last) is accepted and stripped,
//! as `LinearRing::new` does. It is not a `DuplicatePoint`. Validation
//! and construction follow one convention for the same ring.
//! - **Orientation** is the certified sign of the ring's area
//! (`LinearRing::signed_area`): ring 0 must be positive, every other
//! ring negative. An exactly zero area is a `DegenerateRing`, by the
//! same exact test `Polygon::new` applies, and no orientation is reported
//! for it.
//! - **Self-intersection** is the exact self-contact test of the vouches
//! (`exact::kernel::self_contacts`): every pair of edges that cross, touch,
//! pinch or fold back, by edge index. No intersection point is
//! constructed, and none is offered.
//!
//! # Indices
//!
//! Every issue names its `ring` (0 the exterior) and, where it applies, a
//! vertex or edge index into **that ring as given, less the closing vertex
//! if it had one**. When a ring has consecutive repeats, orientation and
//! self-contact are measured on the ring with the repeats removed (the
//! repeats are already reported), and edge indices are mapped back: edge `e`
//! is the edge that leaves vertex `e` toward the next *distinct* vertex.
//!
//! # What is skipped
//!
//! A ring with a non-finite coordinate gets that issue for each offending
//! vertex and nothing else: area and orientation of such a ring are not
//! numbers. A ring with fewer than three distinct vertices gets
//! `TooFewPoints` (and any `DuplicatePoint`s) and nothing else: it has no
//! area and no pair of non-adjacent edges.
pub use disconnected_interior;
pub use ;
pub use validate_multipolygon;
use crateself_contacts;
use cratering_is_degenerate;
use crate;
use Prepared;
// ===========================================================================
// The issue type
// ===========================================================================
/// One reason a ring could not become part of a valid `Polygon` or
/// `LineString`. Returned by [`validate`] and [`validate_line`], as many as
/// are found.
///
/// Every variant is a statement about coordinates and indices into the
/// caller's input. Nothing here names a record, a schema, a reference system
/// or a format; a consumer that wants per-feature reporting wraps a
/// `Vec<ValidityIssue>` with its own identifier.
// ===========================================================================
// Entry points
// ===========================================================================
/// Every issue that would keep `rings` from becoming a `Polygon`: ring 0 is
/// the would-be exterior, rings 1.. the would-be holes.
///
/// Returns an empty `Vec` when the rings pass every check (the module doc
/// lists them). The per-ring issues come first, in ring order and within a
/// ring in the order of the module doc's first table; then the placement
/// issues, in `Polygon::new`'s order (each hole against the exterior, then
/// each pair of holes); then a disconnected interior, if every other check
/// passed (an orientation issue aside).
///
/// An empty slice has no rings and no issues: `Polygon::new` is not reached
/// with nothing, and there is nothing to report.
/// Every issue that would keep `line` from becoming a `LineString`: a
/// non-finite coordinate, fewer than two vertices, a consecutive repeat.
///
/// A line is not closed, so a last vertex equal to the first is an ordinary
/// pair of vertices; and a line may cross itself, so no self-contact is
/// reported. Issues name ring 0.
// ===========================================================================
// Per-ring checks
// ===========================================================================
/// Pushes every `NonFiniteCoordinate` of `v`; `true` if there was one.
/// The ring without the OGC closing vertex, as `LinearRing::new` sees it.
/// Runs every per-ring check on `coords` as ring `ring`, appending to
/// `issues`, and returns the ring prepared for placement, or `None` if it
/// bounds no region to place (non-finite, too few points, an area that is
/// not a number, degenerate).