Skip to content

Commit

Permalink
get DIST alerts filter dataset/versions
Browse files Browse the repository at this point in the history
  • Loading branch information
solomon-negusse committed Nov 7, 2024
1 parent 454d840 commit 693e0c2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
18 changes: 11 additions & 7 deletions app/routes/titiler/umd_glad_dist_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from titiler.core.utils import render_image

from ...models.enumerators.titiler import AlertConfidence, RenderType
from ...settings.globals import GLOBALS
from .. import DATE_REGEX, raster_xyz
from .algorithms.dist_alerts import DISTAlerts
from .readers import AlertsReader
Expand All @@ -18,13 +19,13 @@
router = APIRouter()

# TODO: update to the actual dataset when ready
dataset = "dan_test"
DATASET = "dan_test"

today = date.today()


@router.get(
f"/{dataset}/{{version}}/dynamic/{{z}}/{{x}}/{{y}}.png",
f"/{DATASET}/{{version}}/dynamic/{{z}}/{{x}}/{{y}}.png",
response_class=Response,
tags=["Raster Tiles"],
response_description="PNG Raster Tile",
Expand Down Expand Up @@ -70,7 +71,7 @@ async def glad_dist_alerts_raster_tile(

tile_x, tile_y, zoom = xyz
bands = ["default", "intensity"]
folder: str = f"s3://{DATA_LAKE_BUCKET}/{dataset}/{version}/raster/epsg-4326/cog"
folder: str = f"s3://{DATA_LAKE_BUCKET}/{DATASET}/{version}/raster/epsg-4326/cog"
with AlertsReader(input=folder) as reader:
# NOTE: the bands in the output `image_data` array will be in the order of
# the input `bands` list
Expand All @@ -86,22 +87,25 @@ async def glad_dist_alerts_raster_tile(
tree_cover_loss_mask=tree_cover_loss_cutoff,
)

filter_datasets = GLOBALS.dist_alerts_forest_filters
if tree_cover_density:
dataset = filter_datasets["tree_cover_density"]
with COGReader(
f"s3://{DATA_LAKE_BUCKET}/umd_tree_cover_density_2010/v1.6/raster/epsg-4326/cog/default.tif"
f"s3://{DATA_LAKE_BUCKET}/{dataset['dataset']}/{dataset['version']}/raster/epsg-4326/cog/default.tif"
) as reader:
dist_alert.tree_cover_density_data = reader.tile(tile_x, tile_y, zoom)

if tree_cover_height:
dataset = filter_datasets["tree_cover_height"]
with COGReader(
f"s3://{DATA_LAKE_BUCKET}/umd_tree_cover_height_2020/v2022/raster/epsg-4326/cog/default.tif"
f"s3://{DATA_LAKE_BUCKET}/{dataset['dataset']}/{dataset['version']}/raster/epsg-4326/cog/default.tif"
) as reader:
dist_alert.tree_cover_height_data = reader.tile(tile_x, tile_y, zoom)

if tree_cover_loss_cutoff:
# TODO: update to v1.11 in production
dataset = filter_datasets["tree_cover_loss"]
with COGReader(
f"s3://{DATA_LAKE_BUCKET}/umd_tree_cover_loss/v1.10.1/raster/epsg-4326/cog/default.tif"
f"s3://{DATA_LAKE_BUCKET}/{dataset['dataset']}/{dataset['version']}/raster/epsg-4326/cog/default.tif"
) as reader:
dist_alert.tree_cover_loss_data = reader.tile(tile_x, tile_y, zoom)

Expand Down
26 changes: 24 additions & 2 deletions app/settings/globals.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import json
from json import JSONDecodeError
from typing import Optional
from typing import Dict, Optional

from fastapi.logger import logger
from pydantic import Field, field_validator
from pydantic_settings import SettingsConfigDict, BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict
from starlette.datastructures import Secret

from ..models.pydantic.database import DatabaseURL

# NOTE: For non-local deployment, need to update dataset/version here until the terraform
# JSON decode issue preventing setting this env variable is resolved.
dist_alerts_forest_filters = {
"tree_cover_loss": {"dataset": "umd_tree_cover_loss", "version": "v1.10.1"},
"tree_cover_height": {"dataset": "umd_tree_cover_height_2020", "version": "v2022"},
"tree_cover_density": {"dataset": "umd_tree_cover_density_2010", "version": "v1.6"},
}


class Globals(BaseSettings):
env: str = Field("dev", description="Environment name.")
Expand Down Expand Up @@ -68,6 +76,11 @@ class Globals(BaseSettings):
)
api_key_name: str = Field("x-api-key", description="Header key name for API key.")

dist_alerts_forest_filters: Dict = Field(
dist_alerts_forest_filters,
description="Datasets that are used as forest filters for DIST alerts",
)

@field_validator("token", mode="before")
def get_token(cls, v: Optional[str]) -> Optional[str]:
if v:
Expand Down Expand Up @@ -122,6 +135,15 @@ def set_lambda_host(cls, v, values, **kwargs) -> str:
v = f"https://lambda.{aws_region}.amazonaws.com"
return v

@field_validator("dist_alerts_forest_filters", mode="before")
def parse_dist_alerts_forest_filters(cls, v: str | Dict) -> Dict:
if isinstance(v, dict):
return v
try:
return json.loads(v)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON for dist_alerts_forest_filters: {e}")

model_config = SettingsConfigDict(case_sensitive=False, validate_assignment=True)


Expand Down
1 change: 1 addition & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
- ENV=dev
- PLANET_API_KEY
- AWS_REQUEST_PAYER=requester
- 'DIST_ALERTS_FOREST_FILTERS={"tree_cover_loss": {"dataset": "umd_tree_cover_loss", "version": "v1.10.1"}, "tree_cover_height": {"dataset": "umd_tree_cover_height_2020", "version": "v2022"}, "tree_cover_density": {"dataset": "umd_tree_cover_density_2010", "version": "v1.6"}}'
ports:
- 8088:80
entrypoint: wait_for_postgres.sh /start-reload.sh
Expand Down

0 comments on commit 693e0c2

Please sign in to comment.