Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add environment_dependency to define GDAL env at runtime #483

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add environment_dependency to define GDAL env at runtime
  • Loading branch information
vincentsarago committed Jun 7, 2022
commit 202aadbb22247eb33e44ce1907c550a9e9c3fd6b
28 changes: 28 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# Release Notes

## 0.7.0 (TBD)

* add `environment_dependency` option in `BaseTilerFactory` to define GDAL environment at runtime.
* remove `gdal_config` option in `BaseTilerFactory` **breaking**

```python
# before
router = TilerFactory(gdal_config={"GDAL_DISABLE_READDIR_ON_OPEN": "FALSE"}).router

# now
router = TilerFactory(environment_dependency=lambda: {"GDAL_DISABLE_READDIR_ON_OPEN": "FALSE"}).router


class ReaddirType(str, Enum):

false = "false"
true = "true"
empty_dir = "empty_dir"


# or at endpoint call. The user could choose between false/true/empty_dir
def gdal_env(disable_read: ReaddirType = Query(ReaddirType.false)):
return {"GDAL_DISABLE_READDIR_ON_OPEN": disable_read.value.upper()}

router = TilerFactory(environment_dependency=gdal_env).router
```


## 0.6.0 (2022-05-13)

* no change since `0.6.0a2`
Expand Down
8 changes: 6 additions & 2 deletions docs/src/advanced/APIRoute_and_environment_variables.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
!!! important
This has been deprecated. You can now pass `gdal_config={"GDAL_DISABLE_READDIR_ON_OPEN":"FALSE"}` to the Tiler Factory. This will be passed to a `rasterio.Env()` context manager on top of all gdal related blocks.
This has been deprecated. You can now pass `environment_dependency=lambda: {"GDAL_DISABLE_READDIR_ON_OPEN":"FALSE"}` to the Tiler Factory. This will be passed to a `rasterio.Env()` context manager on top of all gdal related blocks.

```python
from titiler.core.factory import TilerFactory
cog = TilerFactory(reader=COGReader, router_prefix="cog", gdal_config={"GDAL_DISABLE_READDIR_ON_OPEN":"FALSE"})
cog = TilerFactory(
reader=COGReader,
router_prefix="cog",
environment_dependency=lambda: {"GDAL_DISABLE_READDIR_ON_OPEN":"FALSE"},
)
```

Sometimes, specifically when using GDAL, it can be useful to have environment variables set for certain endpoints
Expand Down
14 changes: 10 additions & 4 deletions docs/src/advanced/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ class CustomMosaicFactory(MosaicTilerFactory):
@self.router.post(
"", response_model=MosaicJSON, response_model_exclude_none=True
)
def create(body: CreateMosaicJSON):
def create(
body: CreateMosaicJSON,
env=Depends(self.environment_dependency),
):
"""Create a MosaicJSON"""
# Write can write to either a local path, a S3 path...
# See https://developmentseed.org/cogeo-mosaic/advanced/backends/ for the list of supported backends
Expand All @@ -150,7 +153,7 @@ class CustomMosaicFactory(MosaicTilerFactory):
)

# Write the MosaicJSON using a cogeo-mosaic backend
with rasterio.Env(**self.gdal_config):
with rasterio.Env(**env):
with self.reader(
body.url, mosaic_def=mosaic, reader=self.dataset_reader
) as mosaic:
Expand All @@ -168,9 +171,12 @@ class CustomMosaicFactory(MosaicTilerFactory):
@self.router.put(
"", response_model=MosaicJSON, response_model_exclude_none=True
)
def update_mosaicjson(body: UpdateMosaicJSON):
def update_mosaicjson(
body: UpdateMosaicJSON,
env=Depends(self.environment_dependency),
):
"""Update an existing MosaicJSON"""
with rasterio.Env(**self.gdal_config):
with rasterio.Env(**env):
with self.reader(body.url, reader=self.dataset_reader) as mosaic:
features = get_footprints(body.files, max_threads=body.max_threads)
try:
Expand Down
2 changes: 2 additions & 0 deletions src/titiler/application/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools>=46.4", "wheel"]
2 changes: 2 additions & 0 deletions src/titiler/core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools>=46.4", "wheel"]
Binary file added src/titiler/core/tests/fixtures/non_cog.tif
Binary file not shown.
Binary file added src/titiler/core/tests/fixtures/non_cog.tif.ovr
Binary file not shown.
48 changes: 48 additions & 0 deletions src/titiler/core/tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import pathlib
from dataclasses import dataclass
from enum import Enum
from io import BytesIO
from typing import Dict, Type
from unittest.mock import patch
Expand Down Expand Up @@ -1324,3 +1325,50 @@ def must_be_bob(credentials: security.HTTPBasicCredentials = Depends(http_basic)
)
assert response.status_code == 401
assert response.json()["detail"] == "You're not Bob"


def test_TilerFactory_WithGdalEnv():
"""test environment_dependency option."""

router = TilerFactory(
environment_dependency=lambda: {"GDAL_DISABLE_READDIR_ON_OPEN": "EMPTY_DIR"}
).router
app = FastAPI()
app.include_router(router)
client = TestClient(app)

response = client.get(f"/info?url={DATA_DIR}/non_cog.tif")
assert not response.json()["overviews"]

router = TilerFactory(
environment_dependency=lambda: {"GDAL_DISABLE_READDIR_ON_OPEN": "FALSE"}
).router
app = FastAPI()
app.include_router(router)
client = TestClient(app)

response = client.get(f"/info?url={DATA_DIR}/non_cog.tif")
assert response.json()["overviews"]

class ReaddirType(str, Enum):

false = "false"
true = "true"
empty_dir = "empty_dir"

def gdal_env(disable_read: ReaddirType = Query(ReaddirType.false)):
return {"GDAL_DISABLE_READDIR_ON_OPEN": disable_read.value.upper()}

router = TilerFactory(environment_dependency=gdal_env).router
app = FastAPI()
app.include_router(router)
client = TestClient(app)

response = client.get(f"/info?url={DATA_DIR}/non_cog.tif")
assert response.json()["overviews"]

response = client.get(f"/info?url={DATA_DIR}/non_cog.tif&disable_read=false")
assert response.json()["overviews"]

response = client.get(f"/info?url={DATA_DIR}/non_cog.tif&disable_read=empty_dir")
assert not response.json()["overviews"]
Loading