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

fix hillsahde algorithm #985

Merged
merged 1 commit into from
Sep 19, 2024
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
>> {'value': 1}
```

* fix Hillshade algorithm (bad `azimuth` angle)

* set default `azimuth` and `altitude` angles to 45º for the Hillshade algorithm **breaking change**

* Use `.as_dict()` method when passing option to rio-tiler Reader's methods to avoid parameter conflicts when using custom Readers.

## 0.18.6 (2024-08-27)
Expand Down
2 changes: 1 addition & 1 deletion src/titiler/core/tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def _endpoint(algorithm=Depends(PostProcessParams)):
assert response.status_code == 422

response = client.get("/?algorithm=hillshade")
assert response.json()["azimuth"] == 90
assert response.json()["azimuth"] == 45
assert response.json()["buffer"] == 3
assert response.json()["input_nbands"] == 1

Expand Down
8 changes: 5 additions & 3 deletions src/titiler/core/titiler/core/algorithm/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class HillShade(BaseAlgorithm):
description: str = "Create hillshade from DEM dataset."

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

# metadata
Expand All @@ -31,12 +31,14 @@ def __call__(self, img: ImageData) -> ImageData:
x, y = numpy.gradient(img.array[0])
slope = numpy.pi / 2.0 - numpy.arctan(numpy.sqrt(x * x + y * y))
aspect = numpy.arctan2(-x, y)
azimuthrad = self.azimuth * numpy.pi / 180.0
azimuth = 360.0 - self.azimuth
azimuthrad = azimuth * numpy.pi / 180.0
altituderad = self.angle_altitude * numpy.pi / 180.0
shaded = numpy.sin(altituderad) * numpy.sin(slope) + numpy.cos(
altituderad
) * numpy.cos(slope) * numpy.cos(azimuthrad - aspect)
data = 255 * (shaded + 1) / 2
data[data < 0] = 0 # set hillshade values to min of 0.

bounds = img.bounds
if self.buffer:
Expand Down
Loading