You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ja...@apache.org on 2026/09/04 20:41:17 UTC

(sedona-db) branch main updated: test(integration): RS_Resample parity between SedonaDB and Sedona Spark (#1219)

This is an automated email from the ASF dual-hosted git repository.

james-willis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/sedona-db.git


The following commit(s) were added to refs/heads/main by this push:
     new d45636e60 test(integration): RS_Resample parity between SedonaDB and Sedona Spark (#1219)
d45636e60 is described below

commit d45636e60c7dee9534cca1fc4266f70d79568353
Author: James Willis <ja...@users.noreply.github.com>
AuthorDate: Fri Sep 4 13:41:12 2026 -0700

    test(integration): RS_Resample parity between SedonaDB and Sedona Spark (#1219)
---
 integration/spark-parity/test_rs_resample.py       | 322 +++++++++++++++++++++
 ...raster_out.py => test_rs_setbandnodatavalue.py} |  89 ++----
 python/sedonadb/python/sedonadb/raster_testing.py  |  86 +++++-
 python/sedonadb/python/sedonadb/testing.py         |  39 +--
 python/sedonadb/tests/test_raster_testing.py       |  21 ++
 5 files changed, 468 insertions(+), 89 deletions(-)

diff --git a/integration/spark-parity/test_rs_resample.py b/integration/spark-parity/test_rs_resample.py
new file mode 100644
index 000000000..7d4b8d0a2
--- /dev/null
+++ b/integration/spark-parity/test_rs_resample.py
@@ -0,0 +1,322 @@
+# 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.
+"""SedonaDB vs Sedona Spark parity for RS_Resample.
+
+SedonaDB's kernel deliberately mirrors Sedona Spark's positional overloads
+(the 4-arg reference-raster form, the 5-arg `(widthOrScale, heightOrScale)`
+form, and the 7-arg grid-snap form), so one SQL string drives both engines.
+The nearest-neighbour path agrees bit-for-bit — dimension mode, scale mode
+including the grown-extent border fill, the reference-raster and grid-snap
+overloads — and those cases are anchored: nearest resampling on the standard
+grid is hand-computable (integer upsampling is block replication, any regrid
+picks the source pixel under each output pixel centre), so `expected=` states
+the exact raster rather than trusting cross-engine agreement alone.
+
+Interpolation is where the engines part ways. Sedona Spark really implements
+only NearestNeighbor, Bilinear, and Bicubic, silently resampling with
+NearestNeighbor for every other name it accepts (including SedonaDB's other
+documented spellings Cubic, CubicSpline, Lanczos, Average, Mode — and unknown
+names outright). SedonaDB runs a real kernel for all seven spellings and
+rejects unknown names. Even where both engines really interpolate (Bilinear,
+Bicubic) the kernels differ pixel-wise. All of that is `xfail`-cataloged, same
+policy as the other suites: the reason states both engines' observed behavior,
+and where one engine raises, that error is what trips the xfail.
+"""
+
+import numpy as np
+import pytest
+
+from sedonadb.raster_testing import (
+    RANDOM_GRID_BANDS as BANDS,
+    RANDOM_GRID_BBOX as BBOX,
+    RANDOM_GRID_HEIGHT as HEIGHT,
+    RANDOM_GRID_WIDTH as WIDTH,
+    DecodedRaster,
+    random_raster_data,
+    write_grid_geotiff,
+)
+from sedonadb.testing import SedonaDB, compare
+from sedonadb.testing_spark import SedonaSpark
+
+# The standard grid (BBOX over 7x6 gives 2x3 pixels, extent 14 wide, 18 tall),
+# imported under short names because the anchors do arithmetic with it.
+
+# Representable in every dtype used here, so nodata packs into the band exactly.
+NODATA = 200.0
+
+
+def _data(dtype="uint8"):
+    """The seeded pixels every view registered on the standard grid holds."""
+    return random_raster_data(dtype, bands=BANDS, height=HEIGHT, width=WIDTH)
+
+
+def _register(name, tmp_path, **kwargs):
+    """Both engines with the standard random raster registered as view `name`."""
+    sedona, spark = SedonaDB(), SedonaSpark()
+    for eng in (sedona, spark):
+        eng.create_random_raster_view(name, tmp_path / f"{name}.tif", **kwargs)
+    return sedona, spark
+
+
+def _nearest_picks(n_src, n_out):
+    """Source index under each output pixel centre — the nearest-neighbour
+    rule both engines share on an extent-preserving regrid."""
+    return [int((i + 0.5) * n_src / n_out) for i in range(n_out)]
+
+
+# Upsampling the standard grid 2x on each axis: pure block replication. Several
+# tests share this anchor (it is what NearestNeighbor — and, on Sedona Spark,
+# every fallback spelling — produces for 14x12). Dimension mode preserves the
+# extent, so these anchors sit on the fixture's own BBOX.
+UP_PIXELS = np.repeat(np.repeat(_data(), 2, axis=1), 2, axis=2)
+
+
+@pytest.mark.parametrize(
+    "dtype,nodata",
+    [("uint8", None), ("float64", -12345.5)],
+    ids=["uint8", "float64-nodata"],
+)
+def test_rs_resample_upsample(dtype, nodata, tmp_path):
+    """Dimension-mode nearest upsampling by 2x replicates each pixel into a
+    2x2 block, halves the pixel size, and passes band nodata through."""
+    sedona, spark = _register("up_src", tmp_path, dtype=dtype, nodata=nodata)
+    sql = "SELECT RS_Resample(rast, 14.0, 12.0, false, 'NearestNeighbor') FROM up_src"
+    pixels = np.repeat(np.repeat(_data(dtype), 2, axis=1), 2, axis=2)
+    anchor = DecodedRaster(pixels, nodata=[nodata] * BANDS, bbox=BBOX)
+    compare(sql, sedona, spark, expected=anchor)
+
+
+def test_rs_resample_identity(tmp_path):
+    """Resampling to the source's own dimensions returns the raster unchanged."""
+    sedona, spark = _register("id_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 7.0, 6.0, false, 'NearestNeighbor') FROM id_src"
+    compare(sql, sedona, spark, expected=DecodedRaster.random())
+
+
+def test_rs_resample_downsample(tmp_path):
+    """Dimension-mode nearest downsampling to 4x3 (a non-integer ratio on the
+    x axis) picks the source pixel under each output pixel centre."""
+    sedona, spark = _register("down_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 4.0, 3.0, false, 'NearestNeighbor') FROM down_src"
+    pixels = _data()[:, _nearest_picks(HEIGHT, 3)][:, :, _nearest_picks(WIDTH, 4)]
+    anchor = DecodedRaster(pixels, nodata=[None, None], bbox=BBOX)
+    compare(sql, sedona, spark, expected=anchor)
+
+
+def test_rs_resample_integer_literal_dimensions(tmp_path):
+    """Integer SQL literals for widthOrScale/heightOrScale work on both
+    engines (the docs write 14.0; users write 14)."""
+    sedona, spark = _register("int_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 14, 12, false, 'NearestNeighbor') FROM int_src"
+    anchor = DecodedRaster(UP_PIXELS, nodata=[None, None], bbox=BBOX)
+    compare(sql, sedona, spark, expected=anchor)
+
+
+def test_rs_resample_lowercase_algorithm(tmp_path):
+    """'nearestneighbor' in lowercase resamples nearest on both engines.
+
+    Only SedonaDB's case-folding is really on trial: Sedona Spark resamples
+    nearest for any name it doesn't recognize, so it cannot fail this."""
+    sedona, spark = _register("lower_src", tmp_path)
+    sql = (
+        "SELECT RS_Resample(rast, 14.0, 12.0, false, 'nearestneighbor') FROM lower_src"
+    )
+    anchor = DecodedRaster(UP_PIXELS, nodata=[None, None], bbox=BBOX)
+    compare(sql, sedona, spark, expected=anchor)
+
+
+def test_rs_resample_scale_exact_tiling(tmp_path):
+    """Scale mode with a pixel size that tiles the extent exactly (1x1 into
+    14x18) keeps the origin and replicates each 2x3 pixel into a 2x3 block."""
+    sedona, spark = _register("tile_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 1.0, -1.0, true, 'NearestNeighbor') FROM tile_src"
+    pixels = np.repeat(np.repeat(_data(), 3, axis=1), 2, axis=2)
+    anchor = DecodedRaster(pixels, nodata=[None, None], bbox=BBOX)
+    compare(sql, sedona, spark, expected=anchor)
+
+
+@pytest.mark.parametrize(
+    "nodata,fill", [(NODATA, NODATA), (None, 0)], ids=["nodata-fill", "zero-fill"]
+)
+def test_rs_resample_scale_grows_extent(nodata, fill, tmp_path):
+    """Scale mode keeps the requested 4x4 pixel size exact and grows the
+    extent to whole pixels: ceil(14/4) x ceil(18/4) = 4x5 output pixels
+    spanning 16x20. Centres over the source pick nearest (columns 1/3/5,
+    rows 0/2/3/4); the grown right column and bottom row fall outside and
+    fill with the band nodata — or 0 when the band has none, a convention
+    both engines share."""
+    sedona, spark = _register("grow_src", tmp_path, nodata=nodata)
+    sql = "SELECT RS_Resample(rast, 4.0, -4.0, true, 'NearestNeighbor') FROM grow_src"
+    pixels = np.full((BANDS, 5, 4), fill, dtype="uint8")
+    pixels[:, :4, :3] = _data()[:, [0, 2, 3, 4]][:, :, [1, 3, 5]]
+    anchor = DecodedRaster(
+        pixels, nodata=[nodata] * BANDS, bbox=(100.0, 480.0, 116.0, 500.0)
+    )
+    compare(sql, sedona, spark, expected=anchor)
+
+
+def test_rs_resample_reference_raster(tmp_path):
+    """The 4-arg overload takes the grid from a reference raster: a 14x12
+    reference over the source's extent reproduces the 2x upsample."""
+    sedona, spark = _register("ref_src", tmp_path)
+    write_grid_geotiff(tmp_path / "refgrid.tif", bbox=BBOX, width=14, height=12)
+    for eng in (sedona, spark):
+        eng.create_raster_view("refgrid", tmp_path / "refgrid.tif")
+    sql = (
+        "SELECT RS_Resample(a.rast, b.rast, false, 'NearestNeighbor') "
+        "FROM ref_src a CROSS JOIN refgrid b"
+    )
+    anchor = DecodedRaster(UP_PIXELS, nodata=[None, None], bbox=BBOX)
+    compare(sql, sedona, spark, expected=anchor)
+
+
+def test_rs_resample_grid_snap(tmp_path):
+    """The 7-arg overload snaps the origin to (gridX, gridY). Snapping to
+    (99.5, 500.5) at the source pixel size shifts the grid half a pixel
+    up-left, so the output grows to 8x7: centres over the source pick the
+    pixel they land in, and the far column/row (centres at x=114.5, y=481)
+    fall outside and zero-fill."""
+    sedona, spark = _register("snap_src", tmp_path)
+    sql = (
+        "SELECT RS_Resample(rast, 2.0, -3.0, 99.5, 500.5, true, 'NearestNeighbor') "
+        "FROM snap_src"
+    )
+    pixels = np.zeros((BANDS, 7, 8), dtype="uint8")
+    pixels[:, :6, :7] = _data()
+    anchor = DecodedRaster(
+        pixels, nodata=[None, None], bbox=(99.5, 479.5, 115.5, 500.5)
+    )
+    compare(sql, sedona, spark, expected=anchor)
+
+
+@pytest.mark.parametrize(
+    "width_or_scale,height_or_scale,use_scale",
+    [(0.0, 6.0, "false"), (0.0, -1.0, "true")],
+    ids=["zero-width", "zero-scale"],
+)
+def test_rs_resample_zero_rejected(
+    width_or_scale, height_or_scale, use_scale, tmp_path
+):
+    """Both engines refuse a zero width (dimension mode) or zero scale (scale
+    mode). Error types and messages differ, so parity here is parity on
+    refusal."""
+    sedona, spark = _register("zero_src", tmp_path)
+    sql = (
+        f"SELECT RS_Resample(rast, {width_or_scale}, {height_or_scale}, "
+        f"{use_scale}, 'NearestNeighbor') FROM zero_src"
+    )
+    for eng in (sedona, spark):
+        with pytest.raises(Exception):
+            eng.decode_raster_result(sql)
+
+
+@pytest.mark.xfail(
+    reason="scale mode with a positive scaleY: SedonaDB keeps the origin at the "
+    "source's top edge (y=500) and grids upward off the raster; Sedona Spark "
+    "rebases the origin to the bottom edge (y=482) and covers the source "
+    "south-up"
+)
+def test_rs_resample_scale_positive_scale_y(tmp_path):
+    """A positive scaleY (south-up target grid) resamples the same coverage
+    from both engines."""
+    sedona, spark = _register("posy_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 2.0, 3.0, true, 'NearestNeighbor') FROM posy_src"
+    compare(sql, sedona, spark)
+
+
+@pytest.mark.parametrize("dtype", ["uint8", "float64"])
+@pytest.mark.xfail(
+    reason="both engines really interpolate Bilinear, but the kernels differ: "
+    "SedonaDB (GDAL) clamps edge neighbourhoods to the source edge where "
+    "Sedona Spark (JAI) fills them with the band nodata or 0, and integer "
+    "results round differently"
+)
+def test_rs_resample_bilinear(dtype, tmp_path):
+    """Bilinear resampling produces the same pixels from both engines."""
+    nodata = -12345.5 if dtype == "float64" else None
+    sedona, spark = _register("bilin_src", tmp_path, dtype=dtype, nodata=nodata)
+    sql = "SELECT RS_Resample(rast, 14.0, 12.0, false, 'Bilinear') FROM bilin_src"
+    compare(sql, sedona, spark)
+
+
+@pytest.mark.xfail(
+    reason="both engines accept 'Bicubic' and really interpolate, but GDAL's "
+    "cubic and JAI's bicubic are different kernels — nearly every pixel differs"
+)
+def test_rs_resample_bicubic(tmp_path):
+    """Bicubic resampling produces the same pixels from both engines."""
+    sedona, spark = _register("bicubic_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 14.0, 12.0, false, 'Bicubic') FROM bicubic_src"
+    compare(sql, sedona, spark)
+
+
+@pytest.mark.parametrize(
+    "algorithm", ["Cubic", "CubicSpline", "Lanczos", "Average", "Mode"]
+)
+@pytest.mark.xfail(
+    reason="Sedona Spark only implements NearestNeighbor, Bilinear, and "
+    "Bicubic, silently resampling nearest for the other names it accepts "
+    "(apache/sedona#3320); SedonaDB runs the named kernel"
+)
+def test_rs_resample_spark_fallback_algorithms(algorithm, tmp_path):
+    """Every algorithm spelling SedonaDB documents resamples the same way on
+    both engines. Downsampled, because on an upsample Average and Mode agree
+    with NearestNeighbor by construction and the fallback would be invisible."""
+    sedona, spark = _register("fallback_src", tmp_path)
+    sql = f"SELECT RS_Resample(rast, 4.0, 3.0, false, '{algorithm}') FROM fallback_src"
+    compare(sql, sedona, spark)
+
+
+@pytest.mark.xfail(
+    reason="SedonaDB rejects an unknown algorithm name; Sedona Spark silently "
+    "resamples with NearestNeighbor (apache/sedona#3320)"
+)
+def test_rs_resample_unknown_algorithm(tmp_path):
+    """An unrecognized algorithm name gets the same treatment from both
+    engines."""
+    sedona, spark = _register("unknown_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 14.0, 12.0, false, 'sinc') FROM unknown_src"
+    compare(sql, sedona, spark)
+
+
+@pytest.mark.xfail(
+    reason="SedonaDB rejects a fractional width in dimension mode; Sedona "
+    "Spark truncates it (3.5 becomes 3 columns)"
+)
+def test_rs_resample_fractional_dimension(tmp_path):
+    """A fractional width in dimension mode gets the same treatment from both
+    engines."""
+    sedona, spark = _register("frac_src", tmp_path)
+    sql = "SELECT RS_Resample(rast, 3.5, 6.0, false, 'NearestNeighbor') FROM frac_src"
+    compare(sql, sedona, spark)
+
+
+@pytest.mark.xfail(
+    reason="output pixel centres landing exactly on the source's left edge "
+    "(x=100): SedonaDB (GDAL warp) samples the edge pixel; Sedona Spark "
+    "treats them as outside and zero-fills, so the first column differs"
+)
+def test_rs_resample_grid_snap_edge_centres(tmp_path):
+    """A grid snap that puts output pixel centres exactly on the source
+    boundary samples the same pixels from both engines (contrast
+    test_rs_resample_grid_snap, whose centres fall strictly inside)."""
+    sedona, spark = _register("edge_src", tmp_path)
+    sql = (
+        "SELECT RS_Resample(rast, 2.0, -3.0, 99.0, 501.0, true, 'NearestNeighbor') "
+        "FROM edge_src"
+    )
+    compare(sql, sedona, spark)
diff --git a/integration/spark-parity/test_rs_raster_out.py b/integration/spark-parity/test_rs_setbandnodatavalue.py
similarity index 80%
rename from integration/spark-parity/test_rs_raster_out.py
rename to integration/spark-parity/test_rs_setbandnodatavalue.py
index b99ebf603..ce37ce2e0 100644
--- a/integration/spark-parity/test_rs_raster_out.py
+++ b/integration/spark-parity/test_rs_setbandnodatavalue.py
@@ -14,55 +14,39 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-"""SedonaDB vs Sedona Spark parity for raster-in / raster-out functions.
+"""SedonaDB vs Sedona Spark parity for RS_SetBandNoDataValue.
 
-These exercise the raster round-trip *out* of each engine: SedonaDB decodes its
+The parity suite keeps one module per RS_ function. This one exercises the
+raster round-trip *out* of each engine: SedonaDB decodes its
 native raster column; Sedona Spark transports the result as GeoTIFF bytes
 (`RS_AsGeoTiff`) and decodes it with rasterio. Both sides land as a
 `DecodedRaster` (pixels + geotransform + per-band nodata), which is how the
 harness-level `sedonadb.testing.compare` compares a raster result — a NULL
-raster from every engine also counts as agreement. Same
-`xfail`-for-known-divergence policy as the scalar suite.
+raster from every engine also counts as agreement. Anchored expectations are
+built with `DecodedRaster.random`, the same definition
+`create_random_raster_view` registers, so a fixture and its anchor cannot
+drift apart. Same `xfail`-for-known-divergence policy as the scalar suite.
 
 Both engines are constructed directly rather than through fixtures: this suite is
 only run deliberately, so a missing pyspark, JVM, or Sedona jar should be a
 failure with a real traceback, not a skip. `SedonaSpark` caches its
 `SparkSession` on the class, so building one per test reuses the same JVM.
 
-`RS_SetBandNoDataValue` is the first case on purpose: it is raster-in/raster-out
-but passes pixels through untouched, so a mismatch is a round-trip bug, not an
-operation divergence — it isolates the transport. Pixel-transforming ops
-(RS_Resample, RS_MapAlgebra) come next, with their known divergences as xfails.
+`RS_SetBandNoDataValue` was the suite's first raster-in/raster-out function on
+purpose: it passes pixels through untouched, so a mismatch is a round-trip bug,
+not an operation divergence — it isolates the transport that every
+raster-returning module (e.g. test_rs_resample.py) rides on.
 """
 
 import pytest
 
-from sedonadb.raster_testing import DecodedRaster, random_raster_data
+from sedonadb.raster_testing import DecodedRaster
 from sedonadb.testing import SedonaDB, compare
 from sedonadb.testing_spark import SedonaSpark
 
 # Each value is representable in its dtype, so it packs into the band exactly.
 BAND_NODATA = {"uint8": 200.0, "int32": -99999.0, "float64": -12345.5}
 
-# The grid the anchored tests write and reconstruct. Anchors must state the
-# exact raster they expect, so the placement is passed to
-# create_random_raster_view explicitly rather than relying on its defaults
-# staying in sync — and the anchors state the same BBOX.
-BANDS, HEIGHT, WIDTH = 2, 6, 7
-BBOX = (100.0, 482.0, 114.0, 500.0)
-
-
-def _anchor(dtype, nodata, *, bands=BANDS, plants=None):
-    """The `DecodedRaster` a pixel-preserving setter must return on the
-    standard grid: the seeded pixels unchanged, with `nodata` per band."""
-    return DecodedRaster(
-        random_raster_data(
-            dtype, bands=bands, height=HEIGHT, width=WIDTH, plants=plants
-        ),
-        nodata=nodata,
-        bbox=BBOX,
-    )
-
 
 @pytest.mark.parametrize("dtype", list(BAND_NODATA))
 def test_rs_setbandnodata(dtype, tmp_path):
@@ -76,14 +60,7 @@ def test_rs_setbandnodata(dtype, tmp_path):
         # band's nodata must not mask or rewrite pixels that happen to hold
         # the sentinel. No nodata to start.
         eng.create_random_raster_view(
-            "src",
-            tmp_path / "src.tif",
-            dtype=dtype,
-            bands=BANDS,
-            height=HEIGHT,
-            width=WIDTH,
-            bbox=BBOX,
-            plants=plants,
+            "src", tmp_path / "src.tif", dtype=dtype, plants=plants
         )
 
     sql = f"SELECT RS_SetBandNoDataValue(rast, 1, {BAND_NODATA[dtype]}) FROM src"
@@ -92,7 +69,9 @@ def test_rs_setbandnodata(dtype, tmp_path):
         sql,
         sedona,
         spark,
-        expected=_anchor(dtype, [BAND_NODATA[dtype], None], plants=plants),
+        expected=DecodedRaster.random(
+            dtype, nodata=[BAND_NODATA[dtype], None], plants=plants
+        ),
     )
 
 
@@ -102,16 +81,12 @@ def test_rs_setbandnodata_band2(tmp_path):
     sedona, spark = SedonaDB(), SedonaSpark()
     for eng in (sedona, spark):
         eng.create_random_raster_view(
-            "band2_src",
-            tmp_path / "band2_src.tif",
-            dtype="float64",
-            bands=BANDS,
-            height=HEIGHT,
-            width=WIDTH,
-            bbox=BBOX,
+            "band2_src", tmp_path / "band2_src.tif", dtype="float64"
         )
     sql = "SELECT RS_SetBandNoDataValue(rast, 2, 5.0) FROM band2_src"
-    compare(sql, sedona, spark, expected=_anchor("float64", [None, 5.0]))
+    compare(
+        sql, sedona, spark, expected=DecodedRaster.random("float64", nodata=[None, 5.0])
+    )
 
 
 def test_rs_setbandnodata_overwrite(tmp_path):
@@ -120,19 +95,18 @@ def test_rs_setbandnodata_overwrite(tmp_path):
     sedona, spark = SedonaDB(), SedonaSpark()
     for eng in (sedona, spark):
         eng.create_random_raster_view(
-            "ow_src",
-            tmp_path / "ow_src.tif",
-            bands=1,
-            height=HEIGHT,
-            width=WIDTH,
-            bbox=BBOX,
-            nodata=7.0,
+            "ow_src", tmp_path / "ow_src.tif", bands=1, nodata=7.0
         )
     for sql, new_nodata in (
         ("SELECT RS_SetBandNoDataValue(rast, 1, 9.0) FROM ow_src", 9.0),
         ("SELECT RS_SetBandNoDataValue(rast, 1, 7.0) FROM ow_src", 7.0),
     ):
-        compare(sql, sedona, spark, expected=_anchor("uint8", [new_nodata], bands=1))
+        compare(
+            sql,
+            sedona,
+            spark,
+            expected=DecodedRaster.random(nodata=new_nodata, bands=1),
+        )
 
 
 def test_rs_setbandnodata_two_arg_single_band(tmp_path):
@@ -141,15 +115,10 @@ def test_rs_setbandnodata_two_arg_single_band(tmp_path):
     sedona, spark = SedonaDB(), SedonaSpark()
     for eng in (sedona, spark):
         eng.create_random_raster_view(
-            "two_arg_src",
-            tmp_path / "two_arg_src.tif",
-            bands=1,
-            height=HEIGHT,
-            width=WIDTH,
-            bbox=BBOX,
+            "two_arg_src", tmp_path / "two_arg_src.tif", bands=1
         )
     sql = "SELECT RS_SetBandNoDataValue(rast, 5.0) FROM two_arg_src"
-    compare(sql, sedona, spark, expected=_anchor("uint8", [5.0], bands=1))
+    compare(sql, sedona, spark, expected=DecodedRaster.random(nodata=5.0, bands=1))
 
 
 @pytest.mark.xfail(
diff --git a/python/sedonadb/python/sedonadb/raster_testing.py b/python/sedonadb/python/sedonadb/raster_testing.py
index 0a8870d9b..0f3266238 100644
--- a/python/sedonadb/python/sedonadb/raster_testing.py
+++ b/python/sedonadb/python/sedonadb/raster_testing.py
@@ -41,6 +41,16 @@ from typing import Any, List, Mapping, Optional, Tuple
 
 import numpy as np
 
+# The standard random-raster grid. `DecodedRaster.random` and
+# `DBEngine.create_random_raster_view` both default to it, so a test's fixture
+# and its `expected=` anchor derive from one definition and cannot drift
+# apart. The bbox over the 7x6 grid gives 2x3 pixels — the transform
+# (100, 2, 0, 500, 0, -3), pinned by test_raster_testing.py.
+RANDOM_GRID_BANDS = 2
+RANDOM_GRID_HEIGHT = 6
+RANDOM_GRID_WIDTH = 7
+RANDOM_GRID_BBOX = (100.0, 482.0, 114.0, 500.0)
+
 
 @dataclass
 class DecodedRaster:
@@ -79,6 +89,57 @@ class DecodedRaster:
                 ).to_gdal()
             )
 
+    @classmethod
+    def random(
+        cls,
+        dtype="uint8",
+        *,
+        bands=RANDOM_GRID_BANDS,
+        height=RANDOM_GRID_HEIGHT,
+        width=RANDOM_GRID_WIDTH,
+        bbox=None,
+        gdal_transform=None,
+        nodata=None,
+        plants=None,
+    ) -> "DecodedRaster":
+        """The standard random raster: `random_raster_data` pixels on the
+        default grid (`RANDOM_GRID_BBOX` when neither `bbox` nor
+        `gdal_transform` is given). This is exactly the raster
+        `DBEngine.create_random_raster_view` registers, so a test's
+        `expected=` anchor and the fixture it checks share one definition.
+        `nodata` is one value applied to every band, or a per-band list —
+        anchors state what an operation leaves per band; only the uniform
+        spelling is writable (see `write_geotiff`)."""
+        if bbox is None and gdal_transform is None:
+            bbox = RANDOM_GRID_BBOX
+        if not isinstance(nodata, list):
+            nodata = [nodata] * bands
+        return cls(
+            random_raster_data(
+                dtype, bands=bands, height=height, width=width, plants=plants
+            ),
+            nodata=nodata,
+            bbox=bbox,
+            gdal_transform=gdal_transform,
+        )
+
+    def write_geotiff(self, path, *, crs=None) -> None:
+        """Write this raster as a GeoTIFF at `path` (via the module-level
+        `write_geotiff`). GeoTIFF nodata is file-wide (TIFFTAG_GDAL_NODATA),
+        so the per-band `nodata` must be uniform to write."""
+        if len(set(self.nodata)) > 1:
+            raise ValueError(
+                "GeoTIFF nodata is file-wide; per-band nodata must be "
+                f"uniform to write, got {self.nodata}"
+            )
+        write_geotiff(
+            path,
+            self.pixels,
+            gdal_transform=self.gdal_transform,
+            nodata=self.nodata[0],
+            crs=crs,
+        )
+
 
 def _is_nodata(sampled, nodata) -> bool:
     """Whether a sampled value equals the band nodata, NaN-aware (a NaN
@@ -206,17 +267,22 @@ def write_random_geotiff(
 ) -> None:
     """Write a GeoTIFF of random `dtype` pixels on the given grid.
 
-    Combines `random_raster_data` (dtype extremes planted in opposite corners)
-    with `write_geotiff` — the input-raster fixture shape shared by raster warp
-    parity tests. `bbox`/`gdal_transform` (exactly one), `crs`, and `nodata`
-    are as `write_geotiff`; `plants` is as `random_raster_data`.
+    `DecodedRaster.random(...).write_geotiff(path)` as a function — the
+    input-raster fixture shape shared by raster warp parity tests.
+    `bbox`/`gdal_transform` place the grid (at most one; the standard
+    `RANDOM_GRID_BBOX` when neither is given); `crs` and `nodata` are as
+    `write_geotiff`; `plants` is as `random_raster_data`.
     """
-    data = random_raster_data(
-        dtype, bands=bands, height=height, width=width, plants=plants
-    )
-    write_geotiff(
-        path, data, bbox=bbox, gdal_transform=gdal_transform, nodata=nodata, crs=crs
-    )
+    DecodedRaster.random(
+        dtype,
+        bands=bands,
+        height=height,
+        width=width,
+        bbox=bbox,
+        gdal_transform=gdal_transform,
+        nodata=nodata,
+        plants=plants,
+    ).write_geotiff(path, crs=crs)
 
 
 def write_grid_geotiff(
diff --git a/python/sedonadb/python/sedonadb/testing.py b/python/sedonadb/python/sedonadb/testing.py
index 15ae3634a..abd096711 100644
--- a/python/sedonadb/python/sedonadb/testing.py
+++ b/python/sedonadb/python/sedonadb/testing.py
@@ -23,6 +23,11 @@ from typing import TYPE_CHECKING, Any, List, Tuple
 import geoarrow.pyarrow as ga
 import pyarrow as pa
 
+from sedonadb.raster_testing import (
+    RANDOM_GRID_BANDS,
+    RANDOM_GRID_HEIGHT,
+    RANDOM_GRID_WIDTH,
+)
 from sedonadb.utility import register_pyarrow_extension_types
 
 register_pyarrow_extension_types()
@@ -193,9 +198,9 @@ class DBEngine:
         path,
         *,
         dtype="uint8",
-        bands=2,
-        height=6,
-        width=7,
+        bands=RANDOM_GRID_BANDS,
+        height=RANDOM_GRID_HEIGHT,
+        width=RANDOM_GRID_WIDTH,
         bbox=None,
         gdal_transform=None,
         nodata=None,
@@ -204,23 +209,19 @@ class DBEngine:
         """Write a random GeoTIFF at `path` and register it as view `name`
         (see `create_raster_view`).
 
-        The grid is placed by `bbox` (`(minx, miny, maxx, maxy)`, north-up,
-        no skew) or, for grids a bbox cannot express, a raw GDAL
-        `gdal_transform` — at most one; the default is
-        `bbox=(100, 482, 114, 500)`, whose extent divided by the default 7x6
-        grid gives 2x3 pixels. The grid is small and north-up/CRS-less so
-        nothing reprojects and results stay bit-comparable across engines.
-        The pixels come from `sedonadb.raster_testing.random_raster_data`
-        with a fixed seed, so registering the same `path` on several engines
-        rewrites identical bytes and every engine sees the same raster.
-        `nodata` and `plants` are as `write_random_geotiff`.
+        The raster is exactly `DecodedRaster.random(...)` with the same
+        arguments, so a test can anchor with that constructor and the fixture
+        cannot drift from the expectation. The grid defaults to
+        `RANDOM_GRID_BBOX` on the 7x6 grid (2x3 pixels; override with `bbox`
+        or, for grids a bbox cannot express, a raw GDAL `gdal_transform` —
+        at most one). It is small and north-up/CRS-less so nothing reprojects
+        and results stay bit-comparable across engines, and the pixels are
+        seeded, so registering the same `path` on several engines rewrites
+        identical bytes and every engine sees the same raster.
         """
-        from sedonadb.raster_testing import write_random_geotiff
+        from sedonadb.raster_testing import DecodedRaster
 
-        if bbox is None and gdal_transform is None:
-            bbox = (100.0, 482.0, 114.0, 500.0)
-        write_random_geotiff(
-            path,
+        DecodedRaster.random(
             dtype,
             bands=bands,
             height=height,
@@ -229,7 +230,7 @@ class DBEngine:
             gdal_transform=gdal_transform,
             nodata=nodata,
             plants=plants,
-        )
+        ).write_geotiff(path)
         return self.create_raster_view(name, path)
 
     def decode_raster_result(self, sql):
diff --git a/python/sedonadb/tests/test_raster_testing.py b/python/sedonadb/tests/test_raster_testing.py
index 290fccd8c..33ffe6bf4 100644
--- a/python/sedonadb/tests/test_raster_testing.py
+++ b/python/sedonadb/tests/test_raster_testing.py
@@ -27,6 +27,7 @@ import pytest
 
 from sedonadb.raster_testing import (
     DecodedRaster,
+    assert_decoded_equal,
     decode_geotiff,
     write_geotiff,
     write_random_geotiff,
@@ -36,6 +37,26 @@ from sedonadb.raster_testing import (
 BBOX = (100.0, 482.0, 114.0, 500.0)
 
 
+def test_decoded_raster_random_roundtrips_through_geotiff(tmp_path):
+    """`DecodedRaster.random()` on its defaults writes and decodes back
+    unchanged, and its default grid is the historical transform — the pin
+    that keeps every fixture-vs-anchor pairing honest."""
+    pytest.importorskip("rasterio")
+    raster = DecodedRaster.random(nodata=7.0)
+    assert raster.gdal_transform == (100.0, 2.0, 0.0, 500.0, 0.0, -3.0)
+    path = tmp_path / "roundtrip.tif"
+    raster.write_geotiff(path)
+    assert_decoded_equal(decode_geotiff(path), raster)
+
+
+def test_decoded_raster_write_geotiff_requires_uniform_nodata(tmp_path):
+    """GeoTIFF nodata is file-wide, so a per-band anchor cannot be written."""
+    pytest.importorskip("rasterio")  # the bbox construction resolves through it
+    raster = DecodedRaster.random(nodata=[7.0, None])
+    with pytest.raises(ValueError, match="file-wide"):
+        raster.write_geotiff(tmp_path / "nonuniform.tif")
+
+
 def test_write_geotiff_bbox_places_the_grid(tmp_path):
     pytest.importorskip("rasterio")
     path = tmp_path / "bbox.tif"