Exemplo n.º 1
0
def fix_item(item: Item, strategy: Strategy) -> None:
    """Modifies an item in-place to deal with antimeridian issues.

    If the item's geometry is not a `Polygon`, raises a `ValueError`.

    Args:
        item (pystac.Item): The item to be modified.
    """
    geometry = shapely.geometry.shape(item.geometry)
    if not isinstance(geometry, Polygon):
        raise ValueError(
            f"Can only fix antimeridian issues for Polygons, geometry={geometry}"
        )
    if strategy == Strategy.NORMALIZE:
        normalized_geometry = normalize(geometry)
        if normalized_geometry:
            bbox = normalized_geometry.bounds
            item.geometry = shapely.geometry.mapping(normalized_geometry)
            item.bbox = bbox
    elif strategy == Strategy.SPLIT:
        split_geometry = split(geometry)
        if split_geometry:
            xmin = 180
            xmax = -180
            for geom in split_geometry.geoms:
                if geom.bounds[0] > xmax:
                    xmax = geom.bounds[0]
                if geom.bounds[2] < xmin:
                    xmin = geom.bounds[2]
            bounds = split_geometry.bounds
            # https://datatracker.ietf.org/doc/html/rfc7946#section-5.2
            item.bbox = [xmax, bounds[1], xmin, bounds[3]]
            item.geometry = shapely.geometry.mapping(split_geometry)
Exemplo n.º 2
0
def merge_items(source_item: pystac.Item,
                target_item: pystac.Item,
                move_assets: bool = False,
                ignore_conflicts: bool = False) -> None:
    """Merges the assets from source_item into target_item.

    The geometry and bounding box of the items will also be merged.

    Args:
        source_item (pystac.Item): The Item that will be merged into target_item.
            This item is not mutated in this operation.
        target_item (pystac.Item): The target item that will be merged into.
            This item will 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.
    """
    target_item_href = target_item.get_self_href()
    if target_item_href is None:
        raise ValueError(
            f"Target Item {target_item.id} must have an HREF for merge")
    for key, asset in source_item.assets.items():
        if key in target_item.assets:
            if ignore_conflicts:
                continue
            else:
                raise Exception(
                    'Target item {} already has asset with key {}, '
                    'cannot merge asset in from {}'.format(
                        target_item, key, source_item))
        else:
            asset_href = asset.get_absolute_href()
            if asset_href is None:
                raise ValueError(
                    f"Asset {asset.title} must have an HREF for merge")
            if move_assets:
                new_asset_href = move_asset_file_to_item(
                    target_item, asset_href, ignore_conflicts=ignore_conflicts)
            else:
                if not is_absolute_href(asset.href):
                    asset_href = make_relative_href(asset_href,
                                                    target_item_href)
                new_asset_href = asset_href
            new_asset = asset.clone()
            new_asset.href = new_asset_href
            target_item.add_asset(key, new_asset)

    source_geom = shape(source_item.geometry)
    target_geom = shape(target_item.geometry)
    union_geom = source_geom.union(target_geom).buffer(0)
    target_item.geometry = mapping(union_geom)
    target_item.bbox = list(union_geom.bounds)