Skip to content

Commit

Permalink
Allow a default color_formula to be defined on the factory (#707)
Browse files Browse the repository at this point in the history
instead of needing it to be passed through query params.
  • Loading branch information
samn authored Oct 3, 2023
1 parent 9c2005b commit 50934c9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## Next Release

* Allow a default `color_formula` parameter to be set via a dependency (author @samn, https://github.com/developmentseed/titiler/pull/707)


## 0.15.0 (2023-09-28)

### titiler.core
Expand Down
31 changes: 31 additions & 0 deletions src/titiler/core/tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,3 +1648,34 @@ def test_dst_crs_option():
)
meta = parse_img(response.content)
assert meta["crs"] == CRS.from_epsg(32621)


def test_color_formula_dependency():
"""Ensure that we can set default color formulae via the color_formula_dependency"""

def custom_color_formula_params() -> Optional[str]:
return "sigmoidal R 7 0.4"

cog = TilerFactory()
cog_custom_color_formula = TilerFactory(
color_formula_dependency=custom_color_formula_params
)

app = FastAPI()
app.include_router(cog.router, prefix="/cog")
app.include_router(cog_custom_color_formula.router, prefix="/cog_custom")

with TestClient(app) as client:
response = client.get(
f"/cog/tiles/8/87/48.npy?url={DATA_DIR}/cog.tif&color_formula=sigmoidal R 10 0.1"
)
assert response.status_code == 200
assert response.headers["content-type"] == "application/x-binary"
npy_tile = numpy.load(BytesIO(response.content))
assert npy_tile.shape == (2, 256, 256) # mask + data

response = client.get(f"/cog_custom/tiles/8/87/48.npy?url={DATA_DIR}/cog.tif")
assert response.status_code == 200
assert response.headers["content-type"] == "application/x-binary"
numpy.load(BytesIO(response.content))
assert npy_tile.shape == (2, 256, 256) # mask + data
15 changes: 8 additions & 7 deletions src/titiler/core/titiler/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class BaseTilerFactory(metaclass=abc.ABCMeta):
# Image rendering Dependencies
render_dependency: Type[DefaultDependency] = ImageRenderingParams
colormap_dependency: Callable[..., Optional[ColorMapType]] = ColorMapParams
color_formula_dependency: Callable[..., Optional[str]] = ColorFormulaParams

rescale_dependency: Callable[..., Optional[RescaleType]] = RescalingParams

Expand Down Expand Up @@ -539,7 +540,7 @@ def tile(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down Expand Up @@ -625,7 +626,7 @@ def tilejson(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down Expand Up @@ -705,7 +706,7 @@ def map_viewer(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down Expand Up @@ -767,7 +768,7 @@ def wmts(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down Expand Up @@ -898,7 +899,7 @@ def preview(
image_params=Depends(self.img_preview_dependency),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down Expand Up @@ -965,7 +966,7 @@ def bbox_image(
image_params=Depends(self.img_part_dependency),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down Expand Up @@ -1028,7 +1029,7 @@ def feature_image(
image_params=Depends(self.img_part_dependency),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
reader_params=Depends(self.reader_dependency),
Expand Down
15 changes: 5 additions & 10 deletions src/titiler/mosaic/titiler/mosaic/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
from starlette.responses import HTMLResponse, Response
from typing_extensions import Annotated

from titiler.core.dependencies import (
BufferParams,
ColorFormulaParams,
CoordCRSParams,
DefaultDependency,
)
from titiler.core.dependencies import BufferParams, CoordCRSParams, DefaultDependency
from titiler.core.factory import BaseTilerFactory, img_endpoint_params
from titiler.core.models.mapbox import TileJSON
from titiler.core.resources.enums import ImageType, MediaType, OptionalHeader
Expand Down Expand Up @@ -274,7 +269,7 @@ def tile(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
backend_params=Depends(self.backend_dependency),
Expand Down Expand Up @@ -393,7 +388,7 @@ def tilejson(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
backend_params=Depends(self.backend_dependency),
Expand Down Expand Up @@ -485,7 +480,7 @@ def map_viewer(
pixel_selection=Depends(self.pixel_selection_dependency),
buffer=Depends(BufferParams),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
backend_params=Depends(self.backend_dependency),
Expand Down Expand Up @@ -549,7 +544,7 @@ def wmts(
buffer=Depends(BufferParams),
post_process=Depends(self.process_dependency),
rescale=Depends(self.rescale_dependency),
color_formula=Depends(ColorFormulaParams),
color_formula=Depends(self.color_formula_dependency),
colormap=Depends(self.colormap_dependency),
render_params=Depends(self.render_dependency),
backend_params=Depends(self.backend_dependency),
Expand Down

0 comments on commit 50934c9

Please sign in to comment.