Ejemplo n.º 1
0
    def test_item_collection_features(self):
        ic = ItemCollection.from_file(self.IC_URI)
        self.assertIsInstance(ic, ItemCollection)

        ic_json = deepcopy(self.IC_DICT)

        self.assertEqual(ic_json.keys(), ic.to_dict().keys())
        self.assertEqual(ic_json['links'],
                         ic.to_dict(include_self_link=True)['links'])
        self.assertNotEqual(ic_json['links'],
                            ic.to_dict(include_self_link=False)['links'])

        for item in ic.get_items():
            self.assertIsInstance(item, Item)

        for link in ic.links:
            self.assertIsInstance(link, Link)

        href = ic.get_self_href()
        self.assertEqual(href,
                         'http://stacspec.org/sample-item-collection.json')

        ic_empty = ItemCollection([])
        self.assertIsInstance(ic_empty, ItemCollection)
        self.assertEqual(len(ic_empty.links), 0)
Ejemplo n.º 2
0
 def test_validate_item_collection(self):
     sv = SchemaValidator()
     ic_1 = ItemCollection([])
     sv.validate_object(ic_1)
     sv.validate_dict(self.IC_DICT, STACObjectType.ITEMCOLLECTION)
     ic_2 = ItemCollection.from_file(self.IC_URI)
     ic_val_dict = ic_2.to_dict()
     ic_val_dict['features'] = 'not an array'
     with self.assertRaises(STACValidationError):
         sv.validate_dict(ic_val_dict, STACObjectType.ITEMCOLLECTION)
Ejemplo n.º 3
0
 def test_validate_item_collection(self):
     sv = SchemaValidator()
     ic_1 = ItemCollection([])
     sv.validate_object(ic_1)
     sv.validate_dict(self.IC_DICT, ItemCollection)
     ic_2 = ItemCollection.from_file(self.IC_URI)
     ic_val_dict = ic_2.to_dict()
     ic_val_dict['features'] = 'not an array'
     with self.assertRaises(ValidationError):
         print('[Validation error expected] - ', end='')
         sv.validate_dict(ic_val_dict, ItemCollection)
Ejemplo n.º 4
0
def stac_object_from_dict(d, href=None, root=None):
    """Determines how to deserialize a dictionary into a STAC object.

    Args:
        d (dict): The dict to parse.
        href (str): Optional href that is the file location of the object being
            parsed.
        root (Catalog or Collection): Optional root of the catalog for this object.
            If provided, the root's resolved object cache can be used to search for
            previously resolved instances of the STAC object.

    Note: This is used internally in STAC_IO to deserialize STAC Objects.
    It is in the top level __init__ in order to avoid circular dependencies.
    """
    if identify_stac_object_type(d) == STACObjectType.ITEM:
        collection_cache = None
        if root is not None:
            collection_cache = root._resolved_objects.as_collection_cache()

        merge_common_properties(d, json_href=href, collection_cache=collection_cache)

    info = identify_stac_object(d)

    d = migrate_to_latest(d, info)

    if info.object_type == STACObjectType.CATALOG:
        return Catalog.from_dict(d, href=href, root=root)

    if info.object_type == STACObjectType.COLLECTION:
        return Collection.from_dict(d, href=href, root=root)

    if info.object_type == STACObjectType.ITEMCOLLECTION:
        if Extension.SINGLE_FILE_STAC in info.common_extensions:
            return SingleFileSTAC.from_dict(d, href=href, root=root)

        return ItemCollection.from_dict(d, href=href, root=root)

    if info.object_type == STACObjectType.ITEM:
        if Extension.EO in info.common_extensions:
            return EOItem.from_dict(d, href=href, root=root)

        if Extension.LABEL in info.common_extensions:
            return LabelItem.from_dict(d, href=href, root=root)

        return Item.from_dict(d, href=href, root=root)
Ejemplo n.º 5
0
    def test_minimal_item_collection(self):
        with TemporaryDirectory() as tmp_dir:
            path = os.path.join(tmp_dir, 'item_collection.json')
            ic = ItemCollection.from_file(self.IC_MINIMAL_URI)
            ic.set_self_href(path)
            self.assertIsInstance(ic, ItemCollection)
            self.assertEqual(len(ic.links), 1)
            self.assertEqual(ic.get_self_href(), path)
            self.assertEqual(len(ic.links), 1)

            ic.links = [
                Link(l.rel, join(tmp_dir, basename(l.target)))
                for l in ic.links
            ]
            ic.save()
            self.assertTrue(isfile(path))
            with open(path) as f:
                ic_val_dict = json.load(f)
            SchemaValidator().validate_dict(ic_val_dict, ItemCollection)