geometres 0.1.0

Exact, non-constructive computational geometry: validation, DE-9IM predicates and certified measurement over f64 coordinates
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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// src/types/geometry_collection.rs

//! Heterogeneous collection geometry (`GeometryCollection`).
//!
//! # Nesting, and why it is limited
//!
//! A collection may hold other collections, as in OGC Simple Features and
//! WKB. Its *depth* counts collection levels: a collection whose
//! elements are all non-collections has depth 1, and one holding such a
//! collection has depth 2. The empty collection has depth 1.
//!
//! **The depth is at most [`GeometryCollection::MAX_NESTING`], 32. This is a
//! permanent contract of the type and of the native format.**
//!
//! - **What the limit protects:** The derived `Clone`, `PartialEq` and
//!   `Debug`, and the compiler's drop glue for `Vec<Geometry>`, each recurse
//!   once per level, and so does any caller's code that matches its way down.
//!   With the depth bounded, the worst-case recursion anywhere is a few dozen
//!   small frames, on every thread stack and every target. This crate's own
//!   algorithms do not recurse at all: they walk a collection with
//!   [`GeometryCollection::leaves`] or an explicit stack.
//! - **Why it is checked at construction:** Every value that exists then also
//!   encodes and decodes, so the codec still round-trips. The decoder checks
//!   the same limit before it enters a level, so a hostile byte stream is
//!   refused before it can nest any deeper.
//! - **Why it never changes:** Lowering it would make existing values
//!   unconstructible and existing documents undecodable; raising it would
//!   make older decoders refuse the documents a newer encoder writes.
//!
//! # No condition between elements
//!
//! OGC imposes none: elements may overlap, repeat or touch. Every element is
//! valid by its own construction, so the only check here is the depth. The
//! measures over a collection (`ops::collection_area`,
//! `ops::collection_length`) are sums over its elements, so overlapping
//! elements count twice.

use crate::GeometryError;
use crate::topology::label::Dimension;
use crate::types::{Coord2d, Envelope, Geometry};

/// A collection of geometries of any types, including other collections,
/// nested at most [`MAX_NESTING`](Self::MAX_NESTING) levels deep.
///
/// # Invariants
///
/// - Every element is a valid geometry of its own type (its constructor
///   checked it), so every coordinate is finite.
/// - The depth (see the module documentation) is at most
///   [`MAX_NESTING`](Self::MAX_NESTING).
#[derive(Clone, Debug, PartialEq)]
pub struct GeometryCollection {
    geometries: Vec<Geometry>,
    /// The nesting depth, cached so that building a nest bottom-up costs
    /// `O(n)` rather than a walk of every child on each construction. A
    /// function of `geometries`, so the derived `PartialEq` stays correct.
    depth: u8,
}

impl GeometryCollection {
    /// The deepest nesting a collection may have: a permanent contract (see
    /// the module documentation).
    pub const MAX_NESTING: usize = 32;

    /// Construct a collection from its elements, in order.
    ///
    /// # Errors
    ///
    /// [`GeometryError::NestingTooDeep`] if the result would be more than
    /// [`MAX_NESTING`](Self::MAX_NESTING) collection levels deep, that is, if
    /// an element is a collection of depth `MAX_NESTING`.
    pub fn new(geometries: Vec<Geometry>) -> Result<Self, GeometryError> {
        let deepest_child = geometries
            .iter()
            .map(|g| match g {
                Geometry::GeometryCollection(c) => usize::from(c.depth),
                _ => 0,
            })
            .max()
            .unwrap_or(0);
        let depth = deepest_child + 1;
        if depth > Self::MAX_NESTING {
            return Err(GeometryError::NestingTooDeep {
                limit: Self::MAX_NESTING,
            });
        }
        // `depth <= MAX_NESTING = 32`, so it fits; the fallback is unreachable.
        let depth = u8::try_from(depth).unwrap_or(u8::MAX);
        Ok(Self { geometries, depth })
    }

    /// A collection whose depth is already known within the limit: a
    /// validated collection's elements, each replaced by one of the same
    /// nesting depth and permuted. Crate-private.
    pub(crate) fn from_valid(geometries: Vec<Geometry>, depth: usize) -> Self {
        debug_assert!((1..=Self::MAX_NESTING).contains(&depth));
        Self {
            geometries,
            depth: u8::try_from(depth).unwrap_or(u8::MAX),
        }
    }

    /// The collection with no elements, of depth 1.
    #[must_use]
    pub fn empty() -> Self {
        Self {
            geometries: Vec::new(),
            depth: 1,
        }
    }

    /// The elements, in construction order.
    #[must_use]
    pub fn geometries(&self) -> &[Geometry] {
        &self.geometries
    }

    /// The elements, by value.
    #[must_use]
    pub fn into_geometries(self) -> Vec<Geometry> {
        self.geometries
    }

    /// The number of direct elements (a nested collection counts as one).
    #[must_use]
    pub fn len(&self) -> usize {
        self.geometries.len()
    }

    /// The nesting depth: 1 for a collection of non-collections (or of
    /// nothing), one more for each level of collections inside it.
    #[must_use]
    pub fn depth(&self) -> usize {
        usize::from(self.depth)
    }

    /// `true` if the collection has no points at all: no elements, or only
    /// empty ones (an empty multipart geometry, or an empty collection), at
    /// any depth. OGC's `IsEmpty`.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.leaves().all(geometry_is_empty)
    }

    /// The highest dimension of any non-empty element at any depth, or
    /// [`Dimension::Empty`] if the collection [is empty](Self::is_empty).
    #[must_use]
    pub fn dimension(&self) -> Dimension {
        self.leaves()
            .filter(|g| !geometry_is_empty(g))
            .map(leaf_dimension)
            .max()
            .unwrap_or(Dimension::Empty)
    }

    /// The closed box of every element's coordinates, or `None` if the
    /// collection is empty.
    ///
    /// Each bound is an input coordinate, selected by exact comparison
    /// ([`Envelope::from_points`]); nothing is computed. A polygon
    /// contributes its exterior's vertices only, since its holes lie inside
    /// the exterior by construction.
    #[must_use]
    pub fn envelope(&self) -> Option<Envelope> {
        // Every coordinate is finite by the element invariants, so
        // `from_points` cannot return its non-finite error.
        Envelope::from_points(self.leaves().flat_map(leaf_coords)).unwrap_or(None)
    }

    /// The non-collection elements, depth first in element order, at every
    /// depth. Nested collections are entered, not yielded.
    ///
    /// Walks an explicit stack of at most [`MAX_NESTING`](Self::MAX_NESTING)
    /// slice iterators; it never recurses.
    #[must_use]
    pub fn leaves(&self) -> Leaves<'_> {
        Leaves {
            stack: vec![self.geometries.iter()],
        }
    }
}

/// The iterator of [`GeometryCollection::leaves`].
#[derive(Clone, Debug)]
pub struct Leaves<'a> {
    stack: Vec<core::slice::Iter<'a, Geometry>>,
}

impl<'a> Iterator for Leaves<'a> {
    type Item = &'a Geometry;

    fn next(&mut self) -> Option<&'a Geometry> {
        loop {
            let top = self.stack.last_mut()?;
            match top.next() {
                None => {
                    self.stack.pop();
                }
                Some(Geometry::GeometryCollection(c)) => self.stack.push(c.geometries.iter()),
                Some(g) => return Some(g),
            }
        }
    }
}

/// `true` for an empty multipart geometry or an empty collection.
fn geometry_is_empty(g: &Geometry) -> bool {
    match g {
        Geometry::Point(_) | Geometry::LineString(_) | Geometry::Polygon(_) => false,
        Geometry::MultiPoint(m) => m.is_empty(),
        Geometry::MultiLineString(m) => m.is_empty(),
        Geometry::MultiPolygon(m) => m.is_empty(),
        Geometry::GeometryCollection(c) => c.is_empty(),
    }
}

/// The dimension of a non-collection, non-empty element.
fn leaf_dimension(g: &Geometry) -> Dimension {
    match g {
        Geometry::Point(_) | Geometry::MultiPoint(_) => Dimension::Point,
        Geometry::LineString(_) | Geometry::MultiLineString(_) => Dimension::Line,
        Geometry::Polygon(_) | Geometry::MultiPolygon(_) => Dimension::Area,
        Geometry::GeometryCollection(c) => c.dimension(),
    }
}

/// Every coordinate of a non-collection element that bounds it: all of a
/// point's or line's, a polygon's exterior.
fn leaf_coords(g: &Geometry) -> Box<dyn Iterator<Item = Coord2d> + '_> {
    match g {
        Geometry::Point(p) => Box::new(core::iter::once(p.coord())),
        Geometry::MultiPoint(m) => Box::new(m.points().iter().map(crate::types::Point::coord)),
        Geometry::LineString(l) => Box::new(l.coords().as_slice().iter().copied()),
        Geometry::MultiLineString(m) => Box::new(
            m.line_strings()
                .iter()
                .flat_map(|l| l.coords().as_slice().iter().copied()),
        ),
        Geometry::Polygon(p) => Box::new(p.exterior().coords().as_slice().iter().copied()),
        Geometry::MultiPolygon(m) => Box::new(
            m.polygons()
                .iter()
                .flat_map(|p| p.exterior().coords().as_slice().iter().copied()),
        ),
        // `leaves` never yields a collection; an empty iterator keeps this
        // total rather than panicking.
        Geometry::GeometryCollection(_) => Box::new(core::iter::empty()),
    }
}

// ======================================================================
// Tests
// ======================================================================

#[cfg(test)]
#[allow(clippy::unwrap_used)]
// test-lint-posture: see the workspace lint comment in Cargo.toml
#[allow(
    clippy::float_cmp,
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap,
    clippy::range_plus_one,
    clippy::no_effect_underscore_binding
)]
mod tests {
    use super::*;
    use crate::types::{
        CoordSeq, LineString, LinearRing, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
    };

    fn pt(x: f64, y: f64) -> Geometry {
        Geometry::Point(Point::new(Coord2d::new(x, y)).unwrap())
    }

    fn line(pts: &[(f64, f64)]) -> Geometry {
        Geometry::LineString(
            LineString::new(CoordSeq::new(
                pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect(),
            ))
            .unwrap(),
        )
    }

    fn square(x0: f64, y0: f64, s: f64) -> Polygon {
        let pts = [(x0, y0), (x0 + s, y0), (x0 + s, y0 + s), (x0, y0 + s)];
        Polygon::new_simple(
            LinearRing::new(CoordSeq::new(
                pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect(),
            ))
            .unwrap(),
        )
        .unwrap()
    }

    /// A collection nested `depth` levels deep, a point at the bottom.
    fn nest(depth: usize) -> GeometryCollection {
        let mut c = GeometryCollection::new(vec![pt(1.0, 2.0)]).unwrap();
        for _ in 1..depth {
            c = GeometryCollection::new(vec![Geometry::GeometryCollection(c)]).unwrap();
        }
        c
    }

    #[test]
    fn construction_keeps_elements_in_order() {
        let gs = vec![pt(0.0, 0.0), line(&[(0.0, 0.0), (1.0, 1.0)])];
        let c = GeometryCollection::new(gs.clone()).unwrap();
        assert_eq!(c.geometries(), gs.as_slice());
        assert_eq!(c.len(), 2);
        assert_eq!(c.depth(), 1);
        assert_eq!(c.clone().into_geometries(), gs);
    }

    #[test]
    fn the_empty_collection_is_empty_of_depth_one_with_no_envelope() {
        let e = GeometryCollection::empty();
        assert_eq!(e, GeometryCollection::new(vec![]).unwrap());
        assert!(e.is_empty());
        assert_eq!(e.len(), 0);
        assert_eq!(e.depth(), 1);
        assert_eq!(e.dimension(), Dimension::Empty);
        assert_eq!(e.envelope(), None);
        assert_eq!(e.leaves().count(), 0);
    }

    #[test]
    fn a_collection_of_empty_elements_is_empty_at_any_depth() {
        let inner = GeometryCollection::new(vec![
            Geometry::MultiPoint(MultiPoint::empty()),
            Geometry::GeometryCollection(GeometryCollection::empty()),
        ])
        .unwrap();
        let c = GeometryCollection::new(vec![
            Geometry::MultiLineString(MultiLineString::empty()),
            Geometry::MultiPolygon(MultiPolygon::empty()),
            Geometry::GeometryCollection(inner),
        ])
        .unwrap();
        assert!(c.is_empty());
        assert_eq!(c.len(), 3);
        assert_eq!(c.depth(), 3);
        assert_eq!(c.dimension(), Dimension::Empty);
        assert_eq!(c.envelope(), None);
    }

    #[test]
    fn dimension_is_the_highest_non_empty_element() {
        let points = GeometryCollection::new(vec![pt(0.0, 0.0)]).unwrap();
        assert_eq!(points.dimension(), Dimension::Point);
        let mixed = GeometryCollection::new(vec![
            Geometry::GeometryCollection(points),
            line(&[(0.0, 0.0), (1.0, 0.0)]),
            // An empty areal element does not raise the dimension.
            Geometry::MultiPolygon(MultiPolygon::empty()),
        ])
        .unwrap();
        assert!(!mixed.is_empty());
        assert_eq!(mixed.dimension(), Dimension::Line);
        let with_area = GeometryCollection::new(vec![
            Geometry::GeometryCollection(mixed),
            Geometry::Polygon(square(0.0, 0.0, 1.0)),
        ])
        .unwrap();
        assert_eq!(with_area.dimension(), Dimension::Area);
    }

    #[test]
    fn the_envelope_selects_input_coordinates_across_depths() {
        let inner = GeometryCollection::new(vec![
            pt(-3.0, 7.0),
            Geometry::MultiPolygon(MultiPolygon::new(vec![square(10.0, -2.0, 1.0)]).unwrap()),
        ])
        .unwrap();
        let c = GeometryCollection::new(vec![
            line(&[(0.0, 0.0), (1.0, 1.0)]),
            Geometry::GeometryCollection(inner),
        ])
        .unwrap();
        let e = c.envelope().unwrap();
        assert_eq!(e.min(), Coord2d::new(-3.0, -2.0));
        assert_eq!(e.max(), Coord2d::new(11.0, 7.0));
    }

    #[test]
    fn leaves_are_depth_first_in_element_order() {
        let a = pt(1.0, 0.0);
        let b = pt(2.0, 0.0);
        let c = pt(3.0, 0.0);
        let d = pt(4.0, 0.0);
        let e = pt(5.0, 0.0);
        let deep = GeometryCollection::new(vec![c.clone()]).unwrap();
        let mid = GeometryCollection::new(vec![
            b.clone(),
            Geometry::GeometryCollection(deep),
            Geometry::GeometryCollection(GeometryCollection::empty()),
            d.clone(),
        ])
        .unwrap();
        let top = GeometryCollection::new(vec![
            a.clone(),
            Geometry::GeometryCollection(mid),
            e.clone(),
        ])
        .unwrap();
        let got: Vec<Geometry> = top.leaves().cloned().collect();
        assert_eq!(got, vec![a, b, c, d, e]);
        assert_eq!(top.depth(), 3);
    }

    #[test]
    fn nesting_is_accepted_at_the_limit_and_refused_one_beyond() {
        let at_limit = nest(GeometryCollection::MAX_NESTING);
        assert_eq!(at_limit.depth(), 32);
        assert_eq!(at_limit.leaves().count(), 1);
        let err = GeometryCollection::new(vec![Geometry::GeometryCollection(at_limit.clone())])
            .unwrap_err();
        assert_eq!(err, GeometryError::NestingTooDeep { limit: 32 });
        // A shallow sibling does not rescue a deep one, and order does not
        // matter.
        let err =
            GeometryCollection::new(vec![pt(0.0, 0.0), Geometry::GeometryCollection(at_limit)])
                .unwrap_err();
        assert_eq!(err, GeometryError::NestingTooDeep { limit: 32 });
    }

    #[test]
    fn depth_is_one_more_than_the_deepest_child() {
        let c = GeometryCollection::new(vec![
            Geometry::GeometryCollection(nest(2)),
            Geometry::GeometryCollection(nest(5)),
            pt(0.0, 0.0),
        ])
        .unwrap();
        assert_eq!(c.depth(), 6);
    }

    // --- measures (ops::measure) ---

    use crate::accum::Accum;
    use crate::ops::{area, collection_area, collection_length, length, polygon_length};

    fn holed() -> Polygon {
        let ring = |pts: &[(f64, f64)]| {
            LinearRing::new(CoordSeq::new(
                pts.iter().map(|&(x, y)| Coord2d::new(x, y)).collect(),
            ))
            .unwrap()
        };
        Polygon::new(
            ring(&[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0), (0.0, 4.0)]),
            vec![ring(&[(1.0, 1.0), (1.0, 2.0), (2.0, 2.0), (2.0, 1.0)])],
        )
        .unwrap()
    }

    #[test]
    fn polygon_length_counts_every_ring() {
        assert_eq!(polygon_length(&holed()), 16.0 + 4.0);
        assert_eq!(polygon_length(&square(0.0, 0.0, 1.0)), 4.0);
    }

    #[test]
    fn collection_measures_are_the_accum_sum_over_components_in_leaf_order() {
        let a = square(0.0, 0.0, 0.1);
        let b = holed();
        let c = square(10.0, 10.0, 0.3);
        let l1 = LineString::new(CoordSeq::new(vec![
            Coord2d::new(0.0, 0.0),
            Coord2d::new(0.1, 0.7),
        ]))
        .unwrap();
        let l2 = LineString::new(CoordSeq::new(vec![
            Coord2d::new(5.0, 5.0),
            Coord2d::new(5.3, 5.9),
            Coord2d::new(6.0, 5.0),
        ]))
        .unwrap();
        let inner = GeometryCollection::new(vec![
            Geometry::MultiPolygon(MultiPolygon::new(vec![b.clone(), c.clone()]).unwrap()),
            Geometry::MultiLineString(MultiLineString::new(vec![l2.clone()])),
            pt(9.0, 9.0),
        ])
        .unwrap();
        let gc = GeometryCollection::new(vec![
            Geometry::Polygon(a.clone()),
            Geometry::LineString(l1.clone()),
            Geometry::GeometryCollection(inner),
        ])
        .unwrap();

        let mut acc = Accum::new();
        for p in [&a, &b, &c] {
            acc.add(area(p));
        }
        assert_eq!(collection_area(&gc).to_bits(), acc.value().to_bits());

        let mut acc = Accum::new();
        acc.add(polygon_length(&a));
        acc.add(length(&l1));
        acc.add(polygon_length(&b));
        acc.add(polygon_length(&c));
        acc.add(length(&l2));
        assert_eq!(collection_length(&gc).to_bits(), acc.value().to_bits());
    }

    #[test]
    fn overlapping_elements_count_twice() {
        let p = Geometry::Polygon(square(0.0, 0.0, 2.0));
        let gc = GeometryCollection::new(vec![p.clone(), p]).unwrap();
        assert_eq!(collection_area(&gc), 8.0);
        assert_eq!(collection_length(&gc), 16.0);
    }

    #[test]
    fn a_one_polygon_collection_measures_exactly_as_the_polygon() {
        let p = holed();
        let gc = GeometryCollection::new(vec![Geometry::Polygon(p.clone())]).unwrap();
        assert_eq!(collection_area(&gc).to_bits(), area(&p).to_bits());
        assert_eq!(
            collection_length(&gc).to_bits(),
            polygon_length(&p).to_bits()
        );
    }

    #[test]
    fn measures_are_zero_exactly_when_there_is_nothing_to_measure() {
        let points = GeometryCollection::new(vec![
            pt(0.0, 0.0),
            Geometry::MultiPolygon(MultiPolygon::empty()),
            Geometry::GeometryCollection(GeometryCollection::empty()),
        ])
        .unwrap();
        assert_eq!(collection_area(&points), 0.0);
        assert_eq!(collection_length(&points), 0.0);
        assert_eq!(collection_area(&GeometryCollection::empty()), 0.0);
        let lines = GeometryCollection::new(vec![line(&[(0.0, 0.0), (3.0, 4.0)])]).unwrap();
        assert_eq!(collection_area(&lines), 0.0);
        assert_eq!(collection_length(&lines), 5.0);
        // A tiny polygon among large ones still makes the area nonzero.
        let tiny = square(1e6, 1e6, 1e-6);
        let only_tiny = GeometryCollection::new(vec![Geometry::Polygon(tiny)]).unwrap();
        assert!(collection_area(&only_tiny) > 0.0);
    }

    // --- the predicates on collections ---

    #[test]
    fn relate_accepts_collections_and_refuses_overlapping_polygon_leaves() {
        use crate::predicates::{dimension, intersects, relate};
        use crate::topology::de9im::IntersectionMatrix;
        // A singleton collection relates exactly as its element, on either
        // side, through every entry point.
        let gc = GeometryCollection::new(vec![Geometry::Polygon(square(0.0, 0.0, 1.0))]).unwrap();
        let p = square(0.0, 0.0, 1.0);
        let reference = IntersectionMatrix::compute(&p, &p);
        assert_eq!(relate(&gc, &p).unwrap().matrix(), reference);
        assert_eq!(relate(&p, &gc).unwrap().matrix(), reference);
        assert!(intersects(&Geometry::GeometryCollection(gc.clone()), &p).unwrap());
        assert_eq!(
            IntersectionMatrix::compute_geometry(
                &Geometry::Polygon(p.clone()),
                &Geometry::GeometryCollection(gc.clone())
            )
            .unwrap(),
            reference
        );
        let mask = crate::prepared::PreparedPolygonal::new(&p).unwrap();
        assert_eq!(
            mask.relate(&Geometry::GeometryCollection(gc.clone()))
                .unwrap(),
            reference
        );
        // Overlapping polygonal leaves are refused with both leaf paths.
        let overlapping = GeometryCollection::new(vec![
            Geometry::Polygon(square(0.0, 0.0, 2.0)),
            Geometry::Polygon(square(1.0, 1.0, 2.0)),
        ])
        .unwrap();
        assert_eq!(
            relate(&overlapping, &p).unwrap_err(),
            GeometryError::CollectionElementsOverlap {
                first: vec![0],
                second: vec![1],
            }
        );
        assert_eq!(dimension(&gc), Dimension::Area);
        assert_eq!(dimension(&GeometryCollection::empty()), Dimension::Empty);
    }
}