Skip to content

Commit

Permalink
serving w/ algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
GondekNP committed Jan 6, 2024
1 parent bc5fcfa commit 8db51ba
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .deployment/tofu/modules/burn_backend/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "burn_backend_server_endpoint" {
description = "The endpoint of the Cloud Run burn-backend service"
value = google_cloud_run_v2_service.tf-rest-burn-severity.uri
}
19 changes: 19 additions & 0 deletions .deployment/tofu/modules/sftp/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ resource "aws_s3_bucket" "burn-severity-backend" {
bucket = "burn-severity-backend" # replace with your bucket name
}

data "aws_iam_policy_document" "burn-severity-backend-policy" {
statement {
sid = "PublicReadGetObject"
effect = "Allow"
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.burn-severity-backend.arn}/*"]

principals {
type = "*"
identifiers = ["*"]
}
}
}

resource "aws_s3_bucket_policy" "burn-severity-backend-policy" {
bucket = aws_s3_bucket.burn-severity-backend.id
policy = data.aws_iam_policy_document.burn-severity-backend-policy.json
}

resource "aws_s3_bucket_ownership_controls" "burn-severity-backend" {
bucket = aws_s3_bucket.burn-severity-backend.id
rule {
Expand Down
5 changes: 5 additions & 0 deletions .deployment/tofu/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ output "sftp_server_endpoint" {
output "sftp_admin_username" {
description = "The username of the SFTP admin user"
value = module.sftp.sftp_admin_username
}

output "gcp_cloud_run_endpoint" {
description = "The endpoint of the Cloud Run burn-backend service"
value = module.burn_backend.burn_backend_server_endpoint
}
38 changes: 32 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from fastapi import FastAPI, Depends, HTTPException
from fastapi.responses import HTMLResponse
import os
import json
from pathlib import Path
import uvicorn
from pydantic import BaseModel
Expand All @@ -11,6 +10,9 @@
import requests
from fastapi import HTTPException

from fastapi import FastAPI, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from titiler.core.factory import TilerFactory
from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers
Expand All @@ -27,6 +29,8 @@
app.include_router(cog.router, prefix='/cog', tags=["Cloud Optimized GeoTIFF"])
add_exception_handlers(app, DEFAULT_STATUS_CODES)

templates = Jinja2Templates(directory="src/")

logging_client = logging.Client(project='dse-nps')
log_name = "burn-backend"
logger = logging_client.logger(log_name)
Expand Down Expand Up @@ -68,6 +72,16 @@ def get_sftp_client():

return SFTPClient(SFTP_SERVER_ENDPOINT, SFTP_ADMIN_USERNAME, SSH_SECRET)

def get_manifest(sfpt_client: SFTPClient = Depends(get_sftp_client)):
try:
sfpt_client.connect()
manifest = sfpt_client.get_manifest()
sfpt_client.disconnect()
return manifest
except Exception as e:
logger.log_text(f"Error: {e}")
return f"Error: {e}", 400

@app.get("/available-cogs")
def available_cogs(sftp_client: SFTPClient = Depends(get_sftp_client)):
try:
Expand Down Expand Up @@ -130,7 +144,19 @@ def analyze_burn(body: AnaylzeBurnPOSTBody, sftp_client: SFTPClient = Depends(ge
return f"Error: {e}", 400

@app.get("/map/{fire_event_name}", response_class=HTMLResponse)
def serve_map(fire_event_name: str):
html_content = Path("src/index.html").read_text()
logger.log_text(f"Serving map for {fire_event_name}")
return html_content
def serve_map(request: Request, fire_event_name: str, manifest: dict = Depends(get_manifest)):
# tileserver_endpoint = 'https://tf-rest-burn-severity-ohi6r6qs2a-uc.a.run.app'
tileserver_endpoint = 'http://localhost:5050'
cog_url = f"https://burn-severity-backend.s3.us-east-2.amazonaws.com/public/{fire_event_name}/rbr.tif"
cog_tileserver_url_prefix = tileserver_endpoint + f"/cog/tiles/WebMercatorQuad/{{z}}/{{x}}/{{y}}.png?url={cog_url}&nodata=0&algorithm=classify&algorithm_params="
# cog_tileserver_url_prefix = tileserver_endpoint + f"/cog/tiles/WebMercatorQuad/{{z}}/{{x}}/{{y}}.png?url={cog_url}"

fire_metadata = manifest[fire_event_name]
fire_metadata_json = json.dumps(fire_metadata)

return templates.TemplateResponse("index.html", {
"request": request,
"fire_event_name": fire_event_name,
"fire_metadata_json": fire_metadata_json,
"cog_tileserver_url_prefix": cog_tileserver_url_prefix
})
41 changes: 28 additions & 13 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Leaflet Map</title>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Expand All @@ -25,19 +26,33 @@
<body>
<div id="mapid"></div>

<script>
var map = L.map('mapid').setView([51.505, -0.09], 13);

// Add base map
L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);

// Add COG layer
L.tileLayer('s-453d99da7f8849d6a.server.transfer.us-east-2.amazonaws.com/cog/tiles/WebMercatorQuad/{z}/{x}/{y}.png?url=https://burn-severity-backend.s3.us-east-2.amazonaws.com/public/geology/rbr.tif&nodata=0&algorithm=classify&algorithm_params={%22thresholds%22:{%220.045%22:100,%220.222%22:150,%220.8%22:200}}', {
maxZoom: 20,
}).addTo(map);
</script>
<script>
var map = L.map('mapid').setView([51.505, -0.09], 13);

console.log("tileserver_prefix - {{ cog_tileserver_url_prefix }}")
console.log("fire_event_name - {{ fire_event_name }}")
console.log("fire metadata")
var fireMetadata = JSON.parse({{fire_metadata_json | tojson | safe}});
console.log(fireMetadata);

var fire_bounds = [[fireMetadata.bounds[0], fireMetadata.bounds[1]], [fireMetadata.bounds[2], fireMetadata.bounds[3]]]
console.log("fire bounds - ", fire_bounds)

// Add base map
L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
}).addTo(map);

// Add COG layer
// {"thresholds":{"0.045":100,"0.222":150,"0.8":200}}
L.tileLayer('{{ cog_tileserver_url_prefix | safe }}{"thresholds":{"0.045":100,"0.222":150,"0.8":200}}', {
maxZoom: 20,
}).addTo(map);

// Center map on fire, based on `bounds` metadata
map.fitBounds(fire_bounds);

</script>

</body>
</html>
14 changes: 10 additions & 4 deletions src/util/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ def upload_cogs(self, metrics_stack, fire_event_name, prefire_date_range, postfi

def update_manifest(self, fire_event_name, bounds, prefire_date_range, postfire_date_range):
with tempfile.TemporaryDirectory() as tmpdir:
self.download('manifest.json', '/tmp_manifest.json')
self.logger.log_text(f"Downloaded manifest.json")
manifest = json.load(open('/tmp_manifest.json', 'r'))

manifest = self.get_manifest()

if fire_event_name in manifest:
self.logger.log_text(f"Fire event {fire_event_name} already exists in manifest. Overwriting.")
Expand Down Expand Up @@ -182,4 +181,11 @@ def upload_fire_event(self, metrics_stack, fire_event_name, prefire_date_range,
bounds=bounds,
prefire_date_range=prefire_date_range,
postfire_date_range=postfire_date_range
)
)

def get_manifest(self):
with tempfile.TemporaryDirectory() as tmpdir:
self.download('manifest.json', tmpdir + 'tmp_manifest.json')
self.logger.log_text(f"Got manifest.json")
manifest = json.load(open(tmpdir + 'tmp_manifest.json', 'r'))
return manifest

0 comments on commit 8db51ba

Please sign in to comment.