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
7 changes: 5 additions & 2 deletions vortex-spatial/src/scalar_fn/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use vortex_error::VortexResult;
use vortex_session::VortexSession;
use vortex_session::registry::CachedId;

use crate::scalar_fn::row::ensure_eq_crs;
use crate::scalar_fn::row::visit_binary_geo_predicate;

/// OGC `ST_Contains` between two native geometry operands, each a column or a constant
Expand Down Expand Up @@ -57,9 +58,10 @@ impl RowFn for SpatialContains {
fn dispatch<V: RowVisitor>(
&self,
_options: &Self::Options,
_args: &[DType],
args: &[DType],
visitor: V,
) -> VortexResult<V::VisitResult> {
ensure_eq_crs(args)?;
// Containment is not symmetric: `a` is always the container and `b` the contained. A
// container's rect must cover the contained's rect (`Rect::contains` is the closed
// test), so a contained rect poking outside proves the row false.
Expand Down Expand Up @@ -124,8 +126,9 @@ mod tests {
let mut buf = Vec::new();
wkb::writer::write_geometry(&mut buf, geometry, &WriteOptions::default())
.map_err(|e| vortex_err!("writing WKB failed: {e}"))?;
// The CRS of the `test_harness` columns these constants are compared with.
let wkb = Scalar::extension::<WellKnownBinary>(
SpatialMetadata::default(),
SpatialMetadata::new(Some("EPSG:4326".to_owned())),
Scalar::binary(buf, Nullability::NonNullable),
);
let session = VortexSession::empty();
Expand Down
4 changes: 3 additions & 1 deletion vortex-spatial/src/scalar_fn/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use vortex_session::VortexSession;
use vortex_session::registry::CachedId;

use crate::scalar_fn::row::GeometryRow;
use crate::scalar_fn::row::ensure_eq_crs;

/// Planar (Euclidean) `ST_Distance` (no geodesic correction) between two native geometry
/// operands, each a column or a constant literal.
Expand Down Expand Up @@ -61,9 +62,10 @@ impl RowFn for SpatialDistance {
fn dispatch<V: RowVisitor>(
&self,
_options: &Self::Options,
_args: &[DType],
args: &[DType],
visitor: V,
) -> VortexResult<V::VisitResult> {
ensure_eq_crs(args)?;
// Distance is a value, not a verdict: no bounding-rect test can decide it.
visitor.visit::<(GeometryRow, GeometryRow), f64>(|(a, b)| Euclidean.distance(a, b))
}
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
7 changes: 5 additions & 2 deletions vortex-spatial/src/scalar_fn/intersects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use vortex_error::VortexResult;
use vortex_session::VortexSession;
use vortex_session::registry::CachedId;

use crate::scalar_fn::row::ensure_eq_crs;
use crate::scalar_fn::row::visit_binary_geo_predicate;

/// OGC `ST_Intersects` (not disjoint; boundary contact counts) between two native geometry
Expand Down Expand Up @@ -56,9 +57,10 @@ impl RowFn for SpatialIntersects {
fn dispatch<V: RowVisitor>(
&self,
_options: &Self::Options,
_args: &[DType],
args: &[DType],
visitor: V,
) -> VortexResult<V::VisitResult> {
ensure_eq_crs(args)?;
// Disjoint bounding rects prove the geometries disjoint; rect contact (closed test)
// falls through to the exact test.
visit_binary_geo_predicate(
Expand Down Expand Up @@ -145,8 +147,9 @@ mod tests {
let mut buf = Vec::new();
wkb::writer::write_geometry(&mut buf, geometry, &WriteOptions::default())
.map_err(|e| vortex_err!("writing WKB failed: {e}"))?;
// The CRS of the `test_harness` columns these constants are compared with.
let wkb = Scalar::extension::<WellKnownBinary>(
SpatialMetadata::default(),
SpatialMetadata::new(Some("EPSG:4326".to_owned())),
Scalar::binary(buf, Nullability::NonNullable),
);
let session = VortexSession::empty();
Expand Down
Loading
Loading