@@ -151,7 +151,7 @@ def set_root(self, root):
151
151
def is_relative (self ):
152
152
return self .catalog_type in [CatalogType .RELATIVE_PUBLISHED , CatalogType .SELF_CONTAINED ]
153
153
154
- def add_child (self , child , title = None ):
154
+ def add_child (self , child , title = None , strategy = None ):
155
155
"""Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`.
156
156
This method will set the child's parent to this object, and its root to
157
157
this Catalog's root.
@@ -165,8 +165,18 @@ def add_child(self, child, title=None):
165
165
if isinstance (child , pystac .Item ):
166
166
raise STACError ('Cannot add item as child. Use add_item instead.' )
167
167
168
+ if strategy is None :
169
+ strategy = BestPracticesLayoutStrategy ()
170
+
168
171
child .set_root (self .get_root ())
169
172
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
+
170
180
self .add_link (Link .child (child , title = title ))
171
181
172
182
def add_children (self , children ):
@@ -180,7 +190,7 @@ def add_children(self, children):
180
190
for child in children :
181
191
self .add_child (child )
182
192
183
- def add_item (self , item , title = None ):
193
+ def add_item (self , item , title = None , strategy = None ):
184
194
"""Adds a link to an :class:`~pystac.Item`.
185
195
This method will set the item's parent to this object, and its root to
186
196
this Catalog's root.
@@ -194,8 +204,18 @@ def add_item(self, item, title=None):
194
204
if isinstance (item , pystac .Catalog ):
195
205
raise STACError ('Cannot add catalog as item. Use add_child instead.' )
196
206
207
+ if strategy is None :
208
+ strategy = BestPracticesLayoutStrategy ()
209
+
197
210
item .set_root (self .get_root ())
198
211
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
+
199
219
self .add_link (Link .item (item , title = title ))
200
220
201
221
def add_items (self , items ):
@@ -559,6 +579,10 @@ def generate_subcatalogs(self, template, defaults=None, parent_ids=None, **kwarg
559
579
result .append (subcat )
560
580
curr_parent = subcat
561
581
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 ()
562
586
curr_parent .add_item (item )
563
587
564
588
return result
0 commit comments