Skip to content

Commit 467fc51

Browse files
committed
[wip] add STAC items self-validation procedure
1 parent d341e11 commit 467fc51

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

STACpopulator/models.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import datetime
1+
from datetime import datetime as datetime_type
22
from typing import List, Literal, Optional
33

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

4343
def __setitem__(self, key, value):
4444
setattr(self, key, value)
@@ -48,3 +48,6 @@ def __getitem__(self, item):
4848

4949
def __delitem__(self, item):
5050
return delattr(self, item)
51+
52+
def __contains__(self, item):
53+
return hasattr(self, item)

STACpopulator/stac_utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pystac
1111
import yaml
1212
from colorlog import ColoredFormatter
13+
from pystac.validation import validate as pystac_validate
1314

1415
from STACpopulator.models import Geometry, STACItemProperties
1516

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

183184
# Create pydantic STAC item
185+
props = item_props_data_model(**attrs["attributes"])
186+
geom = item_geometry_model(**ncattrs_to_geometry(attrs))
184187
item = pystac.Item(
185188
id=iid,
186-
geometry=item_geometry_model(**ncattrs_to_geometry(attrs)),
189+
geometry=json.loads(geom.model_dump_json(by_alias=True)),
187190
bbox=ncattrs_to_bbox(attrs),
188-
properties=item_props_data_model(
189-
**attrs["attributes"],
190-
),
191+
properties=json.loads(props.model_dump_json(by_alias=True)),
191192
datetime=None,
192193
start_datetime=dt_parser.parse(cfmeta["time_coverage_start"]),
193194
end_datetime=dt_parser.parse(cfmeta["time_coverage_end"]),
194195
)
195196

196-
# Convert pydantic STAC item to a PySTAC Item
197-
item = pystac.Item(**json.loads(item.model_dump_json(by_alias=True)))
198-
199197
root = attrs["access_urls"]
200198

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

207205
item.add_link(magpie_resource_link(root["HTTPServer"]))
208-
206+
item.validate()
209207
return item
210208

211209

0 commit comments

Comments
 (0)