vortex-spatial 0.91.0

Spatial data types and compute for Vortex
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! The [`Polygon`] geometry extension type (`vortex.st.polygon`): rings of the
//! [`Point`](super::Point) coordinate struct, stored as `List<List<Struct<x, y[, z][, m]>>>` and tagged with
//! [`SpatialMetadata`] (CRS). The first ring is the exterior boundary; the rest are holes.

use std::sync::Arc;

use arrow_array::ArrayRef as ArrowArrayRef;
use arrow_schema::DataType;
use arrow_schema::Field;
use arrow_schema::extension::ExtensionType;
use geo::HasDimensions;
use geo_traits::to_geo::ToGeoGeometry;
use geo_types::Geometry;
use geoarrow::array::GeoArrowArrayAccessor;
use geoarrow::array::IntoArrow;
use geoarrow::array::PolygonArray;
use geoarrow::datatypes::CoordType;
use geoarrow::datatypes::PolygonType;
use prost::Message;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::ExtensionArray;
use vortex_array::arrays::ListArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::StructArray;
use vortex_array::arrays::extension::ExtensionArrayExt;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::extension::ExtDType;
use vortex_array::dtype::extension::ExtId;
use vortex_array::dtype::extension::ExtVTable;
use vortex_array::memory::BufferAllocatorRef;
use vortex_array::scalar::ScalarValue;
use vortex_array::validity::Validity;
use vortex_arrow::ArrowExport;
use vortex_arrow::ArrowExportOptions;
use vortex_arrow::ArrowExportVTable;
use vortex_arrow::ArrowImport;
use vortex_arrow::ArrowImportVTable;
use vortex_arrow::ArrowSession;
use vortex_arrow::ArrowSessionExt;
use vortex_error::VortexError;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_ensure_eq;
use vortex_error::vortex_err;
use vortex_session::registry::CachedId;
use vortex_session::registry::Id;

use super::SpatialMetadata;
use super::coordinate::Dimension;
use super::coordinate::coordinate_dimension;
use super::coordinate::coordinate_storage_dtype;
use super::geoarrow_metadata;
use super::geoarrow_to_wkb;
use super::spatial_metadata_from_arrow;

/// A polygon: `geoarrow.polygon`, stored as `List<List<Struct<x, y[, z][, m]>>>` (rings of vertices).
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct Polygon;

impl ExtVTable for Polygon {
    type Metadata = SpatialMetadata;
    // No cheap owned value like Point's `Coordinate`; expose the raw storage scalar.
    type NativeValue<'a> = &'a ScalarValue;

    fn id(&self) -> ExtId {
        static ID: CachedId = CachedId::new("vortex.st.polygon");
        *ID
    }

    fn serialize_metadata(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
        Ok(metadata.encode_to_vec())
    }

    fn deserialize_metadata(&self, metadata: &[u8]) -> VortexResult<Self::Metadata> {
        Ok(SpatialMetadata::decode(metadata)?)
    }

    fn validate_dtype(ext_dtype: &ExtDType<Self>) -> VortexResult<()> {
        polygon_dimension(ext_dtype.storage_dtype()).map(|_| ())
    }

    fn unpack_native<'a>(
        _ext_dtype: &'a ExtDType<Self>,
        storage_value: &'a ScalarValue,
    ) -> VortexResult<&'a ScalarValue> {
        Ok(storage_value)
    }
}

/// Canonical polygon storage: an outer list of rings, each a list of the coordinate `Struct`.
pub(crate) fn polygon_storage_dtype(dim: Dimension, nullability: Nullability) -> DType {
    let coords = coordinate_storage_dtype(dim, Nullability::NonNullable);
    let ring = DType::List(Arc::new(coords), Nullability::NonNullable);
    DType::List(Arc::new(ring), nullability)
}

/// Validate that `dtype` equals [`polygon_storage_dtype`] for its [`Dimension`] and top-level
/// nullability, and return that dimension. As in GeoArrow, only the outer `List` can be nullable.
pub(crate) fn polygon_dimension(dtype: &DType) -> VortexResult<Dimension> {
    let DType::List(ring, _) = dtype else {
        vortex_bail!("polygon storage must be a List of rings, was {dtype}");
    };
    let DType::List(coords, _) = ring.as_ref() else {
        vortex_bail!("polygon ring storage must be a List of coordinates, was {ring}");
    };
    let dimension = coordinate_dimension(coords)?;

    vortex_ensure_eq!(
        *dtype,
        polygon_storage_dtype(dimension, dtype.nullability()),
        "polygon storage can be nullable only at its outer List"
    );

    Ok(dimension)
}

static ARROW_POLYGON: CachedId = CachedId::new(PolygonType::NAME);

/// The `geoarrow.polygon` extension type for `dimension`, with separated (struct) coordinates
/// matching `Polygon` storage.
fn polygon_type(spatial_metadata: &SpatialMetadata, dimension: Dimension) -> PolygonType {
    PolygonType::new(dimension.into(), geoarrow_metadata(spatial_metadata))
}

/// Build canonical non-nullable 2-D polygon storage from row-oriented `geo_types` polygons.
pub(crate) fn build_polygon_storage(
    polygons: &[geo_types::Polygon<f64>],
    allocator: &BufferAllocatorRef,
) -> VortexResult<ArrayRef> {
    let mut xs = allocator.with_capacity::<f64>(0);
    let mut ys = allocator.with_capacity::<f64>(0);
    let mut ring_offsets = allocator.with_capacity::<u64>(1);
    ring_offsets.push(0);
    let mut polygon_offsets = allocator.with_capacity::<u64>(polygons.len() + 1);
    polygon_offsets.push(0);

    for polygon in polygons {
        let exterior = (!polygon.exterior().is_empty()).then_some(polygon.exterior());
        for ring in exterior.into_iter().chain(polygon.interiors()) {
            xs.extend(ring.0.iter().map(|coord| coord.x));
            ys.extend(ring.0.iter().map(|coord| coord.y));
            ring_offsets.push(
                u64::try_from(xs.len())
                    .map_err(|_| vortex_err!("spatial: polygon coordinate count exceeds u64"))?,
            );
        }
        polygon_offsets.push(
            u64::try_from(ring_offsets.len() - 1)
                .map_err(|_| vortex_err!("spatial: polygon ring count exceeds u64"))?,
        );
    }

    let coordinates = StructArray::from_fields(&[
        (
            "x",
            PrimitiveArray::new(xs.freeze(), Validity::NonNullable).into_array(),
        ),
        (
            "y",
            PrimitiveArray::new(ys.freeze(), Validity::NonNullable).into_array(),
        ),
    ])?
    .into_array();
    // SAFETY: the offsets start at zero and are pushed as the running coordinate and ring counts,
    // so they are sorted and end at the length of their elements.
    let rings = unsafe {
        ListArray::new_unchecked(
            coordinates,
            PrimitiveArray::new(ring_offsets.freeze(), Validity::NonNullable).into_array(),
            Validity::NonNullable,
        )
    }
    .into_array();
    // SAFETY: as above, for the polygon offsets into the rings.
    let storage = unsafe {
        ListArray::new_unchecked(
            rings,
            PrimitiveArray::new(polygon_offsets.freeze(), Validity::NonNullable).into_array(),
            Validity::NonNullable,
        )
    }
    .into_array();
    Ok(storage)
}

/// Decode `Polygon` storage (`List<List<coordinate>>`) to `geo_types` polygons, for the spatial scalar
/// functions. CRS does not affect planar geometry ops, so default metadata is used.
pub(crate) fn polygon_geometries(
    storage: &ArrayRef,
    ctx: &mut ExecutionCtx,
) -> VortexResult<Vec<Geometry<f64>>> {
    polygon_array(storage, ctx)?
        .iter()
        .map(|geometry| -> VortexResult<Geometry<f64>> {
            Ok(geometry
                .ok_or_else(|| vortex_err!("spatial: null geometry is not supported"))?
                .map_err(|e| vortex_err!("spatial: geometry access failed: {e}"))?
                .to_geometry())
        })
        .collect()
}

/// Build a geoarrow `PolygonArray` from a `Polygon`'s `List<List<coordinate>>` storage.
fn polygon_array(storage: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<PolygonArray> {
    let polygon_type = polygon_type(
        &SpatialMetadata::default(),
        polygon_dimension(storage.dtype())?,
    );
    let session = ctx.session().clone();
    let arrow = session.arrow().execute_arrow(storage.clone(), None, ctx)?;
    PolygonArray::try_from((arrow.as_ref(), polygon_type))
        .map_err(|e| vortex_err!("failed to construct PolygonArray: {e}"))
}

/// A validated `Polygon` array (`try_from` checks the extension type).
pub struct PolygonData(ExtensionArray);

impl TryFrom<ExtensionArray> for PolygonData {
    type Error = VortexError;

    fn try_from(ext: ExtensionArray) -> Result<Self, Self::Error> {
        vortex_ensure!(
            ext.ext_dtype().is::<Polygon>(),
            "expected a Polygon extension array"
        );
        Ok(PolygonData(ext))
    }
}

impl PolygonData {
    /// Serialize polygons to WKB (a view array) — the form DuckDB `GEOMETRY` takes.
    pub fn to_wkb(&self, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
        geoarrow_to_wkb(
            &polygon_array(self.0.storage_array(), ctx)?,
            &ctx.session().arrow(),
        )
    }
}

impl ArrowExportVTable for Polygon {
    fn arrow_ext_id(&self) -> Id {
        *ARROW_POLYGON
    }

    fn vortex_id(&self) -> Id {
        self.id()
    }

    fn to_arrow_field(
        &self,
        name: &str,
        dtype: &DType,
        session: &ArrowSession,
    ) -> VortexResult<Option<Field>> {
        let ext_type = dtype.as_extension();
        let spatial_metadata = ext_type.metadata::<Polygon>();
        let dimension = polygon_dimension(ext_type.storage_dtype())?;

        let mut field = session.to_arrow_field(name, ext_type.storage_dtype())?;
        field.try_with_extension_type(polygon_type(spatial_metadata, dimension))?;

        Ok(Some(field))
    }

    fn execute_arrow(
        &self,
        array: ArrayRef,
        target: &Field,
        options: &ArrowExportOptions,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<ArrowExport> {
        let is_polygon = array
            .dtype()
            .as_extension_opt()
            .map(|ext| ext.is::<Polygon>())
            .unwrap_or(false);
        if !is_polygon {
            return Ok(ArrowExport::Unsupported(array));
        }

        let Ok(polygon_meta) = target.try_extension_type::<PolygonType>() else {
            return Ok(ArrowExport::Unsupported(array));
        };
        if polygon_meta.coord_type() != CoordType::Separated {
            return Ok(ArrowExport::Unsupported(array));
        }

        let executed = array.execute::<ExtensionArray>(ctx)?;
        let storage = executed.storage_array().clone();

        let storage_field = Field::new(
            String::new(),
            target.data_type().clone(),
            target.is_nullable(),
        );
        let session = ctx.session().clone();
        let arrow_storage =
            session
                .arrow()
                .exporter(options)
                .execute_arrow(storage, Some(&storage_field), ctx)?;

        // Round-trip through GeoArrow's polygon array; `into_arrow` is concrete, so wrap in `Arc`.
        let polygons = PolygonArray::try_from((arrow_storage.as_ref(), polygon_meta))
            .map_err(|e| vortex_err!("failed to construct PolygonArray: {e}"))?;

        Ok(ArrowExport::Exported(Arc::new(polygons.into_arrow())))
    }
}

impl ArrowImportVTable for Polygon {
    fn arrow_ext_id(&self) -> Id {
        *ARROW_POLYGON
    }

    /// Import a `geoarrow.polygon` field as the [`Polygon`] dtype. Keyed off the standard GeoArrow
    /// name, so any producer (DataFusion, DuckDB, geoarrow-rs, …) resolves here. Accepts the full
    /// `PolygonType` extension, or — for a metadata-less geometry literal — the name alone, inferring
    /// the dimension from the coordinate field names.
    fn from_arrow_field(
        &self,
        field: &Field,
        session: &ArrowSession,
    ) -> VortexResult<Option<DType>> {
        let (dimension, metadata) =
            if let Ok(polygon_meta) = field.try_extension_type::<PolygonType>() {
                vortex_ensure!(
                    polygon_meta.coord_type() == CoordType::Separated,
                    "geoarrow.polygon with interleaved coordinates is not supported; \
             re-encode with separated (struct) coordinates"
                );
                (
                    polygon_meta.dimension().into(),
                    spatial_metadata_from_arrow(polygon_meta.metadata()),
                )
            } else {
                // Infer the dimension from the field names, not the canonical storage check: a literal's
                // coordinate fields may be nullable, which that check rejects. Peel the two `List` layers
                // (polygon → rings → coordinates) to reach the struct.
                if field.extension_type_name() != Some(PolygonType::NAME) {
                    return Ok(None);
                }
                let Ok(DType::List(ring, _)) =
                    session.from_arrow_datatype(field.data_type(), field.is_nullable().into())
                else {
                    return Ok(None);
                };
                let DType::List(coords, _) = ring.as_ref() else {
                    return Ok(None);
                };
                let DType::Struct(fields, _) = coords.as_ref() else {
                    return Ok(None);
                };
                let Ok(dimension) = Dimension::from_field_names(fields.names()) else {
                    return Ok(None);
                };
                (dimension, SpatialMetadata::default())
            };

        let storage_dtype = polygon_storage_dtype(dimension, field.is_nullable().into());
        Ok(Some(DType::Extension(
            ExtDType::try_with_vtable(Polygon, metadata, storage_dtype)?.erased(),
        )))
    }

    fn from_arrow_array(
        &self,
        array: ArrowArrayRef,
        field: &Field,
        dtype: &DType,
        session: &ArrowSession,
    ) -> VortexResult<ArrowImport> {
        let Some(ext_dtype) = dtype.as_extension_opt() else {
            return Ok(ArrowImport::Unsupported(array));
        };
        if !ext_dtype.is::<Polygon>()
            || field.try_extension_type::<PolygonType>().is_err()
            || !matches!(array.data_type(), DataType::List(_))
        {
            return Ok(ArrowImport::Unsupported(array));
        }

        let storage = session.from_arrow_array(array, field.is_nullable())?;
        Ok(ArrowImport::Imported(
            ExtensionArray::try_new(ext_dtype.clone(), storage)?.into_array(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use rstest::rstest;
    use vortex_array::dtype::DType;
    use vortex_array::dtype::Nullability;
    use vortex_array::dtype::PType;
    use vortex_array::dtype::extension::ExtDType;
    use vortex_error::VortexResult;

    use super::Polygon;
    use super::polygon_storage_dtype;
    use crate::extension::SpatialMetadata;
    use crate::extension::coordinate::Dimension;
    use crate::extension::coordinate::coordinate_storage_dtype;

    fn spatial_meta() -> SpatialMetadata {
        SpatialMetadata {
            crs: Some("EPSG:4326".to_string()),
        }
    }

    /// `Polygon` accepts the canonical `List<List<coordinate-struct>>` storage of every dimension.
    #[rstest]
    #[case::xy(Dimension::Xy)]
    #[case::xyz(Dimension::Xyz)]
    #[case::xym(Dimension::Xym)]
    #[case::xyzm(Dimension::Xyzm)]
    fn polygon_validates_every_dimension(#[case] dim: Dimension) -> VortexResult<()> {
        let storage = polygon_storage_dtype(dim, Nullability::NonNullable);
        ExtDType::<Polygon>::try_new(spatial_meta(), storage)?;
        Ok(())
    }

    /// Non-polygon storage is rejected at dtype construction: a bare struct (point) and a single
    /// list (linestring) both fail.
    #[test]
    fn polygon_rejects_invalid_storage() -> VortexResult<()> {
        let primitive = DType::Primitive(PType::F64, Nullability::NonNullable);
        assert!(ExtDType::<Polygon>::try_new(spatial_meta(), primitive).is_err());

        // A single list of coordinates is a LineString, not a Polygon.
        let coords = coordinate_storage_dtype(Dimension::Xy, Nullability::NonNullable);
        let line = DType::List(Arc::new(coords), Nullability::NonNullable);
        assert!(ExtDType::<Polygon>::try_new(spatial_meta(), line).is_err());
        Ok(())
    }
}