-
Notifications
You must be signed in to change notification settings - Fork 20
[Experiment] sketch algorithm support #51
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""rio_viz.algorithm.""" | ||
|
||
from typing import Dict, Type | ||
|
||
from rio_viz.algorithm.base import AlgorithmMetadata, BaseAlgorithm # noqa | ||
from rio_viz.algorithm.dem import Contours, HillShade | ||
from rio_viz.algorithm.index import NormalizedIndex | ||
|
||
AVAILABLE_ALGORITHM: Dict[str, Type[BaseAlgorithm]] = { | ||
"hillshade": HillShade, | ||
"contours": Contours, | ||
"normalizedIndex": NormalizedIndex, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Algorithm base class.""" | ||
|
||
import abc | ||
from typing import Dict, Optional, Sequence | ||
|
||
from pydantic import BaseModel | ||
from rio_tiler.models import ImageData | ||
|
||
|
||
class BaseAlgorithm(BaseModel, metaclass=abc.ABCMeta): | ||
"""Algorithm baseclass.""" | ||
|
||
input_nbands: int | ||
|
||
output_nbands: int | ||
output_dtype: str | ||
output_min: Optional[Sequence] | ||
output_max: Optional[Sequence] | ||
|
||
@abc.abstractmethod | ||
def apply(self, img: ImageData) -> ImageData: | ||
"""Apply""" | ||
... | ||
|
||
class Config: | ||
"""Config for model.""" | ||
|
||
extra = "allow" | ||
|
||
|
||
class AlgorithmMetadata(BaseModel): | ||
"""Algorithm metadata.""" | ||
|
||
name: str | ||
inputs: Dict | ||
outputs: Dict | ||
params: Dict |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"""rio_viz.algorithm DEM.""" | ||
|
||
import numpy | ||
from rio_tiler.colormap import apply_cmap, cmap | ||
from rio_tiler.models import ImageData | ||
from rio_tiler.utils import linear_rescale | ||
|
||
from rio_viz.algorithm.base import BaseAlgorithm | ||
|
||
|
||
class HillShade(BaseAlgorithm): | ||
"""Hillshade.""" | ||
|
||
azimuth: int = 90 | ||
angle_altitude: float = 90 | ||
|
||
input_nbands: int = 1 | ||
|
||
output_nbands: int = 1 | ||
output_dtype: str = "uint8" | ||
|
||
def apply(self, img: ImageData) -> ImageData: | ||
"""Create hillshade from DEM dataset.""" | ||
data = img.data[0] | ||
mask = img.mask | ||
|
||
x, y = numpy.gradient(data) | ||
|
||
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 | ||
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) | ||
hillshade_array = 255 * (shaded + 1) / 2 | ||
|
||
# ImageData only accept image in form of (count, height, width) | ||
arr = numpy.expand_dims(hillshade_array, axis=0).astype(dtype=numpy.uint8) | ||
|
||
return ImageData( | ||
arr, | ||
mask, | ||
assets=img.assets, | ||
crs=img.crs, | ||
bounds=img.bounds, | ||
) | ||
|
||
|
||
class Contours(BaseAlgorithm): | ||
"""Contours. | ||
|
||
Original idea from https://custom-scripts.sentinel-hub.com/dem/contour-lines/ | ||
""" | ||
|
||
increment: int = 35 | ||
thickness: int = 1 | ||
minz: int = -12000 | ||
maxz: int = 8000 | ||
|
||
input_nbands: int = 1 | ||
|
||
output_nbands: int = 3 | ||
output_dtype: str = "uint8" | ||
|
||
def apply(self, img: ImageData) -> ImageData: | ||
"""Add contours.""" | ||
data = img.data | ||
|
||
# Apply rescaling for minz,maxz to 1->255 and apply Terrain colormap | ||
arr = linear_rescale(data, (self.minz, self.maxz), (1, 255)).astype("uint8") | ||
arr, _ = apply_cmap(arr, cmap.get("terrain")) | ||
|
||
# set black (0) for contour lines | ||
arr = numpy.where(data % self.increment < self.thickness, 0, arr) | ||
|
||
return ImageData( | ||
arr, | ||
img.mask, | ||
assets=img.assets, | ||
crs=img.crs, | ||
bounds=img.bounds, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""rio_viz.algorithm Normalized Index.""" | ||
|
||
from typing import Sequence | ||
|
||
import numpy | ||
from rio_tiler.models import ImageData | ||
|
||
from rio_viz.algorithm.base import BaseAlgorithm | ||
|
||
|
||
class NormalizedIndex(BaseAlgorithm): | ||
"""Normalized Difference Index.""" | ||
|
||
input_nbands: int = 2 | ||
|
||
output_nbands: int = 1 | ||
output_dtype: str = "float32" | ||
output_min: Sequence[float] = [-1.0] | ||
output_max: Sequence[float] = [1.0] | ||
|
||
def apply(self, img: ImageData) -> ImageData: | ||
"""Normalized difference.""" | ||
b1 = img.data[0] | ||
b2 = img.data[1] | ||
|
||
arr = numpy.where(img.mask, (b2 - b1) / (b2 + b1), 0) | ||
|
||
# ImageData only accept image in form of (count, height, width) | ||
arr = numpy.expand_dims(arr, axis=0).astype(self.output_dtype) | ||
|
||
return ImageData( | ||
arr, | ||
img.mask, | ||
assets=img.assets, | ||
crs=img.crs, | ||
bounds=img.bounds, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are metadata about the input/outputs of the algorithm