Skip to main content

Extensions for TiTiler Factories.

Project description

titiler.extensions

Extent TiTiler Tiler Factories

Installation

python -m pip install -U pip

# From Pypi
python -m pip install titiler.extensions

# Or from sources
git clone https://github.com/developmentseed/titiler.git
cd titiler && python -m pip install -e src/titiler/core -e src/titiler/extensions

Available extensions

cogValidateExtension

  • Goal: adds a /validate endpoint which return the content of rio-cogeo info method
  • Additional requirements: titiler.extensions["cogeo"]

cogViewerExtension

  • Goal: adds a /viewer endpoint which return an HTML viewer for simple COGs

stacViewerExtension

  • Goal: adds a /viewer endpoint which return an HTML viewer for STAC item

wmsExtension

  • Goal: adds a /wms endpoint to support OGC Web Map Service (GetTile and GetCapabilities) specification

Use extensions

Extensions must be set at TilerFactory's creation using the extensions= options.

from fastapi import FastAPI
from titiler.core.factory import TilerFactory
from titiler.extensions import cogValidateExtension

# Create a FastAPI application
app = FastAPI(description="A lightweight Cloud Optimized GeoTIFF tile server")

# Create a set of endpoints using TiTiler TilerFactory
tiler = TilerFactory(
    router_prefix="/cog",
    extensions=[
        cogValidateExtension()  # the cogeoExtension will add a rio-cogeo /validate endpoint
    ]
)

# Register endpoints to the application
app.include_router(tiler.router, prefix="/cog")

Create your own

from dataclasses import dataclass, field
from typing import Tuple, List, Optional
import rasterio
from starlette.responses import Response
from fastapi import Depends, FastAPI, Query
from titiler.core.factory import TilerFactory, FactoryExtension
from titiler.core.dependencies import ImageRenderingParams
from titiler.core.factory import TilerFactory
from titiler.core.resources.enums import ImageType


@dataclass
class thumbnailExtension(FactoryExtension):
    """Add endpoint to a TilerFactory."""

    # Set some options
    max_size: int = field(default=128)

    # Register method is mandatory and must take a TilerFactory object as input
    def register(self, factory: TilerFactory):
        """Register endpoint to the tiler factory."""

        # register an endpoint to the factory's router
        @factory.router.get(
            "/thumbnail",
            responses={
                200: {
                    "content": {
                        "image/png": {},
                        "image/jpeg": {},
                    },
                    "description": "Return an image.",
                }
            },
            response_class=Response,
        )
        def thumbnail(
            # we can reuse the factory dependency
            src_path: str = Depends(factory.path_dependency),
            reader_params=Depends(factory.reader_dependency),
            layer_params=Depends(factory.layer_dependency),
            dataset_params=Depends(factory.dataset_dependency),
            post_process=Depends(factory.process_dependency),
            colormap=Depends(factory.colormap_dependency),
            render_params=Depends(factory.render_dependency),
            env=Depends(factory.environment_dependency),
        ):
            with rasterio.Env(**env):
                with factory.reader(src_path, **reader_params.as_dict()) as src:
                    image = src.preview(
                        max_size=self.max_size,
                        **layer_params.as_dict(),
                        **dataset_params.as_dict(),
                    )

            if post_process:
                image = post_process(image)

            format = ImageType.jpeg if image.mask.all() else ImageType.png

            if post_process:
                image = post_process(image)

            content, media_type = factory.render_func(
                image,
                colormap=colormap,
                **render_params.as_dict(),
            )

            return Response(content, media_type=media_type)

# Use it
app = FastAPI()
tiler = TilerFactory(
    extensions=[
        thumbnailExtension(max_size=64)
    ]
)
app.include_router(tiler.router)

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

titiler_extensions-2.0.0.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

titiler_extensions-2.0.0-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

File details

Details for the file titiler_extensions-2.0.0.tar.gz.

File metadata

  • Download URL: titiler_extensions-2.0.0.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for titiler_extensions-2.0.0.tar.gz
Algorithm Hash digest
SHA256 7ca8ccb63f5b05ee0a26dc88199df6c2932c2b7ec9a2abacde9515492a9a5295
MD5 53ba0a6805f528944caa7b39f1f5eac9
BLAKE2b-256 b60cc286f07813cf67a730f6891c85462d406f4e7484e7ab37eef4b0b32be26a

See more details on using hashes here.

File details

Details for the file titiler_extensions-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: titiler_extensions-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for titiler_extensions-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 102c52a07e30ebe4fa619e1977022e2af6bcf6f092b2ea185683249c81d18ee5
MD5 1f0cc61b962e28a945795e6e43622fe1
BLAKE2b-256 75579befd0b24ac858e5762d9772ef29d19acd8fcf0ee4b5761fab29ec3daf1a

See more details on using hashes here.

Supported by

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