From 0422a2f4d81abc53ccf2ec9a48c24b258e4a1632 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 11 Feb 2021 20:07:32 -0500 Subject: [PATCH] move STACTiler dependencies in titiler.dependencies (#225) * move STACTiler dependencies in titiler.dependencies * update changelog --- CHANGES.md | 1 + titiler/dependencies.py | 55 ++++++++++++++++++++++++++++++ titiler/endpoints/stac.py | 70 ++++++++------------------------------- 3 files changed, 70 insertions(+), 56 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index f2cb0eee9..371ddeaa6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/titiler/dependencies.py b/titiler/dependencies.py index e33df69b7..13b648a65 100644 --- a/titiler/dependencies.py +++ b/titiler/dependencies.py @@ -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.""" @@ -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.""" diff --git a/titiler/endpoints/stac.py b/titiler/endpoints/stac.py index 012df3f95..f30b051d6 100644 --- a/titiler/endpoints/stac.py +++ b/titiler/endpoints/stac.py @@ -1,8 +1,7 @@ """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 @@ -10,76 +9,35 @@ 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.