Beispiel #1
0
    def test_identify(self):
        collection_cache = {}
        for example in self.examples:
            path = example['path']
            d = STAC_IO.read_json(path)

            actual = identify_stac_object(d,
                                          merge_collection_properties=True,
                                          json_href=path,
                                          collection_cache=collection_cache)

            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 #2
0
def paginate(
    request: Request,
    next_resolver: Callable,
) -> Iterator[dict]:
    """

    Parameters
    ----------
    request : urllib.request.Request
        The initial request to start paging. Subsequent requests will be determined by the ``next_resolver``.
    next_resolver : Callable
        An callable that will be used to construct the request for the next page of results based on the ``"next"``
        link from the previous page.
    """
    while True:
        # Yield all items
        page = STAC_IO.read_json(request)
        yield page

        # Get the next link and make the next request
        next_link = next(
            (link for link in page.get('links', []) if link['rel'] == 'next'),
            None)
        if next_link is None:
            break
        request = next_resolver(next_link, request)
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_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)
Beispiel #5
0
        def validate_item_link_type(href, link_type, should_include_self):
            item_dict = STAC_IO.read_json(href)
            item = STACObject.from_file(href)
            for link in item.get_links():
                if not link.rel == 'self':
                    self.assertEqual(link.link_type, link_type)

            rels = set([link['rel'] for link in item_dict['links']])
            self.assertEqual('self' in rels, should_include_self)
Beispiel #6
0
    def test_from_file_pre_081(self):
        d = STAC_IO.read_json(self.label_example_1_uri)

        d['properties']['label:property'] = d['properties']['label:properties']
        d['properties'].pop('label:properties')
        d['properties']['label:overview'] = d['properties']['label:overviews']
        d['properties'].pop('label:overviews')
        d['properties']['label:method'] = d['properties']['label:methods']
        d['properties'].pop('label:methods')
        d['properties']['label:task'] = d['properties']['label:tasks']
        d['properties'].pop('label:tasks')
        label_example_1 = LabelItem.from_dict(d)

        self.assertEqual(len(label_example_1.label_tasks), 1)
Beispiel #7
0
    def test_from_file_pre_081(self):
        d = STAC_IO.read_json(self.label_example_1_uri)

        d['stac_version'] = '0.8.0-rc1'
        d['properties']['label:property'] = d['properties']['label:properties']
        d['properties'].pop('label:properties')
        d['properties']['label:overview'] = d['properties']['label:overviews']
        d['properties'].pop('label:overviews')
        d['properties']['label:method'] = d['properties']['label:methods']
        d['properties'].pop('label:methods')
        d['properties']['label:task'] = d['properties']['label:tasks']
        d['properties'].pop('label:tasks')
        label_example_1 = STAC_IO.stac_object_from_dict(d)

        self.assertEqual(len(label_example_1.ext.label.label_tasks), 2)
Beispiel #8
0
        def validate_catalog_link_type(href, link_type, should_include_self):
            cat_dict = STAC_IO.read_json(href)
            cat = STACObject.from_file(href)
            for link in cat.get_links():
                if not link.rel == 'self':
                    self.assertEqual(link.link_type, link_type)

            rels = set([link['rel'] for link in cat_dict['links']])
            self.assertEqual('self' in rels, should_include_self)

            for child_link in cat.get_child_links():
                child_href = make_absolute_href(child_link.target, href)
                validate_catalog_link_type(child_href, link_type,
                                           catalog_type == CatalogType.ABSOLUTE_PUBLISHED)

            for item_link in cat.get_item_links():
                item_href = make_absolute_href(item_link.target, href)
                validate_item_link_type(item_href, link_type,
                                        catalog_type == CatalogType.ABSOLUTE_PUBLISHED)
Beispiel #9
0
 def validate_file(self, path, object_type):
     d = STAC_IO.read_json(path)
     return self.schema_validator.validate_dict(d, object_type, print_on_error=True)
Beispiel #10
0
 def validate_file(self, path, object_type):
     d = STAC_IO.read_json(path)
     return validate_dict(d, object_type)