vortex-spatial 0.95.0

Spatial data types and compute for Vortex
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Native line-string geometry (`vortex.st.linestring`).
//!
//! [`LineString`] stores an ordered path as `List<Struct<x, y[, z][, m]>>`. [`SpatialMetadata`]
//! declares its coordinate dimension and optional coordinate reference system. Coordinates use
//! non-nullable `f64` fields, and only the outer list can be nullable.

use std::sync::Arc;

use arrow_array::ArrayRef as ArrowArrayRef;
use arrow_schema::Field;
use arrow_schema::extension::ExtensionType;
use geo_traits::to_geo::ToGeoGeometry;
use geo_types::Geometry;
use geoarrow::array::GeoArrowArrayAccessor;
use geoarrow::array::LineStringArray;
use geoarrow::datatypes::CoordType;
use geoarrow::datatypes::LineStringType;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::ExtensionArray;
use vortex_array::arrays::InterleaveArray;
use vortex_array::arrays::ListArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::StructArray;
use vortex_array::arrays::extension::ExtensionArrayExt;
use vortex_array::arrays::struct_::StructArrayExt;
use vortex_array::dtype::DType;
use vortex_array::dtype::FieldNames;
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::scalar::ScalarValue;
use vortex_array::validity::Validity;
use vortex_arrow::ArrowExport;
use vortex_arrow::ArrowExportOptions;
use vortex_arrow::ArrowExtVTable;
use vortex_arrow::ArrowImport;
use vortex_arrow::ArrowMetadataPolicy;
use vortex_arrow::ArrowSession;
use vortex_arrow::ArrowSessionExt;
use vortex_arrow::ExecuteArrowExt;
use vortex_buffer::Buffer;
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 line string: `geoarrow.linestring`, stored as `List<Struct<x, y[, z][, m]>>` (an ordered path
/// of vertices).
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct LineString;

impl ExtVTable for LineString {
    type Metadata = SpatialMetadata;
    type StoredMetadata = super::SpatialStoredMetadata;
    // 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.linestring");
        *ID
    }

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

    fn deserialize_metadata(&self, metadata: &[u8]) -> VortexResult<Self::StoredMetadata> {
        super::SpatialStoredMetadata::deserialize(metadata)
    }

    fn metadata_from_storage(
        &self,
        stored_metadata: Self::StoredMetadata,
        storage_dtype: &DType,
    ) -> VortexResult<Self::Metadata> {
        Ok(SpatialMetadata::from_stored_metadata(
            stored_metadata,
            Some(linestring_dimension(storage_dtype)?),
        ))
    }

    fn storage_dtype(metadata: &Self::Metadata) -> DType {
        linestring_storage_dtype(metadata.native_dimension(), Nullability::NonNullable)
    }

    fn validate_metadata(metadata: &Self::Metadata) -> VortexResult<()> {
        metadata.validate_native(LineString.id())
    }

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

/// Canonical line-string storage: a list of the coordinate `Struct`.
pub(crate) fn linestring_storage_dtype(dim: Dimension, nullability: Nullability) -> DType {
    let coords = coordinate_storage_dtype(dim, Nullability::NonNullable);
    DType::List(Arc::new(coords), nullability)
}

/// Infers the coordinate [`Dimension`] of `List<coordinate-struct>` storage.
///
/// Checks the list nesting and delegates coordinate field-name inference to
/// [`coordinate_dimension`]. Reads validate the complete storage dtype against the declaration.
pub(crate) fn linestring_dimension(dtype: &DType) -> VortexResult<Dimension> {
    let DType::List(coords, _) = dtype else {
        vortex_bail!("linestring storage must be a List of coordinates, got {dtype}");
    };
    coordinate_dimension(coords)
}

/// Return one coordinate ordinate, filling an ordinate absent from the point dimension with zero.
fn point_ordinate(
    points: &StructArray,
    dimension: Dimension,
    name: &str,
) -> VortexResult<ArrayRef> {
    if dimension.field_names().contains(&name) {
        points.unmasked_field_by_name(name).cloned()
    } else {
        Ok(ConstantArray::new(0.0f64, points.len()).into_array())
    }
}

/// Build one native [`LineString`] per corresponding pair of point coordinate rows.
pub(crate) fn linestring_array_from_point_pairs(
    ext_dtype: &ExtDType<LineString>,
    starts: &StructArray,
    ends: &StructArray,
    validity: Validity,
) -> VortexResult<ArrayRef> {
    let len = starts.len();
    vortex_ensure_eq!(
        len,
        ends.len(),
        "spatial: line string point columns must have equal lengths"
    );
    let vertex_count = len
        .checked_mul(2)
        .ok_or_else(|| vortex_err!("spatial: two-vertex line string length overflow"))?;
    let last_offset = i32::try_from(vertex_count)
        .map_err(|_| vortex_err!("spatial: two-vertex line string offset overflow"))?;
    let row_count = u32::try_from(len)
        .map_err(|_| vortex_err!("spatial: two-vertex line string row count overflow"))?;
    let dimension = ext_dtype.metadata().native_dimension();
    let start_dimension = coordinate_dimension(starts.dtype())?;
    let end_dimension = coordinate_dimension(ends.dtype())?;

    let array_indices = PrimitiveArray::from_iter((0..len).flat_map(|_| [0u8, 1])).into_array();
    let row_indices = Buffer::from_iter((0..row_count).flat_map(|row| [row; 2])).into_array();

    let ordinates = dimension
        .field_names()
        .iter()
        .map(|name| {
            Ok(InterleaveArray::try_new(
                vec![
                    point_ordinate(starts, start_dimension, name)?,
                    point_ordinate(ends, end_dimension, name)?,
                ],
                array_indices.clone(),
                row_indices.clone(),
            )?
            .into_array())
        })
        .collect::<VortexResult<Vec<_>>>()?;

    let vertices = StructArray::try_new(
        FieldNames::from(dimension.field_names()),
        ordinates,
        vertex_count,
        Validity::NonNullable,
    )?
    .into_array();

    let offsets = Buffer::from_iter((0..=last_offset).step_by(2)).into_array();
    // SAFETY: each line string has two vertices, so the offsets step by two from zero up to the
    // vertex count. The validity has one entry per row.
    let storage = unsafe { ListArray::new_unchecked(vertices, offsets, validity) }.into_array();

    Ok(ExtensionArray::try_new(ext_dtype.clone().erased(), storage)?.into_array())
}

static ARROW_LINESTRING: CachedId = CachedId::new(LineStringType::NAME);

/// The `geoarrow.linestring` extension type for `dimension`, with separated (struct) coordinates
/// matching `LineString` storage.
fn linestring_type(spatial_metadata: &SpatialMetadata, dimension: Dimension) -> LineStringType {
    LineStringType::new(dimension.into(), geoarrow_metadata(spatial_metadata))
}

/// Decode `LineString` storage (`List<coordinate>`) to `geo_types` line strings, for the spatial scalar
/// functions. CRS does not affect planar geometry ops, so default metadata is used.
pub(crate) fn linestring_geometries(
    storage: &ArrayRef,
    ctx: &mut ExecutionCtx,
) -> VortexResult<Vec<Geometry<f64>>> {
    linestring_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 `LineStringArray` from a `LineString`'s `List<coordinate>` storage.
fn linestring_array(storage: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<LineStringArray> {
    let linestring_type = linestring_type(
        &SpatialMetadata::default(),
        linestring_dimension(storage.dtype())?,
    );
    let arrow = storage.clone().execute_arrow(None, ctx)?;
    LineStringArray::try_from((arrow.as_ref(), linestring_type))
        .map_err(|e| vortex_err!("failed to construct LineStringArray: {e}"))
}

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

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

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

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

impl ArrowExtVTable for LineString {
    fn metadata_policy() -> ArrowMetadataPolicy {
        ArrowMetadataPolicy::ArrowNative
    }

    fn arrow_ext_id(&self) -> Id {
        *ARROW_LINESTRING
    }

    fn to_arrow_field(
        &self,
        name: &str,
        ext_dtype: &ExtDType<Self>,
        _session: &ArrowSession,
    ) -> VortexResult<Option<Field>> {
        let spatial_metadata = ext_dtype.metadata();
        let dimension = spatial_metadata.native_dimension();

        let arrow_type = linestring_type(spatial_metadata, dimension);
        let mut field = Field::new(
            name,
            arrow_type.data_type(),
            ext_dtype.storage_dtype().is_nullable(),
        );
        field.try_with_extension_type(arrow_type)?;

        Ok(Some(field))
    }

    fn execute_arrow(
        &self,
        array: ArrayRef,
        target: &Field,
        options: &ArrowExportOptions,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<ArrowExport> {
        let Ok(linestring_meta) = target.try_extension_type::<LineStringType>() else {
            return Ok(ArrowExport::Unsupported(array));
        };
        if linestring_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 arrow_storage =
            storage.execute_arrow_with_options(Some(&storage_field), options, ctx)?;

        // The validated target already shapes the storage. GeoArrow constructors can panic on
        // valid Arrow lists that retain unused child values after filtering.
        Ok(ArrowExport::Exported(arrow_storage))
    }

    fn from_arrow_field(
        field: &Field,
        _session: &ArrowSession,
    ) -> VortexResult<Option<ExtDType<Self>>> {
        let linestring_meta = field.try_extension_type::<LineStringType>()?;
        vortex_ensure!(
            linestring_meta.coord_type() == CoordType::Separated,
            "geoarrow.linestring with interleaved coordinates is not supported; \
             re-encode with separated (struct) coordinates"
        );

        let metadata = spatial_metadata_from_arrow(linestring_meta.metadata())
            .with_dimension(linestring_meta.dimension().into());
        Ok(Some(ExtDType::try_with_vtable(
            LineString,
            metadata,
            field.is_nullable().into(),
        )?))
    }

    fn from_arrow_array(
        array: ArrowArrayRef,
        _field: &Field,
        ext_dtype: &ExtDType<Self>,
        session: &ArrowSession,
    ) -> VortexResult<ArrowImport> {
        session
            .from_arrow_storage(array, &ext_dtype.clone().erased())
            .map(ArrowImport::Imported)
    }
}

#[cfg(test)]
mod tests {
    use rstest::rstest;
    use vortex_array::dtype::DType;
    use vortex_array::dtype::Nullability;
    use vortex_array::dtype::PType;
    use vortex_array::dtype::extension::ExtDTypePlugin;
    use vortex_error::VortexResult;

    use super::LineString;
    use super::linestring_storage_dtype;
    use crate::extension::SpatialMetadata;
    use crate::extension::coordinate::Dimension;

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

    /// `LineString` reads the canonical `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 linestring_reads_every_dimension(#[case] dim: Dimension) -> VortexResult<()> {
        let storage = linestring_storage_dtype(dim, Nullability::NonNullable);
        LineString.deserialize(&spatial_meta().serialize(), storage)?;
        Ok(())
    }

    /// Non-linestring storage fails to read: `f64` is not a list of coordinates.
    #[test]
    fn linestring_rejects_invalid_storage() -> VortexResult<()> {
        let primitive = DType::Primitive(PType::F64, Nullability::NonNullable);
        assert!(
            LineString
                .deserialize(&spatial_meta().serialize(), primitive)
                .is_err()
        );
        Ok(())
    }
}