Beispiel #1
0
    def test_migrate(self) -> None:
        collection_cache = CollectionCache()
        for example in self.examples:
            with self.subTest(example.path):
                path = example.path

                d = pystac.StacIO.default().read_json(path)
                if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
                    merge_common_properties(d,
                                            json_href=path,
                                            collection_cache=collection_cache)

                info = identify_stac_object(d)

                migrated_d = migrate_to_latest(d, info)

                migrated_info = identify_stac_object(migrated_d)

                self.assertEqual(migrated_info.object_type, info.object_type)
                self.assertEqual(
                    migrated_info.version_range.latest_valid_version(),
                    pystac.get_stac_version(),
                )

                # Ensure all stac_extensions are schema URIs
                for e_id in migrated_d["stac_extensions"]:
                    self.assertTrue(e_id.endswith(".json"),
                                    f"{e_id} is not a JSON schema URI")
Beispiel #2
0
    def stac_object_from_dict(
        self,
        d: Dict[str, Any],
        href: Optional[HREF] = None,
        root: Optional["Catalog_Type"] = None,
        preserve_dict: bool = True,
    ) -> "STACObject_Type":
        """Deserializes a :class:`~pystac.STACObject` sub-class instance from a
        dictionary.

        Args:

            d : The dictionary to deserialize
            href : Optional href to associate with the STAC object
            root : Optional root :class:`~pystac.Catalog` to associate with the
                STAC object.
            preserve_dict: If ``False``, the dict parameter ``d`` may be modified
                during this method call. Otherwise the dict is not mutated.
                Defaults to ``True``, which results results in a deepcopy of the
                parameter. Set to ``False`` when possible to avoid the performance
                hit of a deepcopy.
        """
        href_str = None if href is None else str(os.fspath(href))
        if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
            collection_cache = None
            if root is not None:
                collection_cache = root._resolved_objects.as_collection_cache()

            # Merge common properties in case this is an older STAC object.
            merge_common_properties(d,
                                    json_href=href_str,
                                    collection_cache=collection_cache)

        info = identify_stac_object(d)
        d = migrate_to_latest(d, info)

        if info.object_type == pystac.STACObjectType.CATALOG:
            result = pystac.Catalog.from_dict(d,
                                              href=href_str,
                                              root=root,
                                              migrate=False,
                                              preserve_dict=preserve_dict)
            result._stac_io = self
            return result

        if info.object_type == pystac.STACObjectType.COLLECTION:
            return pystac.Collection.from_dict(d,
                                               href=href_str,
                                               root=root,
                                               migrate=False,
                                               preserve_dict=preserve_dict)

        if info.object_type == pystac.STACObjectType.ITEM:
            return pystac.Item.from_dict(d,
                                         href=href_str,
                                         root=root,
                                         migrate=False,
                                         preserve_dict=preserve_dict)

        raise ValueError(f"Unknown STAC object type {info.object_type}")
Beispiel #3
0
    def test_identify(self):
        collection_cache = CollectionCache()
        for example in self.examples:
            path = example['path']
            d = STAC_IO.read_json(path)
            if identify_stac_object_type(d) == STACObjectType.ITEM:
                try:
                    merge_common_properties(d, json_href=path, collection_cache=collection_cache)
                except HTTPError:
                    pass

            actual = identify_stac_object(d)
            # Explicitly cover __repr__ functions in tests
            str_info = str(actual)
            self.assertIsInstance(str_info, str)

            msg = 'Failed {}:'.format(path)

            self.assertEqual(actual.object_type, example['object_type'], msg=msg)
            version_contained_in_range = actual.version_range.contains(example['stac_version'])
            self.assertTrue(version_contained_in_range, msg=msg)
            self.assertEqual(set(actual.common_extensions),
                             set(example['common_extensions']),
                             msg=msg)
            self.assertEqual(set(actual.custom_extensions),
                             set(example['custom_extensions']),
                             msg=msg)
Beispiel #4
0
    def test_identify(self) -> None:
        collection_cache = CollectionCache()
        for example in self.examples:
            with self.subTest(example.path):
                path = example.path
                d = pystac.StacIO.default().read_json(path)
                if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
                    merge_common_properties(
                        d, json_href=path, collection_cache=collection_cache
                    )

                actual = identify_stac_object(d)
                # Explicitly cover __repr__ functions in tests
                str_info = str(actual)
                self.assertIsInstance(str_info, str)

                msg = "Failed {}:".format(path)

                self.assertEqual(actual.object_type, example.object_type, msg=msg)
                version_contained_in_range = actual.version_range.contains(
                    example.stac_version
                )
                self.assertTrue(version_contained_in_range, msg=msg)
                self.assertEqual(
                    set(actual.extensions), set(example.extensions), msg=msg
                )
Beispiel #5
0
    def test_migrate(self):
        collection_cache = CollectionCache()
        for example in self.examples:
            path = example['path']
            d = STAC_IO.read_json(path)
            if identify_stac_object_type(d) == STACObjectType.ITEM:
                merge_common_properties(d,
                                        json_href=path,
                                        collection_cache=collection_cache)

            info = identify_stac_object(d)

            migrated_d = migrate_to_latest(d, info)

            migrated_info = identify_stac_object(migrated_d)

            self.assertEqual(migrated_info.object_type, info.object_type)
            self.assertEqual(
                migrated_info.version_range.latest_valid_version(),
                STAC_VERSION)
            self.assertEqual(set(migrated_info.common_extensions),
                             set(info.common_extensions))
            self.assertEqual(set(migrated_info.custom_extensions),
                             set(info.custom_extensions))

            # Test that PySTAC can read it without errors.
            self.assertIsInstance(
                STAC_IO.stac_object_from_dict(migrated_d, href=path),
                STACObject)