Skip to content

Commit 1ec50c7

Browse files
committed
Block write_geotiff_gpu(file_like, cog=True) for parity with to_geotiff (#1652)
to_geotiff has long rejected cog=True + file-like destinations, but the explicit write_geotiff_gpu entry point silently accepted the combo and emitted a COG into the buffer. The two writers should agree on which inputs they refuse: to_geotiff(gpu=True, cog=True, path=BytesIO) raises, so write_geotiff_gpu(da, BytesIO, cog=True) should too. Mirror the existing to_geotiff guard on the GPU entry point. Non-cog file-like writes remain supported on this path (the gate is targeted at cog=True only). Add regression coverage in test_bytesio_source.py. Also clarify the path/compression docstring on write_geotiff_gpu: - path: document that file-like destinations are accepted (cog=True requires a string path). - compression: list the full codec set the function actually accepts and note the deliberate JPEG asymmetry with to_geotiff (#1651 downgraded to docs-only after PR #1647 confirmed advanced-API intent). Update .claude/sweep-api-consistency-state.csv with the 2026-05-11 re-audit row.
1 parent 181056e commit 1ec50c7

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module,last_inspected,issue,severity_max,categories_found,notes
22
geotiff,2026-05-11,1644,MEDIUM,3,"Filed write_geotiff_gpu compression docstring drift vs to_geotiff (MEDIUM Cat 3, #1644). Fix on deep-sweep-api-consistency-geotiff-2026-05-11-1778545740: sync the full 9-codec list into the docstring and note GPU vs CPU encode paths; regression test test_compression_docstring_1644.py pins the codec list and exercises each CPU-fallback codec end-to-end. Other potential drifts surveyed: write_vrt returns str while to_geotiff/write_geotiff_gpu return None (LOW, intentional backward-compat); write_vrt nodata typed float|None vs int-accepting siblings (LOW, PEP 484 int->float compat); kwarg-only ordering drift across read functions (LOW, no user impact). Prior issues 1631/1637/1615/1560/1541/1562 all CLOSED."
3+
geotiff,2026-05-11,1652,MEDIUM,5,"Filed MEDIUM file-like cog=True drift #1652 (write_geotiff_gpu accepted BytesIO+cog=True; to_geotiff blocked it). Fixed in PR (TBD): mirror to_geotiff's gate on the explicit GPU writer; add regression tests in test_bytesio_source.py. Also filed #1651 (JPEG acceptance drift) but downgraded to LOW after #1647 confirmed write_geotiff_gpu(jpeg) is deliberate advanced-API; PR (TBD) carries the docstring clarification. Prior 1631/1644 noted in earlier rows (1644 open, fix in PR #1649). LOW: streaming_buffer_bytes default drift to_geotiff=256MB vs write_geotiff_gpu=None (no functional impact, explicit forwarding); to_geotiff data: annotation misses cupy.ndarray (accepted via auto-dispatch). cuda-validated."
34
reproject,2026-05-10,1570,HIGH,2;5,"Filed cross-module attrs['vertical_crs'] type collision (string vs EPSG int) vs xrspatial.geotiff. Fixed in PR (TBD): reproject now writes EPSG int and preserves friendly token under vertical_datum. MEDIUM kwarg-order drift (transform_precision vs chunk_size) and missing type hints vs geotiff documented but not fixed (cosmetic, kwarg-only)."

xrspatial/geotiff/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ def _read_once():
27142714

27152715

27162716
def write_geotiff_gpu(data: xr.DataArray | cupy.ndarray | np.ndarray,
2717-
path: str, *,
2717+
path, *,
27182718
crs: int | str | None = None,
27192719
nodata=None,
27202720
compression: str = 'zstd',
@@ -2747,8 +2747,12 @@ def write_geotiff_gpu(data: xr.DataArray | cupy.ndarray | np.ndarray,
27472747
2D or 3D raster. CuPy-backed inputs stay on device; NumPy/Dask
27482748
inputs are uploaded via ``cupy.asarray(np.asarray(data))``
27492749
before compression (matches ``to_geotiff`` parity).
2750-
path : str
2751-
Output file path.
2750+
path : str or binary file-like
2751+
Output file path or any object with a ``write`` method
2752+
(e.g. ``io.BytesIO``). ``cog=True`` requires a string path:
2753+
the auto-dispatch path through ``to_geotiff(gpu=True, cog=True)``
2754+
rejects file-like destinations, and the explicit GPU writer
2755+
mirrors that rule (issue #1652).
27522756
crs : int, str, or None
27532757
EPSG code or WKT string.
27542758
nodata : float, int, or None
@@ -2837,6 +2841,17 @@ def write_geotiff_gpu(data: xr.DataArray | cupy.ndarray | np.ndarray,
28372841
"max_z_error is not supported on the GPU writer "
28382842
"(nvCOMP has no LERC backend). Use to_geotiff(..., gpu=False) "
28392843
"or omit max_z_error.")
2844+
# Mirror to_geotiff's file-like + cog=True rejection. The auto-dispatch
2845+
# path through ``to_geotiff(gpu=True, cog=True, path=BytesIO)`` raises
2846+
# before reaching here; the explicit GPU writer mirrors the gate so
2847+
# callers cannot bypass it (issue #1652). Non-cog file-like writes
2848+
# remain supported on this entry point.
2849+
_path_is_file_like = (
2850+
not isinstance(path, str)) and hasattr(path, 'write')
2851+
if _path_is_file_like and cog:
2852+
raise ValueError(
2853+
"cog=True is not supported for file-like destinations on the "
2854+
"GPU writer. Pass a string path or set cog=False.")
28402855
# streaming_buffer_bytes is intentionally a no-op on the GPU path;
28412856
# the kwarg exists for API parity with to_geotiff so callers can pass
28422857
# the same kwargs to both entry points without filtering.

xrspatial/geotiff/tests/test_bytesio_source.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,50 @@ def seek(self, *a, **k):
268268

269269
assert _is_file_like(io.BytesIO(b'x')) is True
270270
assert _is_file_like(ReadSeekNoTell()) is False
271+
272+
273+
class TestWriteGeotiffGpuBytesIO:
274+
"""Regression coverage for ``write_geotiff_gpu`` file-like behaviour.
275+
276+
``to_geotiff(gpu=True, ...)`` always rejects BytesIO destinations paired
277+
with ``cog=True`` (the auto-dispatch path's existing guard). The explicit
278+
GPU writer used to silently accept that combo and produce a COG into the
279+
buffer, so the two entry points disagreed on what ``to_geotiff(gpu=True,
280+
cog=True, path=BytesIO)`` does. These tests pin the mirrored gate added
281+
by issue #1652 and confirm the non-cog file-like path still works.
282+
"""
283+
284+
def test_cog_with_bytesio_rejected_1652(self):
285+
cupy = pytest.importorskip("cupy")
286+
da = xr.DataArray(
287+
cupy.asarray(np.random.rand(64, 64).astype(np.float32)),
288+
dims=['y', 'x'],
289+
coords={'y': np.arange(64.0), 'x': np.arange(64.0)},
290+
attrs={'crs': 4326},
291+
)
292+
from xrspatial.geotiff import write_geotiff_gpu
293+
294+
buf = io.BytesIO()
295+
with pytest.raises(ValueError, match='cog=True'):
296+
write_geotiff_gpu(da, buf, cog=True)
297+
298+
def test_non_cog_bytesio_still_works_1652(self):
299+
cupy = pytest.importorskip("cupy")
300+
arr_cpu = np.random.rand(64, 64).astype(np.float32)
301+
da = xr.DataArray(
302+
cupy.asarray(arr_cpu),
303+
dims=['y', 'x'],
304+
coords={'y': np.arange(64.0), 'x': np.arange(64.0)},
305+
attrs={'crs': 4326},
306+
)
307+
from xrspatial.geotiff import write_geotiff_gpu
308+
309+
buf = io.BytesIO()
310+
# Non-cog file-like write is still supported on the explicit GPU
311+
# writer; only cog=True is gated.
312+
write_geotiff_gpu(da, buf)
313+
assert len(buf.getvalue()) > 0
314+
315+
# Verify it round-trips through open_geotiff
316+
rd = open_geotiff(io.BytesIO(buf.getvalue()))
317+
np.testing.assert_allclose(np.asarray(rd.values), arr_cpu)

0 commit comments

Comments
 (0)