Skip to content

Commit

Permalink
add date range filter to integrated_alerts titiler algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
solomon-negusse committed Aug 30, 2024
1 parent 73735b6 commit b95e22f
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions app/routes/titiler/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# from datetime import date
from datetime import date

import numpy as np
from dateutil.relativedelta import relativedelta
from pydantic import Field
from rio_tiler.models import ImageData
from titiler.core.algorithm import BaseAlgorithm
Expand All @@ -11,11 +14,21 @@ class IntegratedAlerts(BaseAlgorithm):
title: str = "Integrated Deforestation Alerts"
description: str = "Decode and vizualize alerts"

# START_DATE: np.datetime64 = np.datetime64("2014-12-31").astype(int)
START_DATE: str = "2014-12-31" # start of record

# Parameters
start_date: int = Field(None, description="start date of alert")
end_date: int = Field(None, description="end date of alert")
default_start_date: str = (date.today() - relativedelta(days=90)).strftime(
"%Y-%m-%d"
)
start_date: str = Field(
default_start_date,
description="start date of alert in YYYY-MM-DD format.",
)

default_end_date: str = date.today().strftime("%Y-%m-%d")
end_date: str = Field(
default_end_date, description="end date of alert in YYYY-MM-DD format."
)

# metadata
input_nbands: int = 2
Expand All @@ -26,24 +39,31 @@ def __call__(self, img: ImageData) -> ImageData:
"""Encode Integrated alerts to RGBA."""
mask = img.array.mask[0].astype(int)
data = img.data[0]
alert_date = data % 10000 # in days since 2014-12-31

# Will add this in next iteration
# if self.start_date:
# start_mask = data % 10000 >= (
# np.datetime64(self.start_date).astype(int) - self.START_DATE
# )
# mask = mask * start_mask
# if self.end_date:
# end_mask = data % 10000 <= (
# np.datetime64(self.end_date).astype(int) - self.START_DATE
# )
# mask = mask * end_mask
intensity = np.where(mask == 0, img.data[1], 0)
if self.start_date:
start_mask = alert_date >= (
np.datetime64(self.start_date).astype(int)
- np.datetime64(self.START_DATE).astype(int)
)
intensity = np.where(start_mask.astype(int) == 1, img.data[1], 0)
if self.end_date:
end_mask = alert_date <= (
np.datetime64(self.end_date).astype(int)
- np.datetime64(self.START_DATE).astype(int)
)
intensity = np.where(start_mask.astype(int) == 1, img.data[1], 0)

r = np.where(data > 0, 228, 0)
g = np.where(data > 0, 102, 0)
b = np.where(data > 0, 153, 0)

intensity = np.where(mask == 0, img.data[1], 0)
intensity = np.where(
((mask == 0) & (start_mask.astype(int) == 1) & (end_mask.astype(int) == 1)),
img.data[1],
0,
)
intensity = np.minimum(255, intensity * 50)
data = np.stack([r, g, b, intensity]).astype(self.output_dtype)
data = np.ma.MaskedArray(data, mask=False)
Expand Down

0 comments on commit b95e22f

Please sign in to comment.