Ejemplo n.º 1
0
def add_item(source_item: Item,
             target_catalog: Catalog,
             move_assets: bool = False) -> None:
    """Add a item into a catalog.

    Args:
        source_item (pystac.Item): The Item that will be added.
            This item is not mutated in this operation.
        target_catalog (pystac.Item): The destination catalog.
            This catalog will be mutated in this operation.
        move_assets (bool): If true, move the asset files alongside the target item.
    """

    target_item_ids = [item.id for item in target_catalog.get_all_items()]
    if source_item.id in target_item_ids:
        raise ValueError(
            f'An item with ID {source_item.id} already exists in the target catalog'
        )
    self_href = target_catalog.get_self_href()
    if self_href:
        parent_dir = os.path.dirname(self_href)
        layout_strategy = BestPracticesLayoutStrategy()
        item_copy = source_item.clone()
        item_copy.set_self_href(
            layout_strategy.get_item_href(item_copy, parent_dir))
        target_catalog.add_item(item_copy)

        if isinstance(target_catalog, Collection):
            item_copy.set_collection(target_catalog)
            target_catalog.update_extent_from_items()
        else:
            item_copy.set_collection(None)

        if move_assets:
            do_move_assets(item_copy, copy=False)
    else:
        raise ValueError(
            f"Cannot add Item {source_item.id} because {target_catalog} does not have a self href."
        )
Ejemplo n.º 2
0
def merge_all_items(source_catalog: pystac.Catalog,
                    target_catalog: pystac.Catalog,
                    move_assets: bool = False,
                    ignore_conflicts: bool = False,
                    as_child: bool = False,
                    child_folder: Optional[str] = None) -> pystac.Catalog:
    """Merge all items from source_catalog into target_catalog.

    Calls merge_items on any items that have the same ID between the two catalogs.
    Any items that don't exist in the taret_catalog will be added to the target_catalog.
    If the target_catalog is a Collection, it will be set as the collection of any
    new items.

    Args:
        source_catalog (Catalog or Collection): The catalog or collection that items
            will be drawn from to merge into the target catalog.
            This catalog is not mutated in this operation.
        target_item (Catalog or Collection): The target catalog that will be merged into.
            This catalog will not be mutated in this operation.
        move_assets (bool): If true, move the asset files alongside the target item.
        ignore_conflicts (bool): If True, assets with the same keys will not be merged,
            and asset files that would be moved to overwrite an existing file
            will not be moved. If False, either of these situations will throw an error.
        as_child (bool): If True, a child catalog will be added with the content of the
            source catalog. Otherwise, items will be added directly to the destination
            catalog.
        child_folder (str): name of the subfolder to use in case the as_child option is
            set to True. If None, the id of the catalog will be used as folder name.

    Returns:
        Catalog or Collection: The target_catalog
    """
    source_items = source_catalog.get_all_items()
    ids_to_items = {item.id: item for item in source_items}

    parent_dir = os.path.dirname(target_catalog.self_href)
    if as_child:
        child_dir = os.path.join(parent_dir, child_folder or source_catalog.id)
        copy_catalog(source_catalog, child_dir, source_catalog.catalog_type,
                     move_assets)
        child_catalog_path = os.path.join(
            child_dir, os.path.basename(source_catalog.self_href))
        new_source_catalog = pystac.read_file(child_catalog_path)
        if not isinstance(new_source_catalog, pystac.Catalog):
            raise ValueError(
                f"Child catalog {child_catalog_path} is not a STAC Catalog")
        source_catalog = new_source_catalog
        target_catalog.add_child(source_catalog, source_catalog.title)
    else:
        for item in target_catalog.get_all_items():
            source_item = ids_to_items.get(item.id)
            if source_item is not None:
                merge_items(source_item,
                            item,
                            move_assets=move_assets,
                            ignore_conflicts=ignore_conflicts)
                del ids_to_items[item.id]

        # Process source items that did not match existing target items
        layout_strategy = BestPracticesLayoutStrategy()
        for item in ids_to_items.values():
            item_copy = item.clone()
            item_copy.set_self_href(
                layout_strategy.get_item_href(item_copy, parent_dir))
            target_catalog.add_item(item_copy)

            if isinstance(target_catalog, pystac.Collection):
                item_copy.set_collection(target_catalog)
            else:
                item_copy.set_collection(None)

            if move_assets:
                do_move_assets(item_copy, copy=False)

    if isinstance(target_catalog, pystac.Collection):
        target_catalog.update_extent_from_items()

    return target_catalog