Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'eo:instrument': 'WorldView3'
}
collection = Collection(id='test',
description='test',
extent=collection_extent,
properties=common_properties,
license='CC-BY-SA-4.0')
collection.add_items([item1, item2])
with TemporaryDirectory() as tmp_dir:
collection.normalize_hrefs(tmp_dir)
collection.save(catalog_type=CatalogType.SELF_CONTAINED)
read_col = Collection.from_file('{}/collection.json'.format(tmp_dir))
items = list(read_col.get_all_items())
self.assertEqual(len(items), 2)
self.assertIsInstance(items[0], EOItem)
self.assertIsInstance(items[1], EOItem)
def test_can_add_custom_extension(self):
prev_extensions = pystac.STAC_EXTENSIONS.get_registered_extensions()
pystac.STAC_EXTENSIONS.add_extension(
ExtensionDefinition("test", [
ExtendedObject(Catalog, TestCatalogExt),
ExtendedObject(Collection, TestCollectionExt),
ExtendedObject(Item, TestItemExt)
]))
try:
cat = TestCases.test_case_2()
col = cat.get_child('1a8c1632-fa91-4a62-b33e-3a87c2ebdf16')
item = next(cat.get_all_items())
cat.ext.enable("test")
col.ext.enable("test")
item.ext.enable("test")
self.assertEqual(cat.ext.test.test_id, cat.id)
self.assertEqual(col.ext.test.xmin, col.extent.spatial.bboxes[0][0])
self.assertEqual(item.ext.test.asset_keys, set(item.assets))
def test_single_file(self):
with TemporaryDirectory() as tmp_dir:
sf_from_file = SingleFileSTAC.from_file(self.EXAMPLE_SINGLE_FILE)
sf_from_dict = SingleFileSTAC.from_dict(self.EXAMPLE_SF_DICT)
sf_empty = SingleFileSTAC()
single_file_stacs = [sf_from_file, sf_from_dict, sf_empty]
for i, sf in enumerate(single_file_stacs):
for feature in sf.get_items():
self.assertIsInstance(feature, Item)
for collection in sf.collections:
self.assertIsInstance(collection, Collection)
self.assertIsInstance(sf.to_dict(), dict)
dk = ['type', 'features', 'collections', 'links']
if sf.search:
self.assertIsInstance(sf.search, Search)
dk.append('search')
keys = list(sf.to_dict().keys())
self.assertEqual(set(keys), set(dk))
tmp_uri = join(tmp_dir, 'test-single-file-{}.json'.format(i))
sf.save(tmp_uri)
self.assertTrue(isfile(tmp_uri))
Band(name='Near-IR2', description='Near-IR2: 860 - 1040 nm', common_name='nir09')
]
spatial_extent = SpatialExtent(bboxes=[RANDOM_BBOX])
temporal_extent = TemporalExtent(intervals=[[item1.datetime, None]])
collection_extent = Extent(spatial=spatial_extent, temporal=temporal_extent)
common_properties = {
'eo:bands': [b.to_dict() for b in wv3_bands],
'eo:gsd': 0.3,
'eo:platform': 'Maxar',
'eo:instrument': 'WorldView3'
}
collection = Collection(id='test',
description='test',
extent=collection_extent,
properties=common_properties,
license='CC-BY-SA-4.0')
collection.add_items([item1, item2])
with TemporaryDirectory() as tmp_dir:
collection.normalize_hrefs(tmp_dir)
collection.save(catalog_type=CatalogType.SELF_CONTAINED)
read_col = Collection.from_file('{}/collection.json'.format(tmp_dir))
items = list(read_col.get_all_items())
self.assertEqual(len(items), 2)
self.assertIsInstance(items[0], EOItem)
def test_collections_cache_correctly(self):
catalogs = TestCases.all_test_catalogs()
for cat in catalogs:
with MockStacIO() as mock_io:
expected_collection_reads = set([])
for root, children, items in cat.walk():
if isinstance(root, Collection):
expected_collection_reads.add(root.get_self_href())
# Iterate over items to make sure they are read
self.assertNotEqual(list(items), None)
call_uris = [
call[0][0] for call in mock_io.read_text_method.call_args_list
if call[0][0] in expected_collection_reads
]
for collection_uri in expected_collection_reads:
calls = len([x for x in call_uris if x == collection_uri])
self.assertEqual(
calls, 1,
'{} was read {} times instead of once!'.format(collection_uri, calls))
import jsonschema
from jsonschema.validators import RefResolver
from pystac import (Catalog, Collection, Item, ItemCollection, LabelItem, STAC_VERSION, STAC_IO,
EOItem, SingleFileSTAC)
class SchemaValidator:
REPO = 'https://raw.githubusercontent.com/radiantearth/stac-spec'
TAG = 'v{}'.format(STAC_VERSION)
SCHEMA_BASE_URI = '{}/{}'.format(REPO, TAG)
schemas = {
Catalog:
'catalog-spec/json-schema/catalog.json',
Collection:
'collection-spec/json-schema/collection.json',
Item:
'item-spec/json-schema/item.json',
ItemCollection:
'item-spec/json-schema/itemcollection.json',
LabelItem:
'extensions/label/schema.json',
EOItem:
'extensions/eo/json-schema/schema.json',
# TODO: Move off of custom schema once schema in spec is fixed.
SingleFileSTAC: ('https://raw.githubusercontent.com/lossyrob/stac-spec/'
'0.8.1/refactor-extension-schemas/extensions/'
'single-file-stac/json-schema/single-file.json')
}
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)
return ItemCollection.from_dict(d)
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)
collection_link = next((link for link in links if link['rel'] == 'collection'), None)
if collection_link is not None:
collection_href = collection_link['href']
if json_href is not None:
collection_href = make_absolute_href(collection_href, json_href)
if collection_cache is not None:
collection = collection_cache.get_by_href(collection_href)
if collection is None:
collection = STAC_IO.read_json(collection_href)
if collection is not None:
collection_id = None
collection_props = None
if isinstance(collection, Collection):
collection_id = collection.id
collection_props = collection.properties
else:
collection_id = collection['id']
if 'properties' in collection:
collection_props = collection['properties']
if collection_props is not None:
for k in collection_props:
if k not in item_dict['properties']:
properties_merged = True
item_dict['properties'][k] = collection_props[k]
if collection_cache is not None and not collection_cache.contains_id(collection_id):
collection_cache.cache(collection, href=collection_href)