Hi,
The ColorGradientVisualizer is a function which Interpolates a color based on provided interval values. In this case you’re not setting minimum and maximum values so the function uses the default setting 0 to 1, and returns interpolated color (between white and green) as normalized RGB triplet ( number, number, number]) for value.
To get the actual NDVI value, here is an example evalscript. Please notice in this case the layer would be grayscale.
//VERSION=3
function setup() {
return{
input: :{
bands: :"B04", "B08"],
units: "DN"
}],
output: {
id: "default",
bands: 1,
sampleType: SampleType.FLOAT32
}
}
}
function evaluatePixel(sample) {
let ndvi = index(sample.B08, sample.B04)
return n ndvi ]
}
If you want to keep the visualisation and return the actual NDVI value when clicking, you need to create two layers in your configuration. One for the visualisation and the other for the actual NDVI value which you would use in your FIS request.
Hope this would help!
Best regards,
Hi,
Thanks for this!
Do you have an example how to implement this?
I am trying to set a custom script for the Sentinel 2-L2A NDVI layer, but I get only the values and not the image.
The custom script here-
//VERSION=3
function setup( ){
return{
input: [{
bands:["B04", "B08"],
}],
output: [{
id: "default",
bands: 1,
sampleType: SampleType.FLOAT32},
{
id: "ndvi_image",
bands: 3,
sampleType: SampleType.FLOAT32}
]
}
}
function evaluatePixel(sample) {
let ndvi = (sample.B08 - sample.B04) / (sample.B08 + sample.B04)
if (ndvi<-0.5) image = [0.05,0.05,0.05]
else if (ndvi<-0.2) image = [0.75,0.75,0.75]
else if (ndvi<-0.1) image = [0.86,0.86,0.86]
else if (ndvi<0) image = [0.92,0.92,0.92]
else if (ndvi<0.025) image = [1,0.98,0.8]
else if (ndvi<0.05) image = [0.93,0.91,0.71]
else if (ndvi<0.075) image = [0.87,0.85,0.61]
else if (ndvi<0.1) image = [0.8,0.78,0.51]
else if (ndvi<0.125) image = [0.74,0.72,0.42]
else if (ndvi<0.15) image = [0.69,0.76,0.38]
else if (ndvi<0.175) image = [0.64,0.8,0.35]
else if (ndvi<0.2) image = [0.57,0.75,0.32]
else if (ndvi<0.25) image = [0.5,0.7,0.28]
else if (ndvi<0.3) image = [0.44,0.64,0.25]
else if (ndvi<0.35) image = [0.38,0.59,0.21]
else if (ndvi<0.4) image = [0.31,0.54,0.18]
else if (ndvi<0.45) image = [0.25,0.49,0.14]
else if (ndvi<0.5) image = [0.19,0.43,0.11]
else if (ndvi<0.55) image = [0.13,0.38,0.07]
else if (ndvi<0.6) image = [0.06,0.33,0.04]
else image = [0,0.27,0]
return {
default: [ ndvi ],
ndvi_image: image
}
}
Hi,
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:s\"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 e0, 1] to 10, 255] and written into an UINT8 raster, and set the output format to "type": "image/jpeg"
or "type": "image/png"
.