-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-catalog-and-links.py
74 lines (58 loc) · 2.43 KB
/
generate-catalog-and-links.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
import json
import os
import argparse
def create_stac_catalog(directory):
for subdir in os.listdir(directory):
subdir_path = os.path.join(directory, subdir)
if not os.path.isdir(subdir_path):
continue
# Locate collection.json
collection_file = os.path.join(subdir_path, "collection.json")
if not os.path.exists(collection_file):
print(f"Skipping {subdir}, no collection.json found.")
continue
# Load collection.json
with open(collection_file, 'r') as f:
collection = json.load(f)
# Create catalog.json structure
catalog = {
"stac_version": "1.1.0",
"id": subdir,
"type": "Catalog",
"description": collection.get("description", "STAC Catalog"),
"links": []
}
catalog["links"].append({
"rel": "child",
"href": "collection.json",
"type": "application/json",
"title": collection.get("title", "Collection")
})
# Process item files in the directory
item_files = [f for f in os.listdir(subdir_path) if
f.endswith(".json") and f != "collection.json" and f != "catalog.json" and f != "inventory.json"]
for item_file in item_files:
item_path = os.path.join(subdir_path, item_file)
with open(item_path, 'r') as f:
item = json.load(f)
# Add item link to collection
collection.setdefault("links", []).append({
"rel": "item",
"href": item_file,
"type": "application/json",
"title": item.get("id", "Item")
})
# Write updated collection.json
with open(collection_file, 'w') as f:
json.dump(collection, f, indent=2)
# Write catalog.json
catalog_output_path = os.path.join(subdir_path, "catalog.json")
with open(catalog_output_path, 'w') as f:
json.dump(catalog, f, indent=2)
print(f"STAC catalog updated in {subdir_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate STAC catalogs for multiple directories.")
parser.add_argument("directory",
help="Root directory containing subdirectories with collection.json and item JSON files")
args = parser.parse_args()
create_stac_catalog(args.directory)