Skip to content

Commit 1490022

Browse files
fix hillsahde algorithm (#985)
1 parent 725f857 commit 1490022

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
>> {'value': 1}
3535
```
3636

37+
* fix Hillshade algorithm (bad `azimuth` angle)
38+
39+
* set default `azimuth` and `altitude` angles to 45º for the Hillshade algorithm **breaking change**
40+
3741
* Use `.as_dict()` method when passing option to rio-tiler Reader's methods to avoid parameter conflicts when using custom Readers.
3842

3943
## 0.18.6 (2024-08-27)

src/titiler/core/tests/test_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def _endpoint(algorithm=Depends(PostProcessParams)):
480480
assert response.status_code == 422
481481

482482
response = client.get("/?algorithm=hillshade")
483-
assert response.json()["azimuth"] == 90
483+
assert response.json()["azimuth"] == 45
484484
assert response.json()["buffer"] == 3
485485
assert response.json()["input_nbands"] == 1
486486

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class HillShade(BaseAlgorithm):
1717
description: str = "Create hillshade from DEM dataset."
1818

1919
# parameters
20-
azimuth: int = Field(90, ge=0, le=360)
21-
angle_altitude: float = Field(90.0, ge=-90.0, le=90.0)
20+
azimuth: int = Field(45, ge=0, le=360)
21+
angle_altitude: float = Field(45.0, ge=-90.0, le=90.0)
2222
buffer: int = Field(3, ge=0, le=99)
2323

2424
# metadata
@@ -31,12 +31,14 @@ def __call__(self, img: ImageData) -> ImageData:
3131
x, y = numpy.gradient(img.array[0])
3232
slope = numpy.pi / 2.0 - numpy.arctan(numpy.sqrt(x * x + y * y))
3333
aspect = numpy.arctan2(-x, y)
34-
azimuthrad = self.azimuth * numpy.pi / 180.0
34+
azimuth = 360.0 - self.azimuth
35+
azimuthrad = azimuth * numpy.pi / 180.0
3536
altituderad = self.angle_altitude * numpy.pi / 180.0
3637
shaded = numpy.sin(altituderad) * numpy.sin(slope) + numpy.cos(
3738
altituderad
3839
) * numpy.cos(slope) * numpy.cos(azimuthrad - aspect)
3940
data = 255 * (shaded + 1) / 2
41+
data[data < 0] = 0 # set hillshade values to min of 0.
4042

4143
bounds = img.bounds
4244
if self.buffer:

0 commit comments

Comments
 (0)