Skip to content

Commit

Permalink
move STACTiler dependencies in titiler.dependencies (#225)
Browse files Browse the repository at this point in the history
* move STACTiler dependencies in titiler.dependencies

* update changelog
  • Loading branch information
vincentsarago authored Feb 12, 2021
1 parent b10fee8 commit 0422a2f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* remove MosaicTilerFactory `create` and `update` endpoints (https://github.com/developmentseed/titiler/pull/218)
* deleted `titiler.models.mosaics` because the models are not used anymore (https://github.com/developmentseed/titiler/pull/221)
* update rio-tiler and cogeo-mosaic minimal versions (https://github.com/developmentseed/titiler/pull/220, https://github.com/developmentseed/titiler/pull/213)
* move STAC related dependencies to `titiler.dependencies (https://github.com/developmentseed/titiler/pull/225)

## 0.1.0a14 (2021-01-05)

Expand Down
55 changes: 55 additions & 0 deletions titiler/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class PathParams:
url: str = Query(..., description="Dataset URL")


# Dependencies for simple BaseReader (e.g COGReader)
@dataclass
class BidxParams(DefaultDependency):
"""Band Indexes parameters."""
Expand Down Expand Up @@ -121,6 +122,60 @@ def __post_init__(self):
self.kwargs["expression"] = self.expression


# Dependencies for MultiBaseReader (e.g STACReader)
@dataclass
class AssetsBidxParams(DefaultDependency):
"""Asset and Band indexes parameters."""

assets: Optional[str] = Query(
None,
title="Asset indexes",
description="comma (',') delimited asset names (might not be an available options of some readers)",
)
bidx: Optional[str] = Query(
None, title="Band indexes", description="comma (',') delimited band indexes",
)

def __post_init__(self):
"""Post Init."""
if self.assets is not None:
self.kwargs["assets"] = self.assets.split(",")
if self.bidx is not None:
self.kwargs["indexes"] = tuple(
int(s) for s in re.findall(r"\d+", self.bidx)
)


@dataclass
class AssetsBidxExprParams(DefaultDependency):
"""Assets, Band Indexes and Expression parameters."""

assets: Optional[str] = Query(
None,
title="Asset indexes",
description="comma (',') delimited asset names (might not be an available options of some readers)",
)
expression: Optional[str] = Query(
None,
title="Band Math expression",
description="rio-tiler's band math expression (e.g B1/B2)",
)
bidx: Optional[str] = Query(
None, title="Band indexes", description="comma (',') delimited band indexes",
)

def __post_init__(self):
"""Post Init."""
if self.assets is not None:
self.kwargs["assets"] = self.assets.split(",")
if self.expression is not None:
self.kwargs["expression"] = self.expression
if self.bidx is not None:
self.kwargs["indexes"] = tuple(
int(s) for s in re.findall(r"\d+", self.bidx)
)


@dataclass
class MetadataParams(DefaultDependency):
"""Common Metadada parameters."""
Expand Down
70 changes: 14 additions & 56 deletions titiler/endpoints/stac.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,43 @@
"""TiTiler STAC Demo endpoint."""

import re
from dataclasses import dataclass
from typing import Dict, List, Optional, Type, Union
from typing import Dict, List, Type, Union

import rasterio
from geojson_pydantic.features import Feature
from rio_tiler.io import STACReader
from rio_tiler.models import Info, Metadata

from .. import utils
from ..dependencies import DefaultDependency
from ..dependencies import AssetsBidxExprParams, AssetsBidxParams
from ..resources.responses import GeoJSONResponse
from ..templates import templates
from .factory import TilerFactory

from fastapi import Depends, Query
from fastapi import Depends

from starlette.requests import Request
from starlette.responses import HTMLResponse


@dataclass
class AssetsBidxParams(DefaultDependency):
"""Asset and Band indexes parameters."""

assets: Optional[str] = Query(
None,
title="Asset indexes",
description="comma (',') delimited asset names (might not be an available options of some readers)",
)
bidx: Optional[str] = Query(
None, title="Band indexes", description="comma (',') delimited band indexes",
)

def __post_init__(self):
"""Post Init."""
if self.assets is not None:
self.kwargs["assets"] = self.assets.split(",")
if self.bidx is not None:
self.kwargs["indexes"] = tuple(
int(s) for s in re.findall(r"\d+", self.bidx)
)


@dataclass
class AssetsBidxExprParams(DefaultDependency):
"""Assets, Band Indexes and Expression parameters."""

assets: Optional[str] = Query(
None,
title="Asset indexes",
description="comma (',') delimited asset names (might not be an available options of some readers)",
)
expression: Optional[str] = Query(
None,
title="Band Math expression",
description="rio-tiler's band math expression (e.g B1/B2)",
)
bidx: Optional[str] = Query(
None, title="Band indexes", description="comma (',') delimited band indexes",
)
class STACTiler(TilerFactory):
"""Custom Tiler Class for STAC.
def __post_init__(self):
"""Post Init."""
if self.assets is not None:
self.kwargs["assets"] = self.assets.split(",")
if self.expression is not None:
self.kwargs["expression"] = self.expression
if self.bidx is not None:
self.kwargs["indexes"] = tuple(
int(s) for s in re.findall(r"\d+", self.bidx)
)
Note:
To be able to use the rio_tiler.io.STACReader we need to be able to pass a `assets`
argument to most of its methods. By using the `AssetsBidxExprParams` for the `layer_dependency`, the
.tile(), .point(), .preview() and the .part() methods will receive assets, expression or indexes arguments.
The rio_tiler.io.STACReader `.info()` and `.metadata()` have `assets` as
a requirement arguments (https://github.com/cogeotiff/rio-tiler/blob/master/rio_tiler/io/base.py#L365).
This means we have to update the /info and /metadata endpoints in order to add the `assets` dependency.
@dataclass
class STACTiler(TilerFactory):
"""Custom Tiler Class for STAC."""
"""

reader: Type[STACReader] = STACReader

# Assets,Indexes/Expression Dependencies
layer_dependency: Type[AssetsBidxExprParams] = AssetsBidxExprParams

# Overwrite _info method to return the list of assets when no assets is passed.
Expand Down

0 comments on commit 0422a2f

Please sign in to comment.