Skip to main content

Windowed Raster Processing

Windowed processing limits the array held in memory, but a window is meaningful only with its location on the source grid. Carry the window transform, CRS, mask, band contract, and resampling method with every chunk.

Prerequisites​

Complete raster bands, resolution, and NoData. You should be able to explain all six coefficients of the fixture's affine transform.

Learning goals​

  • Define windows in row/column order and derive their spatial bounds and transform.
  • Preserve NoData and band semantics when reading subsets.
  • Use nearest-neighbor resampling for categories and explicit alternatives for continuous values.
  • Distinguish array size from physical I/O caused by raster block layout.

Windows are spatial objects​

Rasterio's Window uses column offset, row offset, width, and height. Array slices are conventionally row first, column second. Confusing those orders selects the wrong place even when the returned shape looks plausible. rasterio.windows.transform derives the affine transform for the subset; using the full-raster transform would georeference the values incorrectly.

Windowed reads can process data larger than memory and support tile-wise pipelines. They do not guarantee that storage reads only the requested cells. GeoTIFF and remote objects are read in blocks; a one-pixel request may require a whole block. Aligning work to block windows can reduce I/O, but compression, caching, storage latency, concurrency, and query shape determine performance. The 4 by 4 fixture is deliberately too small for performance claims, so its runtime is not a benchmark.

Resampling is part of the read contract. The example downsamples the categorical quality band with nearest neighbor and the continuous temperature band with bilinear interpolation. It also scales the affine transform so the output grid keeps the same extent. The output is a derived raster; provenance should retain source identity and resampling policy.

Worked example​

from pathlib import Path

import rasterio
from rasterio.enums import Resampling
from rasterio.windows import Window, bounds, transform

path = Path("tests/fixtures/geospatial-data-science/tiny-multiband.tif")
with rasterio.open(path) as raster:
window = Window.from_slices((0, 2), (0, 2))
subset = raster.read(1, window=window, masked=True)
subset_transform = transform(window, raster.transform)
categories = raster.read(2, out_shape=(2, 2), masked=True, resampling=Resampling.nearest)
continuous = raster.read(1, out_shape=(2, 2), masked=True, resampling=Resampling.bilinear)
output_transform = raster.transform @ raster.transform.scale(
raster.width / categories.shape[1], raster.height / categories.shape[0]
)
print(f"window=row:0-2,col:0-2 shape={subset.shape[0]}x{subset.shape[1]} valid={subset.count()}")
print(f"window_bounds={tuple(int(value) for value in bounds(window, raster.transform))}")
print(f"window_transform={tuple(float(value) for value in subset_transform)[:6]} crs={raster.crs.to_string()} nodata={raster.nodata}")
print(f"downsample=2x2 resampling=nearest categories={sorted(set(categories.compressed().tolist()))}")
print(f"continuous_resampling=bilinear output_cell={int(output_transform.a)}m")

Expected output:

window=row:0-2,col:0-2 shape=2x2 valid=4
window_bounds=(391000, 5819000, 392000, 5820000)
window_transform=(500.0, 0.0, 391000.0, 0.0, -500.0, 5820000.0) crs=EPSG:32633 nodata=-9999.0
downsample=2x2 resampling=nearest categories=[1, 2]
continuous_resampling=bilinear output_cell=1000m

The example reads rather than writes a derived file. A production writer must copy only intentional metadata, set the output width and height, use output_transform, keep EPSG:32633, set nodata=-9999, document both resampling methods, and verify the reopened dataset.

Limitations and common mistakes​

  • Do not reuse the source transform after cropping or changing output shape.
  • A window that extends outside the dataset needs an explicit boundless-read and fill policy.
  • Parallel workers can repeat block reads or overload remote storage; measure the real deployment path.
  • Bilinear resampling across NoData edges needs inspection; masking and interpolation order matter.
  • Partial processing must not silently omit edge windows or double-count overlaps.

Practice​

Design a loop over 2 by 2 windows. State how it handles right/bottom edge windows, how it combines masks, what metadata each result carries, and how a retry avoids publishing a partial output as complete.

Next steps​

Next, combine polygons and cells in zonal statistics, where pixel-inclusion and aggregation rules shape the result.

Primary sources​

Checked 2026-10-18: Rasterio, windowed reading and writing; Rasterio, resampling; Rasterio API, Window and window transforms; GDAL, raster block size and RasterIO.