rio-cogeo Documentation

repository·main·Indexed 18 days ago

https://github.com/cogeotiff/rio-cogeo

A Rasterio plugin for creating and validating Cloud Optimized GeoTIFF (COG) files. It provides a CLI sub-command for the rasterio (rio) tool to create, validate, and inspect COGs, as well as a Python API via the cog_translate function. Key features include web-optimization for tiling services, support for the native GDAL COG driver (GDAL >= 3.1), internal mask creation, and customizable compression profiles.

Tokens
11.8K
Snippets
60
Records
66
Agent score
63%

What's inside rio-cogeo

  1. What is rio-cogeo?

    main

    rio-cogeo is a plugin for Rasterio designed to facilitate the creation and validation of Cloud Optimized GeoTIFF (COG or COGEO) files.

    While it adheres to the official COG specifications, it enforces specific features by default:

    • Internal overviews: Can be removed using the --overview-level 0 option.
    • Internal tiles: Default profiles use 512x512 internal tiles.
  2. Use the rio cogeo CLI sub-command

    main

    The rio-cogeo module extends the rasterio (rio) CLI by adding a cogeo sub-command. This sub-command provides three primary operations for working with Cloud Optimized GeoTIFFs (COGs):

    1. create: Generate a COG from an input raster.
    2. info: Inspect metadata and properties of a raster dataset.
    3. validate: Check if a COG file adheres to Cloud Optimized GeoTIFF standards.
    $ rio cogeo --help
  3. Use the GDAL COG driver in rio-cogeo

    main
    Starting with rio-cogeo version 2.2, you can use the native GDAL COG driver (introduced in GDAL 3.1) instead of the default creation method. This is controlled via the --use-cog-driver option.
  4. Create a Web-Optimized COG

    main

    Use the --web-optimized option to create a Cloud Optimized GeoTIFF (COG) that is friendly to web-tiling services. This option aligns the dataset bounds and internal tiles with the Web Mercator grid (or a specified TMS grid) and ensures that raw data and overview resolutions match TMS zoom level resolutions.

    Important: Because this process creates a larger file due to padding tiles on the sides, ensure your input dataset contains a nodata value, an alpha band, or an internal mask. If none are present, the original data will be surrounded by black (0) data in the output.

    rio cogeo input.tif output_web_optimized.tif --web-optimized
  5. Validate a Cloud Optimized GeoTIFF with `rio cogeo validate`

    main

    Use rio cogeo validate to check if a GeoTIFF conforms to the COG specification. The tool will report warnings (e.g., missing overviews) or errors (e.g., not tiled, incorrect IFD offsets) and explicitly state if the file is or is not a valid COG.

    $ rio cogeo validate <input_file.tif>
  6. Use specialized compression with `rio cogeo create`

    main

    For specific data types (like RGB/Uint8), you can use more efficient compression algorithms via the -p (profile) flag to significantly reduce file size.

    • JPEG: Use -p jpeg for lossy compression. This is ideal for visual-only COGs and provides massive size reductions.
    • DEFLATE: The default compression used by rio-cogeo.
    $ rio cogeo create HYP_50M_SR.tif HYP_50M_SR_COG_jpeg.tif -p jpeg
  7. Configure internal tile size

    main

    By default, rio-cogeo creates datasets with a 512x512 internal tile size. You can customize this by passing --co BLOCKXSIZE and --co BLOCKYSIZE options.

    When creating a Web-Optimized COG, proper alignment of tiles and bounds with the mercator grid reduces the number of GET requests a dynamic tiling service must perform to generate map tiles.

    rio cogeo input.tif output.tif --co BLOCKXSIZE=64 --co BLOCKYSIZE=64
  8. Use internal masks instead of Nodata or Alpha bands

    main

    For Byte or Uint16 datasets, you can use the --add-mask option to replace a nodata value or an Alpha band with an internal bit mask. This is supported by most GDAL-based backends.

    Note: If your input dataset has an alpha band and you are adding a mask, you must use the bidx option to remove the alpha band from the output.

    Warning: Do not use internal nodata values with lossy compression formats like webp or jpeg. Instead, use internal masking or an alpha band (if using webp).

    # 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
  9. Inspect GeoTIFF metadata with `rio cogeo info`

    main

    Use the rio cogeo info command to inspect a GeoTIFF's properties. To identify if a file is a valid Cloud Optimized GeoTIFF (COG), check the following in the output:

    • Tiled: Must be True.
    • BlockSize: Should represent internal tiles (e.g., 512x512) rather than full row dimensions (e.g., 10800x1).
    • IFD (Image File Directory): A COG should have multiple IFDs representing different overview levels (decimation).
    $ rio cogeo info <input_file.tif>
  10. Create a COG with `rio cogeo create`

    main

    Convert a standard GeoTIFF into a Cloud Optimized GeoTIFF using rio cogeo create.

    By default, this command:

    • Creates internal tiles with a 512x512 blocksize.
    • Adds internal overviews.
    • Uses DEFLATE compression.

    Syntax: rio cogeo create <input_file.tif> <output_file.tif>

    $ rio cogeo create HYP_50M_SR.tif HYP_50M_SR_COG.tif