Read and resize a raster window#
Read a rectangular part of an image into CuPy, resize it when needed, and calculate the transform that places the returned pixels on the source map grid.
Requirements#
Use an installed cugeo-raster build and an input within its
supported profile.
The examples use the RGB TIFF from the
sample download snippet.
Run that snippet to define source_path and keep its temporary directory alive.
The CuPy examples require an NVIDIA GPU, CuPy and the nvtiff native feature.
Keep the current device and stream unchanged while running them. For JP2 or
raw J2K input, use an nvjpeg2k build and make a compatible CUDA context current
on the calling thread. No device read falls back to host decode.
Select a pixel extent#
Window takes column offset, row offset, width and height. Read a
256-by-256 patch starting at column 17 and row 23:
import cupy as cp
import cugeo.raster as raster
from cugeo.raster.windows import Window
window = Window(17, 23, 256, 256)
with raster.open(source_path) as src:
patch = src.read(window=window)
source_transform = src.transform
patch_transform = src.window_transform(window)
patch_crs = src.crs
assert patch.shape == (3, 256, 256)
The red rectangle marks the window in the tutorial’s source image:
The extracted 256-by-256 window retains the stored RGB values, including the source’s black nodata border:

src.transform maps pixel coordinates in the complete source image to map
coordinates in its CRS. src.window_transform(window) shifts that transform’s
origin to the window’s upper-left corner while preserving pixel size, rotation
and shear. Patch corner (0, 0) therefore maps to the same position as source
corner (17, 23). Coordinates here are (column, row) and refer to pixel corners.
Inspect the transforms in the REPL. Map coordinates are eastings and northings in metres in EPSG:32618:
>>> source_transform
Affine(300.0379266750948, 0.0, 101985.0,
0.0, -300.041782729805, 2826915.0)
>>> patch_transform
Affine(300.0379266750948, 0.0, 107085.6447534766,
0.0, -300.041782729805, 2820014.0389972143)
Use patch_transform when locating or saving the patch.
Reusing src.transform would place it at the complete image’s origin.
The read does not change CRS.
Scale the window on read#
Set out_shape to read the same window into a different number of pixels.
The shape is (bands, height, width). Read the 256-by-256 window at half the
width and height using nearest-neighbour sampling:
with raster.open(source_path) as src:
small = src.read(
window=window, out_shape=(src.count, 128, 128),
resampling=raster.enums.Resampling.nearest,
)
small_transform = patch_transform * raster.Affine.scale(
window.width / small.shape[-1], window.height / small.shape[-2],
)
out_shape changes the array dimensions. It does not modify src.transform
or the transform returned by src.window_transform(window). Scale the window
transform by the source-window width divided by output width, and likewise
for height. Here both factors are two: each output pixel covers twice the
map distance along each axis.
>>> small.shape
(3, 128, 128)
>>> patch_transform
Affine(300.0379266750948, 0.0, 107085.6447534766,
0.0, -300.041782729805, 2820014.0389972143)
>>> small_transform
Affine(600.0758533501896, 0.0, 107085.6447534766,
0.0, -600.08356545961, 2820014.0389972143)

The smaller image has the same origin and map extent as the original patch. The preview is displayed at its native 128-by-128 size. Check that the two arrays’ outer corners map to the same positions:
assert small_transform * (0, 0) == patch_transform * (0, 0)
assert small_transform * (128, 128) == patch_transform * (256, 256)
Nearest sampling selects stored values; it does not average source pixels. CUDA TIFF supports nearest downsampling but does not support bilinear downsampling. See the sampling reference for other source profiles and resampling limits.
To save the resized patch, use its output dimensions, small_transform and
patch_crs in the write profile.
To reuse existing GPU storage, follow
Read into a caller-owned PyTorch tensor.