Skip to content

Commit 2901c48

Browse files
committed
add self links (if parent has self link) when adding items or children to catalogs
1 parent 3645114 commit 2901c48

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pystac/catalog.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def set_root(self, root):
151151
def is_relative(self):
152152
return self.catalog_type in [CatalogType.RELATIVE_PUBLISHED, CatalogType.SELF_CONTAINED]
153153

154-
def add_child(self, child, title=None):
154+
def add_child(self, child, title=None, strategy=None):
155155
"""Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`.
156156
This method will set the child's parent to this object, and its root to
157157
this Catalog's root.
@@ -165,8 +165,18 @@ def add_child(self, child, title=None):
165165
if isinstance(child, pystac.Item):
166166
raise STACError('Cannot add item as child. Use add_item instead.')
167167

168+
if strategy is None:
169+
strategy = BestPracticesLayoutStrategy()
170+
168171
child.set_root(self.get_root())
169172
child.set_parent(self)
173+
174+
# set self link
175+
self_href = self.get_self_href()
176+
if self_href:
177+
child_href = strategy.get_href(child, os.path.dirname(self_href))
178+
child.set_self_href(child_href)
179+
170180
self.add_link(Link.child(child, title=title))
171181

172182
def add_children(self, children):
@@ -180,7 +190,7 @@ def add_children(self, children):
180190
for child in children:
181191
self.add_child(child)
182192

183-
def add_item(self, item, title=None):
193+
def add_item(self, item, title=None, strategy=None):
184194
"""Adds a link to an :class:`~pystac.Item`.
185195
This method will set the item's parent to this object, and its root to
186196
this Catalog's root.
@@ -194,8 +204,18 @@ def add_item(self, item, title=None):
194204
if isinstance(item, pystac.Catalog):
195205
raise STACError('Cannot add catalog as item. Use add_child instead.')
196206

207+
if strategy is None:
208+
strategy = BestPracticesLayoutStrategy()
209+
197210
item.set_root(self.get_root())
198211
item.set_parent(self)
212+
213+
# set self link
214+
self_href = self.get_self_href()
215+
if self_href:
216+
item_href = strategy.get_href(item, os.path.dirname(self_href))
217+
item.set_self_href(item_href)
218+
199219
self.add_link(Link.item(item, title=title))
200220

201221
def add_items(self, items):
@@ -559,6 +579,10 @@ def generate_subcatalogs(self, template, defaults=None, parent_ids=None, **kwarg
559579
result.append(subcat)
560580
curr_parent = subcat
561581
self.remove_item(item.id)
582+
# resolve collection link so when added back points to correct location
583+
link = item.get_single_link('collection')
584+
if link is not None:
585+
link.resolve_stac_object()
562586
curr_parent.add_item(item)
563587

564588
return result

0 commit comments

Comments
 (0)