Skip to content

Commit b17aeb5

Browse files
author
Jon Duckworth
authored
Merge pull request #549 from stac-utils/feature/rde/itemcollect-todict
Allow root to be set during to_dict
2 parents 073aad2 + e892910 commit b17aeb5

File tree

10 files changed

+48
-5
lines changed

10 files changed

+48
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
### Changed
1414

15+
- The `from_dict` method on STACObjects will set the object's root link when a `root` parameter is present. An ItemCollection `from_dict` with a root parameter will set the root on each of it's Items. ([#549](https://github.com/stac-utils/pystac/pull/549))
16+
1517
### Deprecated
1618

1719
## [v1.0.0-rc.3]

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")}

pystac/stac_object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ def from_dict(
505505
d : The dict to parse.
506506
href : Optional href that is the file location of the object being
507507
parsed.
508-
root : Optional root of the catalog for this object.
509-
If provided, the root's resolved object cache can be used to search for
510-
previously resolved instances of the STAC object.
508+
root : Optional root catalog for this object.
509+
If provided, the root of the returned STACObject will be set
510+
to this parameter.
511511
migrate: Use True if this dict represents JSON from an older STAC object,
512512
so that migrations are run against it.
513513
preserve_dict: If False, the dict parameter ``d`` may be modified

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)