Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions vortex-spatial/src/extension/matcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Matcher for the native geometry extension types.

use vortex_array::dtype::extension::ExtDTypeRef;
use vortex_array::dtype::extension::Matcher;

use crate::extension::LineString;
use crate::extension::MultiLineString;
use crate::extension::MultiPoint;
use crate::extension::MultiPolygon;
use crate::extension::Point;
use crate::extension::Polygon;
use crate::extension::Rect;
use crate::extension::SpatialMetadata;

/// Matcher for any native geometry extension type: `Point`, `LineString`, `MultiPoint`,
/// `Polygon`, `MultiLineString`, `MultiPolygon`, and `Rect`. Matches to its [`SpatialMetadata`].
pub struct AnyNativeGeometry;

impl Matcher for AnyNativeGeometry {
type Match<'a> = &'a SpatialMetadata;

fn try_match<'a>(ext_dtype: &'a ExtDTypeRef) -> Option<Self::Match<'a>> {
ext_dtype
.metadata_opt::<Point>()
.or_else(|| ext_dtype.metadata_opt::<LineString>())
.or_else(|| ext_dtype.metadata_opt::<MultiPoint>())
.or_else(|| ext_dtype.metadata_opt::<Polygon>())
.or_else(|| ext_dtype.metadata_opt::<MultiLineString>())
.or_else(|| ext_dtype.metadata_opt::<MultiPolygon>())
.or_else(|| ext_dtype.metadata_opt::<Rect>())
}
}
15 changes: 6 additions & 9 deletions vortex-spatial/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub use coordinate::Dimension;
mod linestring;
pub use linestring::*;

mod matcher;
pub use matcher::AnyNativeGeometry;

mod multilinestring;
pub use multilinestring::*;

Expand Down Expand Up @@ -86,15 +89,9 @@ pub use wkb::*;

/// Whether `dtype` is one of the native geometry extension types the spatial kernels operate on.
pub(crate) fn is_native_geometry(dtype: &DType) -> bool {
dtype.as_extension_opt().is_some_and(|ext| {
ext.is::<Point>()
|| ext.is::<LineString>()
|| ext.is::<MultiPoint>()
|| ext.is::<Polygon>()
|| ext.is::<MultiLineString>()
|| ext.is::<MultiPolygon>()
|| ext.is::<Rect>()
})
dtype
.as_extension_opt()
.is_some_and(|ext| ext.is::<AnyNativeGeometry>())
}

/// Flatten a native geometry column into a single coordinate `Struct<x, y, ...>` containing
Expand Down
72 changes: 50 additions & 22 deletions vortex-spatial/src/scalar_fn/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use vortex_array::arrays::struct_::StructArrayExt;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::extension::ExtDType;
use vortex_array::dtype::extension::Matcher;
use vortex_array::scalar::Scalar;
use vortex_array::scalar_fn::Arity;
use vortex_array::scalar_fn::ChildName;
Expand All @@ -39,6 +40,7 @@ use vortex_mask::Mask;
use vortex_session::VortexSession;
use vortex_session::registry::CachedId;

use crate::extension::AnyNativeGeometry;
use crate::extension::Rect;
use crate::extension::SpatialMetadata;
use crate::extension::box_field_names;
Expand Down Expand Up @@ -86,11 +88,15 @@ impl SpatialEnvelope {
}
}

/// The output dtype: a nullable, unreferenced 2D box ([`Rect`], `geoarrow.box`). Rows with null or
/// empty geometries produce null boxes.
fn output_box_dtype() -> VortexResult<ExtDType<Rect>> {
/// The output dtype: a nullable 2D box ([`Rect`], `geoarrow.box`) in the CRS of the operand
/// `dtype`. Rows with null or empty geometries produce null boxes.
fn output_box_dtype(dtype: &DType) -> VortexResult<ExtDType<Rect>> {
let crs = dtype
.as_extension_opt()
.and_then(AnyNativeGeometry::try_match)
.and_then(SpatialMetadata::get_crs);
ExtDType::<Rect>::try_new(
SpatialMetadata::default().with_dimension(Dimension::Xy),
SpatialMetadata::new(crs.map(str::to_owned)).with_dimension(Dimension::Xy),
Nullability::Nullable,
)
}
Expand Down Expand Up @@ -235,7 +241,7 @@ impl ScalarFnVTable for SpatialEnvelope {
validate_envelope_operands(dtypes)?;
// Always nullable: an empty geometry has no box, so nulls can appear even over a
// non-nullable operand.
Ok(DType::Extension(output_box_dtype()?.erased()))
Ok(DType::Extension(output_box_dtype(&dtypes[0])?.erased()))
}

/// Compute each row's box directly over the native coordinate storage — no decode to
Expand All @@ -248,7 +254,7 @@ impl ScalarFnVTable for SpatialEnvelope {
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let array = args.get(0)?;
let output_dtype = output_box_dtype()?;
let output_dtype = output_box_dtype(array.dtype())?;
dispatch_unary(
&array,
DType::Extension(output_dtype.clone().erased()),
Expand Down Expand Up @@ -278,6 +284,7 @@ impl ScalarFnVTable for SpatialEnvelope {

#[cfg(test)]
mod tests {
use rstest::rstest;
use vortex_array::ArrayRef;
use vortex_array::Columnar;
use vortex_array::IntoArray;
Expand Down Expand Up @@ -308,7 +315,9 @@ mod tests {
use crate::test_harness::nullable_multipolygon_column;
use crate::test_harness::nullable_point_column;
use crate::test_harness::nullable_rect_column;
use crate::test_harness::nullable_wgs84_rect_column;
use crate::test_harness::point_column;
use crate::test_harness::point_column_with_crs;
use crate::test_harness::polygon_column;
use crate::test_harness::rect_column;

Expand All @@ -324,8 +333,10 @@ mod tests {
let mut ctx = session.create_execution_ctx();

let points = point_column(vec![1.0, 3.0], vec![2.0, 4.0])?;
let expected =
nullable_rect_column(vec![Some((1.0, 2.0, 1.0, 2.0)), Some((3.0, 4.0, 3.0, 4.0))])?;
let expected = nullable_wgs84_rect_column(vec![
Some((1.0, 2.0, 1.0, 2.0)),
Some((3.0, 4.0, 3.0, 4.0)),
])?;
assert_arrays_eq!(boxes(points)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -340,7 +351,7 @@ mod tests {
vec![vec![(0.0, 0.0), (4.0, 0.0), (2.0, 5.0)]],
vec![vec![(-3.0, 7.0), (-2.0, 7.0), (-2.0, 9.0), (-3.0, 9.0)]],
])?;
let expected = nullable_rect_column(vec![
let expected = nullable_wgs84_rect_column(vec![
Some((0.0, 0.0, 4.0, 5.0)),
Some((-3.0, 7.0, -2.0, 9.0)),
])?;
Expand All @@ -355,7 +366,7 @@ mod tests {
let mut ctx = session.create_execution_ctx();

let rects = rect_column(vec![(0.0, 0.0, 2.0, 3.0), (-1.0, -1.0, 1.0, 1.0)])?;
let expected = nullable_rect_column(vec![
let expected = nullable_wgs84_rect_column(vec![
Some((0.0, 0.0, 2.0, 3.0)),
Some((-1.0, -1.0, 1.0, 1.0)),
])?;
Expand Down Expand Up @@ -406,7 +417,7 @@ mod tests {
multilinestring_column(vec![vec![vertices.clone()]])?,
multipolygon_column(vec![vec![vec![vertices]]])?,
];
let expected = nullable_rect_column(vec![Some((-1.0, 2.0, 3.0, 5.0))])?;
let expected = nullable_wgs84_rect_column(vec![Some((-1.0, 2.0, 3.0, 5.0))])?;
for column in columns {
assert_arrays_eq!(boxes(column)?, expected.clone(), &mut ctx);
}
Expand All @@ -428,7 +439,7 @@ mod tests {
],
vec![vec![vec![(10.0, 10.0), (11.0, 12.0)]]],
])?;
let expected = nullable_rect_column(vec![
let expected = nullable_wgs84_rect_column(vec![
Some((0.0, -3.0, 6.0, 7.0)),
Some((10.0, 10.0, 11.0, 12.0)),
])?;
Expand All @@ -447,7 +458,7 @@ mod tests {
vec![vec![vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]]],
vec![],
])?;
let expected = nullable_rect_column(vec![Some((0.0, 0.0, 1.0, 1.0)), None])?;
let expected = nullable_wgs84_rect_column(vec![Some((0.0, 0.0, 1.0, 1.0)), None])?;
assert_arrays_eq!(boxes(multipolygons)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -463,7 +474,7 @@ mod tests {
vec![vec![]],
vec![vec![(1.0, 2.0), (3.0, 4.0), (1.0, 4.0)]],
])?;
let expected = nullable_rect_column(vec![None, Some((1.0, 2.0, 3.0, 4.0))])?;
let expected = nullable_wgs84_rect_column(vec![None, Some((1.0, 2.0, 3.0, 4.0))])?;
assert_arrays_eq!(boxes(polygons)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -480,8 +491,10 @@ mod tests {
vec![(1.0, 2.0), (3.0, 4.0)],
vec![(5.0, 6.0)],
])?;
let expected =
nullable_rect_column(vec![Some((1.0, 2.0, 3.0, 4.0)), Some((5.0, 6.0, 5.0, 6.0))])?;
let expected = nullable_wgs84_rect_column(vec![
Some((1.0, 2.0, 3.0, 4.0)),
Some((5.0, 6.0, 5.0, 6.0)),
])?;
assert_arrays_eq!(boxes(multipoints.slice(1..3)?)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -493,8 +506,10 @@ mod tests {
let mut ctx = session.create_execution_ctx();

let points = point_column(vec![9.0, 1.0, 3.0], vec![8.0, 2.0, 4.0])?;
let expected =
nullable_rect_column(vec![Some((1.0, 2.0, 1.0, 2.0)), Some((3.0, 4.0, 3.0, 4.0))])?;
let expected = nullable_wgs84_rect_column(vec![
Some((1.0, 2.0, 1.0, 2.0)),
Some((3.0, 4.0, 3.0, 4.0)),
])?;
assert_arrays_eq!(boxes(points.slice(1..3)?)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -506,7 +521,7 @@ mod tests {
let mut ctx = session.create_execution_ctx();

let points = nullable_point_column(vec![Some((1.0, 2.0)), None, Some((3.0, 4.0))])?;
let expected = nullable_rect_column(vec![
let expected = nullable_wgs84_rect_column(vec![
Some((1.0, 2.0, 1.0, 2.0)),
None,
Some((3.0, 4.0, 3.0, 4.0)),
Expand All @@ -532,7 +547,7 @@ mod tests {
Some(vec![]),
None,
])?;
let expected = nullable_rect_column(vec![Some((0.0, 0.0, 1.0, 1.0)), None, None])?;
let expected = nullable_wgs84_rect_column(vec![Some((0.0, 0.0, 1.0, 1.0)), None, None])?;
assert_arrays_eq!(boxes(multipolygons)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -545,7 +560,7 @@ mod tests {

let point_dtype = point_column(vec![0.0], vec![0.0])?.dtype().as_nullable();
let null_const = ConstantArray::new(Scalar::null(point_dtype), 2).into_array();
let expected = nullable_rect_column(vec![None, None])?;
let expected = nullable_wgs84_rect_column(vec![None, None])?;
assert_arrays_eq!(boxes(null_const)?, expected, &mut ctx);
Ok(())
}
Expand All @@ -564,11 +579,24 @@ mod tests {
};
assert_eq!(boxes.len(), 3);

let expected = nullable_rect_column(vec![Some((1.0, 2.0, 1.0, 2.0)); 3])?;
let expected = nullable_wgs84_rect_column(vec![Some((1.0, 2.0, 1.0, 2.0)); 3])?;
assert_arrays_eq!(boxes.into_array(), expected, &mut ctx);
Ok(())
}

/// The box is in the operand's CRS, so it can be compared with other geometries in that CRS.
#[rstest]
#[case::referenced(Some("EPSG:32633"))]
#[case::unreferenced(None)]
fn box_keeps_operand_crs(#[case] crs: Option<&str>) -> VortexResult<()> {
let dtype = point_column_with_crs(vec![0.0], vec![0.0], crs)?
.dtype()
.clone();
let out = SpatialEnvelope.return_dtype(&EmptyOptions, &[dtype])?;
assert_eq!(out.as_extension().metadata::<Rect>().get_crs(), crs);
Ok(())
}

/// Output is always nullable, even over a non-nullable operand, since an empty geometry has no
/// box.
#[test]
Expand Down
41 changes: 37 additions & 4 deletions vortex-spatial/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ fn vertex_list_lists(rows: &[Vec<Vec<(f64, f64)>>]) -> VortexResult<ArrayRef> {
fn spatial_column<V: ExtVTable<Metadata = SpatialMetadata> + Default>(
storage: ArrayRef,
) -> VortexResult<ArrayRef> {
let dtype = ExtDType::<V>::try_new(wgs84(), storage.dtype().nullability())?;
spatial_column_with_metadata::<V>(wgs84(), storage)
}

/// Wrap `storage` in the geometry extension type `V` with `metadata`.
fn spatial_column_with_metadata<V: ExtVTable<Metadata = SpatialMetadata> + Default>(
metadata: SpatialMetadata,
storage: ArrayRef,
) -> VortexResult<ArrayRef> {
let dtype = ExtDType::<V>::try_new(metadata, storage.dtype().nullability())?;
Ok(ExtensionArray::try_new(dtype.erased(), storage)?.into_array())
}

Expand All @@ -102,6 +110,16 @@ pub fn point_column(xs: Vec<f64>, ys: Vec<f64>) -> VortexResult<ArrayRef> {
spatial_column::<Point>(xy_struct(xs, ys)?)
}

/// A `Point` column over the given x/y coordinates in coordinate reference system `crs`.
pub fn point_column_with_crs(
xs: Vec<f64>,
ys: Vec<f64>,
crs: Option<&str>,
) -> VortexResult<ArrayRef> {
let metadata = SpatialMetadata::new(crs.map(str::to_owned)).with_dimension(Dimension::Xy);
spatial_column_with_metadata::<Point>(metadata, xy_struct(xs, ys)?)
}

/// A nullable `Point` column: `None` rows are null. Null rows carry placeholder coordinates in
/// storage that the spatial kernels must never decode (they filter nulls before decoding).
pub fn nullable_point_column(points: Vec<Option<(f64, f64)>>) -> VortexResult<ArrayRef> {
Expand Down Expand Up @@ -195,9 +213,24 @@ pub fn rect_column(boxes: Vec<(f64, f64, f64, f64)>) -> VortexResult<ArrayRef> {
}

/// A nullable 2D `Rect` (`geoarrow.box`) column: `None` rows are null boxes (placeholder ordinates
/// in storage). Tagged with default metadata, matching the `envelope` scalar fn and `GeometryAabb`
/// stat outputs.
/// in storage). Tagged with default metadata, matching the `GeometryAabb` stat output.
pub fn nullable_rect_column(boxes: Vec<Option<(f64, f64, f64, f64)>>) -> VortexResult<ArrayRef> {
nullable_rect_column_with_metadata(SpatialMetadata::default(), boxes)
}

/// A nullable 2D `Rect` column like [`nullable_rect_column`], in the WGS 84 CRS of the other test
/// geometry columns, matching the `envelope` scalar fn output over them.
pub fn nullable_wgs84_rect_column(
boxes: Vec<Option<(f64, f64, f64, f64)>>,
) -> VortexResult<ArrayRef> {
nullable_rect_column_with_metadata(SpatialMetadata::new(wgs84().crs), boxes)
}

/// A nullable 2D `Rect` column with `metadata`; see [`nullable_rect_column`].
fn nullable_rect_column_with_metadata(
metadata: SpatialMetadata,
boxes: Vec<Option<(f64, f64, f64, f64)>>,
) -> VortexResult<ArrayRef> {
let len = boxes.len();
let field = |select: fn(&(f64, f64, f64, f64)) -> f64| {
PrimitiveArray::from_iter(boxes.iter().map(|b| b.as_ref().map_or(0.0, select))).into_array()
Expand All @@ -215,7 +248,7 @@ pub fn nullable_rect_column(boxes: Vec<Option<(f64, f64, f64, f64)>>) -> VortexR
)?
.into_array();
let ext = ExtDType::<Rect>::try_new(
SpatialMetadata::default().with_dimension(Dimension::Xy),
metadata.with_dimension(Dimension::Xy),
Nullability::Nullable,
)?;
Ok(ExtensionArray::try_new(ext.erased(), storage)?.into_array())
Expand Down
Loading