Introducing a new way to review your PRs.

Read the announcement

maplibre / martin

Blazing fast and lightweight PostGIS, MBtiles and PMtiles tile server, tile generation, and mbtiles tooling.

View on GitHub

22 documents · Updated Nov 23, 2026

docs / cog-raster-tiles

COG raster tiles

The COG tile library turns Cloud Optimized GeoTIFF data into web map tiles or decoded raster pixels. It reads only the metadata and byte ranges needed for a request, supports direct files and mosaics found by a fixed layout, STAC search, or a tile index, and can reproject source imagery onto Web Mercator tiles.

COG tile model

Tile pyramid

The direct COG reader treats a COG as a WebMercatorQuad tile pyramid. The full-resolution image and its overview images represent zoom levels. Each image must already use the resolution and tile boundaries of its zoom, so a requested z/x/y maps to one stored tile without resampling. Indexed mosaics use a separate path for COGs that need reprojection or do not share this exact grid.

The supported geospatial model is:

  • EPSG:3857 with square, north-up pixels for direct tile reads.
  • XYZ tile coordinates with y = 0 at the north edge.
  • A tile size and zoom range learned from the COG.
  • Image bounds reported in both Web Mercator meters and longitude/latitude degrees.

Misaligned or otherwise unusable overview images are left out. The COG remains usable when at least one image can provide tiles.

References: cog-tiles/…/reader.rs• L244-250 cog-tiles/…/geo.rs• L36-58 cog-tiles/…/tile.rs• L16-67

Byte-range reading

Opening a COG reads its TIFF and GeoTIFF metadata. Later tile requests read the byte range for the selected internal tile instead of downloading the full file. This gives the same request model for local files and remote objects; the storage-specific access is supplied by a backend.

The reader keeps the parsed source description so many tile requests can share one opened COG. Header reads use a small range cache, which reduces repeated reads when metadata is spread across nearby parts of the file.

References: cog-tiles/…/mod.rs• L26-49 cog-tiles/…/metadata.rs• L10-18 cog-tiles/…/reader.rs• L271-298

Tile resolution

Single-COG lookup

The reader maps a requested tile to the image that serves its zoom, then to the image's internal column and row. A tile outside the image, a zoom with no usable image, or a sparse tile has no data and returns an empty result rather than invented pixels.

The reader exposes source information before tile reads. This lets a caller choose the right tile format, zoom range, tile size, and geographic extent for downstream responses.

References: cog-tiles/…/reader.rs• L406-431 cog-tiles/…/reader.rs• L433-485 cog-tiles/…/reader.rs• L786-829

Mosaic lookup

An AsyncCogMosaic presents many COG files as one tile source. Each file represents one tile at a configured file zoom. For a request at or above that zoom, the mosaic finds the request's ancestor tile, renders that tile through a key template, and opens the matching file on demand.

The mosaic checks that each file covers the tile named by its key and uses the same tile size and output format as the other files. It caches opened and missing files for a bounded time, so new or replaced files become visible after cache expiry. Files without GeoTIFF georeference tags can be placed by their file name when they are square.

The request path is summarized below.

flowchart LR
    A[Requested z/x/y] --> B[Find ancestor at file zoom]
    B --> C[Render file key]
    C --> D{File in cache?}
    D -- No --> E[Open matching COG]
    D -- Yes --> F[Use cached reader]
    E --> G[Check coverage and layout]
    G --> F
    F --> H[Read tile or pixels]

References: cog-tiles/…/mosaic.rs• L125-155 cog-tiles/…/mosaic.rs• L271-350 cog-tiles/…/template.rs• L39-51

Indexed mosaics

An indexed mosaic finds candidate COGs for each requested tile and paints them in the order returned by its index. The first file with data owns each pixel, so later files fill only gaps. Each file may use Web Mercator or a supported UTM coordinate system, and the library reprojects it with nearest-neighbour sampling.

The shared indexed mosaic model supports two index providers:

  • A STAC index searches an API for items that cover a tile at a configured search zoom. It orders results by item time and resolves each selected asset to a storage backend.
  • A tile index reads file footprints and locations from a GeoPackage. It finds intersecting files with the stored footprints and keeps their row order as paint order.

The mosaic selects one or three source bands and adds an alpha band. A pixel is transparent when it is outside a source, matches the configured or file nodata value, or has zero source alpha. This lets overlapping imagery form one stable tile without treating empty source pixels as opaque data.

This diagram shows how a request moves from an index to a reprojected tile.

flowchart LR
    A[Requested z/x/y] --> B{Index}
    B -->|STAC search| C[Candidate COG assets]
    B -->|GeoPackage footprints| C
    C --> D[Order overlapping files]
    D --> E[Open and cache COG]
    E --> F[Reproject source pixels]
    F --> G[Paint non-empty pixels]
    G --> H[PNG tile with alpha]

References: cog-tiles/…/indexed.rs• L21-45 cog-tiles/…/indexed.rs• L131-140 cog-tiles/…/warp.rs• L129-155 cog-tiles/…/stac.rs• L215-234 cog-tiles/…/tile_index.rs• L40-57

STAC inputs

A STAC input turns a spatial and time-filtered catalog search into an indexed mosaic. One search covers a tile at the configured search zoom, and its result is reused for the more detailed tiles below that zoom. The source reads collection metadata and one matching item when it opens, then searches and caches results as tile requests arrive.

The search can choose the newest or oldest items first. Bands can be selected by number or by the asset's eo:bands names; when no selection is given, named assets prefer red, green, and blue. Assets with a different band order are skipped so the mosaic does not mix unlike channels.

References: cog-tiles/…/stac.rs• L79-124 cog-tiles/…/stac.rs• L236-285 cog-tiles/…/stac.rs• L295-365 cog-tiles/…/stac.rs• L446-480

Tile-indexed inputs

A tile-indexed input turns a GeoPackage made by a tile index into an indexed mosaic. The index supplies each file's footprint and location in geographic coordinates, so the request path can avoid opening files that do not touch the requested tile. Local paths and supported remote locations are resolved before requests begin, and the whole index is loaded for fast per-tile lookup.

The index row order defines paint order. Its footprints must use EPSG:4326, and each location must resolve to a readable local or remote object. The resulting source reports the union of the indexed footprints as its geographic extent.

References: cog-tiles/…/tile_index.rs• L59-74 cog-tiles/…/tile_index.rs• L79-151 cog-tiles/…/tile_index.rs• L155-163

Coordinate transformation

Supported source projections

Direct tile reads require Web Mercator alignment. Warped reads accept Web Mercator and supported UTM zones, then project geographic tile coordinates into the source coordinate system before sampling. This separates the source grid from the output tile grid.

The warped path uses nearest-neighbour sampling. It chooses an overview that is detailed enough for the requested tile and rejects a request when missing overviews would require reading too much finer data. Warped images must be internally tiled; striped images remain available to the direct reader but cannot be reprojected.

References: cog-tiles/…/crs.rs• L23-57 cog-tiles/…/warp.rs• L158-220 cog-tiles/…/warp.rs• L250-325

Pixel painting

For each output tile, the warped reader projects a coarse grid of points into source pixel coordinates and interpolates between them. It reads the source blocks touched by that grid, then copies selected source bands into pixels that are inside the image and are not nodata or transparent.

An indexed mosaic applies this process one file at a time. Its canvas tracks which output pixels already have data, so source order controls overlap without replacing valid pixels from an earlier file.

References: cog-tiles/…/warp.rs• L87-127 cog-tiles/…/warp.rs• L250-317 cog-tiles/…/warp.rs• L423-505

Band and pixel handling

Band model

The library supports unsigned 8-bit pixel samples arranged as interleaved bands. A decoded tile stores samples row by row in a fixed square buffer. Direct reads support grayscale, grayscale with alpha, RGB, and RGBA. Indexed mosaics select one band for grayscale or three bands for RGB, then add alpha to mark coverage.

The library does not treat elevation or other numeric raster data as ordinary image tiles. Direct readers and warped readers reject non-8-bit samples and separate band planes. The direct reader also rejects unsupported layouts or compression when it opens the source.

References: cog-tiles/…/pixels.rs• L9-17 cog-tiles/…/reader.rs• L618-671 cog-tiles/…/warp.rs• L129-155 cog-tiles/…/indexed.rs• L157-173 cog-tiles/README.md• L111-118

Pixel windows

PixelWindow converts a geographic rectangle into whole pixels at one zoom and tile size. It expands the request to include every pixel it touches, lists the tiles that overlap the window, and copies each decoded tile into the matching part of one raster buffer.

This gives callers a simple area-read model without changing the COG's pixel values. Missing tile data stays zero in the output. The window also provides the snapped Web Mercator bounds and the GDAL-style pixel transform needed to place the raster.

References: cog-tiles/…/pixels.rs• L77-102 cog-tiles/…/pixels.rs• L104-169 cog-tiles/…/pixels.rs• L217-264

Striped images

Some TIFF images store uncompressed full-width rows instead of square internal tiles. The library views these images as 256-pixel tiles. Tiles from the same row band can share the row data, which avoids reading the same strip again for nearby tile requests.

References: cog-tiles/…/reader.rs• L83-155 cog-tiles/…/reader.rs• L473-521 cog-tiles/…/bands.rs• L45-75

Raster response encoding

Encoded tiles

The library returns one encoded tile in the format selected from the COG's pixel layout. JPEG and WebP tiles can pass through in their stored form. Other supported image layouts are decoded and encoded as PNG, while indexed mosaics produce PNG tiles with alpha. A caller can also choose PNG, WebP, or JPEG to re-encode every decoded tile.

The output format is consistent for a reader and is exposed with its file extension and MIME type. JPEG table data stored in TIFF metadata is joined to the returned JPEG bytes when needed for a complete response.

Stored or assembled pixelsTile response
JPEG grayscale, YCbCr, or RGBJPEG
WebP RGB or RGBAWebP
Supported 8-bit grayscale, alpha, RGB, or RGBA in other supported compressionPNG
Indexed mosaic pixelsPNG with alpha
Any decoded tile with an explicit encoding choiceSelected PNG, WebP, or JPEG

References: cog-tiles/…/reader.rs• L23-53 cog-tiles/…/reader.rs• L433-461 cog-tiles/…/reader.rs• L618-671 cog-tiles/…/encode.rs• L7-69 martin-core/…/source.rs• L78-100 martin-core/…/source.rs• L197-214

Decoded pixels

Callers that need raster analysis or area assembly can request decoded tile pixels instead of encoded bytes. The result is a square, padded buffer with shape tile_size × tile_size × bands; edge pixels beyond the source image are zero.

The Rust API exposes TilePixels and PixelWindow. Python callers can use the matching tile and read operations described in COG backends and Python bindings.

References: cog-tiles/…/reader.rs• L465-485 cog-tiles/…/pixels.rs• L19-75 cog-tiles-py/cog_tiles.pyi• L151-181

Request lifecycle

Tile request flow

Every request follows the same high-level path. A direct source selects an image level and stored tile. An indexed source first finds and paints source files onto the requested Web Mercator tile. Both paths then return stored bytes, encoded pixels, or no data according to the caller's request.

sequenceDiagram
    participant Client
    participant Source as direct reader or indexed mosaic
    participant Store as Byte-range backend
    participant Encoder as optional encoder
    Client->>Source: request z/x/y
    Source->>Source: resolve source files and image level
    Source->>Store: read needed byte ranges
    Store-->>Source: tile bytes
    Source->>Source: reproject and paint when indexed
    Source->>Encoder: decode and encode when required
    Encoder-->>Source: encoded tile or decoded pixels
    Source-->>Client: tile bytes, pixels, or no data

Malformed metadata, unsupported georeferencing, unsupported pixel layouts, failed index or search reads, and failed byte reads become errors. A valid request with no stored or painted data remains a normal empty result.

References: cog-tiles/…/reader.rs• L282-404 cog-tiles/…/reader.rs• L433-485 cog-tiles/…/indexed.rs• L209-258 cog-tiles/…/mod.rs• L73-91

Access surfaces

The Rust crate is the core tile reader. It accepts any AsyncBackend, while built-in backends cover memory, local files, and object stores. Martin adds direct file and object-store sources plus STAC and tile-indexed sources that publish TileJSON and serve the common tile contract. The Python package wraps the direct reader and adds synchronous and asynchronous access, tile iteration, and NumPy raster reads.

Use COG backends and Python bindings for storage choices and Python-facing behavior. Use file and remote tile sources for the wider source model. This page defines the shared tile, pixel, mosaic, reprojection, and response model.

References: cog-tiles/…/lib.rs• L25-50 cog-tiles/…/mod.rs• L26-49 martin-core/…/source.rs• L20-27 martin-core/…/source.rs• L102-143 cog-tiles-py/…/source.rs• L18-97 cog-tiles-py/…/py.rs• L30-37

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to tile alignment or supported CRS rules affect both single-COG validation and indexed mosaic coverage checks.
  • Changes to band count or output format rules must stay consistent between encoded tiles, TilePixels, indexed alpha output, and the Python array shape.
  • Changes to mosaic key parsing must preserve the relationship between ancestor tile coordinates and file names, including XYZ and TMS row conventions.
  • Changes to index order, STAC sort order, or search zoom must preserve which source paints each overlapping pixel.
  • Changes to reprojection or nodata rules must remain consistent with alpha creation and selected-band output.
  • Changes to supported source CRS values must update both source validation and the coordinate conversion used for tile painting.
  • Changes to empty-tile behavior must remain consistent for single COGs, mosaics, pixel windows, indexed mosaics, and Python area reads.

Important failure modes

TriggerConsequenceGuard
COG uses an unsupported CRS or is not aligned to the direct tile gridThe reader rejects the source or skips the affected imageValidate GeoTIFF georeferencing and report skipped images before serving tiles
A mosaic file does not cover its named tileRequests routed to that file failCheck file bounds against the file tile during open
Mosaic files have different tile sizes or output formatsThe mosaic cannot provide one stable tile contractCompare each file with the first accepted layout
Pixel samples are not supported 8-bit interleaved dataThe source cannot be decoded or encoded by the libraryReject the source during metadata validation
A warped COG uses an unsupported CRS, strips, or lacks a usable overviewThe indexed mosaic cannot paint the requested tileValidate the source before adding it and require a supported projected CRS and detail level
STAC search results exceed the configured item limit or contain mismatched band orderSome coverage is skipped or the asset is not paintedSet search limits with expected overlap in mind and keep the selected band layout stable
Tile-index footprints are not in EPSG:4326 or a location cannot be resolvedThe index cannot provide candidate filesBuild the index with geographic footprints and validate every file location when opening it
Requested tile is outside the image or is sparseThe caller receives no tile dataTreat the empty result as a normal no-data response

Key files

FileWhy to read it
cog-tiles/…/reader.rsDefines COG opening, zoom-level selection, tile lookup, decoding, and response format choice.
cog-tiles/…/geo.rsDefines the Web Mercator georeference and tile-grid alignment model.
cog-tiles/…/mosaic.rsDefines ancestor-based file selection and the multi-COG source contract.
cog-tiles/…/indexed.rsDefines index-driven file selection, paint order, band selection, and indexed mosaic output.
cog-tiles/…/warp.rsDefines reprojection from supported source grids and nodata or alpha painting.
cog-tiles/…/crs.rsDefines the projected coordinate systems supported by warped COGs.
cog-tiles/…/pixels.rsDefines decoded tile buffers and geographic pixel-window assembly.
cog-tiles/…/mod.rsDefines the byte-range source contract used by local and remote readers.
cog-tiles/…/encode.rsDefines PNG, WebP, and JPEG output for decoded 8-bit tiles.
cog-tiles/…/stac.rsDefines STAC search, item ordering, asset band selection, and asset resolution.
cog-tiles/…/tile_index.rsDefines GeoPackage tile-index loading, footprint lookup, and location resolution.
martin-core/…/source.rsConnects direct and indexed COG readers to Martin tile responses and optional re-encoding.
cog-tiles-py/…/py.rsShows how the shared Rust model is exposed as Python tiles, arrays, and rasters.