2

I want to be able to classify my image using bands for prediction (B2, B3, B4, etc.) but I also want to use indices as "bands" (B2, B3, B4, NDVI, EBBI, MSAVI2, etc.). Unfortunately, I can't seem to think of the proper script for this and it keeps giving me the error:

Error

Here is my script:

// Creating a buffer around my region of interest
var buffer = ee.Geometry.Point([103.83841283715412,1.3736828122619071]).buffer(10000);
Map.centerObject(buffer, 12);

// Load Landsat 8 surface reflectance data
// Look for adequate image filtered by date and cloud cover and region
var l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
              .filterBounds(buffer)
              .filter(ee.Filter.calendarRange(2018,2019,'year'))
              .filter(ee.Filter.calendarRange(4,5,'month'))
              .map(function(image){return image.clip(buffer)});

// Function to cloud mask from the Fmask band of Landsat 8 SR data.
function maskL8sr(image) {
  // Bits 3 and 5 are cloud shadow and cloud, respectively.
  var cloudShadowBitMask = ee.Number(2).pow(3).int();
  var cloudsBitMask = ee.Number(2).pow(5).int();

  // Get the pixel QA band.
  var qa = image.select('pixel_qa');

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
      .and(qa.bitwiseAnd(cloudsBitMask).eq(0));

  // Return the masked image, scaled to [0, 1].
  return image.updateMask(mask).divide(10000);
}




// Map the function over years of data and take the median.
var composite = l8sr.map(maskL8sr)
                    .median();


//The Band Variables for Landsat 8
var B2_Blue = composite.select('B2');
var B3_Green = composite.select('B3');
var B4_Red = composite.select('B4');
var B5_NIR = composite.select('B5');
var B6_SWIR1 = composite.select('B6');
var B7_SWIR2 = composite.select('B7');
var B10_TIR1 = composite.select('B10');
var B11_TIR2 = composite.select('B11');


// Creating an add variable function for Landsat 8 NDVI. 
var addNDVI_L8 = function(image) {
  var ndvi = composite.expression(
  '(B5-B4)/(B5+B4)', {
    'B5': B5_NIR,
    'B4': B4_Red
  }).rename('ndvi');
  return composite.addBands(ndvi);
};


// Creating an add variable function for Landsat 8 MSAVI2. 
var addMSAVI2_L8 = function(image) {
  var msavi2 = composite.expression(
  '(2 * B5 + 1 - sqrt(pow((2 * B5 + 1), 2) - 8 * (B5 - B4)) ) / 2', {
    'B5': B5_NIR,
    'B4': B4_Red
  }).rename('msavi2');
  return composite.addBands(msavi2);
};

// Creating an add variable function for Landsat 8 EBBI. 
var addEBBI_L8 = function(image) {
  var ebbi = composite.expression(
    '(B6-B5)/(sqrt(B6+B10)*(10))', {
    'B5': B5_NIR,
    'B6': B6_SWIR1,
    'B10': B10_TIR1
  }).rename('ebbi');
  return composite.addBands(ebbi);
};

//Defining the composite image with the added NDVI band
var composite_NDVI = addNDVI_L8(composite);

//printing to see if it has 13 bands (i.e. with the NDVI band added) (THIS WORKS!)
print(composite_NDVI, 'Image with added NDVI band')

//THIS CODE DOESN'T WORK "composite.map is not a function"
var composite_indices = composite.map(addNDVI_L8).map(addMSAVI2_L8).map(addEBBI_L8);

print(composite_indices, 'composite indices');

so my script, doesn't seem to work:

var composite_indices = composite.map(addNDVI_L8).map(addMSAVI2_L8).map(addEBBI_L8);
1

map() function is for ImageCollection object. Computing median, you're converting ImageCollection object in Image object, thas why doesn't work your code. Try this:

// Creating a buffer around my region of interest
var buffer = ee.Geometry.Point([103.83841283715412,1.3736828122619071]).buffer(10000);
Map.centerObject(buffer, 12);

// Load Landsat 8 surface reflectance data
// Look for adequate image filtered by date and cloud cover and region
var l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
              .filterBounds(buffer)
              .filter(ee.Filter.calendarRange(2018,2019,'year'))
              .filter(ee.Filter.calendarRange(4,5,'month'))
              .map(function(image){return image.clip(buffer)});

// Function to cloud mask from the Fmask band of Landsat 8 SR data.
function maskL8sr(image) {
  // Bits 3 and 5 are cloud shadow and cloud, respectively.
  var cloudShadowBitMask = ee.Number(2).pow(3).int();
  var cloudsBitMask = ee.Number(2).pow(5).int();

  // Get the pixel QA band.
  var qa = image.select('pixel_qa');

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
      .and(qa.bitwiseAnd(cloudsBitMask).eq(0));

  // Return the masked image, scaled to [0, 1].
  return image.updateMask(mask).divide(10000);
}

// Map the function over years of data and take the median.
var composite = l8sr.map(maskL8sr);

// Creating an add variable function for Landsat 8 NDVI. 
var addNDVI_L8 = function(image) {
  var ndvi = image.expression(
  '(B5-B4)/(B5+B4)', {
    'B5': image.select('B5'),
    'B4': image.select('B4')
  }).rename('ndvi');
  return image.addBands(ndvi);
};

// Creating an add variable function for Landsat 8 MSAVI2. 
var addMSAVI2_L8 = function(image) {
  var msavi2 = image.expression(
  '(2 * B5 + 1 - sqrt(pow((2 * B5 + 1), 2) - 8 * (B5 - B4)) ) / 2', {
    'B5': image.select('B5'),
    'B4': image.select('B4')
  }).rename('msavi2');
  return image.addBands(msavi2);
};

// Creating an add variable function for Landsat 8 EBBI. 
var addEBBI_L8 = function(image) {
  var ebbi = image.expression(
    '(B6-B5)/(sqrt(B6+B10)*(10))', {
    'B5': image.select('B5'),
    'B6': image.select('B6'),
    'B10': image.select('B10')
  }).rename('ebbi');
  return image.addBands(ebbi);
};

//THIS CODE DOESN'T WORK "composite.map is not a function"
var composite_indices = composite.map(addNDVI_L8)//.map(addMSAVI2_L8).map(addEBBI_L8);

I modified your functions to make them more readable


An specific approach for Image object:

// Creating a buffer around my region of interest
var buffer = ee.Geometry.Point([103.83841283715412,1.3736828122619071]).buffer(10000);
Map.centerObject(buffer, 12);

// Load Landsat 8 surface reflectance data
// Look for adequate image filtered by date and cloud cover and region
var l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
              .filterBounds(buffer)
              .filter(ee.Filter.calendarRange(2018,2019,'year'))
              .filter(ee.Filter.calendarRange(4,5,'month'))
              .map(function(image){return image.clip(buffer)});

// Function to cloud mask from the Fmask band of Landsat 8 SR data.
function maskL8sr(image) {
  // Bits 3 and 5 are cloud shadow and cloud, respectively.
  var cloudShadowBitMask = ee.Number(2).pow(3).int();
  var cloudsBitMask = ee.Number(2).pow(5).int();

  // Get the pixel QA band.
  var qa = image.select('pixel_qa');

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
      .and(qa.bitwiseAnd(cloudsBitMask).eq(0));

  // Return the masked image, scaled to [0, 1].
  return image.updateMask(mask).divide(10000);
}

// Map the function over years of data and take the median.
var composite = l8sr.map(maskL8sr).median();

// Creating an add variable function for Landsat 8 NDVI. 
var compute_indices = function(image) {
  var ndvi = image.expression(
  '(B5-B4)/(B5+B4)', {
    'B5': image.select('B5'),
    'B4': image.select('B4')
  }).rename('ndvi');

  var msavi2 = image.expression(
  '(2 * B5 + 1 - sqrt(pow((2 * B5 + 1), 2) - 8 * (B5 - B4)) ) / 2', {
    'B5': image.select('B5'),
    'B4': image.select('B4')
  }).rename('msavi2');

  var ebbi = image.expression(
    '(B6-B5)/(sqrt(B6+B10)*(10))', {
    'B5': image.select('B5'),
    'B6': image.select('B6'),
    'B10': image.select('B10')
  }).rename('ebbi');

  return image.addBands(ndvi).addBands(msavi2).addBands(ebbi);
};

// Compute multiple indices
var composite_indices = compute_indices(composite);

enter image description here

  • Hi! Thank you so much! My main goal is to add the indices as bands for training/prediction parameters. I just started with GEE around a month ago and this has helped immensely! :) – Linus AG May 24 at 17:26
  • No problem. If this was helpful, you can accept the answer to close this topic. Cheers – aldo_tapia May 24 at 18:45

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.