def test_multiple_extents(self): cat1 = TestCases.test_case_1() col1 = cat1.get_child('country-1').get_child('area-1-1') col1.validate() self.assertIsInstance(col1, Collection) validate_dict(col1.to_dict(), STACObjectType.COLLECTION) multi_ext_uri = TestCases.get_path('data-files/collections/multi-extent.json') with open(multi_ext_uri) as f: multi_ext_dict = json.load(f) validate_dict(multi_ext_dict, STACObjectType.COLLECTION) self.assertIsInstance(Collection.from_dict(multi_ext_dict), Collection) multi_ext_col = Collection.from_file(multi_ext_uri) multi_ext_col.validate() ext = multi_ext_col.extent extent_dict = multi_ext_dict['extent'] self.assertIsInstance(ext, Extent) self.assertIsInstance(ext.spatial.bboxes[0], list) self.assertEqual(len(ext.spatial.bboxes), 2) self.assertDictEqual(ext.to_dict(), extent_dict) cloned_ext = ext.clone() self.assertDictEqual(cloned_ext.to_dict(), multi_ext_dict['extent']) multi_ext_dict['extent']['spatial']['bbox'] = multi_ext_dict['extent']['spatial']['bbox'][0] invalid_col = Collection.from_dict(multi_ext_dict) with self.assertRaises(STACValidationError): invalid_col.validate()
def test_from_dict_preserves_dict(self) -> None: path = TestCases.get_path("data-files/collections/with-assets.json") with open(path) as f: collection_dict = json.load(f) param_dict = deepcopy(collection_dict) # test that the parameter is preserved _ = Collection.from_dict(param_dict) self.assertEqual(param_dict, collection_dict) # assert that the parameter is not preserved with # non-default parameter _ = Collection.from_dict(param_dict, preserve_dict=False) self.assertNotEqual(param_dict, collection_dict)
def test_multiple_extents(self) -> None: cat1 = TestCases.test_case_1() country = cat1.get_child("country-1") assert country is not None col1 = country.get_child("area-1-1") assert col1 is not None col1.validate() self.assertIsInstance(col1, Collection) validate_dict(col1.to_dict(), pystac.STACObjectType.COLLECTION) multi_ext_uri = TestCases.get_path( "data-files/collections/multi-extent.json") with open(multi_ext_uri) as f: multi_ext_dict = json.load(f) validate_dict(multi_ext_dict, pystac.STACObjectType.COLLECTION) self.assertIsInstance(Collection.from_dict(multi_ext_dict), Collection) multi_ext_col = Collection.from_file(multi_ext_uri) multi_ext_col.validate() ext = multi_ext_col.extent extent_dict = multi_ext_dict["extent"] self.assertIsInstance(ext, Extent) self.assertIsInstance(ext.spatial.bboxes[0], list) self.assertEqual(len(ext.spatial.bboxes), 2) self.assertDictEqual(ext.to_dict(), extent_dict) cloned_ext = ext.clone() self.assertDictEqual(cloned_ext.to_dict(), multi_ext_dict["extent"])
def test_from_dict_set_root(self) -> None: path = TestCases.get_path("data-files/examples/hand-0.8.1/collection.json") with open(path) as f: collection_dict = json.load(f) catalog = pystac.Catalog(id="test", description="test desc") collection = Collection.from_dict(collection_dict, root=catalog) self.assertIs(collection.get_root(), catalog)
def get_collections(self, collection_id=None, headers=None, **kwargs): """get all collections or get collection by ID :param collection_id: ID of collection (optional) :param headers: headers (optional) :param kwargs: search parameters (optional) :returns list with pystac.collections""" url = urljoin( self.url, f"collections/{collection_id}" if collection_id else "collections") res = self._handle_query(url=url, headers=headers, **kwargs) if isinstance(res, dict): res = res.get("collections", [res]) return [Collection.from_dict(c) for c in res]
def lambda_handler(event, context={}): logger.debug('Event: %s' % json.dumps(event)) # check if collection and if so, add to Cirrus if 'extent' in event: stac.add_collections([Collection.from_dict(event)]) # check if URL to catalog - ingest all collections if 'catalog_url' in event: collections = [] cat = Catalog.from_file(event['catalog_url']) for child in cat.get_children(): if isinstance(child, Collection): collections.append(child) stac.add_collections(collections)
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)