Copy for LLM[View as Markdown](https://docs.planet.com/develop/apis/batch-processing/examples/) # Examples The requests below are written in Python. To execute them you need to create an OAuth client as is explained [here](https://docs.planet.com/develop/authentication.md#sentinel-hub-authentication). It is named `oauth` in these examples. ### Create a Batch 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 `{bucket}` with the name of your S3 bucket and run the following: * Python SDK ```python url = "https://services.sentinel-hub.com/batch/v2/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://{bucket}", "iamRoleARN": "{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 ```python 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 `{bucket}` with the name of your S3 bucket and run the following: * Python SDK ```python url = "https://services.sentinel-hub.com/batch/v2/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://{bucket}/{key}", "iamRoleARN": "{IAM-role-ARN}" } }, "group": { "zarr_format": 2 }, "arrayParameters": { "dtype": " 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://{bucket}/{path-to-geopackage}", "iamRoleARN": "{IAM-role-ARN}", } } }, "output": { "type": "raster", "delivery": { "s3": { "url": "s3://{bucket}", "iamRoleARN": "{IAM-role-ARN}" } } }, "description": "Max NDVI over Corsica" } headers = { 'Content-Type': 'application/json' } response = oauth.request("POST", url, headers=headers, json=payload) response.json() ``` #### Option 4: Google Storage Bucket Input and Delivery This example demonstrates how to use Google Cloud Storage buckets for both GeoPackage input and delivery of results. * Python SDK ```python url = "https://services.sentinel-hub.com/batch/v2/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": { "gs": { "url": "gs://{bucket}/{path-to-geopackage}", "credentials": "{base64-encoded-credentials}" } } }, "output": { "type": "raster", "delivery": { "gs": { "url": "gs://{bucket}/{key}", "credentials": "{base64-encoded-credentials}" } } }, "description": "Max NDVI over Corsica with Google Storage" } headers = { 'Content-Type': 'application/json' } response = oauth.request("POST", url, headers=headers, json=payload) response.json() ``` To prepare your Google Cloud Storage credentials, download your service account credentials in JSON format and encode them as base64: * CURL ```bash 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 All of Your Batch Processing Requests * Python SDK ```python url = "https://services.sentinel-hub.com/batch/v2/process" response = oauth.request("GET", url) response.json() ``` ### Get Information About a Batch Processing Request * Python SDK ```python url = f"https://services.sentinel-hub.com/batch/v2/process/{batch_request_id}" response = oauth.request("GET", url) response.json() ``` ### Request Detailed Analysis (ANALYSE) * Python SDK ```python url = f"https://services.sentinel-hub.com/batch/v2/process/{batch_request_id}/analyse" response = oauth.request("POST", url) response.status_code ``` ### Request the Start of Processing (START) * Python SDK ```python url = f"https://services.sentinel-hub.com/batch/v2/process/{batch_request_id}/start" response = oauth.request("POST", url) response.status_code ``` ### Cancel a Batch Processing Request (STOP) * Python SDK ```python url = f"https://services.sentinel-hub.com/batch/v2/process/{batch_request_id}/stop" response = oauth.request("POST", url) response.status_code ```