-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from stac-utils/rde/fixes
Small fixes
- Loading branch information
Showing
11 changed files
with
167 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import pystac | ||
|
||
PLANET_EXTENSION_PREFIX = 'pl' | ||
|
||
PLANET_PROVIDER = pystac.Provider( | ||
name='Planet', | ||
description=( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os | ||
from tempfile import TemporaryDirectory | ||
|
||
import pystac | ||
|
||
from stactools.cli.commands.merge import create_merge_command | ||
from stactools.core import move_all_assets | ||
from tests.utils import (TestCases, CliTestCase) | ||
|
||
|
||
def copy_two_planet_disaster_subsets(tmp_dir): | ||
"""Test setup util that makes two copies of subset of the planet | ||
disaster data, each containing a single item. Updates the collection | ||
extents to match the single items. | ||
Returns a list of collection paths in the temporary directory. | ||
""" | ||
item_ids = ['20170831_172754_101c', '20170831_162740_ssc1d1'] | ||
new_cols = [] | ||
for item_id in item_ids: | ||
col = TestCases.planet_disaster() | ||
for item in list(col.get_all_items()): | ||
if item.id != item_id: | ||
item.get_parent().remove_item(item.id) | ||
col.update_extent_from_items() | ||
col.normalize_hrefs(os.path.join(tmp_dir, item_id)) | ||
col.save(catalog_type=pystac.CatalogType.SELF_CONTAINED) | ||
move_all_assets(col, copy=True) | ||
col.save() | ||
new_cols.append(col.get_self_href()) | ||
|
||
return new_cols | ||
|
||
|
||
class MergeTest(CliTestCase): | ||
def create_subcommand_functions(self): | ||
return [create_merge_command] | ||
|
||
def test_merge_moves_assets(self): | ||
with TemporaryDirectory() as tmp_dir: | ||
col_paths = copy_two_planet_disaster_subsets(tmp_dir) | ||
|
||
cmd = ['merge', '-a', col_paths[0], col_paths[1]] | ||
|
||
self.run_command(cmd) | ||
|
||
target_col = pystac.read_file(col_paths[1]) | ||
|
||
items = list(target_col.get_all_items()) | ||
self.assertEqual(len(items), 2) | ||
|
||
for item in items: | ||
for asset in item.assets.values(): | ||
self.assertEqual( | ||
os.path.dirname(asset.get_absolute_href()), | ||
os.path.dirname(item.get_self_href())) | ||
|
||
def test_merge_updates_collection_extent(self): | ||
with TemporaryDirectory() as tmp_dir: | ||
col_paths = copy_two_planet_disaster_subsets(tmp_dir) | ||
|
||
extent1 = pystac.read_file(col_paths[0]).extent | ||
extent2 = pystac.read_file(col_paths[1]).extent | ||
|
||
xmin = min( | ||
[extent1.spatial.bboxes[0][0], extent2.spatial.bboxes[0][0]]) | ||
time_max = max([ | ||
extent1.temporal.intervals[0][1], | ||
extent2.temporal.intervals[0][1], | ||
]) | ||
|
||
cmd = ['merge', col_paths[0], col_paths[1]] | ||
|
||
self.run_command(cmd) | ||
|
||
result_extent = pystac.read_file(col_paths[1]).extent | ||
|
||
def set_of_values(x): | ||
result = set([]) | ||
if type(x) is dict: | ||
result |= set_of_values(list(x.values())) | ||
elif type(x) is list: | ||
for e in x: | ||
if type(e) is list: | ||
result |= set_of_values(e) | ||
else: | ||
result.add(e) | ||
return result | ||
|
||
# Make sure it didn't just carry forward the old extent | ||
self.assertNotEqual(set_of_values(result_extent.spatial.bboxes), | ||
set_of_values(extent2.spatial.bboxes)) | ||
|
||
self.assertNotEqual( | ||
set_of_values(result_extent.temporal.intervals), | ||
set_of_values(extent2.temporal.intervals)) | ||
|
||
self.assertEqual(result_extent.spatial.bboxes[0][0], xmin) | ||
self.assertEqual(result_extent.temporal.intervals[0][1], time_max) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# flake8: noqa | ||
|
||
from tests.utils.test_cases import TestCases | ||
from tests.utils.cli_test import CliTestCase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from abc import (ABC, abstractmethod) | ||
import unittest | ||
|
||
import click | ||
from click.testing import CliRunner | ||
|
||
|
||
class CliTestCase(unittest.TestCase, ABC): | ||
def setUp(self): | ||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
for create_subcommand in self.create_subcommand_functions(): | ||
create_subcommand(cli) | ||
self.cli = cli | ||
|
||
def run_command(self, cmd): | ||
runner = CliRunner() | ||
return runner.invoke(self.cli, cmd, catch_exceptions=False) | ||
|
||
@abstractmethod | ||
def create_subcommand_functions(self): | ||
"""Return list of 'create_command' functions from implementations""" | ||
pass |