Skip to main content

Examples of Batch Statistical Workflow

The requests below are written in Python. To execute them, you need to create an OAuth client as is explained here. It is named oauth in these examples.

Create a Batch Statistical Request

This request defines which data is requested and how it will be processed. In this example, we will receive the statistics for a single band on a given day. To create a batch statistical request, replace the input.features.s3.url field with the actual path to the GeoPackage features, and the output.s3.url field with the desired path where the output data will be processed.

evalscript = """
//VERSION=3
function setup() {
return {
input: [{
bands: [
"B04",
"dataMask"
]
}],
output: [
{
id: "output_B04",
bands: 1,
sampleType: "FLOAT32"
},
{
id: "dataMask",
bands: 1
}]
}
}
function evaluatePixel(samples) {
return {
output_B04: [samples.B04],
dataMask: [samples.dataMask]
}
}
"""

request_payload = {
"input": {
"features":{
"s3": {
"url": "s3://{bucket}/{path-to-geopackage}",
"accessKey": "{access-key},
"secretAccessKey": "{secret-access-key}
}
},
"data": [
{
"type": "sentinel-2-l2a",
"dataFilter": {
"mosaickingOrder": "leastCC"
}
}
]
},
"aggregation": {
"timeRange": {
"from": "2020-06-01T00:00:00Z",
"to": "2020-07-31T00:00:00Z"
},
"aggregationInterval": {
"of": "P30D"
},
"evalscript": evalscript,
"resx": 10,
"resy": 10
},
"output": {
"s3": {
"url": "s3://{bucket}/{key}",
"accessKey": "{access-key},
"secretAccessKey": "{secret-access-key}
}
}
}

headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}

url = "https://services.sentinel-hub.com/api/v1/statistics/batch"

response = oauth.request("POST", url=url, headers=headers, json=request_payload)

request_id = response.json()['id']

Note that in the above example, we are specifying an accessKey and secretAccessKey, so we can read and write to the user's bucket. You can find more details about this under the Object Storage Configuration section.

Create a Batch Statistical Request with Google Storage

This example demonstrates how to use Google Cloud Storage for both GeoPackage input and delivery of results instead of S3.

evalscript = """
//VERSION=3
function setup() {
return {
input: [{
bands: [
"B04",
"dataMask"
]
}],
output: [
{
id: "output_B04",
bands: 1,
sampleType: "FLOAT32"
},
{
id: "dataMask",
bands: 1
}]
}
}
function evaluatePixel(samples) {
return {
output_B04: [samples.B04],
dataMask: [samples.dataMask]
}
}
"""

request_payload = {
"input": {
"features":{
"gs": {
"url": "gs://{bucket}/{path-to-geopackage}",
"credentials": "{base64-encoded-credentials}"
}
},
"data": [
{
"type": "sentinel-2-l2a",
"dataFilter": {
"mosaickingOrder": "leastCC"
}
}
]
},
"aggregation": {
"timeRange": {
"from": "2020-06-01T00:00:00Z",
"to": "2020-07-31T00:00:00Z"
},
"aggregationInterval": {
"of": "P30D"
},
"evalscript": evalscript,
"resx": 10,
"resy": 10
},
"output": {
"gs": {
"url": "gs://{bucket}/{key}",
"credentials": "{base64-encoded-credentials}"
}
}
}

headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}

url = "https://services.sentinel-hub.com/api/v1/statistics/batch"

response = oauth.request("POST", url=url, headers=headers, json=request_payload)

request_id = response.json()['id']

To prepare your Google Cloud Storage credentials, download your service account credentials in JSON format and encode them as base64:

cat my_creds.json | base64

Replace {base64-encoded-credentials} with the output of this command in both the input and output sections of your request.

Get Information About a Batch Statistical Request

response = oauth.request("GET", f"https://services.sentinel-hub.com/api/v1/statistics/batch/{request_id}")

response.json()

Get Status Information About a Batch Statistical Request

response = oauth.request("GET", f"https://services.sentinel-hub.com/api/v1/statistics/batch/{request_id}/status")

response.json()

Request Analysis of a Batch Statistical Request (ANALYSIS)

response = oauth.request("POST", f"https://services.sentinel-hub.com/api/v1/statistics/batch/{request_id}/analyse")

response.status_code

Request the Start of a Batch Statistical Request (START)

response = oauth.request("POST", f"https://services.sentinel-hub.com/api/v1/statistics/batch/{request_id}/start")

response.status_code

Stop a Batch Statistical Request (STOP)

response = oauth.request("POST", f"https://services.sentinel-hub.com/api/v1/statistics/batch/{request_id}/stop")

response.status_code