Skip to content

Commit

Permalink
test updates and updates for sat-search
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhanson committed Jun 11, 2020
1 parent 2bcad38 commit 1cb3617
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 54 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

[![CircleCI](https://circleci.com/gh/sat-utils/sat-stac.svg?style=svg&circle-token=ef97f3eea6cf901646fc2951e5a941686456b0da)](https://circleci.com/gh/sat-utils/sat-stac) [![PyPI version](https://badge.fury.io/py/sat-stac.svg)](https://badge.fury.io/py/sat-stac) [![codecov](https://codecov.io/gh/sat-utils/sat-stac/branch/master/graph/badge.svg)](https://codecov.io/gh/sat-utils/sat-stac)

This is a Python 3 library for working with [Spatio-Temporal Asset Catalogs (STAC)](https://github.com/radiantearth/stac-spec). It can be used to
This is a Python 3 library for reading and working with existing [Spatio-Temporal Asset Catalogs (STAC)](https://github.com/radiantearth/stac-spec). It can be used to

- Open and traverse STAC catalogs
- Download assets from STAC Items, using templated path names
- Save and load [Self-contained STAC catalogs](https://github.com/radiantearth/stac-spec/tree/v0.9.0-rc1/extensions/single-file-stac)
- Create and update catalogs*
- Add or remove a STAC Collections of Items from a catalog*

* sat-stac works for small catalogs or a small rate of updates. It is more useful for reading existing catalogs (either static or dynamic). For creating or large updating of catalogs, [PySTAC](https://github.com/azavea/pystac) is the better alternative.
The features for creating and updating existing catalogs in past versions have been removed in 0.4.0. If writing catalogs is needed, see [PySTAC](https://github.com/azavea/pystac).

## Installation

Expand Down
11 changes: 6 additions & 5 deletions satstac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def summaries(self):
def parent_catalog(self, item, path_template):
""" Given relative filename to a new Item find parent catalog """
cat = self
path = item.get_path(path_template)
path = item.get_path(os.path.dirname(path_template))
var_names = [v.strip('$').strip('{}') for v in utils.splitall(path_template)]
for i, d in enumerate(utils.splitall(path)):
fname = os.path.join(os.path.join(cat.path, d), 'catalog.json')
Expand All @@ -69,18 +69,17 @@ def parent_catalog(self, item, path_template):
cat = subcat
return cat.filename

def add_item(self, item, path_template='', filename_template='${id}.json'):
def add_item(self, item, filename_template='${id}.json'):
""" Add an item to this collection """
start = datetime.now()
if self.filename is None:
raise STACError('Save catalog before adding items')
item_link = os.path.join(item.get_path(os.path.join(path_template, filename_template)))
item_link = os.path.join(item.get_path(os.path.join(filename_template)))
item_fname = os.path.join(self.path, item_link)
item_path = os.path.dirname(item_fname)
root_link = self.links('root')[0]
#root_path = os.path.dirname(root_link)

parent = Catalog.open(self.parent_catalog(item, path_template))
parent = Catalog.open(self.parent_catalog(item, filename_template))

# create link to item
parent.add_link('item', os.path.relpath(item_fname, parent.path))
Expand All @@ -94,6 +93,8 @@ def add_item(self, item, path_template='', filename_template='${id}.json'):
item.add_link('collection', os.path.relpath(self.filename, item_path))

# save item

item.save(filename=item_fname)
logger.debug('Added %s in %s seconds' % (item.filename, datetime.now()-start))

return self
4 changes: 2 additions & 2 deletions test/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
import shutil

from satstac import __version__, Catalog, STACError, Item, config
from satstac import __version__, Catalog, STACError, Item


testpath = os.path.dirname(__file__)
Expand Down Expand Up @@ -44,7 +44,7 @@ def test_open(self):

def test_properties(self):
cat = self.get_catalog()
assert(cat.stac_version == config.STAC_VERSION)
assert(cat.stac_version == '1.0.0-beta.1')
assert(cat.description == 'An example STAC catalog')

def test_create(self):
Expand Down
6 changes: 2 additions & 4 deletions test/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ def test_add_item_with_subcatalogs(self):
col = Collection.open(os.path.join(testpath, 'catalog/eo/landsat-8-l1/catalog.json'))
cat.add_catalog(col)
item = Item.open(os.path.join(testpath, 'catalog/eo/landsat-8-l1/item.json'))
col.add_item(item, path_template='${landsat:path}/${landsat:row}',
filename_template='${date}/${id}.json')
col.add_item(item, filename_template='${landsat:path}/${landsat:row}/${date}/${id}.json')
assert(item.root().id == cat.id)
assert(item.collection().id == col.id)
# test code using existing catalogs
col.add_item(item, path_template='${landsat:path}/${landsat:row}',
filename_template='${date}/${id}.json')
col.add_item(item, filename_template='${landsat:path}/${landsat:row}/${date}/${id}.json')
assert(item.root().id == cat.id)
40 changes: 6 additions & 34 deletions test/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Test(unittest.TestCase):

path = os.path.join(testpath, 'test-item')
path_template = os.path.join(path, '${collection}/${id}')
filename_template = os.path.join(path, '${collection}/${id}')
filename = os.path.join(testpath, 'catalog/eo/landsat-8-l1/item.json')

@classmethod
Expand All @@ -21,11 +21,6 @@ def tearDownClass(cls):
if os.path.exists(cls.path):
shutil.rmtree(cls.path)

#@classmethod
#def setUpClass(cls):
# """ Configure testing class """
# config.DATADIR = os.path.join(testpath, config.DATADIR)

def test_init_without_collection(self):
with open(self.filename) as f:
data = json.loads(f.read())
Expand Down Expand Up @@ -83,49 +78,26 @@ def test_get_path(self):
def test_download_thumbnail(self):
""" Get thumbnail for item """
item = Item.open(self.filename)
fname = item.download(key='thumbnail', path_template=self.path_template)
fname = item.download(key='thumbnail', filename_template=self.filename_template)
assert(os.path.exists(fname))

def test_download(self):
""" Retrieve a data file """
item = Item.open(self.filename)
fname = item.download(key='MTL', path_template=self.path_template)
fname = item.download(key='MTL', filename_template=self.filename_template)
assert(os.path.exists(fname))
#fname = item.download(key='MTL', path_template=self.path_template)
#fname = item.download(key='MTL', filename_template=self.filename_template)
#assert(os.path.exists(fname))

def test_download_assets(self):
""" Retrieve multiple data files """
item = Item.open(self.filename)
fnames = item.download_assets(keys=['MTL', 'ANG'], path_template=self.path_template)
fnames = item.download_assets(keys=['MTL', 'ANG'], filename_template=self.filename_template)
for f in fnames:
assert(os.path.exists(f))

def test_download_nonexist(self):
""" Test downloading of non-existent file """
item = Item.open(self.filename)
fname = item.download(key='fake_asset', path_template=self.path_template)
fname = item.download(key='fake_asset', filename_template=self.filename_template)
assert(fname is None)

def _test_download_paths(self):
""" Testing of download paths and filenames """
item = Item.open(self.filename)
datadir = config.DATADIR
filename = config.FILENAME
config.DATADIR = os.path.join(testpath, '${date}')
config.FILENAME = '${date}_${id}'
fname = scene.download('MTL')
_fname = os.path.join(testpath, '2017-01-01/2017-01-01_testscene_MTL.txt')
assert(fname == _fname)
assert(os.path.exists(fname))
config.DATADIR = datadir
config.FILENAME = filename
shutil.rmtree(os.path.join(testpath, '2017-01-01'))
assert(os.path.exists(fname) == False)

def _test_create_derived(self):
""" Create single derived scene """
scenes = [self.get_test_scene(), self.get_test_scene()]
scene = Item.create_derived(scenes)
assert(scene.date == scenes[0].date)
assert(scene['c:id'] == scenes[0]['c:id'])
4 changes: 2 additions & 2 deletions test/test_itemcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_filter(self):
def test_download_assets(self):
""" Download multiple assets from all items """
items = self.load_items()
filenames = items.download_assets(keys=['MTL', 'ANG'], path_template=self.path)
filenames = items.download_assets(keys=['MTL', 'ANG'], filename_template=self.path)
assert(len(filenames) == 2)
for fnames in filenames:
assert(len(fnames) == 2)
Expand All @@ -112,7 +112,7 @@ def test_download(self):
""" Download a data file from all items """
items = self.load_items()

fnames = items.download(key='MTL', path_template=self.path)
fnames = items.download(key='MTL', filename_template=self.path)
assert(len(fnames) == 2)
for f in fnames:
assert(os.path.exists(f))
3 changes: 0 additions & 3 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
import unittest

#import satsearch.config as config

#from satsearch.scene import Scenes
from datetime import datetime
from satstac import utils

Expand Down

0 comments on commit 1cb3617

Please sign in to comment.