Skip to content

Commit 28c282e

Browse files
committed
Allow root to be set during to_dict
There was existing unused arguments for seting the root on to_dict. This commit implements and tests that the root is set to the parameter if present, and adds the parameter to ItemCollection, which itself does not have a root but instead passes the root onto each of the Items parsed during the to_dict call. Related to #546
1 parent 12eff70 commit 28c282e

File tree

8 files changed

+43
-2
lines changed

8 files changed

+43
-2
lines changed

pystac/catalog.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ def from_dict(
963963
if link["rel"] != pystac.RelType.SELF or href is None:
964964
cat.add_link(Link.from_dict(link))
965965

966+
if root:
967+
cat.set_root(root)
968+
966969
return cat
967970

968971
def full_copy(

pystac/collection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,9 @@ def from_dict(
732732
for asset_key, asset_dict in assets.items():
733733
collection.add_asset(asset_key, Asset.from_dict(asset_dict))
734734

735+
if root:
736+
collection.set_root(root)
737+
735738
return collection
736739

737740
def get_assets(self) -> Dict[str, Asset]:

pystac/item.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,9 @@ def from_dict(
970970
asset.set_owner(item)
971971
item.assets[k] = asset
972972

973+
if root:
974+
item.set_root(root)
975+
973976
return item
974977

975978
@property

pystac/item_collection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ def clone(self) -> "ItemCollection":
135135

136136
@classmethod
137137
def from_dict(
138-
cls, d: Dict[str, Any], preserve_dict: bool = True
138+
cls,
139+
d: Dict[str, Any],
140+
preserve_dict: bool = True,
141+
root: Optional[pystac.Catalog] = None,
139142
) -> "ItemCollection":
140143
"""Creates a :class:`ItemCollection` instance from a dictionary.
141144
@@ -151,7 +154,7 @@ def from_dict(
151154
raise STACTypeError("Dict is not a valid ItemCollection")
152155

153156
items = [
154-
pystac.Item.from_dict(item, preserve_dict=preserve_dict)
157+
pystac.Item.from_dict(item, preserve_dict=preserve_dict, root=root)
155158
for item in d.get("features", [])
156159
]
157160
extra_fields = {k: v for k, v in d.items() if k not in ("features", "type")}

tests/test_catalog.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def test_from_dict_preserves_dict(self) -> None:
9999
_ = Catalog.from_dict(param_dict, preserve_dict=False)
100100
self.assertNotEqual(param_dict, catalog_dict)
101101

102+
def test_from_dict_set_root(self) -> None:
103+
path = TestCases.get_path("data-files/catalogs/test-case-1/catalog.json")
104+
with open(path) as f:
105+
cat_dict = json.load(f)
106+
root_cat = pystac.Catalog(id="test", description="test desc")
107+
collection = Catalog.from_dict(cat_dict, root=root_cat)
108+
self.assertIs(collection.get_root(), root_cat)
109+
102110
def test_read_remote(self) -> None:
103111
# TODO: Move this URL to the main stac-spec repo once the example JSON is fixed.
104112
catalog_url = (

tests/test_collection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ def test_from_dict_preserves_dict(self) -> None:
228228
_ = Collection.from_dict(param_dict, preserve_dict=False)
229229
self.assertNotEqual(param_dict, collection_dict)
230230

231+
def test_from_dict_set_root(self) -> None:
232+
path = TestCases.get_path("data-files/examples/hand-0.8.1/collection.json")
233+
with open(path) as f:
234+
collection_dict = json.load(f)
235+
catalog = pystac.Catalog(id="test", description="test desc")
236+
collection = Collection.from_dict(collection_dict, root=catalog)
237+
self.assertIs(collection.get_root(), catalog)
238+
231239
def test_schema_summary(self) -> None:
232240
collection = pystac.Collection.from_file(
233241
TestCases.get_path(

tests/test_item.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def test_to_from_dict(self) -> None:
4747
_ = Item.from_dict(param_dict, preserve_dict=False)
4848
self.assertNotEqual(param_dict, item_dict)
4949

50+
def test_from_dict_set_root(self) -> None:
51+
item_dict = self.get_example_item_dict()
52+
catalog = pystac.Catalog(id="test", description="test desc")
53+
item = Item.from_dict(item_dict, root=catalog)
54+
self.assertIs(item.get_root(), catalog)
55+
5056
def test_set_self_href_does_not_break_asset_hrefs(self) -> None:
5157
cat = TestCases.test_case_2()
5258
for item in cat.get_all_items():

tests/test_item_collection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,10 @@ def test_from_dict_preserves_dict(self) -> None:
169169
# non-default parameter
170170
_ = ItemCollection.from_dict(param_dict, preserve_dict=False)
171171
self.assertNotEqual(param_dict, self.item_collection_dict)
172+
173+
def test_from_dict_sets_root(self) -> None:
174+
param_dict = deepcopy(self.item_collection_dict)
175+
catalog = pystac.Catalog(id="test", description="test desc")
176+
item_collection = ItemCollection.from_dict(param_dict, root=catalog)
177+
for item in item_collection.items:
178+
self.assertEqual(item.get_root(), catalog)

0 commit comments

Comments
 (0)