Skip to content

Commit bfb0bd8

Browse files
committed
make stac-to-db method
1 parent 0fa2aad commit bfb0bd8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from stac_fastapi.types import stac as stac_types
88
from stac_fastapi.types.links import CollectionLinks, ItemLinks, resolve_links
9+
from datetime import datetime
10+
from stac_pydantic.shared import DATETIME_RFC339
911

1012

1113
@attr.s # type:ignore
@@ -22,7 +24,24 @@ def db_to_stac(cls, item: dict, base_url: str) -> TypedDict:
2224
class ItemSerializer(Serializer):
2325
"""Serialization methods for STAC items."""
2426
@classmethod
25-
def stac_to_db(cls, stac_data: TypedDict) -> stac_types.Item:
27+
def stac_to_db(cls, stac_data: TypedDict, base_url: str) -> stac_types.Item:
28+
item_links = ItemLinks(
29+
collection_id=stac_data["collection"], item_id=stac_data["id"], base_url=base_url
30+
).create_links()
31+
stac_data["links"] = item_links
32+
33+
# elasticsearch doesn't like the fact that some values are float and some were int
34+
if "eo:bands" in stac_data["properties"]:
35+
for wave in stac_data["properties"]["eo:bands"]:
36+
for k, v in wave.items():
37+
if type(v) != str:
38+
v = float(v)
39+
wave.update({k: v})
40+
41+
now = datetime.utcnow().strftime(DATETIME_RFC339)
42+
if "created" not in stac_data["properties"]:
43+
stac_data["properties"]["created"] = str(now)
44+
stac_data["properties"]["updated"] = str(now)
2645
return stac_data
2746

2847
@classmethod

0 commit comments

Comments
 (0)