Search code examples
chartstime-seriestimestampgoogle-earth-engine

The trouble with Charting with Google Earth Engine


In Google Earth Engine, I want to get the NDVI index from several images of the Sentinel 2 satellite with different dates, then I will estimate other parameters from this index. To do this, I need to convert the resulting NDVI images to an image collection. But to chart it, it gives the following error:

Error generating chart: No features contain non-null values of "system:time_start".

It seems that when converting to a collection image, the temporal information of the images is lost. with this condition, how can I fix it?

Link to the code: https://code.earthengine.google.com/47cd9e7f65b143242ebb238d136bf760

Code:

var sentinel1 = ee.Image('COPERNICUS/S2_SR/20181214T072311_20181214T072733_T39SVV');
var sentinel2 = ee.Image('COPERNICUS/S2_SR/20181219T072319_20181219T072610_T39SVV');
var sentinel3 = ee.Image('COPERNICUS/S2_SR/20181224T072311_20181224T072313_T39SVV');

var ndvi1 = sentinel1.normalizedDifference(['B8','B4']);
var ndvi2 = sentinel2.normalizedDifference(['B8','B4']);
var ndvi3 = sentinel3.normalizedDifference(['B8','B4']);

var NDVI_COL = ee.ImageCollection.fromImages([ndvi1, ndvi2, ndvi3]);

var chart = ui.Chart.image.series(
NDVI_COL, geometry, ee.Reducer.mean(),10,'system:time_start');
print(chart);

Solution

  • Try this code

    Make a collection of selected images, then plot

    var sentinel1 = ee.Image('COPERNICUS/S2_SR/20181214T072311_20181214T072733_T39SVV');
    var sentinel2 = ee.Image('COPERNICUS/S2_SR/20181219T072319_20181219T072610_T39SVV');
    var sentinel3 = ee.Image('COPERNICUS/S2_SR/20181224T072311_20181224T072313_T39SVV');
    
    var S2 = ee.ImageCollection([sentinel1, sentinel2, sentinel3])
                    .filterBounds(geometry)
                    .map(function(image){return image.clip(geometry)});
                    
    print('collection of Selected Images to plot: ', S2);
    
    var addNDVI = function(image) {
      var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
      return image.addBands(ndvi);
    };
    
    var S2_NDVI = S2.map(addNDVI);
    
    var NDVI_S2 = ee.ImageCollection(S2_NDVI.select(["NDVI"], ["NDVI"]));
    
    var chart =
        ui.Chart.image
            .seriesByRegion({
              imageCollection: NDVI_S2,
              band: 'NDVI',
              regions: geometry,
              reducer: ee.Reducer.mean(),
              scale: 10,
              xProperty: 'system:time_start'
            })
            .setOptions({
              title: 'Average NDVI Value by Date',
              hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true}},
              vAxis: {
                title: 'NDVI',
                titleTextStyle: {italic: false, bold: true}
              },
            });
            
    print(chart);