Skip to content

Commit

Permalink
feat: Added metadata to reports (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Oct 12, 2024
1 parent 1d3f612 commit e169b5b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
6 changes: 3 additions & 3 deletions backend/schemas/landsat/landsat_api_by_id_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from backend.schemas import WrsCoordinates
from backend.schemas.landsat.landsat_api_by_id import LandsatAPIById
from backend.schemas.landsat.landsat_item import LandsatItem
from backend.schemas.landsat.landsat_item_advanced import LandsatAdvancedItem
from backend.schemas.landsat.landsat_item_advanced import LandsatAdvancedItem, Metadata
from backend.utils.polygon_utils import polygon_from_nested_list
from backend.utils.reflectance_chart_utils import generate_reflectance_chart_from_tiff
from backend.utils.temperature_chart_utils import generate_temperature_chart_from_item
Expand All @@ -14,7 +13,7 @@ class LandsatAdvancedAPIById(LandsatAPIById):
def __init__(self, **data):
super().__init__(**data)

def landsat_item_builder(self, item: Item) -> LandsatItem:
def landsat_item_builder(self, item: Item) -> LandsatAdvancedItem:
structlog.get_logger().debug(f"Building LandsatItem from {item.id}")
return LandsatAdvancedItem(
platform=item.properties["platform"],
Expand All @@ -31,4 +30,5 @@ def landsat_item_builder(self, item: Item) -> LandsatItem:
polygon=polygon_from_nested_list(item.geometry["coordinates"]),
temperature_chart=generate_temperature_chart_from_item(item),
reflectance_chart=generate_reflectance_chart_from_tiff(bands),
metadata=Metadata.model_validate(item.properties),
)
15 changes: 14 additions & 1 deletion backend/schemas/landsat/landsat_item_advanced.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from pydantic import BaseModel
from pydantic import AliasChoices, BaseModel, Field

from backend.schemas.landsat.landsat_item import LandsatItem

Expand All @@ -15,6 +15,19 @@ class TemperatureChartElement(BaseModel):
distribution: float


class Metadata(BaseModel):
created: str
instruments: list[str]
sun_azimuth: float = Field(validation_alias=AliasChoices("sun_azimuth", "view:sun_azimuth"))
sun_elevation: float = Field(validation_alias=AliasChoices("sun_elevation", "view:sun_elevation"))
cloud_cover: float = Field(validation_alias=AliasChoices("cloud_cover", "landsat:cloud_cover_land"))
collection_number: str = Field(validation_alias=AliasChoices("collection_number", "landsat:collection_number"))
collection_category: str = Field(
validation_alias=AliasChoices("collection_category", "landsat:collection_category")
)


class LandsatAdvancedItem(LandsatItem):
reflectance_chart: List[ReflectanceChartElement]
temperature_chart: List[TemperatureChartElement]
metadata: Metadata
21 changes: 13 additions & 8 deletions backend/tasks/write_report.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import structlog
from sqlalchemy.orm import Session

from backend.models.report import Report
from backend.schemas.landsat.landsat_api_by_id_advanced import LandsatAdvancedAPIById


def write_report_to_db(scene_id: str, db: Session):
landsat_item = LandsatAdvancedAPIById(scene_id=scene_id).first_result
db.query(Report).filter(scene_id == Report.scene_id).update(
{
"is_processed": True,
"raw_data": landsat_item.model_dump_json(),
}
)
db.commit()
try:
landsat_item = LandsatAdvancedAPIById(scene_id=scene_id).first_result
print("Processed")
db.query(Report).filter(scene_id == Report.scene_id).update(
{
"is_processed": True,
"raw_data": landsat_item.model_dump_json(),
}
)
db.commit()
except Exception as e:
structlog.get_logger().error(f"Error while building LandsatItem from {scene_id}: {e}")
Loading

0 comments on commit e169b5b

Please sign in to comment.