Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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))
def test_map_items_multiple_2(self):
catalog = Catalog(id='test-1', description='Test1')
item1 = Item(id='item1',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={})
item1.add_asset('ortho', Asset(href='/some/ortho.tif'))
catalog.add_item(item1)
kitten = Catalog(id='test-kitten', description='A cuter version of catalog')
catalog.add_child(kitten)
item2 = Item(id='item2',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={})
item2.add_asset('ortho', Asset(href='/some/other/ortho.tif'))
kitten.add_item(item2)
def modify_item_title(item):
item.title = 'Some new title'
return item
def create_label_item(item):
# Assumes the GEOJSON labels are in the
# same location as the image
img_href = item.assets['ortho'].href
label_href = '{}.geojson'.format(os.path.splitext(img_href)[0])
def test_full_copy_2(self):
with TemporaryDirectory() as tmp_dir:
cat = Catalog(id='test', description='test catalog')
image_item = Item(id='Imagery',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={})
for key in ['ortho', 'dsm']:
image_item.add_asset(
key, Asset(href='some/{}.tif'.format(key), media_type=MediaType.GEOTIFF))
label_item = Item(id='Labels',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={},
stac_extensions=[Extensions.LABEL])
label_ext = label_item.ext.label
label_ext.apply(
def test_full_copy_1(self):
with TemporaryDirectory() as tmp_dir:
cat = Catalog(id='test', description='test catalog')
item = Item(id='test_item',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={})
cat.add_item(item)
cat.normalize_hrefs(os.path.join(tmp_dir, 'catalog-full-copy-1-source'))
cat2 = cat.full_copy()
cat2.normalize_hrefs(os.path.join(tmp_dir, 'catalog-full-copy-1-dest'))
self.check_catalog(cat, 'source')
self.check_catalog(cat2, 'dest')
def test_eo_items_are_heritable(self):
item1 = Item(id='test-item-1',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={'key': 'one'},
stac_extensions=['eo'])
item2 = Item(id='test-item-2',
geometry=RANDOM_GEOM,
bbox=RANDOM_BBOX,
datetime=datetime.utcnow(),
properties={'key': 'two'},
stac_extensions=['eo'])
wv3_bands = [
Band(name='Coastal', description='Coastal: 400 - 450 nm', common_name='coastal'),
Band(name='Blue', description='Blue: 450 - 510 nm', common_name='blue'),
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')
}
for c in schemas:
if not schemas[c].startswith('https'):
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))
finally:
def test_to_from_dict(self):
with open(self.label_example_1_uri) as f:
label_example_1_dict = json.load(f)
test_to_from_dict(self, Item, label_example_1_dict)
def add_child(self, child, title=None):
"""Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`.
This method will set the child's parent to this object, and its root to
this Catalog's root.
Args:
child (Catalog or Collection): The child to add.
title (str): Optional title to give to the :class:`~pystac.Link`
"""
# Prevent typo confusion
if isinstance(child, pystac.Item):
raise STACError('Cannot add item as child. Use add_item instead.')
child.set_root(self.get_root())
child.set_parent(self)
self.add_link(Link.child(child, title=title))
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)