geoarrow-cast 0.7.0

Functions for converting from one GeoArrow geometry type to another.
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
//! Cast kernels to convert [`GeoArrowArray`] to other geometry types.

use std::sync::Arc;

use arrow_schema::ArrowError;
use geoarrow_array::array::{
    GeometryArray, MultiLineStringArray, MultiPointArray, MultiPolygonArray,
};
use geoarrow_array::builder::{
    GeometryCollectionBuilder, LineStringBuilder, MultiLineStringBuilder, MultiPointBuilder,
    MultiPolygonBuilder, PointBuilder, PolygonBuilder,
};
use geoarrow_array::capacity::{LineStringCapacity, PolygonCapacity};
use geoarrow_array::cast::{
    AsGeoArrowArray, from_wkb, from_wkt, to_wkb, to_wkb_view, to_wkt, to_wkt_view,
};
use geoarrow_array::{GeoArrowArray, GeoArrowArrayAccessor};
use geoarrow_schema::GeoArrowType;
use geoarrow_schema::error::GeoArrowResult;

/// Cast a [`GeoArrowArray`] to another [`GeoArrowType`].
///
/// ### Criteria:
///
/// - Dimension must be compatible:
///     - If the source array and destination type are both dimension-aware, then their dimensions
///       must match.
///     - Casts from dimension-aware to dimensionless arrays (`GeometryArray`, `WkbArray`,
///       `WkbViewArray`, `WktArray`, `WktViewArray`) are always allowed.
/// - GeoArrow [`Metadata`][geoarrow_schema::Metadata] on the [`GeoArrowType`] must match. Use
///   [`GeoArrowArray::with_metadata`]
///   to change the metadata on an array.
///
/// ### Infallible casts:
///
/// As long as the above criteria are met, these casts will always succeed without erroring.
///
/// - The same geometry type with different coord types.
/// - Any source array type to `Geometry`, `Wkb`, `LargeWkb`, `WkbView`, `Wkt`, `LargeWkt`, or
///   `WktView`.
/// - `Point` to `MultiPoint`
/// - `LineString` to `MultiLineString`
/// - `Polygon` to `MultiPolygon`
///
/// ### Fallible casts:
///
/// - `Geometry` to any other native type.
/// - Parsing `WKB` or `WKT` to any native type other than `Geometry`.
/// - `MultiPoint` to `Point`
/// - `MultiLineString` to `LineString`
/// - `MultiPolygon` to `Polygon`
///
// TODO: need to check this behavior:
//
//     - Casts from dimensionless arrays to dimension-aware arrays are never allowed.
pub fn cast(
    array: &dyn GeoArrowArray,
    to_type: &GeoArrowType,
) -> GeoArrowResult<Arc<dyn GeoArrowArray>> {
    // We want to error if the dimensions aren't compatible, but allow conversions to
    // `GeometryArray`, `WKB`, etc where the target array isn't parameterized by a specific
    // dimension.
    if let (Some(from_dim), Some(to_dim)) = (array.data_type().dimension(), to_type.dimension()) {
        if from_dim != to_dim {
            return Err(ArrowError::CastError(format!(
                "Cannot cast from {from_dim:?} to {to_dim:?}: incompatible dimensions",
            ))
            .into());
        }
    }

    if array.data_type().metadata() != to_type.metadata() {
        return Err(ArrowError::CastError(format!(
            "Cannot cast from {:?} to {:?}: incompatible metadata",
            array.data_type().metadata(),
            to_type.metadata(),
        ))
        .into());
    }

    use GeoArrowType::*;
    let out: Arc<dyn GeoArrowArray> = match (array.data_type(), to_type) {
        (Point(_), Point(to_type)) => {
            let array = array.as_point();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (Point(_), MultiPoint(to_type)) => {
            let mp_array = MultiPointArray::from(array.as_point().clone());
            Arc::new(mp_array.into_coord_type(to_type.coord_type()))
        }
        (Point(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_point().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (LineString(_), LineString(to_type)) => {
            let array = array.as_line_string();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (LineString(_), MultiLineString(to_type)) => {
            let mp_array = MultiLineStringArray::from(array.as_line_string().clone());
            Arc::new(mp_array.into_coord_type(to_type.coord_type()))
        }
        (LineString(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_line_string().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (Polygon(_), Polygon(to_type)) => {
            let array = array.as_polygon();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (Polygon(_), MultiPolygon(to_type)) => {
            let mp_array = MultiPolygonArray::from(array.as_polygon().clone());
            Arc::new(mp_array.into_coord_type(to_type.coord_type()))
        }
        (Polygon(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_polygon().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (MultiPoint(_), Point(to_type)) => {
            let mut builder = PointBuilder::with_capacity(to_type.clone(), array.len());
            for geom in array.as_multi_point().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (MultiPoint(_), MultiPoint(to_type)) => {
            let array = array.as_multi_point();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (MultiPoint(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_multi_point().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (MultiLineString(_), LineString(to_type)) => {
            let ml_array = array.as_multi_line_string();
            let ml_capacity = ml_array.buffer_lengths();
            let ls_capacity =
                LineStringCapacity::new(ml_capacity.coord_capacity(), ml_capacity.geom_capacity());
            let mut builder = LineStringBuilder::with_capacity(to_type.clone(), ls_capacity);
            for geom in array.as_multi_line_string().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (MultiLineString(_), MultiLineString(to_type)) => {
            let array = array.as_multi_line_string();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (MultiLineString(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_multi_line_string().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (MultiPolygon(_), Polygon(to_type)) => {
            let mp_array = array.as_multi_polygon();
            let mp_capacity = mp_array.buffer_lengths();
            let p_capacity = PolygonCapacity::new(
                mp_capacity.coord_capacity(),
                mp_capacity.ring_capacity(),
                mp_capacity.geom_capacity(),
            );
            let mut builder = PolygonBuilder::with_capacity(to_type.clone(), p_capacity);
            for geom in mp_array.iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (MultiPolygon(_), MultiPolygon(to_type)) => {
            let array = array.as_multi_polygon();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (MultiPolygon(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_multi_polygon().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (Geometry(_), Point(to_type)) => {
            let mut builder = PointBuilder::with_capacity(to_type.clone(), array.len());
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), LineString(to_type)) => {
            let g_array = array.as_geometry();
            let g_capacity = g_array.buffer_lengths();
            let ls_capacity = g_capacity.line_string(to_type.dimension());
            let mut builder = LineStringBuilder::with_capacity(to_type.clone(), ls_capacity);
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), Polygon(to_type)) => {
            let g_array = array.as_geometry();
            let g_capacity = g_array.buffer_lengths();
            let p_capacity = g_capacity.polygon(to_type.dimension());
            let mut builder = PolygonBuilder::with_capacity(to_type.clone(), p_capacity);
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), MultiPoint(to_type)) => {
            let g_array = array.as_geometry();
            let g_capacity = g_array.buffer_lengths();
            let mp_capacity = g_capacity.multi_point(to_type.dimension());
            let mut builder = MultiPointBuilder::with_capacity(to_type.clone(), mp_capacity);
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), MultiLineString(to_type)) => {
            let g_array = array.as_geometry();
            let g_capacity = g_array.buffer_lengths();
            let ml_capacity = g_capacity.multi_line_string(to_type.dimension());
            let mut builder = MultiLineStringBuilder::with_capacity(to_type.clone(), ml_capacity);
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), MultiPolygon(to_type)) => {
            let g_array = array.as_geometry();
            let g_capacity = g_array.buffer_lengths();
            let mp_capacity = g_capacity.multi_polygon(to_type.dimension());
            let mut builder = MultiPolygonBuilder::with_capacity(to_type.clone(), mp_capacity);
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), GeometryCollection(to_type)) => {
            let g_array = array.as_geometry();
            let g_capacity = g_array.buffer_lengths();
            let gc_capacity = g_capacity.geometry_collection(to_type.dimension());
            let mut builder =
                GeometryCollectionBuilder::with_capacity(to_type.clone(), gc_capacity);
            for geom in array.as_geometry().iter() {
                builder.push_geometry(geom.transpose()?.as_ref())?;
            }
            Arc::new(builder.finish())
        }
        (Geometry(_), Geometry(to_type)) => {
            let array = array.as_geometry();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (GeometryCollection(_), GeometryCollection(to_type)) => {
            let array = array.as_geometry_collection();
            Arc::new(array.clone().into_coord_type(to_type.coord_type()))
        }
        (GeometryCollection(_), Geometry(to_type)) => {
            let geom_array = GeometryArray::from(array.as_geometry_collection().clone());
            Arc::new(geom_array.into_coord_type(to_type.coord_type()))
        }
        (_, Wkb(_)) => Arc::new(to_wkb::<i32>(array)?),
        (_, LargeWkb(_)) => Arc::new(to_wkb::<i64>(array)?),
        (_, WkbView(_)) => Arc::new(to_wkb_view(array)?),
        (_, Wkt(_)) => Arc::new(to_wkt::<i32>(array)?),
        (_, LargeWkt(_)) => Arc::new(to_wkt::<i64>(array)?),
        (_, WktView(_)) => Arc::new(to_wkt_view(array)?),
        (Wkb(_), _) => from_wkb(array.as_wkb::<i32>(), to_type.clone())?,
        (LargeWkb(_), _) => from_wkb(array.as_wkb::<i64>(), to_type.clone())?,
        (WkbView(_), _) => from_wkb(array.as_wkb_view(), to_type.clone())?,
        (Wkt(_), _) => from_wkt(array.as_wkt::<i32>(), to_type.clone())?,
        (LargeWkt(_), _) => from_wkt(array.as_wkt::<i64>(), to_type.clone())?,
        (WktView(_), _) => from_wkt(array.as_wkt_view(), to_type.clone())?,
        (_, _) => {
            return Err(ArrowError::CastError(format!(
                "Unsupported cast from {:?} to {:?}",
                array.data_type(),
                to_type
            ))
            .into());
        }
    };
    Ok(out)
}

#[cfg(test)]
mod test {
    use geoarrow_array::builder::MultiPointBuilder;
    use geoarrow_array::{IntoArrow, test};
    use geoarrow_schema::{
        CoordType, Dimension, GeometryType, LineStringType, MultiLineStringType, MultiPointType,
        MultiPolygonType, PointType, PolygonType, WkbType,
    };
    use wkt::wkt;

    use super::*;

    #[test]
    fn test_point() {
        let array = test::point::array(CoordType::Interleaved, Dimension::XY);

        // Cast to the same type
        let array2 = cast(&array, &array.data_type()).unwrap();
        assert_eq!(&array, array2.as_point());

        // Cast to other coord type
        let p_type = PointType::new(Dimension::XY, array.data_type().metadata().clone())
            .with_coord_type(CoordType::Separated);
        let array3 = cast(&array, &p_type.into()).unwrap();
        assert_eq!(
            array3.as_point().extension_type().coord_type(),
            CoordType::Separated
        );

        // Cast to multi point
        let mp_type = MultiPointType::new(Dimension::XY, array.data_type().metadata().clone())
            .with_coord_type(CoordType::Interleaved);
        let mp_array = cast(&array, &mp_type.into()).unwrap();
        assert!(mp_array.as_multi_point_opt().is_some());

        // Cast to geometry
        let mp_type = GeometryType::new(array.data_type().metadata().clone())
            .with_coord_type(CoordType::Interleaved);
        let mp_array = cast(&array, &mp_type.into()).unwrap();
        assert!(mp_array.as_geometry_opt().is_some());
    }

    #[test]
    fn cast_to_wkb() {
        let array = test::point::array(CoordType::Interleaved, Dimension::XY);

        let wkb_type = GeoArrowType::Wkb(WkbType::new(array.data_type().metadata().clone()));
        let wkb_array = cast(&array, &wkb_type).unwrap();
        assert!(wkb_array.as_wkb_opt::<i32>().is_some());

        let large_wkb_type =
            GeoArrowType::LargeWkb(WkbType::new(array.data_type().metadata().clone()));
        let wkb_array = cast(&array, &large_wkb_type).unwrap();
        assert!(wkb_array.as_wkb_opt::<i64>().is_some());
    }

    #[test]
    fn downcast_multi_points_to_points() {
        let mp1 = wkt! { MULTIPOINT(0.0 0.0) };
        let mp2 = wkt! { MULTIPOINT(1.0 2.0) };
        let mp3 = wkt! { MULTIPOINT(3.0 4.0) };

        let typ = MultiPointType::new(Dimension::XY, Default::default());
        let mp_arr = MultiPointBuilder::from_multi_points(&[mp1, mp2, mp3], typ).finish();
        let (coord_type, dim, metadata) = mp_arr.extension_type().clone().into_inner();
        let p_type = PointType::new(dim, metadata).with_coord_type(coord_type);
        let p_arr = cast(&mp_arr, &p_type.into()).unwrap();
        assert!(p_arr.as_point_opt().is_some());
    }

    #[test]
    fn downcast_multi_points_to_points_fails() {
        let mp1 = wkt! { MULTIPOINT(0.0 0.0) };
        let mp2 = wkt! { MULTIPOINT(1.0 2.0) };
        let mp3 = wkt! { MULTIPOINT(3.0 4.0, 5.0 6.0) };

        let typ = MultiPointType::new(Dimension::XY, Default::default());
        let mp_arr = MultiPointBuilder::from_multi_points(&[mp1, mp2, mp3], typ).finish();
        let (coord_type, dim, metadata) = mp_arr.extension_type().clone().into_inner();
        let p_type = PointType::new(dim, metadata).with_coord_type(coord_type);
        assert!(cast(&mp_arr, &p_type.into()).is_err());
    }

    #[test]
    fn downcast_multi_line_strings_to_line_strings() {
        let geoms = geoarrow_test::raw::multilinestring::xy::geoms();
        let single = geoms[0].clone().unwrap();

        let typ = MultiLineStringType::new(Dimension::XY, Default::default());
        let mp_arr = MultiLineStringBuilder::from_multi_line_strings(
            &[single.clone(), single.clone(), single],
            typ,
        )
        .finish();
        let (coord_type, dim, metadata) = mp_arr.extension_type().clone().into_inner();
        let p_type = LineStringType::new(dim, metadata).with_coord_type(coord_type);
        let p_arr = cast(&mp_arr, &p_type.into()).unwrap();
        assert!(p_arr.as_line_string_opt().is_some());
    }

    #[test]
    fn downcast_multi_line_strings_to_line_strings_fails() {
        let geoms = geoarrow_test::raw::multilinestring::xy::geoms();
        let single = geoms[0].clone().unwrap();
        let multi = geoms[1].clone().unwrap();

        let typ = MultiLineStringType::new(Dimension::XY, Default::default());
        let mp_arr =
            MultiLineStringBuilder::from_multi_line_strings(&[single.clone(), single, multi], typ)
                .finish();
        let (coord_type, dim, metadata) = mp_arr.extension_type().clone().into_inner();
        let p_type = LineStringType::new(dim, metadata).with_coord_type(coord_type);
        assert!(cast(&mp_arr, &p_type.into()).is_err());
    }

    #[test]
    fn downcast_multi_polygons_to_polygons() {
        let geoms = geoarrow_test::raw::multipolygon::xy::geoms();
        let single = geoms[0].clone().unwrap();

        let typ = MultiPolygonType::new(Dimension::XY, Default::default());
        let mp_arr = MultiPolygonBuilder::from_multi_polygons(
            &[single.clone(), single.clone(), single],
            typ,
        )
        .finish();
        let (coord_type, dim, metadata) = mp_arr.extension_type().clone().into_inner();
        let p_type = PolygonType::new(dim, metadata).with_coord_type(coord_type);
        let p_arr = cast(&mp_arr, &p_type.into()).unwrap();
        assert!(p_arr.as_polygon_opt().is_some());
    }

    #[test]
    fn downcast_multi_polygons_to_polygons_fails() {
        let geoms = geoarrow_test::raw::multipolygon::xy::geoms();
        let single = geoms[0].clone().unwrap();
        let multi = geoms[1].clone().unwrap();

        let typ = MultiPolygonType::new(Dimension::XY, Default::default());
        let mp_arr =
            MultiPolygonBuilder::from_multi_polygons(&[single.clone(), single, multi], typ)
                .finish();
        let (coord_type, dim, metadata) = mp_arr.extension_type().clone().into_inner();
        let p_type = PolygonType::new(dim, metadata).with_coord_type(coord_type);
        assert!(cast(&mp_arr, &p_type.into()).is_err());
    }

    #[test]
    fn downcast_geometry_to_point() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                let array = test::point::array(coord_type, dim);
                let orig_type = array.data_type().clone();
                let g_array = GeometryArray::from(array.clone());

                let casted = cast(&g_array, &orig_type).unwrap();
                assert_eq!(casted.as_point(), &array);
            }
        }
    }

    #[test]
    fn downcast_geometry_to_line_string() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                let array = test::linestring::array(coord_type, dim);
                let orig_type = array.data_type().clone();
                let g_array = GeometryArray::from(array.clone());

                let casted = cast(&g_array, &orig_type).unwrap();
                assert_eq!(casted.as_line_string(), &array);
            }
        }
    }

    #[test]
    fn downcast_geometry_to_polygon() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                let array = test::polygon::array(coord_type, dim);
                let orig_type = array.data_type().clone();
                let g_array = GeometryArray::from(array.clone());

                let casted = cast(&g_array, &orig_type).unwrap();
                assert_eq!(casted.as_polygon(), &array);
            }
        }
    }

    #[test]
    fn downcast_geometry_to_multi_point() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                let array = test::multipoint::array(coord_type, dim);
                let orig_type = array.data_type().clone();
                let g_array = GeometryArray::from(array.clone());

                let casted = cast(&g_array, &orig_type).unwrap();
                assert_eq!(casted.as_multi_point(), &array);
            }
        }
    }

    #[test]
    fn downcast_geometry_to_multi_line_string() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                let array = test::multilinestring::array(coord_type, dim);
                let orig_type = array.data_type().clone();
                let g_array = GeometryArray::from(array.clone());

                let casted = cast(&g_array, &orig_type).unwrap();
                assert_eq!(casted.as_multi_line_string(), &array);
            }
        }
    }

    #[test]
    fn downcast_geometry_to_multi_polygon() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                let array = test::multipolygon::array(coord_type, dim);
                let orig_type = array.data_type().clone();
                let g_array = GeometryArray::from(array.clone());

                let casted = cast(&g_array, &orig_type).unwrap();
                assert_eq!(casted.as_multi_polygon(), &array);
            }
        }
    }

    #[test]
    fn downcast_geometry_to_geometry_collection() {
        for coord_type in [CoordType::Interleaved, CoordType::Separated] {
            for dim in [
                Dimension::XY,
                Dimension::XYZ,
                Dimension::XYM,
                Dimension::XYZM,
            ] {
                for prefer_multi in [false, true] {
                    let array = test::geometrycollection::array(coord_type, dim, prefer_multi);
                    let orig_type = array.data_type().clone();
                    let g_array = GeometryArray::from(array.clone());

                    let casted = cast(&g_array, &orig_type).unwrap();
                    assert_eq!(casted.as_geometry_collection(), &array);
                }
            }
        }
    }

    #[test]
    fn downcast_geometry_to_point_fails() {
        let array = test::geometry::array(Default::default(), false);
        let point_type = PointType::new(Dimension::XY, Default::default());
        assert!(cast(&array, &point_type.into()).is_err());
    }
}