Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Write COGs

geocloud.cog.write_cog writes a geoarray.GeoArray, or any lazy geoarray.RasterSource, as a validated COG to a local path or any bucket. It runs on the base install (GDAL’s COG driver through rasterio). In a pipeline, use the operator gz.WriteCOG.

Write a float, a class map and a mask

The example writes small synthetic rasters to an in-memory bucket (see Mounts) and a temporary directory. It runs offline.

import tempfile
from pathlib import Path

import numpy as np
from affine import Affine
from geoarray import GeoArray
from obstore.store import MemoryStore

from geocloud import files
from geocloud.cog import write_cog
from geocloud.store import mount

mount("s3://demo-bucket", MemoryStore())
rng: np.random.Generator = np.random.default_rng(0)
grid: Affine = Affine(10.0, 0.0, 500_000.0, 0.0, -10.0, 4_200_000.0)   # 10 m, UTM 10N

ndvi: GeoArray = GeoArray(
    rng.uniform(-1, 1, (1, 1024, 1024)).astype(np.float32),
    grid, "EPSG:32610", fill_value=np.nan,
)                                                    # (1, 1024, 1024) float32 · NaN = no data
classes: GeoArray = GeoArray(
    rng.integers(0, 5, (1024, 1024), dtype=np.uint8),
    grid, "EPSG:32610", fill_value=0,
)                                                    # (1024, 1024) uint8 · 0 = no data
mask: GeoArray = ndvi.with_data(
    np.asarray(ndvi) < 0, fill_value=False,
)                                                    # (1, 1024, 1024) bool

out: Path = Path(tempfile.mkdtemp())
write_cog(ndvi, "s3://demo-bucket/products/ndvi.tif")      # deflate + float predictor, average overviews
write_cog(classes, out / "landcover.tif", compress="zstd")  # nearest overviews keep class values
write_cog(mask, out / "water.tif")                          # bool → uint8 0 / 1, no nodata
size: int = files.info("s3://demo-bucket/products/ndvi.tif").size  # ≈ 4.7 MB

A lazy geoarray.RasterSource (a CogSource, a geoproducts reader) is staged strip by strip through read_window, so a scene larger than memory never loads whole. A georeader GeoTensor is refused with a TypeError: convert it with geoarray.interop.georeader.from_georeader, or wrap a lazy georeader reader with source_from_georeader.

How a write runs

  1. Stage. The pixels go to a tiled GeoTIFF in a private temporary directory, with nodata, band descriptions and tags.

  2. Translate. GDAL’s COG driver lays out the tiles, builds the overviews and puts the header first.

  3. Check. With validate=True (the default), the result must read back as LAYOUT=COG with the expected shape, dtype, tiles and overviews.

  4. Place. A local file is renamed into place, so a failed write never leaves a truncated file. A cloud dest is uploaded through geocloud.files with that bucket’s credentials.

Options

KeywordDefaultNotes
compress"deflate"also zstd, lzw, lerc*, webp / jpeg (8-bit), none
levelGDAL’sDEFLATE 1–12, ZSTD 1–22
predictorTrueGDAL picks horizontal (ints) or floating-point (floats) for DEFLATE / LZW / ZSTD
blocksize512a power of two
overviewsTruedown to one tile
resamplingautonearest for integer / bool data, average for floats
nodata"auto"the data’s fill_value if the dtype can hold it; never for a bool mask
descriptionsautofrom attrs["band_names"] (or descriptions) when it has one entry per band
tags, creation_options—dataset tags; raw GDAL COG options that win over the keywords
overwrite, validateTrueoverwrite=False raises FileExistsError

Compared with save_cog

georeader.save.save_cog also writes COGs. write_cog differs where a pipeline needs it to:

save_cogwrite_cog
Optionsa free-form profile dict, modified in placeexplicit keywords with checked values
DefaultsLZW, no predictor, cubic-spline overviews for every dtypevalues that suit the dtype
Validationnonethe output must be a COG
Local writenon-atomicatomic
Cloud writefsspec, via a temp file in the current directorythe shared pool and the credentials registry
Lazy readersnoyes
Unsupported dtypes (bool, float16)failconverted (uint8, float32)
A nodata the dtype cannot holdwritten as isdropped ("auto") or rejected (explicit value)