Write a tiled GeoTIFF from a CUDA array#

Save a CuPy image as a Deflate-compressed GeoTIFF with explicit tile dimensions. The encoder runs on the GPU; the file stores its grid and RGB band interpretation.

Requirements#

Use an NVIDIA GPU, CuPy and cugeo-raster built with the nvtiff native feature, following the GPU installation recipe. NumPy is used below to verify the saved pixels on the host.

The example uses the same synthetic UInt16 RGB image and grid as Write a file. If they are not already in your Python session, create them:

import cupy as cp
import cugeo.raster as raster

rows, cols = cp.indices((256, 256), dtype=cp.uint16)
pixels = cp.stack((cols, rows, (rows + cols) // 2)) * cp.uint16(257)
crs = raster.CRS.from_epsg(32610)
transform = raster.transform.from_origin(500000, 4200000, 10, 10)

These are synthetic colours on a grid with 10-metre pixels, not observations of the assigned location. The array has CHW axes: bands, rows, columns. Keep the current CuPy device and stream unchanged during the example.

Choose tiles and compression#

Set tiled=True and specify blockxsize and blockysize in pixels. Both tile dimensions must be positive multiples of 16. Here 128-by-128 tiles divide the 256-by-256 image into four spatial tiles. Each tile contains all three bands.

Set compress="deflate" for lossless compression. "lzw" and "none" are also supported by this writer. Compression changes the stored bytes, not the decoded sample values. Tiling lets readers select storage blocks for spatial subsets; an individual pixel request can still require a complete tile.

Write the image#

from pathlib import Path

output_path = Path("build/raster-howtos/tiled.tif")
output_path.parent.mkdir(parents=True, exist_ok=True)
stream = cp.cuda.get_current_stream()

with raster.open(
    output_path, "w",
    driver="GTiff",
    width=pixels.shape[2], height=pixels.shape[1], count=pixels.shape[0],
    dtype=pixels.dtype, transform=transform, crs=crs,
    photometric="RGB", compress="deflate",
    tiled=True, blockxsize=128, blockysize=128,
) as dst:
    dst.write(pixels, stream=stream)

write() consumes the complete CUDA image. Passing its stream orders encoding with the preceding CuPy work. The CHW allocation is packed on the GPU for nvTIFF; the file uses pixel-interleaved storage. No decoded-pixel copy to NumPy is needed for writing. File publication uses host-visible I/O and completes when the context manager closes successfully. Re-running the example replaces the destination.

Verify the saved file#

Reopen the file and check its grid, tile dimensions and compression. Supply NumPy storage to select the native host TIFF reader for this verification:

import numpy as np

with raster.open(output_path) as src:
    assert (src.count, src.height, src.width) == pixels.shape
    assert src.crs.to_epsg() == crs.to_epsg()
    assert src.transform == transform
    assert src.block_shapes == [(128, 128)] * 3
    assert src.profile["compress"] == "deflate"
    restored = np.empty(pixels.shape, dtype=pixels.dtype)
    src.read(out=restored)

np.testing.assert_array_equal(restored, cp.asnumpy(pixels))

cp.asnumpy(pixels) explicitly copies the original GPU samples to the host for comparison. The check compares every stored sample. The host read is selected by its NumPy output; it is not a fallback from a failed GPU read. GPU read support has its own input-profile restrictions.

COG restrictions#

A Cloud Optimized GeoTIFF (COG) is a GeoTIFF organized so clients can use HTTP range requests to read selected parts without downloading the entire file.

nvTIFF 0.9 writes the main image directory (IFD) before the encoded pixels by default. cugeo-raster does not expose an option to control that placement.

This output follows the community COG layout specification linked from cogeo.org, where overviews are optional, and passes GDAL’s COG layout validation.

CUDA pyramid writing is currently unsupported. This output therefore does not meet the Optimized GeoTIFF requirements of the OGC Cloud Optimized GeoTIFF standard. See Cloud Optimized GeoTIFF (COG) for an introduction and the COG writer reference for host writing support.

For other sample types, input strides, masks and metadata, consult the writer reference.