Cloud Optimized GeoTIFF (COG) Creation Plugin for Rasterio

7.0.2 · active · verified Thu Apr 16

rio-cogeo is a Rasterio plugin that streamlines the creation and validation of Cloud Optimized GeoTIFF (COG) files. It intelligently handles critical COG specifications, including internal tiling, overview generation, and metadata ordering, ensuring geospatial data is optimized for efficient cloud storage and rapid web-based access. The library is actively maintained with consistent releases, serving as a vital tool in modern geospatial data pipelines.

Common errors

Warnings

Install

Imports

Quickstart

rio-cogeo can be used via its command-line interface (CLI) or directly through its Python API. The CLI (prefixed with `rio cogeo`) is often the simplest for common tasks like creating or validating COGs. The Python API offers more granular control for integration into custom scripts. This example demonstrates both a dummy file creation for testing and a programmatic COG translation, showing how to leverage `cog_translate` and `cog_profiles`.

# Create a dummy GeoTIFF for demonstration
import rasterio
import numpy as np
from rasterio.transform import from_bounds

width, height = 1000, 1000
transform = from_bounds(-10, 40, 10, 60, width, height)

with rasterio.open(
    'input.tif',
    'w',
    driver='GTiff',
    height=height,
    width=width,
    count=1,
    dtype=np.uint8,
    crs='EPSG:4326',
    transform=transform,
) as dst:
    dst.write(np.zeros((height, width), dtype=np.uint8), 1)

print("Created input.tif")

# --- Using rio-cogeo CLI ---
# Create a COG with DEFLATE compression (default profile)
# This command is run from the shell, not directly in Python
# import os
# os.system("rio cogeo create input.tif output_deflate.tif")
# print("Created output_deflate.tif")

# Create a COG with JPEG profile, add internal mask, and select first 3 bands
# os.system("rio cogeo create input.tif output_jpeg.tif -b 1 --add-mask --cog-profile jpeg")
# print("Created output_jpeg.tif")

# Validate a COG
# os.system("rio cogeo validate output_deflate.tif")
# print("Validated output_deflate.tif")

# Example of programmatic COG creation
from rasterio.io import MemoryFile
from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles

# Use a local file path for input
src_path = 'input.tif'
output_path_programmatic = 'output_programmatic.tif'

dst_profile = cog_profiles.get("deflate")

with rasterio.open(src_path) as src_dataset:
    cog_translate(
        src_path,
        output_path_programmatic,
        dst_profile,
        config=dict(GDAL_NUM_THREADS='ALL_CPUS', GDAL_TIFF_OVR_BLOCKSIZE='128'),
        overview_resampling='nearest',
        web_optimized=False,
        in_memory=False,
        quiet=True,
        # For real usage, consider `dst_kwargs` for profile overrides or `resampling` for overviews
    )
print(f"Created {output_path_programmatic} programmatically.")

# Clean up dummy files
import os
os.remove('input.tif')
os.remove('output_programmatic.tif')
# os.remove('output_deflate.tif') # Uncomment if running CLI examples
# os.remove('output_jpeg.tif')   # Uncomment if running CLI examples

view raw JSON →