1

I am trying to add the (amount of pixels of) NDWI and Clouds as bands to my image collection. The NDWI works out, but I get stuck when adding the Clouds. The error refers to an incompatible number of bands in the image, but I don´t know where exactly it refers to.

Any ideas?

Similarly I would like to add the cloudfunction to the chart.

Am I doing this right?

//-------FUNCTION NDWI-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var date_start = ee.Date.fromYMD(2016,1,1);
var date_end = ee.Date.fromYMD(2018,12,31);
l8 = l8.filterBounds(aoi).filterDate(date_start,date_end);
var landsat8 = l8
var waterThreshold = 0;
var cloudThreshold = 0;

//-------WATERFUNCTION-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
var waterfunction = function(image){
//add the NDWI band to the image
var ndwi = image.normalizedDifference(['B3', 'B6']).rename('NDWI');
  //get pixels above the threshold
  var water01 = ndwi.gt(waterThreshold);
  //mask those pixels from the image
  image = image.updateMask(water01).addBands(ndwi);
  var area = ee.Image.pixelArea();
  var waterArea = water01.multiply(area).rename('waterArea');
  image = image.addBands(waterArea);
  var stats = waterArea.reduceRegion({
    reducer: ee.Reducer.sum(), 
    geometry: aoi, 
    scale: 30,
  });
  return image.set(stats);
};

var collection = landsat8.map(waterfunction);
print('NDWI',collection);

//-------CLOUDFUNCTION-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
var cloudfunction = function(image){
  //add the NDWI band to the image
  var cloud= image.select(['B4','B3','B2']).rename('Clouds');
  //get pixels above the threshold
  var cloud01 = cloud.gt(10500).lt(12000);
  //mask those pixels from the image
  image = image.updateMask(cloud01).addBands(cloud);
  var area = ee.Image.pixelArea();
  var cloudArea = cloud01.multiply(area).rename('cloudArea');
  image = image.addBands(cloudArea);
  var stats = cloudArea.reduceRegion({
    reducer: ee.Reducer.sum(), 
    geometry: aoi, 
    scale: 30,
  });
  return image.set(stats);
};

var collection = landsat8.map(cloudfunction);
print('NDWI + Clouds',collection);


var chart = ui.Chart.image.series({
  imageCollection: collection.select('waterArea','cloudArea'), 
  region: aoi, 
  reducer: ee.Reducer.sum(), 
  scale: 30,
});

print(chart);
2

There are a few issues in your code example.

  1. Your statement for calculating the cloud variable is incorrect. It selects 3 bands ['B4','B3','B2'], but only specifies 1 band name 'Clouds':
var cloud = image.select(['B4','B3','B2']).rename('Clouds');
  1. When you map cloudfunction over your image collection, you overwrite the previously defined collection object.
var collection = landsat8.map(waterfunction);
var collection = landsat8.map(cloudfunction);

instead you probably want to map both functions over the landsat8 collection:

var collection = landsat8.map(waterfunction).map(cloudfunction);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.