def test_set_asset_bands(self): eo_item = pystac.read_file( TestCases.get_path('data-files/eo/eo-landsat-example.json')) b1_asset = eo_item.assets['B1'] eo_item.ext.eo.set_asset_bands(b1_asset, ['B2']) eo_item_mod = Item.from_dict(eo_item.to_dict()) b1_asset_mod = eo_item_mod.assets['B1'] asset_bands = eo_item_mod.ext.eo.get_asset_bands(b1_asset_mod) self.assertIsNot(None, asset_bands) self.assertEqual(len(asset_bands), 1) self.assertEqual(asset_bands[0].name, 'B2') self.validator.validate_object(eo_item) # Check setting with invalid keys with self.assertRaises(KeyError): eo_item.ext.eo.set_asset_bands(b1_asset, ['BAD_KEY', 'BAD_KEY_2']) # Check adding a new asset asset = pystac.Asset(href="some/path.tif", media_type=pystac.MediaType.GEOTIFF) eo_item.ext.eo.set_asset_bands(asset, [b.name for b in eo_item.ext.eo.bands]) eo_item.add_asset("test", asset) self.assertEqual(eo_item.assets["test"].properties["eo:bands"], list(range(0, len(eo_item.ext.eo.bands))))
def test_datetime_ISO8601_format(self): item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) formatted_time = item.to_dict()['properties']['datetime'] self.assertEqual('2016-05-03T13:22:30.040000Z', formatted_time)
def test_asset_absolute_href(self): item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) rel_asset = Asset('./data.geojson') rel_asset.set_owner(item) expected_href = 'http://cool-sat.com/catalog/CS3-20160503_132130_04/data.geojson' actual_href = rel_asset.get_absolute_href() self.assertEqual(expected_href, actual_href)
def test_datetime_ISO8601_format(self) -> None: item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) formatted_time = item.to_dict()["properties"]["datetime"] self.assertEqual("2016-05-03T13:22:30.040000Z", formatted_time)
def test_asset_absolute_href(self) -> None: item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) rel_asset = Asset("./data.geojson") rel_asset.set_owner(item) expected_href = os.path.abspath("./data.geojson") actual_href = rel_asset.get_absolute_href() self.assertEqual(expected_href, actual_href)
def test_self_contained_item(self): item_dict = self.get_example_item_dict() item_dict['links'] = [ link for link in item_dict['links'] if link['rel'] == 'self' ] item = Item.from_dict(item_dict) self.assertIsInstance(item, Item) self.assertEqual(len(item.links), 1)
def test_asset_absolute_href_no_item_self(self) -> None: item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) assert item.get_self_href() is None rel_asset = Asset("./data.geojson") rel_asset.set_owner(item) actual_href = rel_asset.get_absolute_href() self.assertEqual(None, actual_href)
def process_stac_item(body: STACTranslateRequest): try: stac_item = Item.from_dict(body.item) return generate_cog(stac_item, body.destination_uri, body.target_assets) except Exception as exception: log.exception(exception) raise HTTPException(status_code=400, detail=f"{exception}")
def test_asset_absolute_href(self) -> None: item_path = TestCases.get_path("data-files/item/sample-item.json") item_dict = self.get_example_item_dict() item = Item.from_dict(item_dict) item.set_self_href(item_path) rel_asset = Asset("./data.geojson") rel_asset.set_owner(item) expected_href = os.path.abspath( os.path.join(os.path.dirname(item_path), "./data.geojson")) actual_href = rel_asset.get_absolute_href() self.assertEqual(expected_href, actual_href)
def test_self_contained_item(self): m = TestCases.get_path( 'data-files/itemcollections/sample-item-collection.json') with open(m) as f: item_dict = json.load(f)['features'][0] item_dict['links'] = [ link for link in item_dict['links'] if link['rel'] == 'self' ] item = Item.from_dict(item_dict) self.assertIsInstance(item, Item) self.assertEqual(len(item.links), 1)
def test_asset_absolute_href(self): m = TestCases.get_path( 'data-files/itemcollections/sample-item-collection.json') with open(m) as f: item_dict = json.load(f)['features'][0] item = Item.from_dict(item_dict) rel_asset = Asset('./data.geojson') rel_asset.set_owner(item) expected_href = 'http://cool-sat.com/catalog/CS3-20160503_132130_04/data.geojson' actual_href = rel_asset.get_absolute_href() self.assertEqual(expected_href, actual_href)
def test_datetime_ISO8601_format(self): m = TestCases.get_path( 'data-files/itemcollections/sample-item-collection.json') with open(m) as f: item_dict = json.load(f)['features'][0] item = Item.from_dict(item_dict) formatted_time = item.to_dict()['properties']['datetime'] self.assertEqual('2016-05-03T13:22:30.040000Z', formatted_time)
def test_to_from_dict(self) -> None: self.maxDiff = None item_dict = self.get_example_item_dict() param_dict = deepcopy(item_dict) assert_to_from_dict(self, Item, param_dict) item = Item.from_dict(param_dict) self.assertEqual(item.id, "CS3-20160503_132131_05") # test asset creation additional field(s) self.assertEqual( item.assets["analytic"].extra_fields["product"], "http://cool-sat.com/catalog/products/analytic.json", ) self.assertEqual(len(item.assets["thumbnail"].extra_fields), 0) # test that the parameter is preserved self.assertEqual(param_dict, item_dict) # assert that the parameter is not preserved with # non-default parameter _ = Item.from_dict(param_dict, preserve_dict=False) self.assertNotEqual(param_dict, item_dict)
def test_null_geometry(self) -> None: m = TestCases.get_path( "data-files/examples/1.0.0-beta.2/item-spec/examples/null-geom-item.json" ) with open(m) as f: item_dict = json.load(f) validate_dict(item_dict, pystac.STACObjectType.ITEM) item = Item.from_dict(item_dict) self.assertIsInstance(item, Item) item.validate() item_dict = item.to_dict() self.assertIsNone(item_dict["geometry"]) self.assertNotIn("bbox", item_dict)
def test_to_from_dict(self): self.maxDiff = None item_dict = self.get_example_item_dict() test_to_from_dict(self, Item, item_dict) item = Item.from_dict(item_dict) self.assertEqual( item.get_self_href(), 'http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json' ) # test asset creation additional field(s) self.assertEqual(item.assets['analytic'].properties['product'], 'http://cool-sat.com/catalog/products/analytic.json') self.assertEqual(len(item.assets['thumbnail'].properties), 0)
def test_null_geometry(self): m = TestCases.get_path( 'data-files/examples/1.0.0-beta.2/item-spec/examples/null-geom-item.json' ) with open(m) as f: item_dict = json.load(f) validate_dict(item_dict, STACObjectType.ITEM) item = Item.from_dict(item_dict) self.assertIsInstance(item, Item) item.validate() item_dict = item.to_dict() self.assertIsNone(item_dict['geometry']) with self.assertRaises(KeyError): item_dict['bbox']
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)
def test_to_from_dict(self): self.maxDiff = None m = TestCases.get_path( 'data-files/itemcollections/sample-item-collection.json') with open(m) as f: item_dict = json.load(f)['features'][0] test_to_from_dict(self, Item, item_dict) item = Item.from_dict(item_dict) self.assertEqual( item.get_self_href(), 'http://cool-sat.com/catalog/CS3-20160503_132130_04/CS3-20160503_132130_04.json' ) # test asset creation additional field(s) self.assertEqual(item.assets['analytic'].properties['product'], 'http://cool-sat.com/catalog/products/analytic.json') self.assertEqual(len(item.assets['thumbnail'].properties), 0)
def validate_item( item_doc: Dict, allow_cached_specs: bool = True, ): """ Validate a document against the Stac Item schema and its declared extensions Requires an internet connection the first time to fetch the relevant specs, but will cache them locally for repeated requests. :param item_doc: :param allow_cached_specs: Allow using a cached spec. Disable to force-download the spec again. :raises NoAvailableSchemaError: When cannot find a spec for the given Stac version+extentions """ # Pystac now does this for us, but we'll keep this method for backwards compat. item = Item.from_dict(item_doc) item.validate()
def get_sherlock_stac(stac_id, auth=None): if not auth: if STAC_USERNAME and STAC_PASSWORD and LOGIN_URL: token = request_auth(STAC_USERNAME, STAC_USERNAME) auth = 'Bearer {0}'.format(token.get('access_token')) req = requests.get(join(STAC_API, 'search?ids={0}'.format(stac_id)), headers={'Authorization': auth}) resp = req.json() features = resp.get('features') if features is None: raise HTTPException(400, 'Sherlock failed to retrieve results.') if len(features) == 0: raise HTTPException(400, 'Sherlock ID did not match any records.') elif len(features) > 1: raise HTTPException(500, 'Sherlock ID returned ambiguous results.') stac_item = features[0] if not 'stac_version' in stac_item: stac_item['stac_version'] = 'v1.0.0-beta.2]' return Item.from_dict(features[0])
async def query_items_page(collection, url, client, semaphore, params={}, item_template="${year}/${month}/${day}"): page = params.get('page', 1) strategy = layout.BestPracticesLayoutStrategy() colpath = os.path.dirname(collection.get_self_href()) _root_path = os.path.dirname(collection.get_root().get_self_href()) payload_str = urlencode(params, safe='/') async with semaphore: logger.debug(f"{dt.datetime.now()}: Requesting page {page}") try: resp = await client.get(url, params=payload_str) except: resp = await client.get(url, params=payload_str) items = resp.json()['features'] logger.debug(f"{dt.datetime.now()}: Retrieved page {page}") [collection.add_item(Item.from_dict(i)) for i in items] return len(items)
def test_validate_eo(self): self.validator.validate_dict(self.eo_dict, STACObjectType.ITEM) with open(self.URI_2) as f: eo_dict_2 = json.load(f) try: self.validator.validate_dict(eo_dict_2, STACObjectType.ITEM) except STACValidationError as e: self.assertTrue('extension eo' in str(e)) with TemporaryDirectory() as tmp_dir: cat_dir = os.path.join(tmp_dir, 'catalog') catalog = TestCases.test_case_1() eo_item = Item.from_dict(self.eo_dict) catalog.add_item(eo_item) catalog.normalize_and_save( cat_dir, catalog_type=CatalogType.ABSOLUTE_PUBLISHED) cat_read = Catalog.from_file(os.path.join(cat_dir, 'catalog.json')) eo_item_read = cat_read.get_item( "LC08_L1TP_107018_20181001_20181001_01_RT") self.assertTrue(eo_item_read.ext.implements('eo')) self.validator.validate_object(eo_item_read)
def default_item_method(feature): return Item.from_dict(feature)
def test_from_dict_set_root(self) -> None: item_dict = self.get_example_item_dict() catalog = pystac.Catalog(id="test", description="test desc") item = Item.from_dict(item_dict, root=catalog) self.assertIs(item.get_root(), catalog)