Skip to content

Commit

Permalink
replace remove_item call with removing links
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhanson committed Mar 29, 2021
1 parent 3645114 commit 5c16701
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,19 @@ def generate_subcatalogs(self, template, defaults=None, parent_ids=None, **kwarg

layout_template = LayoutTemplate(template, defaults=defaults)

items = list(self.get_items())
for item in items:
keep_item_links = []
item_links = [lk for lk in self.links if lk.rel == 'item']
for link in item_links:
link.resolve_stac_object(root=self.get_root())
item = link.target
item_parts = layout_template.get_template_values(item)
id_iter = reversed(parent_ids)
if all(['{}'.format(id) == next(id_iter, None)
for id in reversed(item_parts.values())]):
# Skip items for which the sub-catalog structure already
# matches the template. The list of parent IDs can include more
# elements on the root side, so compare the reversed sequences.
keep_item_links.append(link)
continue
curr_parent = self
for k, v in item_parts.items():
Expand All @@ -558,9 +562,12 @@ def generate_subcatalogs(self, template, defaults=None, parent_ids=None, **kwarg
curr_parent.add_child(subcat)
result.append(subcat)
curr_parent = subcat
self.remove_item(item.id)

curr_parent.add_item(item)

# keep only non-item links and item links that have not been moved elsewhere
self.links = [lk for lk in self.links if lk.rel != 'item'] + keep_item_links

return result

def save(self, catalog_type=None):
Expand Down

0 comments on commit 5c16701

Please sign in to comment.