Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster generate_subcatalogs #295

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,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 @@ -578,13 +582,17 @@ 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)

# resolve collection link so when added back points to correct location
link = item.get_single_link('collection')
if link is not None:
link.resolve_stac_object()

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