Skip to main content

Hi


i would like to know how can i adapt the below posted evalscript to obtain the total average ndvi
evelscript:


// Script to extract a time series of NDVI values using 
// Sentinel 2 Level 2A data and metadata file.
function setup() {
return {
input: [{
bands: ["B04", "B08"],
units: "DN"
}],
output: {
bands: 1,
sampleType: SampleType.FLOAT32
},
mosaicking: Mosaicking.ORBIT
}

}


function evaluatePixel(samples) {
// Precompute an array to contain NDVI observations
var n_observations = samples.length;
let ndvi = new Array(n_observations).fill(0);

// Fill the array with NDVI values
samples.forEach((sample, index) => {
ndvi[index] = (sample.B08 - sample.B04) / (sample.B08 + sample.B04) ;
});

return ndvi;
}

Hi,

To obtain the mean NDVI of your area of interest, please have a look at our Statistical API and the example requests.


thanks, but will the evalscript remain as it is as shown in my question or it should be changed as well?


Hi,

In this example you will find the Evalscript. You just need to change the output from a single band reflectance to the NDVI index.


but to find the ndvi we need two bands, red and NIR?right?


Yes, you can still have two input bands and calculate the NDVI. The only thing different is that you output should be the NDVI index instead of the single band reflectance.


would you please adapt the evalscript posted in my question with the neede change?i want to be sure that i am doing it the correct way please


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

function evaluatePixel(samples) {
let index = (samples.B08 - samples.B04) / (samples.B08+samples.B04);
return {
ndvi: [index],
dataMask: [samples.dataMask]
};
}

appreciate it, many thanks


Reply