Hi @antonio.calle ,
Here’s an example request to get both ndvi values and its images.
curl -X POST https://services.sentinel-hub.com/api/v1/process
-H 'Content-Type: application/json'
-H 'Authorization: Bearer <token>'
-H 'Accept: application/tar'
-d '{
"input": {
"bounds": {
"bbox": [
12.44693,
41.870072,
12.541001,
41.917096
]
},
"data": [
{
"dataFilter": {
"timeRange": {
"from": "2022-08-19T00:00:00Z",
"to": "2022-09-19T23:59:59Z"
}
},
"type": "sentinel-2-l2a"
}
]
},
"output": {
"width": 512,
"height": 343.697,
"responses": [
{
"identifier": "default",
"format": {
"type": "image/tiff"
}
},
{
"identifier": "ndvi_image",
"format": {
"type": "image/tiff"
}
}
]
},
"evalscript": "//VERSION=3\nfunction setup( ){\n return{\n\n input: [{\n bands:[\"B04\", \"B08\"],\n }],\n output: [{\n id: \"default\",\n bands: 1,\n sampleType: SampleType.FLOAT32},\n {\n id: \"ndvi_image\",\n bands: 3,\n sampleType: SampleType.FLOAT32}\n ]\n }\n}\n\nfunction evaluatePixel(sample) {\n let ndvi = (sample.B08 - sample.B04) / (sample.B08 + sample.B04)\n\n if (ndvi<-0.5) image = [0.05,0.05,0.05]\n else if (ndvi<-0.2) image = [0.75,0.75,0.75]\n else if (ndvi<-0.1) image = [0.86,0.86,0.86]\n else if (ndvi<0) image = [0.92,0.92,0.92]\n else if (ndvi<0.025) image = [1,0.98,0.8]\n else if (ndvi<0.05) image = [0.93,0.91,0.71]\n else if (ndvi<0.075) image = [0.87,0.85,0.61]\n else if (ndvi<0.1) image = [0.8,0.78,0.51]\n else if (ndvi<0.125) image = [0.74,0.72,0.42]\n else if (ndvi<0.15) image = [0.69,0.76,0.38]\n else if (ndvi<0.175) image = [0.64,0.8,0.35]\n else if (ndvi<0.2) image = [0.57,0.75,0.32]\n else if (ndvi<0.25) image = [0.5,0.7,0.28]\n else if (ndvi<0.3) image = [0.44,0.64,0.25]\n else if (ndvi<0.35) image = [0.38,0.59,0.21]\n else if (ndvi<0.4) image = [0.31,0.54,0.18]\n else if (ndvi<0.45) image = [0.25,0.49,0.14]\n else if (ndvi<0.5) image = [0.19,0.43,0.11]\n else if (ndvi<0.55) image = [0.13,0.38,0.07]\n else if (ndvi<0.6) image = [0.06,0.33,0.04]\n else image = [0,0.27,0]\n\n return {\n default: [ ndvi ],\n ndvi_image: image\n }\n}\n\n\n\n"
}'
You could copy and paste the above script to the Request Preview window of Requests Builder, parse it and select sh-py from the drop-down list. Then you’ll see the example code in Python.
If you’d like to get your image in JPEG or PNG, you can set the sampleType
of ndvi_image
to AUTO
, which stretches values from the interval [0, 1] to [0, 255] and written into an UINT8 raster, and set the output format to "type": "image/jpeg"
or "type": "image/png"
.