Skip to content

Commit a69d6f9

Browse files
committed
update collection serializer
1 parent cc3b3ee commit a69d6f9

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,39 +74,57 @@ def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item:
7474
links=item_links if "links" in item else [],
7575
assets=item["assets"] if "assets" in item else {},
7676
)
77-
77+
7878

7979
class CollectionSerializer(Serializer):
8080
"""Serialization methods for STAC collections."""
8181

8282
@classmethod
8383
def db_to_stac(cls, collection: dict, base_url: str) -> stac_types.Collection:
84-
"""Transform database model to stac collection."""
84+
"""Transform database model to STAC collection.
85+
86+
Args:
87+
collection (dict): The collection data in dictionary form, extracted from the database.
88+
base_url (str): The base URL for the collection.
89+
90+
Returns:
91+
stac_types.Collection: The STAC collection object.
92+
"""
93+
94+
# Use dictionary unpacking to extract values from the collection dictionary
95+
collection_id = collection.get("id")
96+
stac_extensions = collection.get("stac_extensions", [])
97+
stac_version = collection.get("stac_version", "")
98+
title = collection.get("title", "")
99+
description = collection.get("description", "")
100+
keywords = collection.get("keywords", [])
101+
license = collection.get("license", "")
102+
providers = collection.get("providers", {})
103+
summaries = collection.get("summaries", {})
104+
extent = collection.get("extent", {})
105+
106+
# Create the collection links using CollectionLinks
85107
collection_links = CollectionLinks(
86-
collection_id=collection["id"], base_url=base_url
108+
collection_id=collection_id, base_url=base_url
87109
).create_links()
88110

89-
original_links = collection["links"]
111+
# Add any additional links from the collection dictionary
112+
original_links = collection.get("links")
90113
if original_links:
91114
collection_links += resolve_links(original_links, base_url)
92115

116+
# Return the stac_types.Collection object
93117
return stac_types.Collection(
94118
type="Collection",
95-
id=collection["id"],
96-
stac_extensions=collection["stac_extensions"]
97-
if "stac_extensions" in collection
98-
else [],
99-
stac_version=collection["stac_version"]
100-
if "stac_version" in collection
101-
else "",
102-
title=collection["title"] if "title" in collection else "",
103-
description=collection["description"]
104-
if "description" in collection
105-
else "",
106-
keywords=collection["keywords"] if "keywords" in collection else [],
107-
license=collection["license"] if "license" in collection else "",
108-
providers=collection["providers"] if "providers" in collection else {},
109-
summaries=collection["summaries"] if "summaries" in collection else {},
110-
extent=collection["extent"] if "extent" in collection else {},
119+
id=collection_id,
120+
stac_extensions=stac_extensions,
121+
stac_version=stac_version,
122+
title=title,
123+
description=description,
124+
keywords=keywords,
125+
license=license,
126+
providers=providers,
127+
summaries=summaries,
128+
extent=extent,
111129
links=collection_links,
112130
)

0 commit comments

Comments
 (0)