Skip to main content

How to get total average ndvi please

  • April 26, 2024
  • 8 replies
  • 90 views

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;
  }

8 replies

  • 4852 posts replied to
  • April 26, 2024

Hi,

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


  • Author
  • 3963 posts replied to
  • April 26, 2024

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


  • 4852 posts replied to
  • April 26, 2024

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.


  • Author
  • 3963 posts replied to
  • April 26, 2024

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


  • 4852 posts replied to
  • April 26, 2024

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.


  • Author
  • 3963 posts replied to
  • April 26, 2024

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


  • 4852 posts replied to
  • April 26, 2024
//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]
    };
}

  • Author
  • 3963 posts replied to
  • April 26, 2024

appreciate it, many thanks