Skip to main content

CloudOptimized GeoTIFF (COGEO) creation plugin for rasterio

Project description


Cloud Optimized GeoTIFF (COG) creation and validation plugin for Rasterio

Packaging status CircleCI codecov

Cloud Optimized GeoTIFF

This plugin aim to facilitate the creation and validation of Cloud Optimized GeoTIFF (COG or COGEO). While it respects the COG specifications, this plugin also enforce several features:

  • Internal overviews (User can remove overview with option --overview-level 0)
  • Internal tiles (default profiles have 512x512 internal tiles)

Important: Starting from GDAL 3.1 a new COG generator driver will be added (doc, discussion) and will make rio-cogeo kinda obsolete.


$ pip install -U pip
$ pip install rio-cogeo

Or install from source:

$ git clone
$ cd rio-cogeo
$ pip install -U pip
$ pip install -e .


$ rio cogeo --help
  Usage: rio cogeo [OPTIONS] COMMAND [ARGS]...

  Rasterio cogeo subcommands.

    --help  Show this message and exit.

    create    Create COGEO
    validate  Validate COGEO
  • Create a Cloud Optimized Geotiff.
$ rio cogeo create --help
  Usage: rio cogeo create [OPTIONS] INPUT OUTPUT

  Create Cloud Optimized Geotiff.

    -b, --bidx BIDX                 Band indexes to copy.
    -p, --cog-profile [jpeg|webp|zstd|lzw|deflate|packbits|lzma|lerc|lerc_deflate|lerc_zstd|raw]
                                    CloudOptimized GeoTIFF profile (default: deflate).
    --nodata NUMBER|nan             Set nodata masking values for input dataset.
    --add-mask                      Force output dataset creation with an internal mask (convert alpha band or nodata to mask).
    -t, --dtype [ubyte|uint8|uint16|int16|uint32|int32|float32|float64]
                                    Output data type.
    --overview-level INTEGER        Overview level (if not provided, appropriate overview level will be selected
                                    until the smallest overview is smaller than the value of the internal blocksize)
    --overview-resampling [nearest|bilinear|cubic|cubic_spline|lanczos|average|mode|gauss] Overview creation resampling algorithm.
    --overview-blocksize TEXT       Overview's internal tile size (default defined by GDAL_TIFF_OVR_BLOCKSIZE env or 128)
    -w, --web-optimized             Create COGEO optimized for Web.
    --latitude-adjustment / --global-maxzoom
                                    Use dataset native mercator resolution for MAX_ZOOM calculation (linked to dataset center latitude, default)
                                    or ensure MAX_ZOOM equality for multiple dataset accross latitudes.
    -r, --resampling [nearest|bilinear|cubic|cubic_spline|lanczos|average|mode|gauss] Resampling algorithm.
    --in-memory / --no-in-memory    Force processing raster in memory / not in memory (default: process in memory if smaller than 120 million pixels)
                                    Allow intermediate file compression to reduce memory/disk footprint.
    --forward-band-tags             Forward band tags to output bands.
    --threads THREADS               Number of worker threads for multi-threaded compression (default: ALL_CPUS)
    --co, --profile NAME=VALUE      Driver specific creation options. See the documentation for the selected GTiff driver for more information.
    -q, --quiet                     Remove progressbar and other non-error output.
    --help                          Show this message and exit.
  • Check if a Cloud Optimized Geotiff is valid.
$ rio cogeo validate --help
Usage: rio cogeo validate [OPTIONS] INPUT

  Validate Cloud Optimized Geotiff.

  --strict  Treat warnings as errors.
  --help    Show this message and exit.

The strict options will treat warnings (e.g missing overviews) as errors.


# Create a COGEO with DEFLATE compression (Using default `Deflate` profile)
$ rio cogeo create mydataset.tif mydataset_jpeg.tif

# Validate COGEO
$ rio cogeo validate mydataset_jpeg.tif

# Create a COGEO with JPEG profile and the first 3 bands of the data and add internal mask
$ rio cogeo create mydataset.tif mydataset_jpeg.tif -b 1,2,3 --add-mask --cog-profile jpeg

Default COGEO profiles

Default profiles are tiled with 512x512 blocksizes.


  • JPEG compression
  • PIXEL interleave
  • YCbCr (3 bands) colorspace or MINISBLACK (1 band)
  • limited to uint8 datatype and 3 bands data


  • WEBP compression
  • PIXEL interleave
  • limited to uint8 datatype and 3 or 4 bands data
  • Non-Standard, might not be supported by software not build against GDAL+internal libtiff + libwebp
  • Available for GDAL>=2.4.0


  • ZSTD compression
  • PIXEL interleave
  • Non-Standard, might not be supported by software not build against GDAL + internal libtiff + libzstd
  • Available for GDAL>=2.3.0

Note in Nov 2018, there was a change in libtiff's ZSTD tags which create incompatibility for old ZSTD compressed GeoTIFF (link)


  • LZW compression
  • PIXEL interleave


  • DEFLATE compression
  • PIXEL interleave


  • PACKBITS compression
  • PIXEL interleave


  • LZMA compression
  • PIXEL interleave


  • LERC compression
  • PIXEL interleave
  • Default MAX_Z_ERROR=0 (lossless)
  • Non-Standard, might not be supported by software not build against GDAL + internal libtiff
  • Available for GDAL>=2.4.0


  • LERC_DEFLATE compression
  • PIXEL interleave
  • Default MAX_Z_ERROR=0 (lossless)
  • Non-Standard, might not be supported by software not build against GDAL + internal libtiff + libzstd
  • Available for GDAL>=2.4.0


  • LERC_ZSTD compression
  • PIXEL interleave
  • Default MAX_Z_ERROR=0 (lossless)
  • Non-Standard, might not be supported by software not build against GDAL + internal libtiff + libzstd
  • Available for GDAL>=2.4.0


  • NO compression
  • PIXEL interleave

Profiles can be extended by providing '--co' option in command line

# Create a COGEO without compression and with 1024x1024 block size and 256 overview blocksize
$ rio cogeo create mydataset.tif mydataset_raw.tif --co BLOCKXSIZE=1024 --co BLOCKYSIZE=1024 --cog-profile raw --overview-blocksize 256

See for full details of creation options.


Rio-cogeo can also be integrated directly in your custom script. See rio_cogeo.cogeo.cog_translate function.


from rio_cogeo.cogeo import cog_translate

def _translate(src_path, dst_path, profile="webp", profile_options={}, **options):
    """Convert image to COG."""
    # Format creation option (see gdalwarp `-co` option)
    output_profile = cog_profiles.get(profile)

    # Dataset Open option (see gdalwarp `-oo` option)
    config = dict(

    return True


Using the API with in MemoryFile

  1. Create COG from numpy array
import numpy

import mercantile

from import MemoryFile
from rasterio.transform import from_bounds

from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles

# Create GeoTIFF profile
bounds = mercantile.bounds(mercantile.Tile(0,0,0))
src_transform = from_bounds(*bounds, 1024 1024)

src_profile = dict(

img_array = tile = numpy.random.rand(3, 1024, 1024)

with MemoryFile() as memfile:
    with**src_profile) as mem:
        # Populate the input file with numpy array
        dst_profile = cog_profiles.get("deflate")        
  1. Create output COG in Memory
from import MemoryFile

from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles

from boto3.session import Session as boto3_session

dst_profile = cog_profiles.get("deflate")

with MemoryFile() as mem_dst:
    # Important, we pass `` as output dataset path
    cog_translate("my-input-file.tif",, profile, in_memory=True)

    # You can then use the memoryfile to do something else like
    # upload to AWS S3
    client = boto3_session.client("s3")
    client.upload_fileobj(mem_dst, "my-bucket", "my-key")

Web-Optimized COG

rio-cogeo provide a --web-optimized option which aims to create a web-tiling friendly COG.

Output dataset features:

  • bounds and internal tiles aligned with web-mercator grid.
  • raw data and overviews resolution match mercator zoom level resolution.


Because the mercator projection does not respect the distance, when working with multiple images covering different latitudes, you may want to use the --global-maxzoom option to create output dataset having the same MAX_ZOOM (raw data resolution).

Because it will certainly create a larger file, a nodata value or alpha band should be present in the input dataset. If not the original data will be surrounded by black (0) data.

Internal tile size

By default rio cogeo will create a dataset with 512x512 internal tile size. This can be updated by passing --co BLOCKXSIZE=64 --co BLOCKYSIZE=64 options.

Web tiling optimization

if the input dataset is aligned to web mercator grid, the internal tile size should be equal to the web map tile size (256 or 512px). Dataset should be compressed.

if the input dataset is not aligned to web mercator grid, the tiler will need to fetch multiple internal tiles. Because GDAL can merge range request, using small internal tiles (e.g 128) will reduce the number of byte transfered and minimized the useless bytes transfered.

GDAL configuration to merge consecutive range requests


Overview levels

By default rio cogeo will calculate the optimal overview level based on dataset size and internal tile size (overview should not be smaller than internal tile size (e.g 512px). Overview level will be translated to decimation level of power of two:

overview_level = 3
overviews = [2 ** j for j in range(1, overview_level + 1)]
[2, 4, 8]

GDAL Version

It is recommanded to use GDAL > 2.3.2. Previous version might not be able to create proper COGs (ref:

More info in

Nodata, Alpha and Mask

By default rio-cogeo will forward any nodata value or alpha channel to the output COG.

If your dataset type is Byte or Unit16, you could use internal bit mask (with the --add-mask option) to replace the Nodata value or Alpha band in output dataset (supported by most GDAL based backends).

Note: when adding a mask with an input dataset having an alpha band you'll need to use the bidx options to remove it from the output dataset.

# Replace the alpha band by an internal mask
$ rio cogeo mydataset_withalpha.tif mydataset_withmask.tif --cog-profile raw --add-mask --bidx 1,2,3


Using internal nodata value with lossy compression (webp, jpeg) is not recommanded. Please use internal masking (or alpha band if using webp).

Contribution & Development

The rio-cogeo project was begun at Mapbox and has been transferred to the CogeoTIFF organization in January 2019.

Issues and pull requests are more than welcome.

dev install

$ git clone
$ cd rio-cogeo
$ pip install -e .[dev]

Python3.6 only

This repo is set to use pre-commit to run flake8, pydocstring and black ("uncompromising Python code formatter") when commiting new code.

$ pre-commit install


Blog post on good and bad COG formats:

Checkout rio-glui or rio-viz rasterio plugins to explore COG locally in your web browser.

Project details

Release history Release notifications | RSS feed

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rio-cogeo-1.1.8.tar.gz (20.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page