Skip to content

Commit 820de8e

Browse files
define variable outside endpoint code (#1040)
* define variable outside endpoint code * fix tests
1 parent 50cdfb4 commit 820de8e

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release Notes
22

3+
## 0.19.2 (2024-11-28)
4+
5+
### titiler.mosaic
6+
7+
* Define variable (`MOSAIC_CONCURRENCY` and `MOSAIC_STRICT_ZOOM`) from env-variable outside endpoint code
8+
39
## 0.19.1 (2024-11-14)
410

511
* Add `titiler` links in Map attributions

src/titiler/mosaic/tests/test_factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from contextlib import contextmanager
66
from dataclasses import dataclass
77
from io import BytesIO
8+
from unittest.mock import patch
89

910
import morecantile
1011
import numpy
@@ -356,10 +357,9 @@ def test_MosaicTilerFactory_PixelSelectionParams():
356357
assert (npy_tile != npy_tile_highest).any()
357358

358359

359-
def test_MosaicTilerFactory_strict_zoom(monkeypatch):
360+
@patch("titiler.mosaic.factory.MOSAIC_STRICT_ZOOM", new=True)
361+
def test_MosaicTilerFactory_strict_zoom():
360362
"""Test MosaicTilerFactory factory with STRICT Zoom Mode"""
361-
monkeypatch.setenv("MOSAIC_STRICT_ZOOM", "TRUE")
362-
363363
mosaic = MosaicTilerFactory()
364364
app = FastAPI()
365365
app.include_router(mosaic.router)

src/titiler/mosaic/titiler/mosaic/factory.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
from titiler.core.utils import render_image
5252
from titiler.mosaic.models.responses import Point
5353

54+
MOSAIC_THREADS = int(os.getenv("MOSAIC_CONCURRENCY", MAX_THREADS))
55+
MOSAIC_STRICT_ZOOM = str(os.getenv("MOSAIC_STRICT_ZOOM", False)).lower() in [
56+
"true",
57+
"yes",
58+
]
59+
5460

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

578-
threads = int(os.getenv("MOSAIC_CONCURRENCY", MAX_THREADS))
579-
580-
strict_zoom = str(os.getenv("MOSAIC_STRICT_ZOOM", False)).lower() in [
581-
"true",
582-
"yes",
583-
]
584-
585584
tms = self.supported_tms.get(tileMatrixSetId)
586585
with rasterio.Env(**env):
587586
with self.backend(
@@ -592,7 +591,9 @@ def tile(
592591
**backend_params.as_dict(),
593592
) as src_dst:
594593

595-
if strict_zoom and (z < src_dst.minzoom or z > src_dst.maxzoom):
594+
if MOSAIC_STRICT_ZOOM and (
595+
z < src_dst.minzoom or z > src_dst.maxzoom
596+
):
596597
raise HTTPException(
597598
400,
598599
f"Invalid ZOOM level {z}. Should be between {src_dst.minzoom} and {src_dst.maxzoom}",
@@ -604,7 +605,7 @@ def tile(
604605
z,
605606
pixel_selection=pixel_selection,
606607
tilesize=scale * 256,
607-
threads=threads,
608+
threads=MOSAIC_THREADS,
608609
**tile_params.as_dict(),
609610
**layer_params.as_dict(),
610611
**dataset_params.as_dict(),
@@ -779,10 +780,12 @@ def map_viewer(
779780
):
780781
"""Return TileJSON document for a dataset."""
781782
tilejson_url = self.url_for(
782-
request, "tilejson", tileMatrixSetId=tileMatrixSetId
783+
request,
784+
"tilejson",
785+
tileMatrixSetId=tileMatrixSetId,
783786
)
784787
if request.query_params._list:
785-
tilejson_url += f"?{urlencode(request.query_params._list)}"
788+
tilejson_url += f"?{urlencode(request.query_params._list, doseq=True)}"
786789

787790
tms = self.supported_tms.get(tileMatrixSetId)
788791
return self.templates.TemplateResponse(
@@ -966,8 +969,6 @@ def point(
966969
env=Depends(self.environment_dependency),
967970
):
968971
"""Get Point value for a Mosaic."""
969-
threads = int(os.getenv("MOSAIC_CONCURRENCY", MAX_THREADS))
970-
971972
with rasterio.Env(**env):
972973
with self.backend(
973974
src_path,
@@ -979,7 +980,7 @@ def point(
979980
lon,
980981
lat,
981982
coord_crs=coord_crs or WGS84_CRS,
982-
threads=threads,
983+
threads=MOSAIC_THREADS,
983984
**layer_params.as_dict(),
984985
**dataset_params.as_dict(),
985986
)

0 commit comments

Comments
 (0)