Example code to fetch a Quicklook of a Pleiades images using SH services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requests | |
from oauthlib.oauth2 import BackendApplicationClient | |
from requests_oauthlib import OAuth2Session | |
import requests | |
# Plotting | |
from PIL import Image | |
from io import BytesIO | |
import matplotlib.pyplot as plt | |
# Fetch Oauth token | |
def getauth_token(client_id, client_secret): | |
# Create a session | |
client = BackendApplicationClient(client_id=client_id) | |
oauth = OAuth2Session(client=client) | |
# Get token for the session | |
token = oauth.fetch_token( | |
token_url="https://services.sentinel-hub.com/oauth/token", | |
client_id=client_id, | |
client_secret=client_secret, | |
) | |
# All requests using this session will have an access token automatically added | |
resp = oauth.get("https://services.sentinel-hub.com/oauth/tokeninfo") | |
return oauth | |
# Set ID and secret (SH credentials) | |
shID = "xxxx" | |
shSECRET = "yyyyy" | |
# Fetch a token | |
oauth = getauth_token(shID, shSECRET) | |
# Set input parameters | |
geometry = { | |
"type": "Polygon", | |
"coordinates": [ | |
[ | |
[12.480207, 41.908026], | |
[12.499422, 41.90726], | |
[12.515034, 41.883876], | |
[12.486555, 41.87902], | |
[12.480207, 41.908026], | |
] | |
], | |
} | |
# Max Cloud cover in % | |
maxCC = 25 | |
# Max incidence angle in degrees | |
maxINC = 54 | |
start_date = "2022-04-15" | |
end_date = "2022-05-15" | |
# Make the request body | |
url = "https://services.sentinel-hub.com/api/v1/dataimport/search" | |
settings = { | |
"provider": "AIRBUS", | |
"bounds": {"geometry": geometry}, | |
"data": [ | |
{ | |
"constellation": "PHR", | |
"dataFilter": { | |
"maxCloudCoverage": maxCC, | |
"maxIncidenceAngle": maxINC, | |
"timeRange": { | |
"from": f"{start_date}T00:00:00Z", | |
"to": f"{end_date}T23:59:59Z", | |
}, | |
}, | |
} | |
], | |
} | |
# Run request | |
response = oauth.post(url, json=settings) | |
output = response.json() | |
# Fetch the quicklook URL and stream the data | |
urlQL = output["features"][0]["_links"]["quicklook"]["href"] | |
im = Image.open(requests.get(urlQL, stream=True).raw) | |
# Plot the quicklook | |
fig, ax = plt.subplots(1, 1, figsize=(10, 8)) | |
ax.imshow(im) | |
plt.title(f"Image date:{output['features'][0]['properties']['acquisitionDate']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment