Skip to content

Commit 99771d2

Browse files
committed
item serializer
1 parent a69d6f9 commit 99771d2

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ class ItemSerializer(Serializer):
2525

2626
@classmethod
2727
def stac_to_db(cls, stac_data: stac_types.Item, base_url: str) -> stac_types.Item:
28-
"""Transform STAC Item to database-ready STAC Item."""
28+
"""Transform STAC Item to database-ready STAC Item.
29+
30+
:param stac_data: STAC Item object to be transformed.
31+
:type stac_data: stac_types.Item
32+
:param base_url: Base URL for the STAC API.
33+
:type base_url: str
34+
:return: A database-ready STAC Item object.
35+
:rtype: stac_types.Item
36+
"""
2937
item_links = ItemLinks(
3038
collection_id=stac_data["collection"],
3139
item_id=stac_data["id"],
3240
base_url=base_url,
3341
).create_links()
3442
stac_data["links"] = item_links
3543

36-
# elasticsearch doesn't like the fact that some values are float and some were int
37-
if "eo:bands" in stac_data["properties"]:
38-
for wave in stac_data["properties"]["eo:bands"]:
39-
for k, v in wave.items():
40-
if type(v) != str:
41-
v = float(v)
42-
wave.update({k: v})
43-
4444
now = now_to_rfc3339_str()
4545
if "created" not in stac_data["properties"]:
4646
stac_data["properties"]["created"] = now
@@ -49,32 +49,38 @@ def stac_to_db(cls, stac_data: stac_types.Item, base_url: str) -> stac_types.Ite
4949

5050
@classmethod
5151
def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
52-
"""Transform database model to stac item."""
52+
"""Transform database model to STAC item.
53+
54+
:param item: Database model to be transformed.
55+
:type item: dict
56+
:param base_url: Base URL for the STAC API.
57+
:type base_url: str
58+
:return: A STAC Item object.
59+
:rtype: stac_types.Item
60+
"""
5361
item_id = item["id"]
5462
collection_id = item["collection"]
5563
item_links = ItemLinks(
5664
collection_id=collection_id, item_id=item_id, base_url=base_url
5765
).create_links()
5866

59-
original_links = item["links"]
67+
original_links = item.get("links", [])
6068
if original_links:
6169
item_links += resolve_links(original_links, base_url)
6270

6371
return stac_types.Item(
6472
type="Feature",
65-
stac_version=item["stac_version"] if "stac_version" in item else "",
66-
stac_extensions=item["stac_extensions"]
67-
if "stac_extensions" in item
68-
else [],
73+
stac_version=item.get("stac_version", ""),
74+
stac_extensions=item.get("stac_extensions", []),
6975
id=item_id,
70-
collection=item["collection"] if "collection" in item else "",
71-
geometry=item["geometry"] if "geometry" in item else {},
72-
bbox=item["bbox"] if "bbox" in item else [],
73-
properties=item["properties"] if "properties" in item else {},
74-
links=item_links if "links" in item else [],
75-
assets=item["assets"] if "assets" in item else {},
76+
collection=item.get("collection", ""),
77+
geometry=item.get("geometry", {}),
78+
bbox=item.get("bbox", []),
79+
properties=item.get("properties", {}),
80+
links=item_links,
81+
assets=item.get("assets", {}),
7682
)
77-
83+
7884

7985
class CollectionSerializer(Serializer):
8086
"""Serialization methods for STAC collections."""

0 commit comments

Comments
 (0)