コード例 #1
0
    def test_case_3():
        root_cat = Catalog(id='test3', description='test case 3 catalog', title='test case 3 title')

        image_item = Item(id='imagery-item',
                          geometry=RANDOM_GEOM,
                          bbox=RANDOM_BBOX,
                          datetime=datetime.utcnow(),
                          properties={})

        image_item.add_asset('ortho', Asset(href='some/geotiff.tiff', media_type=MediaType.GEOTIFF))

        overviews = [LabelOverview('label', counts=[LabelCount('one', 1), LabelCount('two', 2)])]

        label_item = LabelItem(id='label-items',
                               geometry=RANDOM_GEOM,
                               bbox=RANDOM_BBOX,
                               datetime=datetime.utcnow(),
                               properties={},
                               label_description='ML Labels',
                               label_type='vector',
                               label_properties=['label'],
                               label_classes=[LabelClasses(classes=['one', 'two'], name='label')],
                               label_tasks=['classification'],
                               label_methods=['manual'],
                               label_overviews=overviews)
        label_item.add_source(image_item, assets=['ortho'])

        root_cat.add_item(image_item)
        root_cat.add_item(label_item)

        return root_cat
コード例 #2
0
 def test_items_with_no_input_source_raise_exceptions(self):
     catalog = Catalog('0', 'Catalog 0')
     catalog.add_item(
         Item('1', None, [0, 0, 1, 1], '2020-01-01T00:00:00.000Z', {}))
     adapter = AdapterTester(Message(full_message),
                             catalog,
                             config=self.config)
     self.assertRaises(RuntimeError, adapter.invoke)
コード例 #3
0
ファイル: test_catalog.py プロジェクト: tyler-c2s/pystac
    def test_map_items_multiple_2(self):
        catalog = Catalog(id='test-1', description='Test1')
        item1 = Item(id='item1',
                     geometry=RANDOM_GEOM,
                     bbox=RANDOM_BBOX,
                     datetime=datetime.utcnow(),
                     properties={})
        item1.add_asset('ortho', Asset(href='/some/ortho.tif'))
        catalog.add_item(item1)
        kitten = Catalog(id='test-kitten',
                         description='A cuter version of catalog')
        catalog.add_child(kitten)
        item2 = Item(id='item2',
                     geometry=RANDOM_GEOM,
                     bbox=RANDOM_BBOX,
                     datetime=datetime.utcnow(),
                     properties={})
        item2.add_asset('ortho', Asset(href='/some/other/ortho.tif'))
        kitten.add_item(item2)

        def modify_item_title(item):
            item.title = 'Some new title'
            return item

        def create_label_item(item):
            # Assumes the GEOJSON labels are in the
            # same location as the image
            img_href = item.assets['ortho'].href
            label_href = '{}.geojson'.format(os.path.splitext(img_href)[0])
            label_item = Item(id='Labels',
                              geometry=item.geometry,
                              bbox=item.bbox,
                              datetime=datetime.utcnow(),
                              properties={})
            label_item.ext.enable(Extensions.LABEL)
            label_ext = label_item.ext.label
            label_ext.apply(label_description='labels',
                            label_type='vector',
                            label_properties=['label'],
                            label_classes=[
                                LabelClasses.create(classes=['one', 'two'],
                                                    name='label')
                            ],
                            label_tasks=['classification'])
            label_ext.add_source(item, assets=['ortho'])
            label_ext.add_geojson_labels(label_href)

            return [item, label_item]

        c = catalog.map_items(modify_item_title)
        c = c.map_items(create_label_item)
        new_catalog = c

        items = new_catalog.get_all_items()
        self.assertTrue(len(list(items)) == 4)
コード例 #4
0
ファイル: test_cases.py プロジェクト: stac-utils/pystac
    def test_case_3() -> Catalog:
        root_cat = Catalog(id="test3",
                           description="test case 3 catalog",
                           title="test case 3 title")

        image_item = Item(
            id="imagery-item",
            geometry=ARBITRARY_GEOM,
            bbox=ARBITRARY_BBOX,
            datetime=datetime.utcnow(),
            properties={},
        )

        image_item.add_asset(
            "ortho",
            Asset(href="some/geotiff.tiff", media_type=MediaType.GEOTIFF))

        overviews = [
            LabelOverview.create(
                "label",
                counts=[
                    LabelCount.create("one", 1),
                    LabelCount.create("two", 2)
                ],
            )
        ]

        label_item = Item(
            id="label-items",
            geometry=ARBITRARY_GEOM,
            bbox=ARBITRARY_BBOX,
            datetime=datetime.utcnow(),
            properties={},
        )

        LabelExtension.add_to(label_item)
        label_ext = LabelExtension.ext(label_item)
        label_ext.apply(
            label_description="ML Labels",
            label_type=LabelType.VECTOR,
            label_properties=["label"],
            label_classes=[
                LabelClasses.create(classes=["one", "two"], name="label")
            ],
            label_tasks=["classification"],
            label_methods=["manual"],
            label_overviews=overviews,
        )
        label_ext.add_source(image_item, assets=["ortho"])

        root_cat.add_item(image_item)
        root_cat.add_item(label_item)

        return root_cat
コード例 #5
0
    def test_full_copy_1(self):
        with TemporaryDirectory() as tmp_dir:
            cat = Catalog(id='test', description='test catalog')

            item = Item(id='test_item',
                        geometry=RANDOM_GEOM,
                        bbox=RANDOM_BBOX,
                        datetime=datetime.utcnow(),
                        properties={})

            cat.add_item(item)

            cat.normalize_hrefs(os.path.join(tmp_dir, 'catalog-full-copy-1-source'))
            cat2 = cat.full_copy()
            cat2.normalize_hrefs(os.path.join(tmp_dir, 'catalog-full-copy-1-dest'))

            self.check_catalog(cat, 'source')
            self.check_catalog(cat2, 'dest')
コード例 #6
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."
        )
コード例 #7
0
    def test_clear_items_removes_from_cache(self):
        catalog = Catalog(id='test', description='test')
        subcat = Catalog(id='subcat', description='test')
        catalog.add_child(subcat)
        item = Item(id='test-item',
                    geometry=RANDOM_GEOM,
                    bbox=RANDOM_BBOX,
                    datetime=datetime.utcnow(),
                    properties={'key': 'one'})
        subcat.add_item(item)

        items = list(catalog.get_all_items())
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].properties['key'], 'one')

        subcat.clear_items()
        item = Item(id='test-item',
                    geometry=RANDOM_GEOM,
                    bbox=RANDOM_BBOX,
                    datetime=datetime.utcnow(),
                    properties={'key': 'two'})
        subcat.add_item(item)

        items = list(catalog.get_all_items())
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].properties['key'], 'two')

        subcat.remove_item('test-item')
        item = Item(id='test-item',
                    geometry=RANDOM_GEOM,
                    bbox=RANDOM_BBOX,
                    datetime=datetime.utcnow(),
                    properties={'key': 'three'})
        subcat.add_item(item)

        items = list(catalog.get_all_items())
        self.assertEqual(len(items), 1)
        self.assertEqual(items[0].properties['key'], 'three')
コード例 #8
0
ファイル: merge.py プロジェクト: stac-utils/stactools
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