diff --git a/titiler/xarray/factory.py b/titiler/xarray/factory.py index 0626d2c..7341e5b 100644 --- a/titiler/xarray/factory.py +++ b/titiler/xarray/factory.py @@ -1,7 +1,7 @@ """TiTiler.xarray factory.""" from dataclasses import dataclass -from typing import Dict, List, Literal, Optional, Type +from typing import Dict, List, Literal, Optional, Type, Union from urllib.parse import urlencode import jinja2 @@ -23,6 +23,22 @@ from titiler.xarray.reader import ZarrReader +def nodata_dependency( + nodata: Annotated[ + Optional[Union[str, int, float]], + Query( + title="Nodata value", + description="Overwrite internal Nodata value", + ), + ] = None, +) -> Optional[float]: + """Nodata dependency.""" + if nodata is not None: + nodata = np.nan if nodata == "nan" else float(nodata) + + return None + + @dataclass class ZarrTilerFactory(BaseTilerFactory): """Zarr Tiler Factory.""" @@ -229,6 +245,7 @@ def tiles_endpoint( # type: ignore description="Whether to expect and open zarr store with consolidated metadata", ), ] = True, + nodata=Depends(nodata_dependency), ) -> Response: """Create map tile from a dataset.""" tms = self.supported_tms.get(tileMatrixSetId) @@ -243,9 +260,12 @@ def tiles_endpoint( # type: ignore tms=tms, consolidated=consolidated, ) as src_dst: - image = src_dst.tile( - x, y, z, tilesize=scale * 256, nodata=src_dst.input.rio.nodata + x, + y, + z, + tilesize=scale * 256, + nodata=nodata if nodata is not None else src_dst.input.rio.nodata, ) if post_process: @@ -348,6 +368,7 @@ def tilejson_endpoint( # type: ignore description="Whether to expect and open zarr store with consolidated metadata", ), ] = True, + nodata=Depends(nodata_dependency), ) -> Dict: """Return TileJSON document for a dataset.""" route_params = { @@ -518,6 +539,7 @@ def map_viewer( color_formula=Depends(ColorFormulaParams), colormap=Depends(self.colormap_dependency), render_params=Depends(self.render_dependency), + nodata=Depends(nodata_dependency), ): """Return map Viewer.""" templates = Jinja2Templates(