1

I've been working on LULC change detection using Earth Engine. When I run to export any of the TIFF file I'm getting the same error.

This is the error I get every time

Error: Collection.toList: Error in map(ID=1): Image.select: Pattern 'blue' did not match any bands. (Error code: 3)

Link for the code in Google Earth Engine: https://code.earthengine.google.com/67d0157210c5635b3d741f1661431bb8

Code:

//apenter the map on the Area of Interest (AOI)
Map.centerObject(AOI)

// Add the AOI as a layer to the map
Map.addLayer(AOI)

// Remove the clouds and haze from the Landsat images
var cloudMaskL457 = function(image) {
var qa = image.select('pixel_qa');
var cloud = qa.bitwiseAnd(1 << 5)
            .and(qa.bitwiseAnd(1 << 7))
            .or(qa.bitwiseAnd(1 << 3))
    
var mask2 = image.mask().reduce(ee.Reducer.min());
return image.updateMask(cloud.not()).updateMask(mask2).divide(10000).copyProperties(image, ["system:time_start"]);
};

function maskL8sr(image) {
  var cloudShadowBitMask = 1 << 3;
  var cloudBitMask = 1 << 5;
  var qa = image.select('pixel_qa');
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
     .and(qa.bitwiseAnd(cloudBitMask).eq(0));
  return image.updateMask(mask).divide(10000).copyProperties(image, ["system:time_start"]);
}

// Set the observation year range
var startyear = 1900;
var endyear = 2021;

// Create a list of years
var years = ee.List.sequence(startyear, endyear);

// Define the Landsat bands and names
var l5Bands = ['B1','B2','B3','B4','B5','B7','pixel_qa'];
var l5names = ['blue','green','red','nir','swir1','swir2','pixel_qa'];

var l7Bands = ['B1','B2','B3','B4','B5','B7','pixel_qa'];
var l7names = ['blue','green','red','nir','swir1','swir2','pixel_qa'];

var l8Bands = ['B2','B3','B4','B5','B6','B7','pixel_qa'];
var l8names = ['blue','green','red','nir','swir1','swir2','pixel_qa'];

// Filter the Landsat imagery by date, cloud cover, and location
var L8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
  .filterDate('2012-01-01','2021-12-31')
  .filterMetadata('CLOUD_COVER','less_than', 50)
  .filterBounds(AOI)
  .select(l8Bands,l8names)
  .map(maskL8sr);

var L7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
  .filterDate('2000-01-01','2014-12-30')
  .filterMetadata('CLOUD_COVER','less_than', 50)
  .filterBounds(AOI)
  .select(l7Bands,l7names)
  .map(cloudMaskL457);

var L5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
  .filterDate('1990-01-01','2011-12-31')
  .filterMetadata('CLOUD_COVER','less_than', 100)
  .filterBounds(AOI)
  .select(l5Bands,l5names)
  .map(cloudMaskL457);

// Merge the Landsat collections into a single collection
var full_coll = L8.merge(L7).merge(L5)
print(full_coll)

//annual collection
var annual_collection = ee.ImageCollection.fromImages(years.map(function (y)
{
   var annual = full_coll.filter(ee.Filter.calendarRange(y, y,'year'))
                     .median().clip(AOI);
   return annual.set('year',y)
}));

print(annual_collection)

var viz = {min:0,max:0.3,bands:"red,green,blue"};


var img_2021 = annual_collection.filter(ee.Filter.eq('year',2021)).mosaic()
Map.addLayer(img_2021,viz,"Landsat 2021",false); 

var img_2011 = annual_collection.filter(ee.Filter.eq('year',2011)).mosaic()
Map.addLayer(img_2011,viz,"Landsat 2011",false);

var img_2006 = annual_collection.filter(ee.Filter.eq('year',2006)).mosaic()
Map.addLayer(img_2006,viz,"Landsat 2006",false);

var img_1991 = annual_collection.filter(ee.Filter.eq('year',1991)).mosaic()
Map.addLayer(img_1991,viz,"Landsat 1991",false);

var img_1996 = annual_collection.filter(ee.Filter.eq('year',1996)).mosaic()
Map.addLayer(img_1996,viz,"Landsat 1996",false);

var img_2012 = annual_collection.filter(ee.Filter.eq('year',2012)).mosaic()
Map.addLayer(img_2012,viz,"Landsat 2012",false);

var img_2002 = annual_collection.filter(ee.Filter.eq('year',2002)).mosaic()
Map.addLayer(img_2002,viz,"Landsat 2002",false);


var samples = vegetation.merge(agriculture).merge(built_up).merge(waterbody).merge(barren_land).merge(others)
var predictionBands = ['blue','green','red','nir','swir1','swir2'];

var TrainingImage_L5 = img_1991.select(predictionBands).float();
var TrainingImage_L7 = img_2002.select(predictionBands).float();
var TrainingImage_L8 = img_2021.select(predictionBands).float();

var classifierTraining_L5 = TrainingImage_L5.select(predictionBands).sampleRegions({collection: samples, properties: ['class'], scale: 30});
var classifierTraining_L7 = TrainingImage_L7.select(predictionBands).sampleRegions({collection: samples, properties: ['class'], scale: 30});
var classifierTraining_L8 = TrainingImage_L8.select(predictionBands).sampleRegions({collection: samples, properties: ['class'], scale: 30});

print('Band value',classifierTraining_L5)

var classifierTraining = classifierTraining_L5.merge(classifierTraining_L7).merge(classifierTraining_L8)

print(classifierTraining)

var withRandom = classifierTraining.randomColumn('random');

var split = 0.7;  //roughly 70% training, 30% testing.
var trainingPartition =withRandom.filter(ee.Filter.lt('random', split));
var testingPartition = withRandom.filter(ee.Filter.gte('random', split));

// Train RF classifier
var RF = ee.Classifier.smileRandomForest(100).train({features:trainingPartition, classProperty: 'class', inputProperties: predictionBands});

print('RF train error matrix: ', RF.confusionMatrix());
print('RF train accuracy: ', RF.confusionMatrix().accuracy());
print('RF train Kappa: ', RF.confusionMatrix().kappa());

var test = testingPartition.classify(RF);

var testAccuracy = test.errorMatrix('class', 'classification');
print('RF test error matrix: ', testAccuracy);
print('RF test accuracy: ', testAccuracy.accuracy());
print('RF test Kappa: ', testAccuracy.kappa());

var LULC = annual_collection.map(function(image){
  var classified_RF = image.select(predictionBands).classify(RF);
  return classified_RF.copyProperties(image, ['year']);
})

var classified_image_2021 = LULC.filter(ee.Filter.eq('year',2021)).first()
Map.addLayer(classified_image_2021,{min:0, max:5,
 palette: ['yellow', 'darkgreen', 'red', 'blue', 'orange', 'pink']},
 'Random_forest 2021');
 
 
 var classified_image_1991 = LULC.filter(ee.Filter.eq('year',2021)).first()
Map.addLayer(classified_image_1991,{min:0, max:5,
 palette: ['yellow', 'darkgreen', 'red', 'blue', 'orange', 'pink']},
 'Random_forest 1991');
 
 var list_year = ['1990','1991','1992','1993','1994','1995','1996','1997','1998','1999',
                  '2000','2001','2002','2003','2004','2005','2006','2007','2008','2009',
                  '2010','2011','2012','2013','2014','2015','2016','2017','2018','2019',
                  '2020','2021']
var n = LULC.size().getInfo();


//------------------LULC-----------------
var colList = LULC.toList(n);

for (var i = 0; i < n; i++) {
     var img = ee.Image(colList.get(i));
     
     var img_id = list_year[i]
     
     Export.image.toDrive({
       image: img,
       description: 'LULC_' + img_id,
       folder: 'LULC',
       fileNamePrefix: 'LULC_' + img_id,
       region: AOI,
       scale: 30,
       maxPixels: 1e13})
}


var difference1991_2021 = classified_image_1991.subtract(classified_image_2021)
difference1991_2021 = difference1991_2021 .neq(0)

Map.addLayer(difference1991_2021, {min:0, max:1,
  palette: ['green', 'red']},
  'difference 1991-2021');
  
Export.image.toDrive({
  image: difference1991_2021,
  description: "difference1991_2021",
  region: AOI,
  scale: 30,
  maxPixels: 1e13})

1 Answer 1

4

The problem is that there are no images available from 1900 - 1990; thus, the classifier outputs an error when it tries to classify an empty image. An easy fix would be to simply change the startyear to 1990.

var startyear = 1990;

Other alternative would be to filter the image collection before classifying all the images inside it.

var LULC = annual_collection.filter(ee.Filter.gte('year', 1990)).map(function(image){
  var classified_RF = image.select(predictionBands).classify(RF);
  return classified_RF.copyProperties(image, ['year']);
});

Your Answer

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