Using a function to invoke the statistics API and NDVI image within an evalscript

I’m quite new to StatisticalAPI. I’m using it via the Python API.
I would get an NDVI mean and NDVI Image with color palette (via custom function) over my AOI.

To get only the NDVI I use:
//VERSION=3
//This script was converted from v1 to v3 using the converter API

//NDVI EVALSCRIPT
if (dataMask == 0) return [0,0,0,0];

//ndvi
var val = (B08-B04)/(B08+B04);

return colorBlend(val,
[-0.2, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 ],
[[0, 0, 0], // < -.2 = #000000 (black)
[165/255,0,38/255,1], // → 0 = #a50026
[215/255,48/255,39/255,1], // → .1 = #d73027
[244/255,109/255,67/255,1], // → .2 = #f46d43
[253/255,174/255,97/255,1], // → .3 = #fdae61
[254/255,224/255,139/255,1], // → .4 = #fee08b
[255/255,255/255,191/255,1], // → .5 = #ffffbf
[217/255,239/255,139/255,1], // → .6 = #d9ef8b
[166/255,217/255,106/255,1], // → .7 = #a6d96a
[102/255,189/255,99/255,1], // → .8 = #66bd63
[26/255,152/255,80/255,1], // → .9 = #1a9850
[0,104/255,55/255,1] // → 1.0 = #006837
]);

And this works well.

However, when I want to add the statistics API I get an error.
The statistics API is:
//VERSION=3
function setup() {
return {
input: [{
bands: [
“B04”,
“dataMask”
]
}],
output: [
{
id: “output_B04”,
bands: 1,
sampleType: “FLOAT32”
},
{
id: “dataMask”,
bands: 1
}]
}
}
function evaluatePixel(samples) {
return {
output_B04: [samples.B04],
dataMask: [samples.dataMask]
}
}

Hi @noureddine.bassa ,

Sorry if I don’t understand correctly. I think you’re trying to get NDVI imagery with color palette using Processing API and get mean NDVI using Statistical API right?

The Evalscript you used to get the NDVI imagery with color palette should work well with Processing API as you mentioned.

To get mean NDVI using Statistical API, below is an example request:

curl -X POST https://services.sentinel-hub.com/api/v1/statistics \
 -H 'Authorization: Bearer' \
 -H 'Accept: application/json' \
 -H 'Content-Type: application/json' \
 -d '{
  "input": {
    "bounds": {
      "bbox": [
        4526313.815668,
        2089673.283266,
        4530446.599833,
        2092523.566946
      ],
      "properties": {
        "crs": "http://www.opengis.net/def/crs/EPSG/0/3035"
      }
    },
    "data": [
      {
        "dataFilter": {},
        "type": "sentinel-2-l2a"
      }
    ]
  },
  "aggregation": {
    "timeRange": {
      "from": "2023-05-01T00:00:00Z",
      "to": "2023-05-30T23:59:59Z"
    },
    "aggregationInterval": {
      "of": "P1D"
    },
    "resx": "10",
    "resy": "10",
    "evalscript": "//VERSION=3\nfunction setup() {\n  return {\n    input: [{\n      bands: [\n        \"B04\",\n        \"B08\",\n        \"dataMask\"\n      ]\n    }],\n    output: [\n      {\n        id: \"ndvi\",\n        bands: 1\n      },\n      {\n        id: \"dataMask\",\n        bands: 1\n      }]\n  };\n}\n\nfunction evaluatePixel(samples) {\n    let index = (samples.B08 - samples.B04) / (samples.B08+samples.B04);\n    return {\n        ndvi: [index],\n        dataMask: [samples.dataMask],\n    };\n}\n"
  },
  "calculations": {
    "ndvi": {}
  }
}'

@chung.horng understood, thank you for the help, I will proceed to do what you recommend

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.