Skip to content

Commit

Permalink
[wip] add STAC items self-validation procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Nov 11, 2023
1 parent d341e11 commit 467fc51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
11 changes: 7 additions & 4 deletions STACpopulator/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime as datetime_type
from typing import List, Literal, Optional

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -36,9 +36,9 @@ class STACItemProperties(BaseModel):
In concrete implementations, users would want to define a new data model that inherits from this base model
and extends it with properties tailored to the data they are ingesting.
"""
start_datetime: Optional[datetime.datetime] = None
end_datetime: Optional[datetime.datetime] = None
datetime_: Optional[datetime.datetime] = Field(None, alias="datetime")
start_datetime: Optional[datetime_type] = None
end_datetime: Optional[datetime_type] = None
datetime: Optional[datetime_type] = None

def __setitem__(self, key, value):
setattr(self, key, value)
Expand All @@ -48,3 +48,6 @@ def __getitem__(self, item):

def __delitem__(self, item):
return delattr(self, item)

def __contains__(self, item):
return hasattr(self, item)
14 changes: 6 additions & 8 deletions STACpopulator/stac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pystac
import yaml
from colorlog import ColoredFormatter
from pystac.validation import validate as pystac_validate

from STACpopulator.models import Geometry, STACItemProperties

Expand Down Expand Up @@ -181,21 +182,18 @@ def STAC_item_from_metadata(
cfmeta = attrs["groups"]["CFMetadata"]["attributes"]

# Create pydantic STAC item
props = item_props_data_model(**attrs["attributes"])
geom = item_geometry_model(**ncattrs_to_geometry(attrs))
item = pystac.Item(
id=iid,
geometry=item_geometry_model(**ncattrs_to_geometry(attrs)),
geometry=json.loads(geom.model_dump_json(by_alias=True)),
bbox=ncattrs_to_bbox(attrs),
properties=item_props_data_model(
**attrs["attributes"],
),
properties=json.loads(props.model_dump_json(by_alias=True)),
datetime=None,
start_datetime=dt_parser.parse(cfmeta["time_coverage_start"]),
end_datetime=dt_parser.parse(cfmeta["time_coverage_end"]),
)

# Convert pydantic STAC item to a PySTAC Item
item = pystac.Item(**json.loads(item.model_dump_json(by_alias=True)))

root = attrs["access_urls"]

for name, url in root.items():
Expand All @@ -205,7 +203,7 @@ def STAC_item_from_metadata(
item.add_asset(name, asset)

item.add_link(magpie_resource_link(root["HTTPServer"]))

item.validate()
return item


Expand Down

0 comments on commit 467fc51

Please sign in to comment.