2

I am trying to create a script and chart in GEE for NDVI time series for several years using LANDSAT/LC08/C01/T1_32DAY_NDVI collection. I developed a short script using GEE library, but the chart shows only for one year instead (and I don't even know which year) of showing for all of them (I need to compare NDVI for different years for my study area): https://code.earthengine.google.com/756d3ff7b718d91582535fd7669d0842 enter image description here

var countries = ee.FeatureCollection("FAO/GAUL/2015/level2");

var uzbekistan = countries.filter(ee.Filter.eq('ADM0_NAME','Uzbekistan'));

var bostanliq =uzbekistan.filter(ee.Filter.eq('ADM2_NAME','Bostanlik district'));
Map.addLayer(bostanliq, {}, "Bostanliq");

///
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_32DAY_NDVI')
                  .filter(ee.Filter.or(
                         ee.Filter.date('2014-01-01', '2015-01-01'),
                         ee.Filter.date('2015-01-01', '2016-01-01'),
                         ee.Filter.date('2016-01-01', '2017-01-01'),
                       ee.Filter.date('2017-01-01', '2018-01-01')))
                  .filterBounds(bostanliq);

var dataset = dataset.select('NDVI');
var colorizedVis = {
  min: 0,
  max: 1,
  palette: [
    'ffffff', 'ce7e45', 'df923d', 'f1b555', 'fcd163', '99b718', '74a901',
    '66a000', '529400', '3e8601', '207401', '056201', '004c00', '023b01',
    '012e01', '011d01', '011301'
  ],
};

///
var chart =
    ui.Chart.image
        .doySeries({
          imageCollection: dataset,
          region: bostanliq,
          regionReducer: ee.Reducer.mean(),
          scale: 30,
          yearReducer: ee.Reducer.mean(),
          startDay: 1,
          endDay: 365
        })
        .setOptions({
          title: 'Average Vegetation Index Value by Day of Year for Bostanliq',
          hAxis: {
            title: 'Day of year',
            titleTextStyle: {italic: false, bold: true}
          },
          vAxis: {
            title: 'Vegetation index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 3,
          colors: ['1d6b99', '39a8a7', '9c4f97'],
        });
print(chart);


1 Answer 1

1

You can use series method instead. Complete script looks as follows:

var countries = ee.FeatureCollection("FAO/GAUL/2015/level2");

var uzbekistan = countries.filter(ee.Filter.eq('ADM0_NAME','Uzbekistan'));

var bostanliq =uzbekistan.filter(ee.Filter.eq('ADM2_NAME','Bostanlik district'));
Map.addLayer(bostanliq, {}, "Bostanliq");

///
var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_32DAY_NDVI')
                  .filter(ee.Filter.or(
                         ee.Filter.date('2014-01-01', '2015-01-01'),
                         ee.Filter.date('2015-01-01', '2016-01-01'),
                         ee.Filter.date('2016-01-01', '2017-01-01'),
                       ee.Filter.date('2017-01-01', '2018-01-01')))
                  .filterBounds(bostanliq);

var dataset = dataset.select('NDVI');
var colorizedVis = {
  min: 0,
  max: 1,
  palette: [
    'ffffff', 'ce7e45', 'df923d', 'f1b555', 'fcd163', '99b718', '74a901',
    '66a000', '529400', '3e8601', '207401', '056201', '004c00', '023b01',
    '012e01', '011d01', '011301'
  ],
};

///
var chart =
    ui.Chart.image
        .series({
          imageCollection: dataset,
          region: bostanliq,
          reducer: ee.Reducer.mean(),
          scale: 30,
          //yearReducer: ee.Reducer.mean(),
          //startDay: 1,
          //endDay: 365
        })
        .setOptions({
          title: 'Average Vegetation Index Value by Year for Bostanliq',
          hAxis: {
            title: 'Dates',
            titleTextStyle: {italic: false, bold: true}
          },
          vAxis: {
            title: 'Vegetation index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 1,
          colors: ['1d6b99', '39a8a7', '9c4f97'],
        });
print(chart);

After running it at GEE code editor, I got the result split by years as follows:

enter image description here

Editing Note:

For graphing each year separately, it can be created a new series for each year renaming NDVI band with its respective year as follows.

var countries = ee.FeatureCollection("FAO/GAUL/2015/level2");

var uzbekistan = countries.filter(ee.Filter.eq('ADM0_NAME','Uzbekistan'));

var bostanliq =uzbekistan.filter(ee.Filter.eq('ADM2_NAME','Bostanlik district'));
Map.addLayer(bostanliq, {}, "Bostanliq");

var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_32DAY_NDVI');

var dates_list = ee.List([['2014-01-01', '2015-01-01'],
                          ['2015-01-01', '2016-01-01'],
                          ['2016-01-01', '2017-01-01'],
                          ['2017-01-01', '2018-01-01']
                          ]);

var new_dataset = dates_list.map(function (ele) {
  
  var new_dataset = dataset.filter(ee.Filter.date(ee.String(ee.List(ele).get(0)), 
                                                  ee.String(ee.List(ele).get(1))));
  
  var new_band = ee.String('NDVI_').cat(ee.String(ee.List(ele).get(0)).slice(0,4));
  
  new_dataset = new_dataset.map(function (e) {
  
    return ee.Image(e).rename(new_band);
  
  });
  
  return new_dataset.toList(new_dataset.size());
  
});

new_dataset = ee.ImageCollection(new_dataset.flatten());

//print(new_dataset);

var chart =
    ui.Chart.image
        .series({
          imageCollection: new_dataset,
          region: bostanliq,
          reducer: ee.Reducer.mean(),
          scale: 30,
        })
        .setOptions({
          title: 'Average Vegetation Index Value by Year for Bostanliq',
          hAxis: {
            title: 'Dates',
            titleTextStyle: {italic: false, bold: true}
          },
          vAxis: {
            title: 'Vegetation index',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 3,
          colors: ['red', 'green', 'blue', 'orange'],
        });

print(chart);

Complete code here and the result at following picture.

enter image description here

3
  • Thanks for the answer, xunilk. The graph looks good. But I wanted to plot a chart with different colored lines for different years with DOY for x-axis. Is this possible? Commented Jul 18, 2023 at 6:47
  • For graphing each year separately, it can be created a new series for each year renaming NDVI bands with its respective year. Please, see my Editing Note. By the way, the code can be simplified by using a function to process each individual dataset.
    – xunilk
    Commented Jul 18, 2023 at 15:50
  • Condensate code can be found here: code.earthengine.google.com/9ce81cf203ea712b9278cc9205390aa5.
    – xunilk
    Commented Jul 18, 2023 at 17:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.