Skip to main content

raster.write_cog()

Write an array, a VRT or a raster file as a Cloud Optimized GeoTIFF.

Usage

Source

raster.write_cog(data: np.ndarray | VRT | AnyPath, dst_file: AnyPath, profile: dict[str, Any] | None = ..., band_names: list[str] | None = ..., color_interp: list[ColorInterp] | None = ..., tags: Mapping[str, str] | None = ..., band_tags: Sequence[Mapping[str, str]] | None = ..., units: list[str] | None = ..., checksum: Literal[False] = ...) -> None
 
raster.write_cog(data: np.ndarray | VRT | AnyPath, dst_file: AnyPath, profile: dict[str, Any] | None = ..., band_names: list[str] | None = ..., color_interp: list[ColorInterp] | None = ..., tags: Mapping[str, str] | None = ..., band_tags: Sequence[Mapping[str, str]] | None = ..., units: list[str] | None = ..., checksum: Literal[True]) -> str
 
raster.write_cog(data: np.ndarray | VRT | AnyPath, dst_file: AnyPath, profile: dict[str, Any] | None = ..., band_names: list[str] | None = ..., color_interp: list[ColorInterp] | None = ..., tags: Mapping[str, str] | None = ..., band_tags: Sequence[Mapping[str, str]] | None = ..., units: list[str] | None = ..., checksum: bool) -> str | None

A VRT or raster path is streamed through GDAL without loading it into memory. A path is read with freezebase.vrt.from_raster, which refuses colour tables and south-up rasters.

Parameters

data: numpy.ndarray | VRT | str | Path | UPath

Single- or multi-band array (or array-like), a VRT, or a raster file.

dst_file: str | Path | UPath

Destination COG (local or S3).

profile: dict[str, Any] or None = None

Raster metadata and native GDAL COG creation options, the latter in any case; required for an array. For a VRT or path only dtype, nodata and the creation options apply: dtype casts every band, and a nodata value that differs from a band’s moves the pixels holding the old one to it. nodata=None keeps the VRT’s nodata, as leaving it out. A cast rounds and clamps out-of-range values, so a valid value can become the nodata value. Bands that differ in dtype or nodata need dtype or nodata here.

band_names: list[str] or None = None

Band descriptions, one per band.

color_interp: list[ColorInterp] or None = None

Per-band color interpretation.

tags: Mapping[str, str] or None = None

Dataset-level tags.

band_tags: Sequence[Mapping[str, str]] or None = None

Per-band tags, one mapping per band.

units: list[str] or None = None

Band unit strings, one per band.

checksum: bool = False
Hash the encoded COG file, the exact bytes that land at dst_file, and return the digest.

Returns

str or None
The SHA-256 digest as a multihash ("1220" + hex) when checksum is True, otherwise None.

Raises

ValueError

If profile is missing for an array, a per-band sequence has the wrong length, the dtype is unknown, a nodata value does not fit the dtype, a raster path is under a GDAL virtual file system such as /vsitar/ or has a colour table or is south-up, or the bands differ in dtype or nodata without profile setting it, or profile holds a COG option that reprojects, such as target_srs or tiling_scheme.

RasterWriteError
If the write fails, also when GeoTIFF cannot store the CRS, such as a projected CRS with ellipsoidal heights.