-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstac_to_geoserver.py
177 lines (152 loc) · 7.04 KB
/
stac_to_geoserver.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
import json
import getpass
import argparse
from pathlib import Path
import requests
import pystac_client
from requests.auth import HTTPBasicAuth
from urllib.parse import urljoin
def json_convert(jsonfile):
"""
jsonfile: json file in dict format
A function to map the Sentinel-2 STAC jsonfiles into the GeoServer database layout.
There are different json layouts for Collections and Items. The function checks if the jsonfile is of type "Collection",
or of type "Feature" (=Item). A number of properties are hardcoded into Sentinel-2 metadata as these are not collected in the STAC jsonfiles.
"""
with open(jsonfile) as f:
content = json.load(f)
if content["type"] == "Collection":
new_json = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
content["extent"]["spatial"]["bbox"][0][2],
content["extent"]["spatial"]["bbox"][0][1]
],
[
content["extent"]["spatial"]["bbox"][0][2],
content["extent"]["spatial"]["bbox"][0][3]
],
[
content["extent"]["spatial"]["bbox"][0][0],
content["extent"]["spatial"]["bbox"][0][3]
],
[
content["extent"]["spatial"]["bbox"][0][0],
content["extent"]["spatial"]["bbox"][0][1]
],
[
content["extent"]["spatial"]["bbox"][0][2],
content["extent"]["spatial"]["bbox"][0][1]
]
]
]
},
"properties": {
"name": content["id"],
"title": content["title"],
"eo:identifier": content["id"],
"description": content["description"],
"timeStart": content["extent"]["temporal"]["interval"][0][0],
"timeEnd": content["extent"]["temporal"]["interval"][0][1],
"primary": True,
"license": content["license"],
"licenseLink" : {
"href" : "https://sentinel.esa.int/documents/247904/690755/Sentinel_Data_Legal_Notice",
"rel" : "license",
"type" : "application/json"
},
"assets": content["assets"],
"licenseLink": None,
"summaries": content["summaries"],
"queryables": [
"eo:identifier",
"eo:cloud_cover"
]
}
}
if "assets" in content:
new_json["properties"]["assets"] = content["assets"]
for link in content["links"]:
if link["rel"] == "license":
new_json["properties"]["licenseLink"] = {
"href": link["href"],
"rel": "license",
"type": "application/json"
} # New License URL link
if content["type"] == "Feature":
new_json = {
"type": "Feature",
"geometry": content["geometry"],
"properties": {
"eop:identifier": content["id"],
"eop:parentIdentifier": content["collection"],
"timeStart": content["properties"]["datetime"],
"timeEnd": content["properties"]["datetime"],
"opt:cloudCover": int(content["properties"]["eo:cloud_cover"]),
"crs": content["properties"]["proj:epsg"],
#"thumbnailURL": content["assets"]["thumbnail"]["href"],
"assets": content["assets"]
}
}
return json.loads(json.dumps(new_json))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, help="Hostname of the selected STAC API", required=True)
args = parser.parse_args()
pwd = getpass.getpass()
collection_name = "sentinel2-l2a"
workingdir = Path(__file__).parent
collection_folder = workingdir / "Sentinel2-tileless" / collection_name
app_host = f"{args.host}/geoserver/rest/oseo/"
if args.host == "http://86.50.229.158:8080/":
catalog = pystac_client.Client.open(f"{args.host}/geoserver/ogc/stac/v1/")
else:
catalog = pystac_client.Client.open(f"{args.host}/geoserver/ogc/stac/v1/", headers={"User-Agent":"update-script"})
# Convert the STAC collection json into json that GeoServer can handle
converted = json_convert(collection_folder / "collection.json")
# Additional code for changing collection data if the collection already exists
collections = catalog.get_collections()
col_ids = [col.id for col in collections]
if collection_name in col_ids:
r = requests.put(urljoin(app_host + "collections/", collection_name), json=converted, auth=HTTPBasicAuth("admin", pwd))
r.raise_for_status()
print(f"Updated {collection_name}")
else:
r = requests.post(urljoin(app_host, "collections/"), json=converted, auth=HTTPBasicAuth("admin", pwd))
r.raise_for_status()
print(f"Added new collection: {collection_name}")
# Get the posted items from the specific collection
posted = catalog.search(collections=[collection_name]).item_collection()
posted_ids = [x.id for x in posted]
print(f"Number of items: {len(posted_ids)}")
with open(collection_folder / "collection.json") as f:
rootcollection = json.load(f)
items = [x['href'] for x in rootcollection["links"] if x["rel"] == "item"]
print("Uploading items:")
for i, item in enumerate(items):
with open(collection_folder / item) as f:
payload = json.load(f)
# Convert the STAC item json into json that GeoServer can handle
converted = json_convert(collection_folder / item)
request_point = f"collections/{rootcollection['id']}/products"
if payload["id"] in posted_ids:
request_point = f"collections/{rootcollection['id']}/products/{payload['id']}"
r = requests.put(urljoin(app_host, request_point), json=converted, auth=HTTPBasicAuth("admin", pwd))
r.raise_for_status()
else:
r = requests.post(urljoin(app_host, request_point), json=converted, auth=HTTPBasicAuth("admin", pwd))
r.raise_for_status()
if len(items) >= 5: # Just to keep track that the script is still running
if i == int(len(items) / 5):
print("~20% of items added")
elif i == int(len(items) / 5) * 2:
print("~40% of items added")
elif i == int(len(items) / 5) * 3:
print("~60% of items added")
elif i == int(len(items) / 5) * 4:
print("~80% of items added")
print("All items added.")