use geo::BoundingRect;
use geo_types::Geometry;
use geo_types::Polygon as GeoPolygon;
use geo_types::Rect;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::memory::BufferAllocatorRef;
use vortex_array::scalar_fn::unstable::row::InputElement;
use vortex_array::scalar_fn::unstable::row::OutputSink;
use vortex_array::scalar_fn::unstable::row::Preinitialized;
use vortex_array::scalar_fn::unstable::row::RowVisitor;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use crate::extension::build_polygon_storage;
use crate::extension::coordinate::Dimension;
use crate::extension::geometries;
use crate::extension::is_native_geometry;
use crate::extension::polygon_storage_dtype;
type ConstBoundingRects = (Option<Option<Rect<f64>>>, Option<Option<Rect<f64>>>);
pub(crate) fn visit_binary_geo_predicate<V: RowVisitor>(
visitor: V,
compute: fn(&Geometry<f64>, &Geometry<f64>) -> bool,
precheck: fn(&Rect<f64>, &Rect<f64>) -> Option<bool>,
) -> VortexResult<V::VisitResult> {
visitor.visit_prepared::<(GeometryRow, GeometryRow), bool, ConstBoundingRects>(
|(left, right)| {
(
left.map(BoundingRect::bounding_rect),
right.map(BoundingRect::bounding_rect),
)
},
move |constant_bounds, (left, right)| {
let result = match constant_bounds {
(Some(Some(left_bounds)), None) => right
.bounding_rect()
.and_then(|right_bounds| precheck(left_bounds, &right_bounds)),
(None, Some(Some(right_bounds))) => left
.bounding_rect()
.and_then(|left_bounds| precheck(&left_bounds, right_bounds)),
_ => None,
};
result.unwrap_or_else(|| compute(left, right))
},
)
}
pub(crate) struct GeometryRow;
unsafe impl InputElement for GeometryRow {
type Column = Vec<Geometry<f64>>;
type Constant = Geometry<f64>;
type View<'a> = &'a [Geometry<f64>];
type Elem<'a> = &'a Geometry<f64>;
const DENSE_SAFE: bool = false;
const DECODE_INFALLIBLE: bool = false;
fn validate(dtype: &DType) -> VortexResult<()> {
vortex_ensure!(
is_native_geometry(dtype),
"spatial: operand {dtype} is not a native geometry type"
);
Ok(())
}
fn decode(array: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self::Column> {
geometries(&array, ctx)
}
fn decode_constant(array: ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self::Constant> {
let mut geometries = Self::decode(array.slice(0..1)?, ctx)?;
vortex_ensure!(
geometries.len() == 1,
"a geometry batch constant must decode to one value, got {}",
geometries.len(),
);
Ok(geometries
.pop()
.vortex_expect("the geometry batch constant length was validated"))
}
fn get(column: &Self::Column, index: usize) -> &Geometry<f64> {
&column[index]
}
fn get_constant(constant: &Self::Constant) -> &Geometry<f64> {
constant
}
fn view(column: &Self::Column) -> Self::View<'_> {
column.as_slice()
}
fn get_from_view<'a>(view: &Self::View<'a>, index: usize) -> &'a Geometry<f64>
where
Self: 'a,
{
&view[index]
}
unsafe fn get_from_view_unchecked<'a>(view: &Self::View<'a>, index: usize) -> &'a Geometry<f64>
where
Self: 'a,
{
unsafe { view.get_unchecked(index) }
}
}
pub(crate) struct PolygonSink {
polygons: Vec<GeoPolygon<f64>>,
allocator: BufferAllocatorRef,
}
fn empty_polygon() -> GeoPolygon<f64> {
GeoPolygon::new(geo_types::LineString::new(vec![]), vec![])
}
unsafe impl OutputSink for PolygonSink {
type Params = ();
type Rows<'a> = Preinitialized<&'a mut [GeoPolygon<f64>]>;
type Row<'a> = &'a mut GeoPolygon<f64>;
type WriteToken = ();
fn storage_dtype((): &Self::Params) -> DType {
polygon_storage_dtype(Dimension::Xy, Nullability::NonNullable)
}
fn with_capacity(
rows: usize,
(): &Self::Params,
allocator: &BufferAllocatorRef,
) -> VortexResult<Self> {
Ok(Self {
polygons: vec![empty_polygon(); rows],
allocator: allocator.clone(),
})
}
fn rows(&mut self) -> Self::Rows<'_> {
Preinitialized(self.polygons.as_mut_slice())
}
unsafe fn row_unchecked<'a>(rows: &'a mut Self::Rows<'_>, index: usize) -> Self::Row<'a> {
unsafe { rows.0.get_unchecked_mut(index) }
}
unsafe fn finish(self) -> VortexResult<ArrayRef> {
build_polygon_storage(&self.polygons, &self.allocator)
}
}