Created
August 24, 2021 16:19
-
-
Save maximlamare/780b151b0e24559e6e3443250c9322cd to your computer and use it in GitHub Desktop.
Batch API request without sh-py on US-WEST endpoint
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
# For requests | |
import requests | |
from oauthlib.oauth2 import BackendApplicationClient | |
from requests_oauthlib import OAuth2Session | |
from sentinelhub import SHConfig | |
config = SHConfig() | |
# Set up credentials for use with batch | |
client = BackendApplicationClient(client_id=config.sh_client_id) | |
oauth = OAuth2Session(client=client) | |
# Fetch a token | |
token = oauth.fetch_token(token_url='https://services.sentinel-hub.com/oauth/token', | |
client_id=config.sh_client_id, client_secret=config.sh_client_secret) | |
# Set-up batch parameters: HERE we change the endpoint | |
url_base = 'https://services-uswest2.sentinel-hub.com/api/v1/batch' | |
headers = {"Authorization": "Bearer " + token['access_token']} | |
# Evalscript for MODIS | |
modis_evalscript = """ | |
//VERSION=3 | |
function setup() { | |
return { | |
input: ["B01","B04","B03"], | |
output: { bands: 3 } | |
}; | |
} | |
function evaluatePixel(sample) { | |
return [2.5 * sample.B01, 2.5 * sample.B04, 2.5 * sample.B03]; | |
} | |
""" | |
# Batch payload | |
payload = { | |
"processRequest": { | |
"input": { | |
"bounds": { | |
"bbox": [ | |
12.26521, | |
41.781553, | |
13.76123, | |
42.328093 | |
], | |
"properties": { | |
"crs": "http://www.opengis.net/def/crs/OGC/1.3/CRS84" | |
} | |
}, | |
"data": [{ | |
"dataFilter": { | |
"timeRange": { | |
"from": "2021-08-14T00:00:00Z", | |
"to": "2021-08-14T23:59:59Z" | |
}, | |
}, | |
"type": "modis" | |
}] | |
}, | |
"output": { | |
"responses": [{ | |
"identifier": "default", | |
"format": { | |
"type": "image/tiff" | |
} | |
}] | |
}, | |
"evalscript": modis_evalscript | |
}, | |
"tilingGrid": { | |
"id": 2, | |
"resolution": 120.0 | |
}, | |
"bucketName": "<MyBucket>", | |
"description": "MODIS test" | |
} | |
headers = { | |
'Content-Type': 'application/json' | |
} | |
response = oauth.request("POST", f"{url_base}/process", headers=headers, json = payload) | |
response.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment