User friendly Rasterio plugin to read raster datasets

9.0.6 · active · verified Thu Apr 16

rio-tiler is an active Python library that provides a user-friendly plugin for Rasterio, designed to read raster datasets efficiently. It acts as a wrapper around Rasterio and GDAL, facilitating dynamic slippy map tile creation and comprehensive data reading from various sources, including local files, HTTP, AWS S3, and Google Cloud Storage. The library is currently at version 9.0.6 and maintains a frequent release cadence, with both minor and major versions introducing new features and breaking changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `rio_tiler.io.Reader` class to open a Cloud Optimized GeoTIFF (COG) from a URL, extract a specific Web Mercator tile (x, y, z), and then render it into a PNG image. The `ImageData` object returned by `tile()` contains both the raster data and its mask.

import os
from rio_tiler.io import Reader
from rio_tiler.utils import render

# Example Cloud Optimized GeoTIFF (COG) URL
# This URL is publicly available for testing
COG_URL = "https://sentinel-cogs.s3.amazonaws.com/sentinel-s2-l2a-cogs/29/R/KH/2020/2/S2A_29RKH_20200219_0_L2A/B04.tif"

# Define a tile (x, y, z) - example from documentation
x, y, z = 239, 220, 9

try:
    with Reader(COG_URL) as src:
        # Read a Web Mercator tile
        img = src.tile(x, y, z)

        # ImageData object holds the data (img.data) and mask (img.mask)
        print(f"Tile shape: {img.data.shape}") # e.g., (1, 256, 256) for a single band
        print(f"Mask shape: {img.mask.shape}") # e.g., (256, 256)

        # Render the tile to a PNG buffer
        buffer = img.render(img_format="PNG")

        # Save the buffer to a file
        output_filename = "output_tile.png"
        with open(output_filename, "wb") as f:
            f.write(buffer)
        print(f"Tile saved to {output_filename}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure the COG_URL is accessible and GDAL is configured correctly.")
    # For AWS S3 requester-pays buckets, you might need: os.environ['AWS_REQUEST_PAYER'] = 'requester'

view raw JSON →