-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (110 loc) · 3.27 KB
/
main.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
import requests
from dotenv import load_dotenv
import os
load_dotenv()
# Sentinel Hub Credentials
CLIENT_ID = os.getenv("SENTINEL_CLIENT_ID")
CLIENT_SECRET = os.getenv("SENTINEL_CLIENT_SECRET")
API_URL = "https://services.sentinel-hub.com/oauth/token"
# Area of Interest (Polygon Coordinates)
coordinates = {
"type": "Polygon",
"coordinates": [[
[23.694077, 37.942031],
[23.694077, 38.262985],
[24.043579, 38.262985],
[24.043579, 37.942031],
[23.694077, 37.942031]
]]
}
# Time range
start_date = "2024-08-12"
end_date = "2024-08-12"
# Evalscript to get the SWIR composite
evalscript = """
//VERSION=3
function setup() {
return {
input: ["B12", "B8A", "B04", "dataMask"],
output: { bands: 4 }
};
}
function evaluatePixel(sample) {
return [2.5 * sample.B12, 2.5 * sample.B8A, 2.5 * sample.B04, sample.dataMask];
}
"""
# Request token from Sentinel Hub
def get_sentinel_token(client_id, client_secret):
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
response = requests.post(API_URL, data=payload)
return response.json().get('access_token')
# Prepare Sentinel Hub request for SWIR Composite
def get_swir_image(token, aoi, start_date, end_date, evalscript):
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
payload = {
"input": {
"bounds": {
"geometry": aoi
},
"data": [
{
"type": "S2L2A",
"dataFilter": {
"timeRange": {
"from": f"{start_date}T00:00:00Z",
"to": f"{end_date}T23:59:59Z"
}
}
}
]
},
"output": {
"width": 512,
"height": 512,
"responses": [
{
"identifier": "default",
"format": {
"type": "image/tiff"
}
}
]
},
"evalscript": evalscript
}
# Post request to Sentinel Hub API
process_url = "https://services.sentinel-hub.com/api/v1/process"
response = requests.post(process_url, headers=headers, json=payload)
if response.status_code == 200:
return response.content
else:
print(f"Error {response.status_code}: {response.text}")
return None
# Save image to file
def save_image(image_data, file_name):
with open(file_name, 'wb') as file:
file.write(image_data)
# Main Program
def main():
token = get_sentinel_token(CLIENT_ID, CLIENT_SECRET)
if token:
print("Token retrieved successfully.")
# Get the SWIR composite image
image_data = get_swir_image(token, coordinates, start_date, end_date, evalscript)
if image_data:
# Save the image locally
save_image(image_data, "swir_composite.tiff")
print("SWIR image saved successfully.")
else:
print("Failed to retrieve SWIR image.")
else:
print("Failed to get Sentinel Hub token.")
if __name__ == "__main__":
main()