Examples
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 BatchV2 Processing Request
Option 1: GeoTiff format output
This request defines which data is requested and how it will be processed.
In this example, we will calculate the maximum NDVI over two months for an area in Corsica and visualize the results using a built-in visualizer.
The resulting image will be in a GeoTIFF format.
To create a batch processing request, replace <MyBucket> with the name of your S3 bucket and run the following:
- Python SDK
url = "https://services.sentinel-hub.com/api/v2/batch/process"
evalscript = """
//VERSION=3
function setup() {
return {
input: [{
bands: ["B04", "B08"]
}],
output: [{
id: "default",
bands: 3
}],
mosaicking: Mosaicking.ORBIT
}
}
function calcNDVI(sample) {
var denom = sample.B04 + sample.B08
return ((denom != 0) ? (sample.B08 - sample.B04) / denom : 0.0)
}
const maxNDVIcolors = [
[-0.2, 0xbfbfbf],
[0, 0xebebeb],
[0.1, 0xc8c682],
[0.2, 0x91bf52],
[0.4, 0x4f8a2e],
[0.6, 0x0f540c]
]
const visualizer = new ColorRampVisualizer(maxNDVIcolors);
function evaluatePixel(samples) {
var max = 0
for (var i = 0; i < samples.length; i++) {
var ndvi = calcNDVI(samples[i])
max = ndvi > max ? ndvi : max
}
ndvi = max
return visualizer.process(ndvi)
}
"""
payload = {
"processRequest": {
"input": {
"bounds": {
"bbox": [
8.44,
41.31,
9.66,
43.1
],
"properties": {
"crs": "http://www.opengis.net/def/crs/OGC/1.3/CRS84"
}
},
"data": [{
"dataFilter": {
"timeRange": {
"from": "2019-04-01T00:00:00Z",
"to": "2019-06-30T00:00:00Z"
},
"maxCloudCoverage": 70.0
},
"type": "sentinel-2-l2a"
}]
},
"output": {
"responses": [{
"identifier": "default",
"format": {
"type": "image/tiff"
}
}]
},
"evalscript": evalscript
},
"input": {
"type" : "tiling-grid",
"id": 0,
"resolution": 60.0
},
"output": {
"type": "raster",
"delivery": {
"s3": {
"url": "s3://<your-bucket>",
"iamRoleARN": "<your-IAM-role-ARN>"
}
}
},
"description": "Max NDVI over Corsica"
}
headers = {
'Content-Type': 'application/json'
}
response = oauth.request("POST", url, headers=headers, json = payload)
response.json()
Extracting the batch request id from the response:
- Python SDK
batch_request_id = response.json()['id']
Option 2: Zarr format output
In this example, we will calculate the maximum NDVI over two months for an area in Corsica.
Besides the maximum NDVI, we will also return the values of bands B04 and B08, which were used to calculate the maximum NDVI.
All three results will be stored as arrays in an output Zarr file.
To create a batch processing request replace <MyBucket> with the name of your S3 bucket and run the following:
- Python SDK
url = "https://services.sentinel-hub.com/api/v2/batch/process"
evalscript = """
//VERSION=3
function setup() {
return {
input: [{
bands: ["B04", "B08"]
}],
output: [{
id: "maxNDVI",
sampleType: "FLOAT32",
bands: 1
},
{
id: "band04",
sampleType: "UINT16",
bands: 1
},
{
id: "band08",
sampleType: "UINT16",
bands: 1
}],
mosaicking: Mosaicking.ORBIT
}
}
function calcNDVI(sample) {
var denom = sample.B04 + sample.B08
return ((denom != 0) ? (sample.B08 - sample.B04) / denom : 0.0)
}
function evaluatePixel(samples) {
var maxNDVI = 0
var band04 = 0
var band08 = 0
for (var i = 0; i < samples.length; i++) {
var ndvi = calcNDVI(samples[i])
if (ndvi > maxNDVI){
maxNDVI = ndvi
band04 = samples[i].B04
band08 = samples[i].B08
}
}
return {
maxNDVI: [maxNDVI],
band04: [band04],
band08: [band08]
}
}
"""
payload = {
"processRequest": {
"input": {
"bounds": {
"bbox": [
8.44,
41.31,
9.66,
43.1
],
"properties": {
"crs": "http://www.opengis.net/def/crs/OGC/1.3/CRS84"
}
},
"data": [
{
"dataFilter": {
"timeRange": {
"from": "2019-04-01T00:00:00Z",
"to": "2019-06-30T00:00:00Z"
},
"maxCloudCoverage": 70
},
"type": "sentinel-2-l2a"
}
]
},
"output": {
"responses": [
{
"identifier": "band08",
"format": {
"type": "zarr/array"
}
},
{
"identifier": "band04",
"format": {
"type": "zarr/array"
}
},
{
"identifier": "maxNDVI",
"format": {
"type": "zarr/array"
}
}
]
},
"evalscript": evalscript
},
"input": {
"type": "tiling-grid",
"id": 6,
"resolution": 100.0
},
"output": {
"type": "zarr",
"delivery": {
"s3": {
"url": "s3://<your-bucket>/<requestId>",
"iamRoleARN": "<your-IAM-role-ARN>"
}
},
"group": {
"zarr_format": 2
},
"arrayParameters": {
"dtype": "<u2",
"order": "C",
"chunks": [
1,
1000,
1000
],
"fill_value": 0
},
"arrayOverrides": {
"maxNDVI": {
"dtype": "<f4",
"fill_value": "NaN"
}
}
},
"description": "Max NDVI over Corsica with Zarr format output"
}
headers = {
'Content-Type': 'application/json'
}
response = oauth.request("POST", url, headers=headers, json=payload)
response.json()
Option 3: GeoPackage input and GeoTiff output
This request defines which data is requested and how it will be processed.
In this particular example we will calculate maximum NDVI over a two-month period for an area in Corsica, and visualize the results using a built-in visualizer.
The resulting image will be in a GeoTIFF format.
To create a batch processing request, replace <MyBucket> with the name of your S3 bucket and run the following:
- Python SDK
url = "https://services.sentinel-hub.com/api/v2/batch/process"
evalscript = """
//VERSION=3
function setup() {
return {
input: [{
bands: ["B04", "B08"]
}],
output: [{
id: "default",
bands: 3
}],
mosaicking: Mosaicking.ORBIT
}
}
function calcNDVI(sample) {
var denom = sample.B04 + sample.B08
return ((denom != 0) ? (sample.B08 - sample.B04) / denom : 0.0)
}
const maxNDVIcolors = [
[-0.2, 0xbfbfbf],
[0, 0xebebeb],
[0.1, 0xc8c682],
[0.2, 0x91bf52],
[0.4, 0x4f8a2e],
[0.6, 0x0f540c]
]
const visualizer = new ColorRampVisualizer(maxNDVIcolors);
function evaluatePixel(samples) {
var max = 0
for (var i = 0; i < samples.length; i++) {
var ndvi = calcNDVI(samples[i])
max = ndvi > max ? ndvi : max
}
ndvi = max
return visualizer.process(ndvi)
}
"""
payload = {
"processRequest": {
"input": {
"bounds": {
"bbox": [
8.44,
41.31,
9.66,
43.1
],
"properties": {
"crs": "http://www.opengis.net/def/crs/OGC/1.3/CRS84"
}
},
"data": [{
"dataFilter": {
"timeRange": {
"from": "2019-04-01T00:00:00Z",
"to": "2019-06-30T00:00:00Z"
},
"maxCloudCoverage": 70.0
},
"type": "sentinel-2-l2a"
}]
},
"output": {
"responses": [{
"identifier": "default",
"format": {
"type": "image/tiff"
}
}]
},
"evalscript": evalscript
},
"input": {
"type" : "geopackage",
"features": {
"s3": {
"url": "s3://<my-bucket>/<path-to-geopackage>",
"iamRoleARN": "<your-IAM-role-ARN>",
}
}
},
"output": {
"type": "raster",
"delivery": {
"s3": {
"url": "s3://<your-bucket>",
"iamRoleARN": "<your-IAM-role-ARN>"
}
}
},
"description": "Max NDVI over Corsica"
}
headers = {
'Content-Type': 'application/json'
}
response = oauth.request("POST", url, headers=headers, json=payload)
response.json()
Get Information About All of Your Batch Processing Requests
- Python SDK
url = "https://services.sentinel-hub.com/api/v2/batch/process"
response = oauth.request("GET", url)
response.json()
Get Information About a Batch Processing Request
- Python SDK
url = f"https://services.sentinel-hub.com/api/v2/batch/process/{batch_request_id}"
response = oauth.request("GET", url)
response.json()
Request Detailed Analysis (ANALYSE)
- Python SDK
url = f"https://services.sentinel-hub.com/api/v2/batch/process/{batch_request_id}/analyse"
response = oauth.request("POST", url)
response.status_code
Request the Start of Processing (START)
- Python SDK
url = f"https://services.sentinel-hub.com/api/v2/batch/process/{batch_request_id}/start"
response = oauth.request("POST", url)
response.status_code
Cancel a Batch Processing Request (STOP)
- Python SDK
url = f"https://services.sentinel-hub.com/api/v2/batch/process/{batch_request_id}/stop"
response = oauth.request("POST", url)
response.status_code