Example #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")
Example #2
0
    def get_extension_schema_uri(cls, extension_id: str,
                                 object_type: STACObjectType,
                                 stac_version: STACVersionID) -> Optional[str]:
        uri = None

        is_latest = stac_version == pystac.get_stac_version()

        ext_map = cls.get_schema_map()
        if extension_id in ext_map:
            if ext_map[extension_id][0] and object_type in ext_map[
                    extension_id][0]:
                uri = ext_map[extension_id][0][object_type]

            if not is_latest:
                if ext_map[extension_id][1]:
                    for version_range, ext_uris in ext_map[extension_id][1]:
                        if version_range.contains(stac_version):
                            if object_type in ext_uris:
                                uri = ext_uris[object_type]
                                break

        if uri is None:
            return uri
        else:
            return cls._append_base_uri_if_needed(uri, stac_version)
Example #3
0
    def to_dict(
        self, include_self_link: bool = True, transform_hrefs: bool = True
    ) -> Dict[str, Any]:
        links = self.links
        if not include_self_link:
            links = [x for x in links if x.rel != pystac.RelType.SELF]

        d: Dict[str, Any] = {
            "type": self.STAC_OBJECT_TYPE.value.title(),
            "id": self.id,
            "stac_version": pystac.get_stac_version(),
            "description": self.description,
            "links": [link.to_dict(transform_href=transform_hrefs) for link in links],
        }

        if self.stac_extensions is not None:
            d["stac_extensions"] = self.stac_extensions

        for key in self.extra_fields:
            d[key] = self.extra_fields[key]

        if self.title is not None:
            d["title"] = self.title

        return d
Example #4
0
    def to_dict(self, include_self_link=True):
        links = self.links
        if not include_self_link:
            links = filter(lambda x: x.rel != 'self', links)

        assets = dict(map(lambda x: (x[0], x[1].to_dict()), self.assets.items()))

        if self.datetime is not None:
            self.properties['datetime'] = datetime_to_str(self.datetime)
        else:
            self.properties['datetime'] = None

        d = {
            'type': 'Feature',
            'stac_version': pystac.get_stac_version(),
            'id': self.id,
            'properties': self.properties,
            'geometry': self.geometry,
            'links': [link.to_dict() for link in links],
            'assets': assets
        }

        if self.bbox is not None:
            d['bbox'] = self.bbox

        if self.stac_extensions is not None:
            d['stac_extensions'] = self.stac_extensions

        if self.collection_id:
            d['collection'] = self.collection_id

        for key in self.extra_fields:
            d[key] = self.extra_fields[key]

        return deepcopy(d)
Example #5
0
    def test_migrate(self):
        collection_cache = CollectionCache()
        for example in self.examples:
            with self.subTest(example['path']):
                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, info = 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())
                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.
                if info.object_type != STACObjectType.ITEMCOLLECTION:
                    self.assertIsInstance(
                        STAC_IO.stac_object_from_dict(migrated_d, href=path),
                        STACObject)
Example #6
0
 def test_override_stac_version_with_call(self):
     version = pystac.get_stac_version()
     try:
         override_version = '1.0.0-delta.2'
         pystac.set_stac_version(override_version)
         cat = TestCases.test_case_1()
         d = cat.to_dict()
         self.assertEqual(d['stac_version'], override_version)
     finally:
         if version == pystac.version.STACVersion.DEFAULT_STAC_VERSION:
             pystac.set_stac_version(None)
         else:
             pystac.set_stac_version(version)
Example #7
0
    def get_object_schema_uri(self, object_type: STACObjectType,
                              stac_version: str) -> Optional[str]:
        is_latest = stac_version == pystac.get_stac_version()

        if object_type not in self.DEFAULT_SCHEMA_MAP:
            raise KeyError("Unknown STAC object type {}".format(object_type))
        uri = self.DEFAULT_SCHEMA_MAP[object_type][0]
        if not is_latest:
            if self.DEFAULT_SCHEMA_MAP[object_type][1]:
                for version_range, range_uri in self.DEFAULT_SCHEMA_MAP[
                        object_type][1]:
                    if version_range.contains(stac_version):
                        uri = range_uri
                        break

        return self._append_base_uri_if_needed(uri, stac_version)
Example #8
0
    def get_core_schema_uri(self, object_type, stac_version):
        uri = None
        is_latest = stac_version == pystac.get_stac_version()

        if object_type not in self.DEFAULT_SCHEMA_MAP['core']:
            raise KeyError('Unknown STAC object type {}'.format(object_type))

        uri = self.DEFAULT_SCHEMA_MAP['core'][object_type][0]
        if not is_latest:
            if self.DEFAULT_SCHEMA_MAP['core'][object_type][1]:
                for version_range, range_uri in self.DEFAULT_SCHEMA_MAP[
                        'core'][object_type][1]:
                    if version_range.contains(stac_version):
                        uri = range_uri
                        break

        return self._append_base_uri_if_needed(uri, stac_version)
Example #9
0
    def to_dict(self, include_self_link=True):
        """Serializes an :class:`ItemCollection` instance to a JSON-like dictionary. """

        links = self.links
        if not include_self_link:
            links = filter(lambda l: l.rel != 'self', links)

        d = {
            'stac_version': pystac.get_stac_version(),
            'links': [link.to_dict() for link in links],
            **deepcopy(self.extra_fields)
        }

        if self.stac_extensions is not None:
            d['stac_extensions'] = self.stac_extensions

        return d
Example #10
0
    def to_dict(self,
                include_self_link: bool = True,
                transform_hrefs: bool = True) -> Dict[str, Any]:
        links = self.links
        if not include_self_link:
            links = [x for x in links if x.rel != pystac.RelType.SELF]

        assets = {k: v.to_dict() for k, v in self.assets.items()}

        if self.datetime is not None:
            self.properties["datetime"] = datetime_to_str(self.datetime)
        else:
            self.properties["datetime"] = None

        d: Dict[str, Any] = {
            "type":
            "Feature",
            "stac_version":
            pystac.get_stac_version(),
            "id":
            self.id,
            "properties":
            self.properties,
            "geometry":
            self.geometry,
            "links":
            [link.to_dict(transform_href=transform_hrefs) for link in links],
            "assets":
            assets,
        }

        if self.bbox is not None:
            d["bbox"] = self.bbox

        if self.stac_extensions is not None:
            d["stac_extensions"] = self.stac_extensions

        if self.collection_id:
            d["collection"] = self.collection_id

        for key in self.extra_fields:
            d[key] = self.extra_fields[key]

        return d
Example #11
0
def validate(stac_object):
    """Validates a :class:`~pystac.STACObject`.

    Args:
        stac_object (STACObject): The stac object to validate.

    Returns:
        List[Object]: List of return values from the validation calls for the
           core object and any extensions. Element type is specific to the
           STACValidator implementation.

    Raises:
        STACValidationError
    """
    validate_dict(stac_dict=stac_object.to_dict(),
                  stac_object_type=stac_object.STAC_OBJECT_TYPE,
                  stac_version=pystac.get_stac_version(),
                  extensions=stac_object.stac_extensions,
                  href=stac_object.get_self_href())
Example #12
0
    def to_dict(self, include_self_link=True):
        links = self.links
        if not include_self_link:
            links = filter(lambda l: l.rel != 'self', links)

        d = {
            'id': self.id,
            'stac_version': pystac.get_stac_version(),
            'description': self.description,
            'links': [link.to_dict() for link in links]
        }

        if self.stac_extensions is not None:
            d['stac_extensions'] = self.stac_extensions

        for key in self.extra_fields:
            d[key] = self.extra_fields[key]

        if self.title is not None:
            d['title'] = self.title

        return deepcopy(d)
Example #13
0
    def get_extension_schema_uri(self, extension_id, object_type,
                                 stac_version):
        uri = None

        is_latest = stac_version == pystac.get_stac_version()

        ext_map = self.DEFAULT_SCHEMA_MAP['extension']
        if extension_id in ext_map:
            if ext_map[extension_id][0] and \
               object_type in ext_map[extension_id][0]:
                uri = ext_map[extension_id][0][object_type]

            if not is_latest:
                if ext_map[extension_id][1]:
                    for version_range, ext_uris in ext_map[extension_id][1]:
                        if version_range.contains(stac_version):
                            if object_type in ext_uris:
                                uri = ext_uris[object_type]
                                break

        if uri is None:
            return uri
        else:
            return self._append_base_uri_if_needed(uri, stac_version)
Example #14
0
                filtered_links.append(link)
        js['links'] = filtered_links
    return js


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Get examples from the stac-spec repo.')
    parser.add_argument(
        'previous_version',
        metavar='PREVIOUS_VERSION',
        help='The previous STAC_VERSION that examples have already been pulled from.')

    args = parser.parse_args()

    stac_repo = 'https://github.com/radiantearth/stac-spec'
    stac_spec_tag = 'v{}'.format(pystac.get_stac_version())

    examples_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'examples'))

    with TemporaryDirectory() as tmp_dir:
        call(['git', 'clone', '--depth', '1', '--branch', stac_spec_tag, stac_repo, tmp_dir])

        example_dirs = []
        for root, _, _ in os.walk(tmp_dir):
            example_dirs.append(os.path.join(root))

        example_csv_lines = set([])

        for example_dir in example_dirs:
            for root, _, files in os.walk(example_dir):
                for fname in files:
Example #15
0
"""
Script to change the version property of the test files for PySTAC.
This is used when upgrading to a new version of STAC.
"""
import os
import re
import argparse
import json

import pystac

TARGET_VERSION = pystac.get_stac_version()


def migrate(path: str) -> None:
    try:
        with open(path) as f:
            stac_json = json.load(f)
    except json.decoder.JSONDecodeError:
        print(f"Cannot read {path}")
        raise

    if "stac_version" in stac_json:
        cur_ver = stac_json["stac_version"]
        if not cur_ver == TARGET_VERSION:
            print("  - Migrating {} from {} to {}...".format(
                path, cur_ver, TARGET_VERSION))
            obj = pystac.read_dict(stac_json, href=path)
            migrated = obj.to_dict(include_self_link=False)
            with open(path, "w") as f:
                json.dump(migrated, f, indent=2)
Example #16
0

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Get examples from the stac-spec repo.")
    parser.add_argument(
        "previous_version",
        metavar="PREVIOUS_VERSION",
        help=
        "The previous STAC_VERSION that examples have already been pulled from.",
    )

    args = parser.parse_args()

    stac_repo = "https://github.com/radiantearth/stac-spec"
    stac_spec_tag = "v{}".format(pystac.get_stac_version())

    examples_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "examples"))

    with tempfile.TemporaryDirectory() as tmp_dir:
        call([
            "git",
            "clone",
            "--depth",
            "1",
            "--branch",
            stac_spec_tag,
            stac_repo,
            tmp_dir,
        ])