-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstac_prepare_iceland.py
94 lines (73 loc) · 3.11 KB
/
stac_prepare_iceland.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
import os
import json
import pystac
import requests
DATA_DIRECTORY = "/home/user/Code/devseed/static-stac/sentinel2-l2a-iceland"
catalog_path = os.path.join(DATA_DIRECTORY, 'catalog.json')
# Load the catalog and its collection using pystac
try:
catalog = pystac.Catalog.from_file(catalog_path)
print("Catalog loaded successfully:")
# Iterate over collections in the catalog
for collection in catalog.get_collections():
print(f"Loading collection: {collection.id}")
# Construct the path to the collection.json file
collection_path = os.path.join(DATA_DIRECTORY, collection.id, 'collection.json')
# Load the collection
collection_data = pystac.Collection.from_file(collection_path)
print(f"Collection loaded successfully: {collection_data.id}")
# Create a directory for items if it doesn't exist
items_directory = os.path.join(DATA_DIRECTORY, collection.id)
os.makedirs(items_directory, exist_ok=True)
except Exception as e:
print(f"Error loading catalog: {e}")
# Obtain items from Earth Search API
url = "https://earth-search.aws.element84.com/v1/search"
payload = {
"collections": ["sentinel-2-l2a"],
"bbox": [-24.95, 63.38, -13.99, 66.56],
"datetime": "2023-01-01T00:00:00Z/2023-12-31T23:59:59Z",
"limit": 500,
"query": {
"eo:cloud_cover": {
"lt": 5
}
}
}
# Make the POST request
response = requests.post(url, json=payload)
# Check if the request was successful
if response.status_code == 200:
data = response.json()
items_list = {
"type": "FeatureCollection",
"features": []
}
# Process the results with pystac
for item in data.get('features', []):
stac_item = pystac.Item.from_dict(item)
print(f"Fetched Item ID: {stac_item.id}, Date: {stac_item.datetime}")
# Filter out assets with 'jp2' in their href
filtered_assets = {k: v for k, v in stac_item.assets.items() if 'jp2' not in v.href}
stac_item.assets = filtered_assets
# # Download assets
# for asset in stac_item.assets.values():
# print(f"Downloading asset: {asset.href}")
# asset_response = requests.get(asset.href)
# os.makedirs(os.path.join(items_directory, stac_item.id), exist_ok=True)
# asset_path = os.path.join(items_directory, stac_item.id, os.path.basename(asset.href))
# with open(asset_path, 'wb') as f:
# f.write(asset_response.content)
items_list["features"].append(stac_item.to_dict()) # Append STAC item to the features list
# Write items to items.json
items_json_path = os.path.join(items_directory, 'items.json')
with open(items_json_path, 'w') as f:
# Write readible list of features
json.dump(items_list, f, indent=2)
# # Write each feature on a new line (pgstac wants that)
# for feature in items_list["features"]:
# json.dump(feature, f)
# f.write('\n')
print(f"Items saved to {items_json_path}")
else:
print(f"Request failed with status code {response.status_code}: {response.text}")