|
4 | 4 |
|
5 | 5 | import numpy |
6 | 6 | from fastapi import Depends, FastAPI |
| 7 | +from rasterio.io import MemoryFile |
7 | 8 | from rio_tiler.models import ImageData |
| 9 | +from starlette.responses import Response |
8 | 10 | from starlette.testclient import TestClient |
9 | 11 |
|
10 | 12 | from titiler.core.algorithm import BaseAlgorithm |
@@ -63,3 +65,46 @@ def main(algorithm=Depends(algorithms.dependency)): |
63 | 65 | params={"algorithm": "multiply", "algorithm_params": json.dumps({"factor": 3})}, |
64 | 66 | ) |
65 | 67 | 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]) |
0 commit comments