Skip to main content

I would like to compute vegetation indices like GCVI or EVI and wanted to see how I can adapt the script below (taken from Sentinel Hub Batch Statistical — Sentinel Hub 3.9.1 documentation):

ndvi_evalscript = """
//VERSION=3

function setup() {
  return {
    input: [
      {
        bands: [
          "B04",
          "B08",
          "dataMask"
        ]
      }
    ],
    output: [
      {
        id: "ndvi",
        bands: 1
      },
      {
        id: "dataMask",
        bands: 1
      }
    ]
  }
}

function evaluatePixel(samples) {
    return {
      ndvi: [index(samples.B08, samples.B04)],
      dataMask: [samples.dataMask]
    };
}
"""

What I am unclear about here is the ndvi: [index(samples.B08, samples.B04)], line, how is NDVI being calculated there? I thought there would be an explicit formula.

The formula for GCVI is:

(band[‘b08’] / band[‘b03’]) - 1.

thanks!

Hi,
“Index” is actually a function that allow you to obtain the difference divided by the sum:

https://docs.sentinel-hub.com/api/latest/evalscript/functions/

To obtain what you want, you just need to substitute that line with the formula you want (and also fix input and output. Like this:

indices_evalscript = """
//VERSION=3

function setup() {
return {
input: [{
bands:[“B03”,“B08”,“dataMask”]}],
output:[{
id: "GCVI",
bands: 1
},
{
id: “dataMask”,
bands: 1
}]
}
}

function evaluatePixel(sample) {
return {
GCVI: [(samples.B08/samples.B03)-1],
dataMask: [samples.dataMask]
};
}
"""


I guess that this should work


 
indices_evalscript = """
//VERSION=3

function setup() {
return {
input: [{
bands:[“B03”,“B08”,“dataMask”]}],
output:[{
id: "GCVI",
bands: 1
},
{
id: “dataMask”,
bands: 1
}]
}
}

function evaluatePixel(sample) {
return {
GCVI: [(samples.B08/samples.B03)-1],
dataMask: [samples.dataMask]
};
}
"""

Thanks! I tried it but getting the following error:
sentinelhub.exceptions.DownloadFailedException: Failed to download from: https://services.sentinel-hub.com/api/v1/statistics with HTTPError: 400 Client Error: Bad Request for url: https://services.sentinel-hub.com/api/v1/statistics Server response: "{"status": 400, "reason": "Bad Request", "message": "Failed to evaluate script!\nevalscript.js:7: SyntaxError: Invalid or unexpected token\n

Any idea about what might be going wrong?


There is a missing “s” in “function evaluatePixel(sample)”

try to substitute with
“function evaluatePixel(samples)”


That worked. Also, I had to change the quotes around dataMask to match those around GCVI. Thank you!