forked from EOEPCA/stac-cat-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_stac_generator.py
280 lines (230 loc) · 15.1 KB
/
test_stac_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import datetime
import os
from pathlib import Path
from unittest import TestCase
from stac_cat_utils.stac import STACCollection
from stac_cat_utils.stac_generator import StacCatalogGenerator
from stac_cat_utils.utils import collection_to_assets
class TestCaseConfig(TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.stac_generator = StacCatalogGenerator()
cls.src_path = os.path.join(os.path.dirname(__file__), './../test_files')
cls.ignore_paths = [f'{cls.src_path}/products', f'{cls.src_path}/cube']
@staticmethod
def remove_output_folder(output_folder):
for root, dirs, files in os.walk(output_folder, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(output_folder)
class TestStacCatalogGenerator(TestCaseConfig):
def test_catalog_creation(self):
catalog = self.stac_generator.create(self.src_path, ignore_paths=self.ignore_paths)
collections = list(catalog.get_all_collections())
self.assertEqual(len(collections), 1, 'Only the generic collection should be created.')
self.assertEqual(len(collections[0].get_assets()), 7,
'Non Rio-Stac supported files should be assets of the generic collection')
items = list(collections[0].get_items())
self.assertEqual(len(items), 1)
self.assertEqual(items[0].id, 'test.png', 'Rio-Stac supported files should be generated as STAC Item.')
def test_simple_collections_path_arg(self):
catalog = self.stac_generator.create(self.src_path, collection_paths=[f'{self.src_path}/logs'],
ignore_paths=self.ignore_paths)
collections = list(catalog.get_collections())
expected_nb_files = {'files': 5, 'logs': 2}
for collection in collections:
self.assertEqual(len(collection.assets), expected_nb_files[collection.id])
def test_glob_collections_path_arg(self):
catalog = self.stac_generator.create(self.src_path, collection_paths=[f'{self.src_path}/**/*logs'],
ignore_paths=self.ignore_paths)
catalog_collections = list(catalog.get_collections())
expected_nb_files = {'files': 5, 'logs': 1, 'extra_logs': 1}
self.assertEqual(len(catalog_collections), 2)
self.assertEqual(set(map(lambda col: col.id, catalog_collections)), {'logs', 'files'})
for collection in catalog_collections:
self.assertEqual(len(collection.assets), expected_nb_files[collection.id])
if collection.id == 'extra_logs':
sub_collections = list(collection.get_collections())
self.assertEqual(len(sub_collections), 1)
self.assertEqual(len(sub_collections[0].assets), expected_nb_files[collection.id])
def test_simple_items_path_arg(self):
catalog = self.stac_generator.create(self.src_path, item_paths=[f'{self.src_path}/logs'],
ignore_paths=self.ignore_paths)
items = list(catalog.get_items())
self.assertEqual(len(items), 1, 'Logs folder should be created as a STAC Item')
self.assertEqual(items[0].id, 'logs')
def test_glob_items_path_arg(self):
catalog = self.stac_generator.create(self.src_path, item_paths=[f'{self.src_path}/**/*logs'],
ignore_paths=self.ignore_paths)
catalog_items = list(catalog.get_items())
self.assertEqual(len(catalog_items), 1)
self.assertEqual(catalog_items[0].id, 'logs')
self.assertEqual(len(catalog_items[0].assets), 2)
def test_simple_ignore_path_arg(self):
catalog = self.stac_generator.create(self.src_path, ignore_paths=[*self.ignore_paths,
f'{self.src_path}/logs',
Path(f'{self.src_path}/test.png')])
collections = list(catalog.get_all_collections())
self.assertEqual(len(collections), 1, 'Only the generic collection should be created.')
self.assertEqual(len(collections[0].assets), 5)
items = list(catalog.get_items())
self.assertEqual(len(items), 0, 'png Item should not be created by Rio-Stac')
def test_glob_ignore_path_arg(self):
catalog = self.stac_generator.create(self.src_path, ignore_paths=[*self.ignore_paths,
f'{self.src_path}/**/*.log'])
collections = list(catalog.get_all_collections())
self.assertEqual(len(collections), 1, 'Only the generic collection should be created.')
self.assertEqual(len(collections[0].assets), 4, 'log files should be ignored.')
catalog = self.stac_generator.create(self.src_path, ignore_paths=[*self.ignore_paths,
f'{self.src_path}/**/test.*'])
collections = list(catalog.get_all_collections())
self.assertEqual(len(collections), 0, 'No generic collection should be created.')
def test_product_are_recognized(self):
src_path = os.path.join(self.src_path, 'products')
catalog = self.stac_generator.create(src_path)
items = list(catalog.get_items())
self.assertEqual(len(items), 6, 'Product folders should be created as a STAC Item.')
def test_asset_href_prefix(self):
prefix = 'test_prefix'
catalog = self.stac_generator.create(self.src_path,
collection_paths=[f'{self.src_path}/logs'],
item_paths=[f'{self.src_path}/logs/extra_logs'],
asset_href_prefix=prefix)
assets = {}
for collection in catalog.get_all_collections():
assets = {**assets, **collection_to_assets(collection)}
for item in catalog.get_all_items():
assets = {**assets, **item.assets}
for title, asset in assets.items():
self.assertTrue(asset.href.startswith(prefix))
def test_save_catalog(self):
folder_output = 'test_catalog'
self.stac_generator.create(self.src_path,
collection_paths=[f'{self.src_path}/logs'],
item_paths=[f'{self.src_path}/logs/extra_logs'])
self.stac_generator.save(dest_path=folder_output)
self.assertTrue(os.path.exists(f'{folder_output}/catalog.json'), 'catalog folder should exist')
self.assertTrue(os.path.exists(f'{folder_output}/files/collection.json'),
'generic collection folder should exist')
self.assertTrue(os.path.exists(f'{folder_output}/files/test.png/test.png.json'),
'png item folder and json should exist')
self.assertTrue(os.path.exists(f'{folder_output}/logs/collection.json'),
'logs collection should exist')
self.assertTrue(os.path.exists(f'{folder_output}/logs/extra_logs/extra_logs.json'),
'extra_logs item should exist')
self.remove_output_folder(folder_output)
class TestDatacubeGeneration(TestCaseConfig):
def test_datacube_compliant_collection(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
catalog.make_datacube_compliant()
cube_collection = list(catalog.get_collections())[0]
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('x', cube_collection.extra_fields['cube:dimensions'])
self.assertIn('y', cube_collection.extra_fields['cube:dimensions'])
self.assertIn('time', cube_collection.extra_fields['cube:dimensions'])
self.assertIn('spectral', cube_collection.extra_fields['cube:dimensions'])
def test_not_datacube_compliant_collection(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/not_cube_collection'],
item_paths=[f'{self.src_path}/cube/not_cube_collection/test.jpg'])
catalog.make_datacube_compliant()
cube_collection = list(catalog.get_collections())[0]
self.assertNotIn('cube:dimensions', cube_collection.extra_fields)
def test_collection_horizontal_dimension(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
cube_collection: STACCollection = list(catalog.get_collections())[0]
self.assertNotIn('cube:dimensions', cube_collection.extra_fields)
extent = [33, 36]
cube_collection.add_horizontal_dimension('x_axis', axis='x', extent=extent)
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('x_axis', cube_collection.extra_fields['cube:dimensions'])
self.assertEqual(cube_collection.extra_fields["cube:dimensions"]["x_axis"]["extent"], extent)
def test_collection_vertical_dimension(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
cube_collection: STACCollection = list(catalog.get_collections())[0]
self.assertNotIn('cube:dimensions', cube_collection.extra_fields)
extent = [34, 37]
cube_collection.add_vertical_dimension('z_axis', extent=extent)
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('z_axis', cube_collection.extra_fields['cube:dimensions'])
self.assertEqual(cube_collection.extra_fields["cube:dimensions"]["z_axis"]["extent"], extent)
def test_collection_additional_dimension(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
cube_collection: STACCollection = list(catalog.get_collections())[0]
self.assertNotIn('cube:dimensions', cube_collection.extra_fields)
values = ['ex1', 'ex2']
dim_type = "test"
cube_collection.add_additional_dimension('extra', type=dim_type , values=values)
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('extra', cube_collection.extra_fields['cube:dimensions'])
self.assertEqual(cube_collection.extra_fields["cube:dimensions"]["extra"]["type"], dim_type)
self.assertEqual(cube_collection.extra_fields["cube:dimensions"]["extra"]["values"], values)
def test_collection_temporal_dimension(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
cube_collection: STACCollection = list(catalog.get_collections())[0]
self.assertNotIn('cube:dimensions', cube_collection.extra_fields)
end = datetime.datetime.now()
start = end - datetime.timedelta(days=1)
extent = [start.isoformat(), end.isoformat()]
cube_collection.add_temporal_dimension('time', extent=extent)
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('time', cube_collection.extra_fields['cube:dimensions'])
self.assertEqual(cube_collection.extra_fields["cube:dimensions"]["time"]["extent"], extent)
def test_collection_variable_dimension(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
cube_collection: STACCollection = list(catalog.get_collections())[0]
self.assertNotIn('cube:variables', cube_collection.extra_fields)
cube_collection.add_dimension_variable('a_variable', type='data', values=['test', 'test1'])
self.assertIn('cube:variables', cube_collection.extra_fields)
self.assertIn('a_variable', cube_collection.extra_fields['cube:variables'])
def test_overwrite_temporal_dimension_in_datacube_compliant_collection(self):
catalog = self.stac_generator.create(f'{self.src_path}/cube',
collection_paths=[f'{self.src_path}/cube/cube_collection'])
catalog.make_datacube_compliant()
cube_collection = list(catalog.get_collections())[0]
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('x', cube_collection.extra_fields['cube:dimensions'])
self.assertIn('y', cube_collection.extra_fields['cube:dimensions'])
self.assertIn('time', cube_collection.extra_fields['cube:dimensions'])
self.assertIn('spectral', cube_collection.extra_fields['cube:dimensions'])
# new extent for this test
end = datetime.datetime.now()
start = end - datetime.timedelta(days=1)
extent = [start.isoformat(), end.isoformat()]
# Try to add without replace
cube_collection.add_temporal_dimension('time', extent=extent, replace=False)
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('time', cube_collection.extra_fields['cube:dimensions'])
# Verify that the dimension has not been updated
self.assertNotEqual(cube_collection.extra_fields["cube:dimensions"]["time"]["extent"], extent)
# Add with replace
cube_collection.add_temporal_dimension('time', extent=extent, replace=True)
self.assertIn('cube:dimensions', cube_collection.extra_fields)
self.assertIn('time', cube_collection.extra_fields['cube:dimensions'])
# Verify that the dimension was replaced
self.assertEqual(cube_collection.extra_fields["cube:dimensions"]["time"]["extent"], extent)
def test_save_datacube_compliant_collection(self):
folder_output = 'test_catalog'
catalog = self.stac_generator.create(self.src_path,
collection_paths=[f'{self.src_path}/logs'],
item_paths=[f'{self.src_path}/logs/extra_logs'])
catalog.make_datacube_compliant()
self.stac_generator.save(dest_path=folder_output)
self.assertTrue(os.path.exists(f'{folder_output}/catalog.json'), 'catalog folder should exist')
self.assertTrue(os.path.exists(f'{folder_output}/files/collection.json'),
'generic collection folder should exist')
self.assertTrue(os.path.exists(f'{folder_output}/files/test.png/test.png.json'),
'png item folder and json should exist')
self.assertTrue(os.path.exists(f'{folder_output}/logs/collection.json'),
'logs collection should exist')
self.assertTrue(os.path.exists(f'{folder_output}/logs/extra_logs/extra_logs.json'),
'extra_logs item should exist')
self.remove_output_folder(folder_output)