0

I am using Landsat 5 to generate my NDVI, but it's giving an Error generating chart: The image collection is empty. However when I change the location of my AOI to the northern hemisphere, for example, India the code generates the desired output.

Is the problem with my code or Landsat Imagery?

Moreso, can you assist in cloud masking as I am not familiar?

Link to the code: https://code.earthengine.google.com/?scriptPath=users%2Fmpalasimbarashe%2FProject1%3AKNDVI%20L5

/// IMPORTING THE FEATURE COLLECTION ///
var points = points.map(function(feature){
  var sample_id = feature.get('Sample_ID');
  return ee.Feature(feature.geometry(), {'id': feature.id()})
    .set('Sample_ID', sample_id);
});

Map.addLayer(points,{color:'green'},'Root Locations');
Map.centerObject(points,10);
print(points);

var points_list = points.toList(points.size());

var keys = points_list.map(function (ele) {

  return ee.Feature(ele).get('id');
  
});

var values = points_list.map(function (ele) {

  return ee.Feature(ele).get('Sample_ID');
  
});

var dict = ee.Dictionary.fromLists(keys, values);


////Cloud Masking//////
var cloudMask = function(image) {
  var qa = image.select('pixel_qa');
  // If the cloud bit (5) is set and the cloud confidence (7) is high
  // or the cloud shadow bit is set (3), then it's a bad pixel.
  var cloud = qa.bitwiseAnd(1 << 5)
          .and(qa.bitwiseAnd(1 << 7))
          .or(qa.bitwiseAnd(1 << 3))
  // Remove edge pixels that don't occur in all bands
  var mask2 = image.mask().reduce(ee.Reducer.min());
  return image.updateMask(cloud.not()).updateMask(mask2);
};



/// DATE RANGE FOR THE TIME SERIES ///
var startDate =  new Date ('2010-09-01')
var endDate =  new Date ('2011-05-30')

/// COMPUTING THE KNDVI ////
var addKNDVI = function(image){
  
  var RED = image.select('SR_B3');
  var NIR = image.select('SR_B4');
  
  /// COMPUTE D2 A RENAME TO d2 ///
  var D2 = NIR.subtract(RED).pow(2)
    .select([0],['d2']);
 
  /// GAMMA DEFINED AS 1/sigma^2
  var gamma = ee.Number(4e6).multiply(-2.0);

/// COMPUTE KERNEL (k) AND KNDVI ///

  var k = D2.divide(gamma).exp();
  var kndvi = ee.Image.constant(1)
    .subtract(k).divide(ee.Image.constant(1).add(k))
    .select([0],['kndvi']).clip(points);
    
     return image.addBands(ee.Image([kndvi]));
 
};

/// IMPORTING THE IMAGE COLLECTION /////

var collection = ee.ImageCollection("LANDSAT/LT05/C02/T1_L2")
                   .filterDate(startDate, endDate)
                  .map(addKNDVI)
                  .map(cloudMask)
                 .filter(ee.Filter.bounds(points))
                   
/// VIEWING THE MAX IN THE COMPOSITE ////
 var vizParams = { bands: ['SR_B3','SR_B2','SR_B1'] , min: 0, max: 2000}
 Map.addLayer(collection.max(),vizParams, 'collection')

var testPoint = ee.Feature(points.first());
print(testPoint)
print(collection)
//Map.centerObject(testPoint, 10)

/// TIMESERIES CHART FOR SINGLE LOCATION ///
var chart = ui.Chart.image.series({
    imageCollection: collection.select('kndvi'),
    region: testPoint.geometry()
    }).setOptions({
      interpolateNulls: true,
      lineWidth: 1,
      pointSize: 3,
      title: 'KNDVI over Time at a Single Location',
      vAxis: {title: 'KNDVI'},
      hAxis: {title: 'Date', format: 'YYYY-MMM', gridlines: {count: 12}}
    })
print(chart)

//// TIMESERIES CHART FOR MULTIPLE LOCATIONS ////
var chart = ui.Chart.image.seriesByRegion({
    imageCollection: collection.select('kndvi'),
    regions: points,
    reducer: ee.Reducer.mean()
})
print(chart)

/// HANDLING MASKED PIXELS ///
var triplets = collection.map(function(image) {
  return image.select('kndvi').reduceRegions({
    collection: points, 
    reducer: ee.Reducer.mean().setOutputs(['kndvi']), 
    scale: 10,
  })// reduceRegion doesn't return any output if the image doesn't intersect
    // with the point or if the image is masked out due to cloud
    // If there was no ndvi value found, we set the ndvi to a NoData value -9999
    .map(function(feature) {
    var kndvi = ee.List([feature.get('kndvi'), -9999])
      .reduce(ee.Reducer.firstNonNull())
    return feature.set({'kndvi': kndvi, 'imageID': image.id()})
    })
  }).flatten();
  
var format = function(table, rowId, colId) {
  var rows = table.distinct(rowId); 
  var joined = ee.Join.saveAll('matches').apply({
    primary: rows, 
    secondary: table, 
    condition: ee.Filter.equals({
      leftField: rowId, 
      rightField: rowId
    })
  });
         
  return joined.map(function(row) {
      var values = ee.List(row.get('matches'))
        .map(function(feature) {
          feature = ee.Feature(feature);
           return [feature.get(colId),ee.Number(feature.get('kndvi')).format('%.3f')];
        });
      return row.select([rowId]).set(ee.Dictionary(values.flatten()));
    });
};
var Results = format(triplets, 'id', 'imageID');

print("testPoint", testPoint);


//// EXPORTING MULTIPLE LOCATION TIMESERIES ///
Export.table.toDrive({
    collection: Results,
    description: 'Multiple_Locations_KNDVI_time_series',
    folder: 'earthengine',
    fileNamePrefix: 'Kndvi_time_series_multiple',
    fileFormat: 'CSV'
})
1
  • 1
    Please ask only one question per post!
    – M. Nicolas
    Commented Aug 29, 2022 at 22:23

1 Answer 1

0

The availability of images for any satellite is not homogeneous for the whole globe, some regions have a higher image density than others. This can be due to how and when the images are collected, or the fact that some images are discarded because the cloud content is too high (this is especially true in tropical regions). Further, older satellites (such as Landsat 5) tend to have an even lower amount of images available.

Given you are considering a very restrained time period (less than a month), it's likely the error message you are getting is telling you the truth: there are no images available. Try expanding your time interval or changing the region.

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.