Skip to content

Commit

Permalink
define variable outside endpoint code (#1040)
Browse files Browse the repository at this point in the history
* define variable outside endpoint code

* fix tests
  • Loading branch information
vincentsarago authored Nov 28, 2024
1 parent 50cdfb4 commit 820de8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## 0.19.2 (2024-11-28)

### titiler.mosaic

* Define variable (`MOSAIC_CONCURRENCY` and `MOSAIC_STRICT_ZOOM`) from env-variable outside endpoint code

## 0.19.1 (2024-11-14)

* Add `titiler` links in Map attributions
Expand Down
6 changes: 3 additions & 3 deletions src/titiler/mosaic/tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from contextlib import contextmanager
from dataclasses import dataclass
from io import BytesIO
from unittest.mock import patch

import morecantile
import numpy
Expand Down Expand Up @@ -356,10 +357,9 @@ def test_MosaicTilerFactory_PixelSelectionParams():
assert (npy_tile != npy_tile_highest).any()


def test_MosaicTilerFactory_strict_zoom(monkeypatch):
@patch("titiler.mosaic.factory.MOSAIC_STRICT_ZOOM", new=True)
def test_MosaicTilerFactory_strict_zoom():
"""Test MosaicTilerFactory factory with STRICT Zoom Mode"""
monkeypatch.setenv("MOSAIC_STRICT_ZOOM", "TRUE")

mosaic = MosaicTilerFactory()
app = FastAPI()
app.include_router(mosaic.router)
Expand Down
29 changes: 15 additions & 14 deletions src/titiler/mosaic/titiler/mosaic/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
from titiler.core.utils import render_image
from titiler.mosaic.models.responses import Point

MOSAIC_THREADS = int(os.getenv("MOSAIC_CONCURRENCY", MAX_THREADS))
MOSAIC_STRICT_ZOOM = str(os.getenv("MOSAIC_STRICT_ZOOM", False)).lower() in [
"true",
"yes",
]


def PixelSelectionParams(
pixel_selection: Annotated[ # type: ignore
Expand Down Expand Up @@ -575,13 +581,6 @@ def tile(
f"Invalid 'scale' parameter: {scale}. Scale HAVE TO be between 1 and 4",
)

threads = int(os.getenv("MOSAIC_CONCURRENCY", MAX_THREADS))

strict_zoom = str(os.getenv("MOSAIC_STRICT_ZOOM", False)).lower() in [
"true",
"yes",
]

tms = self.supported_tms.get(tileMatrixSetId)
with rasterio.Env(**env):
with self.backend(
Expand All @@ -592,7 +591,9 @@ def tile(
**backend_params.as_dict(),
) as src_dst:

if strict_zoom and (z < src_dst.minzoom or z > src_dst.maxzoom):
if MOSAIC_STRICT_ZOOM and (
z < src_dst.minzoom or z > src_dst.maxzoom
):
raise HTTPException(
400,
f"Invalid ZOOM level {z}. Should be between {src_dst.minzoom} and {src_dst.maxzoom}",
Expand All @@ -604,7 +605,7 @@ def tile(
z,
pixel_selection=pixel_selection,
tilesize=scale * 256,
threads=threads,
threads=MOSAIC_THREADS,
**tile_params.as_dict(),
**layer_params.as_dict(),
**dataset_params.as_dict(),
Expand Down Expand Up @@ -779,10 +780,12 @@ def map_viewer(
):
"""Return TileJSON document for a dataset."""
tilejson_url = self.url_for(
request, "tilejson", tileMatrixSetId=tileMatrixSetId
request,
"tilejson",
tileMatrixSetId=tileMatrixSetId,
)
if request.query_params._list:
tilejson_url += f"?{urlencode(request.query_params._list)}"
tilejson_url += f"?{urlencode(request.query_params._list, doseq=True)}"

tms = self.supported_tms.get(tileMatrixSetId)
return self.templates.TemplateResponse(
Expand Down Expand Up @@ -966,8 +969,6 @@ def point(
env=Depends(self.environment_dependency),
):
"""Get Point value for a Mosaic."""
threads = int(os.getenv("MOSAIC_CONCURRENCY", MAX_THREADS))

with rasterio.Env(**env):
with self.backend(
src_path,
Expand All @@ -979,7 +980,7 @@ def point(
lon,
lat,
coord_crs=coord_crs or WGS84_CRS,
threads=threads,
threads=MOSAIC_THREADS,
**layer_params.as_dict(),
**dataset_params.as_dict(),
)
Expand Down

0 comments on commit 820de8e

Please sign in to comment.