-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -5,4 +5,5 @@ postgis==1.0.4 | |
uvicorn==0.29.0 | ||
boto3 | ||
pyyaml | ||
gunicorn | ||
gunicorn | ||
scikit-image |
This file contains 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from titiler.core.algorithm import Algorithms, algorithms as default_algorithms | ||
from .rca import RapidChangeAssessment | ||
from .flood_detection import DetectFlood | ||
|
||
algorithms: Algorithms = default_algorithms.register( | ||
{ | ||
"rca": RapidChangeAssessment | ||
"rca": RapidChangeAssessment, | ||
"flooding": DetectFlood, | ||
} | ||
) |
This file contains 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,65 @@ | ||
from typing import List, Sequence | ||
|
||
import numpy as np | ||
from titiler.core.algorithm import BaseAlgorithm | ||
from rio_tiler.models import ImageData | ||
from skimage.filters import threshold_otsu | ||
|
||
|
||
## Credit: Sashka Warner (https://github.com/sashkaw) | ||
|
||
class DetectFlood(BaseAlgorithm): | ||
title: str = "Flood detection " | ||
description: str = "Algorithm to calculate Modified Normalized Difference Water Index (MNDWI), and apply Otsu thresholding algorithm to identify surface water" | ||
|
||
""" | ||
Desc: Algorithm to calculate Modified Normalized Difference Water Index (MNDWI), | ||
and apply Otsu thresholding algorithm to identify surface water. | ||
""" | ||
|
||
input_bands: List = [ | ||
{'title': 'Green band', 'description': 'The green band with the wavelength between 0.53µm - 0.59µm', | ||
'required': True, | ||
'keywords': ['Green band']}, | ||
{'title': 'Short wave infrared band', 'description': 'The SWIR band with wavelength between 0.9μ – 1.7μm', | ||
'required': True, | ||
'keywords': ['Shortwave infrared band']}, | ||
] | ||
input_description: str = "The bands that will be used to make this calculation" | ||
|
||
# Metadata | ||
input_nbands: int = 2 | ||
output_nbands: int = 1 | ||
output_min: Sequence[int] = [-1] | ||
output_max: Sequence[int] = [1] | ||
output_colormap_name: str = 'viridis' | ||
|
||
def __call__(self, img: ImageData, *args, **kwargs): | ||
# Extract bands of interest | ||
green_band = img.data[0].astype("float32") | ||
swir_band = img.data[1].astype("float32") | ||
|
||
# Calculate Modified Normalized Difference Water Index (MNDWI) | ||
numerator = (green_band - swir_band) | ||
denominator = (green_band + swir_band) | ||
# Use np.divide to avoid divide by zero errors | ||
mndwi_arr = np.divide(numerator, denominator, np.zeros_like(numerator), where=denominator != 0) | ||
|
||
# Apply Otsu thresholding method | ||
otsu_threshold = threshold_otsu(mndwi_arr) | ||
|
||
# Use Otsu threshold to classify the computed MNDWI | ||
classified_arr = mndwi_arr >= otsu_threshold | ||
|
||
# Reshape data -> ImageData only accepts image in form of (count, height, width) | ||
# classified_arr = np.around(classified_arr).astype(int) | ||
# classified_arr = np.expand_dims(classified_arr, axis=0).astype(self.output_dtype) | ||
classified_arr = np.expand_dims(classified_arr, axis=0).astype(int) | ||
|
||
return ImageData( | ||
classified_arr, | ||
img.mask, | ||
assets=img.assets, | ||
crs=img.crs, | ||
bounds=img.bounds, | ||
) |
This file contains 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