Google Earth Engine Error – Layer Error: Image.rename: The Number of Names (1) Must Match the Number of Bands (10)

floodgoogle-earth-enginegoogle-earth-engine-javascript-apilandsat-7

I am using Landsat 7 data (ee.ImageCollection("LANDSAT/LE07/C01/T1_TOA")) for flood mapping. I am trying to write the code but I'm stuck at a line where it says "Layer error: Image.rename: The number of names (1) must match the number of bands (10)". I'm posting the code below.

Map.addLayer(geometry, {color: 'grey'}, 'Brahmani District');
////

//mask clouds
function maskLandsatclouds(image) {
  var qa = image.select('BQA')
  var cloudBitMask = ee.Number(2).pow(4).int()
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
  return image.updateMask(mask)
      .select("B.*")
      .copyProperties(image, ["system:time_start"])
}

var before = collection.filterBounds(geometry)
                .filterDate('2000-11-01','2001-04-30')
                .filter(ee.Filter.lt("CLOUD_COVER", 0.1))
                .filterBounds(geometry)
                .map(maskLandsatclouds)

print(before.size(),'Number of Images in the collection');
print(before,'before_collection');


var mosaic_before = before.mosaic().clip(geometry);
Map.addLayer(mosaic_before,{bands:['B3','B2','B1']},'Mosaic_before')
////
///var first_image = before.first();
///print(first_image,'First Image')
///Map.addLayer(first_image,{bands:['B3','B2','B1']},'First image from collection');
////

//mask clouds2
function maskLandsatclouds(image) {
  var qa = image.select('BQA')
  var cloudBitMask = ee.Number(2).pow(4).int()
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
  return image.updateMask(mask)
      .select("B.*")
      .copyProperties(image, ["system:time_start"])
}

var after = collection.filterBounds(geometry)
                .filterDate('2001-06-01','2002-02-20')
                .filter(ee.Filter.lt("CLOUD_COVER", 0.1))
                .filterBounds(geometry)
                .map(maskLandsatclouds)

print(after.size(),'Number of Images in the collection');
print(after,'after_collection');


var mosaic_after = after.mosaic().clip(geometry);
Map.addLayer(mosaic_after,{bands:['B3','B2','B1']},'Mosaic_after')

/////Till this point everything is correct


var difference = mosaic_after.divide(mosaic_before).clip(geometry);
Map.addLayer(difference, {min:0, max:1}, 'Flood Area', false);


/////
// Define a threshold
var diffThreshold = 1.25;
// Initial estimate of flooded pixels
var flooded = difference.gt(diffThreshold).rename('water').selfMask();
Map.addLayer(flooded, {min:0, max:1, palette: ['orange']}, 'Initial Flood Area', false);


// Mask out area with permanent/semi-permanent water
var permanentWater = gsw.select('seasonality').gte(5).clip(geometry)
var flooded = flooded.where(permanentWater, 0).selfMask()
Map.addLayer(permanentWater.selfMask(), {min:0, max:1, palette: ['blue']}, 'Permanent Water')

// Mask out areas with more than 5 percent slope using the HydroSHEDS DEM
var slopeThreshold = 5;
var terrain = ee.Algorithms.Terrain(hydrosheds);
var slope = terrain.select('slope');
var flooded = flooded.updateMask(slope.lt(slopeThreshold));
Map.addLayer(slope.gte(slopeThreshold).selfMask(), {min:0, max:1, palette: ['cyan']}, 'Steep Areas', false)

        
// Remove isolated pixels
// connectedPixelCount is Zoom dependent, so visual result will vary
var connectedPixelThreshold = 8;
var connections = flooded.connectedPixelCount(25)
var flooded = flooded.updateMask(connections.gt(connectedPixelThreshold))
Map.addLayer(connections.lte(connectedPixelThreshold).selfMask(), {min:0, max:1, palette: ['yellow']}, 'Disconnected Areas', false)

Map.addLayer(flooded, {min:0, max:1, palette: ['red']}, 'Flooded Areas');

Link for the code:
https://code.earthengine.google.co.in/?scriptPath=users%2FTanmoy_Majumder%2FBrahmani_Flood%3APractice_12-04-22

Best Answer

var flooded = difference.gt(diffThreshold).rename('water').selfMask();

difference has multiple bands. When I look back to see where it came from, I find maskLandsatclouds() which did .select("B.*") — selecting multiple bands. You will need to change your code, either in maskLandsatclouds() or after it, to select the single band that you want to calculate with and rename to "water".

Related Question