0

I've calculated NDVI from Sentinel 2 images but when I try to get the chart of the series this message is generated - Error generating chart: No features contain non-null values of "system:time_start". The code is the following:

var s2 = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2019-01-01', '2019-12-31')
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .filterBounds(studyArea);
      
function getNDVI (image) {
    var ndvi = image.normalizedDifference(['B8','B4']).rename("ndvi");
    image = image.addBands(ndvi);
    return image;
}
s2 = s2.median();
s2 = getNDVI(s2).clip(studyArea);
var s2NDVI = s2.select('ndvi');
print(s2NDVI);

var NDVIseries = ui.Chart.image.seriesByRegion(
    s2NDVI, studyArea, ee.Reducer.mean(), 'ndvi', 200,  'system:time_start', 'label')
        .setChartType('ScatterChart')
        .setOptions({
          title: 'NDVI 2016',
          vAxis: {title: '-1+1'},
          lineWidth: 1,
          pointSize: 4,
          series: {
            0: {color: 'FF0000'},
}});

// Display.
print(NDVIseries);

Can somebody help me to fix the code?

1 Answer 1

2

You are making one image (a composite median image) from a timeseries of images (an imageCollection). That single composite image cannot be plotted over time.

You probably want to change these lines:

s2 = s2.median();
s2 = getNDVI(s2).clip(studyArea);

to

s2 = s2.map(getNDVI);

This appends an NDVI-band to every image in the timeseries. Clipping is not necessary for this operation. Here is a smaple code with a randomly drawn geometry. Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.