Skip to main content

Extensions for TiTiler Factories.

Project description

titiler.extensions

Extent TiTiler Tiler Factories

Installation

$ pip install -U pip

# From Pypi
$ pip install titiler.extensions

# Or from sources
$ git clone https://github.com/developmentseed/titiler.git
$ cd titiler && pip install -e titiler/core -e 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")

See titiler.application for a full example.

Create your own

from dataclasses import dataclass, field
from typing import Tuple, List, Optional

from starlette.responses import Response
from fastapi import Depends, FastAPI, Query
from titiler.core.factory import BaseTilerFactory, FactoryExtension, TilerFactory
from titiler.core.dependencies import RescalingParams
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 BaseTilerFactory object as input
    def register(self, factory: BaseTilerFactory):
        """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),
            layer_params=Depends(factory.layer_dependency),
            dataset_params=Depends(factory.dataset_dependency),
            post_process=Depends(factory.process_dependency),
            rescale: Optional[List[Tuple[float, ...]]] = Depends(RescalingParams),
            color_formula: Optional[str] = Query(
                None,
                title="Color Formula",
                description="rio-color formula (info: https://github.com/mapbox/rio-color)",
            ),
            colormap=Depends(factory.colormap_dependency),
            render_params=Depends(factory.render_dependency),
            reader_params=Depends(factory.reader_dependency),
            env=Depends(factory.environment_dependency),
        ):
            with rasterio.Env(**env):
                with self.reader(src_path, **reader_params) as src_dst:
                        im = src.preview(
                            max_size=self.max_size,
                            **layer_params,
                            **dataset_params,
                        )

            if post_process:
                image = post_process(image)

            if rescale:
                image.rescale(rescale)

            if color_formula:
                image.apply_color_formula(color_formula)

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

            content = image.render(
                img_format=format.driver,
                colormap=colormap or dst_colormap,
                **format.profile,
                **render_params,
            )

            return Response(content, media_type=format.mediatype)

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

Project details


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-0.11.1a0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

titiler.extensions-0.11.1a0-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file titiler.extensions-0.11.1a0.tar.gz.

File metadata

  • Download URL: titiler.extensions-0.11.1a0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for titiler.extensions-0.11.1a0.tar.gz
Algorithm Hash digest
SHA256 c2d83efc21d8a71dff429237d9a60975cc088b6bd9e4b62a271e93e973d77d16
MD5 5be9f108cc79dc51f9aadd01158e729a
BLAKE2b-256 9403007682ca126854974014dbb2a5738dd1f2840ddd4b8f9c43a9bf211c3cbc

See more details on using hashes here.

File details

Details for the file titiler.extensions-0.11.1a0-py3-none-any.whl.

File metadata

File hashes

Hashes for titiler.extensions-0.11.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5f8d307dc8569a1d9b538cc7acbebdaf282b9501fbea7b56db1a9e04d6193ac
MD5 971ffacb81e9a9cb971b4c714f2caac8
BLAKE2b-256 0b7b468563a67a12b3551f747f03796da1bfcb95737662101c222af7055ca11c

See more details on using hashes here.

Supported by

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