M Byrne
M Byrne

Reputation: 1

Is there a way to ignore null values in Google Earth Engine?

I am attemting to create 2 NDVI charts for the time range 2000 - 2021 based on ANDSAT/LE07/C01/T1_ANNUAL_NDVI data as follows:

var startyear = 2000
var endyear = 2021
var wetland_study = 'Athlone';
var years = ee.List.sequence(startyear, endyear);

// Function to compute annual NDVI and print chart
var L7 = ee.ImageCollection("LANDSAT/LE07/C01/T1_ANNUAL_NDVI")
               .filterDate('2000-01-01', '2021-01-01')
               .select('NDVI');
               
var computeAndPrint = function(wetlands, title) {
  var annual_collection = ee.ImageCollection.fromImages(years.map(function (y) {
    var annual = L7.filter(ee.Filter.calendarRange(y, y, 'year'))
                          .mean().clip(wetlands);
    return annual.set('year', y);
  }));

  print(annual_collection);

  var meanNDVI = L7.reduce(ee.Reducer.mean());
  var final_study = meanNDVI.clip(wetlands);

  var colorizedVis = {
    min: 0.0,
    max: 1.0,
    palette: [
      'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901', 
      '66A000', '529499', '3E8601', '207401', '056201', '004C00', '023B01', 
      '012E01', '011D01', '011301'
    ], 
  };

  Map.addLayer(final_study, colorizedVis, 'NDVI_' + title);

  var chart = ui.Chart.image.series({
    imageCollection: L7, 
    region: wetlands, 
    reducer: ee.Reducer.mean(), 
    scale: 30,
    xProperty: 'system:time_start'
  }).setOptions({
    title: 'NDVI yearly chart of ' + title, 
    vAxis: {
      title: 'NDVI value',
      gridlines: {count: 0},
      baselineColor: 'black'},
    hAxis: {
      title: 'year', 
      gridlines: {color: 'black', count: 0},
      baselineColor: 'black'
    },
    trendlines: {0: {color: '#aa3319', visibleInLegend: true}}, 
    series: {0: {color: '#161616'}}
  });

  print(chart);
};

// Add layer and center map for the first region
var wetlands1 = MONOVALE;
Map.addLayer(MONOVALE);
Map.centerObject(wetlands1, 15);
computeAndPrint(wetlands1, 'Monovale');

// Add layer and center map for the second region
var wetlands2 = ROI.filter(ee.Filter.eq('name', wetland_study));
Map.addLayer(wetlands2, {color: 'blue'}, wetland_study);
Map.centerObject(wetlands2, 15);
computeAndPrint(wetlands2, wetland_study);

Some of the chats will plot but others will display the errors:

No features contain non-null values of "system:time_start". No images in the collection intersect the specified regions. **

I assume the error comes about from years with no data available but I would just like to parse over these and still display what data is available. I attempted to achive this b filtering out the null_values as follows:

var startyear = 2000;
var endyear = 2023;
var wetland_study = 'Epworth';
var years = ee.List.sequence(startyear, endyear);

// Function to compute annual NDVI and print chart
var L7 = ee.ImageCollection("LANDSAT/LE07/C01/T1_ANNUAL_NDVI")
               .filterDate('2000-01-01', '2021-01-01')
               .select('NDVI');

var computeAndPrint = function(wetlands, title) {
  var validYears = [];

  years.getInfo().forEach(function (y) {
    var annual = L7.filter(ee.Filter.calendarRange(y, y, 'year'))
                   .mean().clip(wetlands);

    var hasData = annual.reduceRegion({
      reducer: ee.Reducer.count(),
      geometry: wetlands,
      scale: 30,
    }).values().get(0);

    if (hasData !== 0) {
      validYears.push(y);
    }
  });

  if (validYears.length > 0) {
    var chart = ui.Chart.image.series({
      imageCollection: L7.filterBounds(wetlands), 
      region: wetlands, 
      reducer: ee.Reducer.mean(), 
      scale: 30,
      xProperty: 'system:time_start',
    }).setOptions({
      title: 'NDVI yearly chart of ' + title, 
      vAxis: {
        title: 'NDVI value',
        gridlines: {count: 0},
        baselineColor: 'black'
      },
      hAxis: {
        title: 'year', 
        gridlines: {color: 'black', count: 0},
        baselineColor: 'black'
      },
      trendlines: {0: {color: '#aa3319', visibleInLegend: true}}, 
      series: {0: {color: '#161616'}}
    });

    print(chart);
  } else {
    print('No data available for ' + title);
  }
};

// Add layer and center map for the first region
var wetlands1 = MONOVALE;
Map.addLayer(MONOVALE);
Map.centerObject(wetlands1, 15);
computeAndPrint(wetlands1, 'Monovale');

// Add layer and center map for the second region
var wetlands2 = ROI.filter(ee.Filter.eq('Name', wetland_study));
Map.addLayer(wetlands2, {color: 'blue'}, wetland_study);
Map.centerObject(wetlands2, 15);
computeAndPrint(wetlands2, wetland_study);

Now I am receiving a slightly different error:

The image collection is empty.

Could there be no data available for any of the requested years?

Upvotes: 0

Views: 379

Answers (1)

Luis Martinez
Luis Martinez

Reputation: 11

If thats the case to parse over missing data i'd use this in your return statement.

return ee.Algorithms.If(annual, annual.set('year', y), null); })).removeAll([null]);

Upvotes: 1

Related Questions