Skip to content

Commit

Permalink
Merge branch 'staging' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jterry64 authored Dec 10, 2024
2 parents 6021ddc + bc6a6c7 commit 98774c3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
67 changes: 44 additions & 23 deletions app/routes/titiler/umd_glad_dist_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

router = APIRouter()

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

today = date.today()

Expand All @@ -30,8 +29,16 @@
tags=["Raster Tiles"],
response_description="PNG Raster Tile",
)
@router.get(
"/{dataset}/{version}/titiler/{z}/{x}/{y}.png", # for testing datasets - hidden from docs.
response_class=Response,
tags=["Raster Tiles"],
response_description="PNG Raster Tile",
include_in_schema=False,
)
async def glad_dist_alerts_raster_tile(
*,
dataset: str = DATASET,
version,
xyz: Tuple[int, int, int] = Depends(raster_xyz),
start_date: Optional[str] = Query(
Expand All @@ -45,33 +52,47 @@ async def glad_dist_alerts_raster_tile(
description="Only show alerts until given date.",
),
render_type: RenderType = Query(
RenderType.encoded, description="Render true color or encoded tiles"
RenderType.encoded,
description=(
"Render true color or encoded tiles. Encoded tiles have the alert "
"date, confidence and intensity (value for use in alpha/transparency channel to fade out isolated alert pixels at low zoom levels) "
"packed in the image RGB channels for front-end interactive use, "
"such as date filtering with supported technologies. "
"Decoding instructions: Alert Date is calculated as `red * 255 + green`, "
"representing days since 2020-12-31. Confidence is calculated as `floor(blue / 100)`, "
"with values of `2` (high) or `1` (low). Intensity is calculated as `mod(blue, 100)`, "
"with a maximum value of `55`. "
"For example, a pixel RGB value of `(3, 26, 255)` would decode to: "
"**alert date**: `3 * 255 + 26 = 791` (or 2023-03-02), "
"**confidence**: `floor(255 / 100) = 2` (high), and "
"**intensity**: `mod(255, 100) = 55`"
),
),
alert_confidence: Optional[AlertConfidence] = Query(
AlertConfidence.low,
description="Show alerts that are at least of this confidence level",
),
tree_cover_density: Optional[int] = Query(
tree_cover_density_threshold: Optional[int] = Query(
None,
ge=0,
le=100,
description="Alerts in pixels with tree cover density (in percent) below this threshold won't be displayed. `umd_tree_cover_density_2010` is used for this masking.",
description="Show alerts in pixels with tree cover density (in percent) greater than or equal to this threshold. `umd_tree_cover_density_2010` is used for this masking.",
),
tree_cover_height: Optional[int] = Query(
tree_cover_height_threshold: Optional[int] = Query(
None,
description="Alerts in pixels with tree cover height (in meters) below this threshold won't be displayed. `umd_tree_cover_height_2020` dataset in the API is used for this masking.",
description="Show alerts in pixels with tree cover height (in meters) greater than or equal to this threshold. `umd_tree_cover_height_2020` dataset in the API is used for this masking.",
),
tree_cover_loss_cutoff: bool = Query(
False,
tree_cover_loss_threshold: Optional[int] = Query(
None,
ge=2021,
description="""This filter is to be used in conjunction with `tree_cover_density` and `tree_cover_height` filters to detect only alerts in forests, by masking out pixels that have had tree cover loss prior to the alert.""",
description="""This filter is to be used in conjunction with `tree_cover_density_threshold` and `tree_cover_height_threshold` filters to detect only alerts in forests, by masking out pixels that have had tree cover loss prior to the alert.""",
),
) -> Response:
"""UMD GLAD DIST alerts raster tiles."""

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 @@ -82,30 +103,30 @@ async def glad_dist_alerts_raster_tile(
end_date=end_date,
render_type=render_type,
alert_confidence=alert_confidence,
tree_cover_density_mask=tree_cover_density,
tree_cover_height_mask=tree_cover_height,
tree_cover_loss_mask=tree_cover_loss_cutoff,
tree_cover_density_mask=tree_cover_density_threshold,
tree_cover_height_mask=tree_cover_height_threshold,
tree_cover_loss_mask=tree_cover_loss_threshold,
)

filter_datasets = GLOBALS.dist_alerts_forest_filters
if tree_cover_density:
dataset = filter_datasets["tree_cover_density"]
if tree_cover_density_threshold:
filter_dataset = filter_datasets["tree_cover_density"]
with COGReader(
f"s3://{DATA_LAKE_BUCKET}/{dataset['dataset']}/{dataset['version']}/raster/epsg-4326/cog/default.tif"
f"s3://{DATA_LAKE_BUCKET}/{filter_dataset['dataset']}/{filter_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"]
if tree_cover_height_threshold:
filter_dataset = filter_datasets["tree_cover_height"]
with COGReader(
f"s3://{DATA_LAKE_BUCKET}/{dataset['dataset']}/{dataset['version']}/raster/epsg-4326/cog/default.tif"
f"s3://{DATA_LAKE_BUCKET}/{filter_dataset['dataset']}/{filter_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:
dataset = filter_datasets["tree_cover_loss"]
if tree_cover_loss_threshold:
filter_dataset = filter_datasets["tree_cover_loss"]
with COGReader(
f"s3://{DATA_LAKE_BUCKET}/{dataset['dataset']}/{dataset['version']}/raster/epsg-4326/cog/default.tif"
f"s3://{DATA_LAKE_BUCKET}/{filter_dataset['dataset']}/{filter_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
28 changes: 27 additions & 1 deletion terraform/modules/content_delivery_network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ resource "aws_cloudfront_distribution" "tiles" {
}
}

# send all Titiler requests to tile cache app
# send all generic Titiler requests to tile cache app
ordered_cache_behavior {
allowed_methods = local.methods
cached_methods = local.methods
Expand Down Expand Up @@ -455,6 +455,32 @@ resource "aws_cloudfront_distribution" "tiles" {
}
}

# pass requests for DIST alerts test datasets to tile cache app
ordered_cache_behavior {
allowed_methods = local.methods
cached_methods = local.methods
target_origin_id = "dynamic"
compress = true
path_pattern = "*/titiler/*"
default_ttl = 86400
max_ttl = 86400
min_ttl = 0
smooth_streaming = false
trusted_signers = []
viewer_protocol_policy = "redirect-to-https"

forwarded_values {
headers = local.headers
query_string = true
query_string_cache_keys = []

cookies {
forward = "none"
whitelisted_names = []
}
}
}

# Default static vector tiles are stored on S3
# They won't change and can stay in cache for a year
# We will set response headers for selected tile caches in S3 if required
Expand Down

0 comments on commit 98774c3

Please sign in to comment.