Mosaic from COG urls

Goal: Create a custom mosaic tiler which takes multiple URL as input

requirements: titiler.core | titiler.mosaic

1 - Create a custom Mosaic Backends

"""mosaic backends.

The goal is to build a minimalist Mosaic Backend which takes COG paths as input.

>>> with MultiFilesBackend(["cog1.tif", "cog2.tif"]) as mosaic:
    img = mosaic.tile(1, 1, 1)


from typing import Type, List, Tuple, Dict, Union

import attr
from import BaseReader, COGReader, MultiBandReader, MultiBaseReader
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS
from import CRS
from morecantile import TileMatrixSet

from cogeo_mosaic.backends.base import BaseBackend
from cogeo_mosaic.mosaic import MosaicJSON

class MultiFilesBackend(BaseBackend):

    input: List[str] = attr.ib()

    reader: Union[
    ] = attr.ib(default=COGReader)
    reader_options: Dict = attr.ib(factory=dict)

    geographic_crs: CRS = attr.ib(default=WGS84_CRS)

    tms: TileMatrixSet = attr.ib(default=WEB_MERCATOR_TMS)
    minzoom: int = attr.ib(default=0)
    maxzoom: int = attr.ib(default=30)

    # default values for bounds
    bounds: Tuple[float, float, float, float] = attr.ib(
        default=(-180, -90, 180, 90)
    crs: CRS = attr.ib(init=False, default=WGS84_CRS)

    # mosaic_def is outside the __init__ method
    mosaic_def: MosaicJSON = attr.ib(init=False)

    _backend_name = "MultiFiles"

    def __attrs_post_init__(self):
        """Post Init."""
        # Construct a FAKE/Empty mosaicJSON
        # mosaic_def has to be defined.
        self.mosaic_def = MosaicJSON(
            name="it's fake but it's ok",
            tiles=[]  # we set `tiles` to an empty list.

    def write(self, overwrite: bool = True):
        """This method is not used but is required by the abstract class."""

    def update(self):
        """We overwrite the default method."""

    def _read(self) -> MosaicJSON:
        """This method is not used but is required by the abstract class."""

    def assets_for_tile(self, x: int, y: int, z: int) -> List[str]:
        """Retrieve assets for tile."""
        return self.get_assets()

    def assets_for_point(self, lng: float, lat: float) -> List[str]:
        """Retrieve assets for point."""
        return self.get_assets()

    def get_assets(self) -> List[str]:
        """assets are just files we give in path"""
        return self.input

    def _quadkeys(self) -> List[str]:
        return []

2 - Create endpoints




from dataclasses import dataclass
from typing import List

from titiler.mosaic.factory import MosaicTilerFactory
from fastapi import Query

from .backends import MultiFilesBackend

class MosaicTiler(MosaicTilerFactory):
    """Custom MosaicTilerFactory.

    Note this is a really simple MosaicTiler Factory with only few endpoints.

    def register_routes(self):
        """This Method register routes to the router. """


def DatasetPathParams(url: str = Query(..., description="Dataset URL")) -> List[str]:
    """Create dataset path from args"""
    return url.split(",")

mosaic = MosaicTiler(reader=MultiFilesBackend, path_dependency=DatasetPathParams)

3 - Create app and register our custom endpoints




from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers
from titiler.mosaic.errors import MOSAIC_STATUS_CODES

from fastapi import FastAPI

from .routers import mosaic

app = FastAPI()
add_exception_handlers(app, DEFAULT_STATUS_CODES)
add_exception_handlers(app, MOSAIC_STATUS_CODES)
  1. Run and Use
$ uvicorn app:app --reload

$ curl,cog2.tif


  • bounds of the mosaic backend is set to [-180, -90, 180, 90]
  • minzoom is set to 0
  • maxzoom is set to 30