You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "james-willis (via GitHub)" <gi...@apache.org> on 2026/06/12 01:47:37 UTC

[PR] feat(rust/sedona-testing): add ergonomic raster function test harness [sedona-db]

james-willis opened a new pull request, #945:
URL: https://github.com/apache/sedona-db/pull/945

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(rust/sedona-testing): add ergonomic raster function test harness [sedona-db]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #945:
URL: https://github.com/apache/sedona-db/pull/945#discussion_r3400389693


##########
python/sedonadb/tests/functions/conftest.py:
##########
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+import sedonadb
+
+
+@pytest.fixture
+def zarr_group(tmp_path):

Review Comment:
   yeah this is just Fable's first go, I havent even look at this code yet. probably will just use numpy for creating rasters



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(rust/sedona-testing): add ergonomic raster function test harness [sedona-db]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #945:
URL: https://github.com/apache/sedona-db/pull/945#discussion_r3400376496


##########
python/sedonadb/tests/functions/conftest.py:
##########
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+import sedonadb
+
+
+@pytest.fixture
+def zarr_group(tmp_path):

Review Comment:
   It would be great to find a way to have an ND fixture without the zarr dependency (for the sedonadb package...for sedonadb-zarr this will be great)



##########
rust/sedona-testing/src/raster_spec.rs:
##########
@@ -0,0 +1,688 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Terse raster fixtures for tests.
+//!
+//! [RasterSpec] is to rasters what WKT is to geometries: a compact way to
+//! declare a test input without hand-driving [RasterBuilder]. A spec is a
+//! plain value — build it once, clone it, pass it to the `invoke_raster_*`
+//! helpers on `ScalarUdfTester`, or use it as the expected side of an
+//! assertion.
+//!
+//! ```
+//! use sedona_schema::raster::BandDataType;
+//! use sedona_testing::raster_spec::RasterSpec;
+//!
+//! // A 4x5 raster with one Float32 band of sequential pixel values:
+//! let raster = RasterSpec::d2(4, 5).band(BandDataType::Float32).build();
+//!
+//! // A 3-D datacube band with explicit pixel values:
+//! let cube = RasterSpec::nd(&["time", "y", "x"], &[2, 2, 2])
+//!     .band_values(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
+//!     .build();
+//! ```
+
+use arrow_array::{Array, ArrayRef, Int64Array, ListArray, StringArray, StructArray};
+use datafusion_common::ScalarValue;
+use datafusion_expr::{Expr, Literal};
+use sedona_raster::array::RasterStructArray;
+use sedona_raster::builder::RasterBuilder;
+use sedona_raster::traits::is_spatial_dim_pair;
+use sedona_schema::crs::lnglat;
+use sedona_schema::raster::BandDataType;
+use std::sync::Arc;
+
+use crate::rasters::assert_raster_arrays_equal;
+
+/// A pixel type that can fill a test band.
+///
+/// Implemented for the ten Rust types matching [BandDataType]. Bytes are
+/// little-endian, matching the raster byte convention.
+pub trait PixelValue: sealed::Sealed + Copy {
+    /// The [BandDataType] corresponding to this Rust type.
+    const DATA_TYPE: BandDataType;
+
+    /// Append this value's little-endian bytes to `out`.
+    fn extend_le_bytes(&self, out: &mut Vec<u8>);
+
+    /// Decode one value from its little-endian bytes.
+    ///
+    /// Panics if `bytes` is not exactly this type's width (test code).
+    fn from_le_slice(bytes: &[u8]) -> Self;
+}
+
+mod sealed {
+    pub trait Sealed {}
+}
+
+macro_rules! impl_pixel_value {
+    ($($ty:ty => $data_type:expr),* $(,)?) => {
+        $(
+            impl sealed::Sealed for $ty {}
+
+            impl PixelValue for $ty {
+                const DATA_TYPE: BandDataType = $data_type;
+
+                fn extend_le_bytes(&self, out: &mut Vec<u8>) {
+                    out.extend_from_slice(&self.to_le_bytes());
+                }
+
+                fn from_le_slice(bytes: &[u8]) -> Self {
+                    Self::from_le_bytes(bytes.try_into().expect("pixel byte width"))
+                }
+            }
+        )*
+    };
+}
+
+impl_pixel_value!(
+    u8 => BandDataType::UInt8,
+    i8 => BandDataType::Int8,
+    u16 => BandDataType::UInt16,
+    i16 => BandDataType::Int16,
+    u32 => BandDataType::UInt32,
+    i32 => BandDataType::Int32,
+    u64 => BandDataType::UInt64,
+    i64 => BandDataType::Int64,
+    f32 => BandDataType::Float32,
+    f64 => BandDataType::Float64,
+);
+
+/// Sequential fill (0, 1, 2, …) for a band's default pixel data,
+/// little-endian, wrapping where the value range is narrower than the
+/// pixel count.
+fn sequential_band_bytes(data_type: BandDataType, pixel_count: usize) -> Vec<u8> {
+    macro_rules! fill {
+        ($ty:ty) => {{
+            let mut out = Vec::with_capacity(pixel_count * std::mem::size_of::<$ty>());
+            for i in 0..pixel_count {
+                out.extend_from_slice(&(i as $ty).to_le_bytes());
+            }
+            out
+        }};
+    }
+
+    match data_type {
+        BandDataType::UInt8 => fill!(u8),
+        BandDataType::Int8 => fill!(i8),
+        BandDataType::UInt16 => fill!(u16),
+        BandDataType::Int16 => fill!(i16),
+        BandDataType::UInt32 => fill!(u32),
+        BandDataType::Int32 => fill!(i32),
+        BandDataType::UInt64 => fill!(u64),
+        BandDataType::Int64 => fill!(i64),
+        BandDataType::Float32 => fill!(f32),
+        BandDataType::Float64 => fill!(f64),
+    }
+}
+
+#[derive(Clone, Debug)]
+struct BandSpec {
+    name: Option<String>,
+    dims: Vec<String>,
+    shape: Vec<i64>,
+    data_type: BandDataType,
+    nodata: Option<Vec<u8>>,
+    /// `None` means "fill with sequential values at build time". OutDb bands
+    /// set `Some(vec![])` — the empty-`data` OutDb convention.
+    data: Option<Vec<u8>>,
+    outdb_uri: Option<String>,
+    outdb_format: Option<String>,
+}
+
+/// A compact, declarative raster fixture.
+///
+/// Construct with [RasterSpec::d2] or [RasterSpec::nd], add bands with the
+/// `band*` methods, then either [build](RasterSpec::build) a one-row
+/// `StructArray`, take a [scalar](RasterSpec::scalar), or combine several
+/// specs (with nulls) via [raster_array].
+///
+/// Defaults: transform `[0, 1, 0, 0, 0, -1]` (unit pixels, north-up, origin
+/// 0,0), lng/lat CRS, sequential pixel values per band.
+#[derive(Clone, Debug)]
+pub struct RasterSpec {
+    transform: [f64; 6],
+    /// Raster-level spatial dims, X-first (e.g. `["x", "y"]`).
+    spatial_dims: Vec<String>,
+    /// Sizes matching `spatial_dims` (`[width, height]`).
+    spatial_shape: Vec<i64>,
+    crs: Option<String>,
+    /// Default band layout, C-order (slowest dim first, e.g. `["y", "x"]`).
+    default_band_dims: Vec<String>,
+    default_band_shape: Vec<i64>,
+    bands: Vec<BandSpec>,
+}
+
+impl RasterSpec {
+    /// A 2-D raster of `width` x `height` pixels. Bands default to
+    /// `dims=["y","x"]`, `shape=[height, width]`.
+    pub fn d2(width: i64, height: i64) -> Self {
+        Self {
+            transform: [0.0, 1.0, 0.0, 0.0, 0.0, -1.0],
+            spatial_dims: vec!["x".to_string(), "y".to_string()],
+            spatial_shape: vec![width, height],
+            crs: Some(lnglat().unwrap().to_crs_string()),
+            default_band_dims: vec!["y".to_string(), "x".to_string()],
+            default_band_shape: vec![height, width],
+            bands: Vec::new(),
+        }
+    }
+
+    /// An N-D raster from a default band layout in C order (slowest dim
+    /// first, e.g. `["time", "y", "x"]` with shape `[t, h, w]`).
+    ///
+    /// The trailing two dims must be a recognized spatial pair (`y`/`x`,
+    /// `lat`/`lon`, `latitude`/`longitude`); they become the raster-level
+    /// `spatial_dims`/`spatial_shape` in X-first order, matching what the
+    /// readers emit.
+    pub fn nd(band_dims: &[&str], band_shape: &[i64]) -> Self {
+        assert!(
+            band_dims.len() >= 2 && band_dims.len() == band_shape.len(),
+            "RasterSpec::nd requires >= 2 dims and dims/shape of equal length, \
+             got dims {band_dims:?} and shape {band_shape:?}"
+        );
+        let n = band_dims.len();
+        assert!(
+            is_spatial_dim_pair(band_dims[n - 2], band_dims[n - 1]),
+            "RasterSpec::nd requires the trailing two dims to be a spatial pair \
+             (e.g. [\"y\", \"x\"]), got {band_dims:?}"
+        );
+        Self {
+            transform: [0.0, 1.0, 0.0, 0.0, 0.0, -1.0],
+            spatial_dims: vec![band_dims[n - 1].to_string(), band_dims[n - 2].to_string()],
+            spatial_shape: vec![band_shape[n - 1], band_shape[n - 2]],
+            crs: Some(lnglat().unwrap().to_crs_string()),
+            default_band_dims: band_dims.iter().map(|d| d.to_string()).collect(),
+            default_band_shape: band_shape.to_vec(),
+            bands: Vec::new(),
+        }
+    }
+
+    /// Override the CRS (`None` clears it; the default is lng/lat).
+    pub fn crs(mut self, crs: Option<&str>) -> Self {
+        self.crs = crs.map(|c| c.to_string());
+        self
+    }
+
+    /// Override the GDAL geotransform
+    /// `[origin_x, scale_x, skew_x, origin_y, skew_y, scale_y]`.
+    pub fn transform(mut self, transform: [f64; 6]) -> Self {
+        self.transform = transform;
+        self
+    }

Review Comment:
   No need to do this here, but Wwhen we get to some more spatial interactions, setting this from a BoundingBox might also be nice



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(rust/sedona-testing): add ergonomic raster function test harness [sedona-db]

Posted by "james-willis (via GitHub)" <gi...@apache.org>.
james-willis commented on code in PR #945:
URL: https://github.com/apache/sedona-db/pull/945#discussion_r3415238089


##########
rust/sedona-testing/src/raster_spec.rs:
##########
@@ -0,0 +1,688 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Terse raster fixtures for tests.
+//!
+//! [RasterSpec] is to rasters what WKT is to geometries: a compact way to
+//! declare a test input without hand-driving [RasterBuilder]. A spec is a
+//! plain value — build it once, clone it, pass it to the `invoke_raster_*`
+//! helpers on `ScalarUdfTester`, or use it as the expected side of an
+//! assertion.
+//!
+//! ```
+//! use sedona_schema::raster::BandDataType;
+//! use sedona_testing::raster_spec::RasterSpec;
+//!
+//! // A 4x5 raster with one Float32 band of sequential pixel values:
+//! let raster = RasterSpec::d2(4, 5).band(BandDataType::Float32).build();
+//!
+//! // A 3-D datacube band with explicit pixel values:
+//! let cube = RasterSpec::nd(&["time", "y", "x"], &[2, 2, 2])
+//!     .band_values(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
+//!     .build();
+//! ```
+
+use arrow_array::{Array, ArrayRef, Int64Array, ListArray, StringArray, StructArray};
+use datafusion_common::ScalarValue;
+use datafusion_expr::{Expr, Literal};
+use sedona_raster::array::RasterStructArray;
+use sedona_raster::builder::RasterBuilder;
+use sedona_raster::traits::is_spatial_dim_pair;
+use sedona_schema::crs::lnglat;
+use sedona_schema::raster::BandDataType;
+use std::sync::Arc;
+
+use crate::rasters::assert_raster_arrays_equal;
+
+/// A pixel type that can fill a test band.
+///
+/// Implemented for the ten Rust types matching [BandDataType]. Bytes are
+/// little-endian, matching the raster byte convention.
+pub trait PixelValue: sealed::Sealed + Copy {
+    /// The [BandDataType] corresponding to this Rust type.
+    const DATA_TYPE: BandDataType;
+
+    /// Append this value's little-endian bytes to `out`.
+    fn extend_le_bytes(&self, out: &mut Vec<u8>);
+
+    /// Decode one value from its little-endian bytes.
+    ///
+    /// Panics if `bytes` is not exactly this type's width (test code).
+    fn from_le_slice(bytes: &[u8]) -> Self;
+}
+
+mod sealed {
+    pub trait Sealed {}
+}
+
+macro_rules! impl_pixel_value {
+    ($($ty:ty => $data_type:expr),* $(,)?) => {
+        $(
+            impl sealed::Sealed for $ty {}
+
+            impl PixelValue for $ty {
+                const DATA_TYPE: BandDataType = $data_type;
+
+                fn extend_le_bytes(&self, out: &mut Vec<u8>) {
+                    out.extend_from_slice(&self.to_le_bytes());
+                }
+
+                fn from_le_slice(bytes: &[u8]) -> Self {
+                    Self::from_le_bytes(bytes.try_into().expect("pixel byte width"))
+                }
+            }
+        )*
+    };
+}
+
+impl_pixel_value!(
+    u8 => BandDataType::UInt8,
+    i8 => BandDataType::Int8,
+    u16 => BandDataType::UInt16,
+    i16 => BandDataType::Int16,
+    u32 => BandDataType::UInt32,
+    i32 => BandDataType::Int32,
+    u64 => BandDataType::UInt64,
+    i64 => BandDataType::Int64,
+    f32 => BandDataType::Float32,
+    f64 => BandDataType::Float64,
+);
+
+/// Sequential fill (0, 1, 2, …) for a band's default pixel data,
+/// little-endian, wrapping where the value range is narrower than the
+/// pixel count.
+fn sequential_band_bytes(data_type: BandDataType, pixel_count: usize) -> Vec<u8> {
+    macro_rules! fill {
+        ($ty:ty) => {{
+            let mut out = Vec::with_capacity(pixel_count * std::mem::size_of::<$ty>());
+            for i in 0..pixel_count {
+                out.extend_from_slice(&(i as $ty).to_le_bytes());
+            }
+            out
+        }};
+    }
+
+    match data_type {
+        BandDataType::UInt8 => fill!(u8),
+        BandDataType::Int8 => fill!(i8),
+        BandDataType::UInt16 => fill!(u16),
+        BandDataType::Int16 => fill!(i16),
+        BandDataType::UInt32 => fill!(u32),
+        BandDataType::Int32 => fill!(i32),
+        BandDataType::UInt64 => fill!(u64),
+        BandDataType::Int64 => fill!(i64),
+        BandDataType::Float32 => fill!(f32),
+        BandDataType::Float64 => fill!(f64),
+    }
+}
+
+#[derive(Clone, Debug)]
+struct BandSpec {
+    name: Option<String>,
+    dims: Vec<String>,
+    shape: Vec<i64>,
+    data_type: BandDataType,
+    nodata: Option<Vec<u8>>,
+    /// `None` means "fill with sequential values at build time". OutDb bands
+    /// set `Some(vec![])` — the empty-`data` OutDb convention.
+    data: Option<Vec<u8>>,
+    outdb_uri: Option<String>,
+    outdb_format: Option<String>,
+}
+
+/// A compact, declarative raster fixture.
+///
+/// Construct with [RasterSpec::d2] or [RasterSpec::nd], add bands with the
+/// `band*` methods, then either [build](RasterSpec::build) a one-row
+/// `StructArray`, take a [scalar](RasterSpec::scalar), or combine several
+/// specs (with nulls) via [raster_array].
+///
+/// Defaults: transform `[0, 1, 0, 0, 0, -1]` (unit pixels, north-up, origin
+/// 0,0), lng/lat CRS, sequential pixel values per band.
+#[derive(Clone, Debug)]
+pub struct RasterSpec {
+    transform: [f64; 6],
+    /// Raster-level spatial dims, X-first (e.g. `["x", "y"]`).
+    spatial_dims: Vec<String>,
+    /// Sizes matching `spatial_dims` (`[width, height]`).
+    spatial_shape: Vec<i64>,
+    crs: Option<String>,
+    /// Default band layout, C-order (slowest dim first, e.g. `["y", "x"]`).
+    default_band_dims: Vec<String>,
+    default_band_shape: Vec<i64>,
+    bands: Vec<BandSpec>,
+}
+
+impl RasterSpec {
+    /// A 2-D raster of `width` x `height` pixels. Bands default to
+    /// `dims=["y","x"]`, `shape=[height, width]`.
+    pub fn d2(width: i64, height: i64) -> Self {
+        Self {
+            transform: [0.0, 1.0, 0.0, 0.0, 0.0, -1.0],
+            spatial_dims: vec!["x".to_string(), "y".to_string()],
+            spatial_shape: vec![width, height],
+            crs: Some(lnglat().unwrap().to_crs_string()),
+            default_band_dims: vec!["y".to_string(), "x".to_string()],
+            default_band_shape: vec![height, width],
+            bands: Vec::new(),
+        }
+    }
+
+    /// An N-D raster from a default band layout in C order (slowest dim
+    /// first, e.g. `["time", "y", "x"]` with shape `[t, h, w]`).
+    ///
+    /// The trailing two dims must be a recognized spatial pair (`y`/`x`,
+    /// `lat`/`lon`, `latitude`/`longitude`); they become the raster-level
+    /// `spatial_dims`/`spatial_shape` in X-first order, matching what the
+    /// readers emit.
+    pub fn nd(band_dims: &[&str], band_shape: &[i64]) -> Self {
+        assert!(
+            band_dims.len() >= 2 && band_dims.len() == band_shape.len(),
+            "RasterSpec::nd requires >= 2 dims and dims/shape of equal length, \
+             got dims {band_dims:?} and shape {band_shape:?}"
+        );
+        let n = band_dims.len();
+        assert!(
+            is_spatial_dim_pair(band_dims[n - 2], band_dims[n - 1]),
+            "RasterSpec::nd requires the trailing two dims to be a spatial pair \
+             (e.g. [\"y\", \"x\"]), got {band_dims:?}"
+        );
+        Self {
+            transform: [0.0, 1.0, 0.0, 0.0, 0.0, -1.0],
+            spatial_dims: vec![band_dims[n - 1].to_string(), band_dims[n - 2].to_string()],
+            spatial_shape: vec![band_shape[n - 1], band_shape[n - 2]],
+            crs: Some(lnglat().unwrap().to_crs_string()),
+            default_band_dims: band_dims.iter().map(|d| d.to_string()).collect(),
+            default_band_shape: band_shape.to_vec(),
+            bands: Vec::new(),
+        }
+    }
+
+    /// Override the CRS (`None` clears it; the default is lng/lat).
+    pub fn crs(mut self, crs: Option<&str>) -> Self {
+        self.crs = crs.map(|c| c.to_string());
+        self
+    }
+
+    /// Override the GDAL geotransform
+    /// `[origin_x, scale_x, skew_x, origin_y, skew_y, scale_y]`.
+    pub fn transform(mut self, transform: [f64; 6]) -> Self {
+        self.transform = transform;
+        self
+    }

Review Comment:
   I'm adding some logics this week to build transforms from bboxes to support `spatial:bbox` attributes as a way to georeference datasets.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(rust/sedona-testing): add ergonomic raster function test harness [sedona-db]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot merged PR #945:
URL: https://github.com/apache/sedona-db/pull/945


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org