Skip to content

Commit

Permalink
add nodata dependency to enable nodata overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Sep 5, 2024
1 parent 940c559 commit 48476bf
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions titiler/xarray/factory.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 48476bf

Please sign in to comment.