Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions openeo_driver/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from urllib.parse import urlparse

from pystac import Asset, Collection, Item, RelType, SpatialExtent, STACObject, TemporalExtent
from pystac.catalog import CatalogType
from pystac.catalog import CatalogType, Catalog
from pystac.layout import CustomLayoutStrategy, HrefLayoutStrategy

from openeo_driver.utils import remove_slash_prefix
Expand Down Expand Up @@ -75,7 +75,7 @@ def merge(self, stac_resource: STACObject, target: PurePath, remove_original: bo

file_operation = shutil.move if remove_original else shutil.copy

if isinstance(stac_resource, Collection):
if isinstance(stac_resource, Catalog): # includes Collection
new_collection = stac_resource

existing_collection = None
Expand Down Expand Up @@ -132,6 +132,9 @@ def replace_asset_href(asset: Asset, src_collection_path: Path) -> Asset:
asset_parent_dir.mkdir(parents=True, exist_ok=True) # asset might not end up next to item
file_operation(asset.extra_fields["_original_absolute_path"], str(asset_parent_dir))
else:
assert isinstance(
new_collection, Collection
), f"Can only merge a Collection into an existing Collection. Was type: {new_collection}"
merged_collection = _merge_collection_metadata(existing_collection, new_collection)
new_collection = new_collection.map_assets(
lambda _, asset: replace_asset_href(asset, src_collection_path=new_collection_path)
Expand Down
53 changes: 51 additions & 2 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from typing import Set

from pystac import Asset, Collection, Extent, Item, SpatialExtent, TemporalExtent, CatalogType, Link, RelType
from pystac import Asset, Catalog, Collection, Extent, Item, SpatialExtent, TemporalExtent, CatalogType, Link, RelType
import pytest

from openeo_driver.util.date_math import now_utc
Expand Down Expand Up @@ -111,6 +111,55 @@ def test_merge_from_disk_new(tmp_path):
assert Path(item.get_self_href()).parent == Path(asset.get_absolute_href()).parent


def test_merge_catalog_from_disk_new(tmp_path):
root_path = tmp_path / "src" / "catalog"
asset_filename = "asset.tif"

new_catalog = Catalog(id="catalog", description="catalog")
item = Item(id=asset_filename.upper(), geometry=None, bbox=None, datetime=now_utc(), properties={})
asset_path = root_path / asset_filename
item.add_asset(key=asset_filename, asset=Asset(href=str(asset_path.absolute())))
new_catalog.add_item(item)
new_catalog.normalize_and_save(root_href=str(root_path), catalog_type=CatalogType.SELF_CONTAINED)
with open(asset_path, "w") as f:
f.write("catalog-asset\n")

target = Path("path") / "to" / "catalog.json"
workspace = DiskWorkspace(root_directory=tmp_path)
workspace_dir = (workspace.root_directory / target).parent
assert not (workspace.root_directory / target).exists()

imported_catalog = workspace.merge(stac_resource=new_catalog, target=target)

assert _paths_relative_to(workspace_dir) == {
Path("catalog.json"),
Path("catalog.json_items") / "ASSET.TIF.json",
Path("catalog.json_items") / "asset.tif",
}

assert isinstance(imported_catalog, Catalog)
assert not isinstance(imported_catalog, Collection)
asset_workspace_uris = {
asset_key: asset.extra_fields["alternate"]["file"]
for item in imported_catalog.get_items()
for asset_key, asset in item.get_assets().items()
}
assert asset_workspace_uris == {
"asset.tif": f"file:{workspace.root_directory / 'path' / 'to' / 'catalog.json_items' / 'asset.tif'}"
}

exported_catalog = Catalog.from_file(str(workspace_dir / "catalog.json"))
assert not Collection.matches_object_type(exported_catalog.to_dict())
assert exported_catalog.validate_all() == 1
assert _downloadable_assets(exported_catalog) == 1

for item in exported_catalog.get_items():
for asset in item.get_assets().values():
assert Path(item.get_self_href()).parent == Path(asset.get_absolute_href()).parent
with open(asset.get_absolute_href()) as f:
assert f.read() == "catalog-asset\n"


def test_merge_from_disk_into_existing(tmp_path):
existing_collection = _collection(
root_path=tmp_path / "src" / "existing_collection",
Expand Down Expand Up @@ -311,7 +360,7 @@ def _collection(
return collection


def _downloadable_assets(collection: Collection) -> int:
def _downloadable_assets(collection: Catalog) -> int:
assets = [asset for item in collection.get_items(recursive=True) for asset in item.get_assets().values()]

for asset in assets:
Expand Down
Loading