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

Better control on subfolders in generate_subcatalogs #595

Merged
merged 6 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 5 additions & 9 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,26 +665,22 @@ def generate_subcatalogs(
for link in item_links:
link.resolve_stac_object(root=self.get_root())
item = cast(pystac.Item, link.target)
item_parts = layout_template.get_template_values(item)
subcat_ids = layout_template.substitute(item).split("/")
duckontheweb marked this conversation as resolved.
Show resolved Hide resolved
id_iter = reversed(parent_ids)
if all(
[
"{}".format(id) == next(id_iter, None)
for id in reversed(list(item_parts.values()))
]
["{}".format(id) == next(id_iter, None) for id in reversed(subcat_ids)]
):
# 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():
subcat_id = "{}".format(v)
for subcat_id in subcat_ids:
subcat = curr_parent.get_child(subcat_id)
if subcat is None:
subcat_desc = "Catalog of items from {} with {} of {}".format(
curr_parent.id, k, v
subcat_desc = "Catalog of items from {} with id {}".format(
curr_parent.id, subcat_id
)
subcat = pystac.Catalog(id=subcat_id, description=subcat_desc)
curr_parent.add_child(subcat)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,30 @@ def test_generate_subcatalogs_does_not_change_item_count(self) -> None:
actual, expected, msg=" for child '{}'".format(child.id)
)

def test_generate_subcatalogs_merge_template_elements(self) -> None:
catalog = Catalog(id="test", description="Test")
item_properties = [
dict(property1=p1, property2=p2) for p1 in ("A", "B") for p2 in (1, 2)
]
for ni, properties in enumerate(item_properties):
catalog.add_item(
Item(
id="item{}".format(ni),
geometry=ARBITRARY_GEOM,
bbox=ARBITRARY_BBOX,
datetime=datetime.utcnow(),
properties=properties,
)
)
result = catalog.generate_subcatalogs("${property1}_${property2}")

actual_subcats = set([cat.id for cat in result])
expected_subcats = set(
["{}_{}".format(d["property1"], d["property2"]) for d in item_properties]
)
self.assertEqual(len(result), len(expected_subcats))
self.assertSetEqual(actual_subcats, expected_subcats)

def test_generate_subcatalogs_can_be_applied_multiple_times(self) -> None:
catalog = TestCases.test_case_8()

Expand Down