Skip to content

Commit f35e575

Browse files
fix terrainRGB algorithm (#616)
1 parent 7a18d64 commit f35e575

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGES.md

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

3+
## 0.11.5 (2023-03-22)
4+
5+
* fix `TerrainRGB` (change interval from `1.0` to `0.1`)
6+
37
## 0.11.4 (2023-03-20)
48

59
* set FastAPI version lower than 0.95 (https://github.com/tiangolo/fastapi/discussions/9278)

src/titiler/core/tests/test_algorithms.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import numpy
66
from fastapi import Depends, FastAPI
7+
from rasterio.io import MemoryFile
78
from rio_tiler.models import ImageData
9+
from starlette.responses import Response
810
from starlette.testclient import TestClient
911

1012
from titiler.core.algorithm import BaseAlgorithm
@@ -63,3 +65,46 @@ def main(algorithm=Depends(algorithms.dependency)):
6365
params={"algorithm": "multiply", "algorithm_params": json.dumps({"factor": 3})},
6466
)
6567
assert response.json() == arr.max().tolist() * 3
68+
69+
70+
def test_terrain_algo():
71+
"""test terrain algorithm deps."""
72+
# Add the `Multiply` algorithm to the default ones
73+
app = FastAPI()
74+
75+
arr = numpy.random.randint(0, 3000, (1, 256, 256))
76+
77+
@app.get("/", response_class=Response)
78+
def main(algorithm=Depends(default_algorithms.dependency)):
79+
"""endpoint."""
80+
img = ImageData(arr)
81+
if algorithm:
82+
img = algorithm(img)
83+
84+
return Response(img.render(img_format="PNG"), media_type="image/png")
85+
86+
client = TestClient(app)
87+
88+
# MAPBOX Terrain RGB
89+
response = client.get("/", params={"algorithm": "terrainrgb"})
90+
assert response.status_code == 200
91+
92+
with MemoryFile(response.content) as mem:
93+
with mem.open() as dst:
94+
data = dst.read().astype(numpy.float64)
95+
96+
# https://docs.mapbox.com/data/tilesets/guides/access-elevation-data/
97+
elevation = -10000 + (((data[0] * 256 * 256) + (data[1] * 256) + data[2]) * 0.1)
98+
numpy.testing.assert_array_equal(elevation, arr[0])
99+
100+
# TILEZEN Terrarium
101+
response = client.get("/", params={"algorithm": "terrarium"})
102+
assert response.status_code == 200
103+
104+
with MemoryFile(response.content) as mem:
105+
with mem.open() as dst:
106+
data = dst.read().astype(numpy.float64)
107+
108+
# https://github.com/tilezen/joerd/blob/master/docs/formats.md#terrarium
109+
elevation = (data[0] * 256 + data[1] + data[2] / 256) - 32768
110+
numpy.testing.assert_array_equal(elevation, arr[0])

src/titiler/core/titiler/core/algorithm/dem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class TerrainRGB(BaseAlgorithm):
128128
"""Encode DEM into RGB (Mapbox Terrain RGB)."""
129129

130130
# parameters
131-
interval: float = 1.0
131+
interval: float = 0.1
132132
baseval: float = -10000.0
133133

134134
# metadata

0 commit comments

Comments
 (0)