From 820de8eb523021f5c1f37650eee618ed858c0954 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 28 Nov 2024 22:44:30 +0100 Subject: [PATCH] define variable outside endpoint code (#1040) * define variable outside endpoint code * fix tests --- CHANGES.md | 6 ++++ src/titiler/mosaic/tests/test_factory.py | 6 ++-- src/titiler/mosaic/titiler/mosaic/factory.py | 29 ++++++++++---------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ddf5729b4..c3b69f755 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/src/titiler/mosaic/tests/test_factory.py b/src/titiler/mosaic/tests/test_factory.py index 47e31ce8d..b960d1544 100644 --- a/src/titiler/mosaic/tests/test_factory.py +++ b/src/titiler/mosaic/tests/test_factory.py @@ -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 @@ -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) diff --git a/src/titiler/mosaic/titiler/mosaic/factory.py b/src/titiler/mosaic/titiler/mosaic/factory.py index fbdf34953..052cbf4ac 100644 --- a/src/titiler/mosaic/titiler/mosaic/factory.py +++ b/src/titiler/mosaic/titiler/mosaic/factory.py @@ -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 @@ -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( @@ -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}", @@ -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(), @@ -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( @@ -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, @@ -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(), )