I want to classify my NDVI raster using training data in Google Earth Engine. For that i am using the following code. But it is returning some error like ndvi is not a usable band. Please help in this

// Cloud Masking function
function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = ee.Number(2).pow(10).int();
  var cirrusBitMask = ee.Number(2).pow(11).int();

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

  // Return the masked and scaled data.
  return image.updateMask(mask).divide(10000);

// AOI of Study Area
var boundary = ee.FeatureCollection('ft:1rbhNtC1TqDBvY9Rt2BZR-DjhpIPuC3nU5kmz49WW');//Jorhat Boundary
//var boundary = ee.FeatureCollection('ft:1ABffZYEE4XhMTOXsoSfWENKXBK2fQfOrdMplEaVo');//Midnapur Boundary

// Import of Images (Sentinel 2 multispectral)
var image = ee.ImageCollection(sent2img

// Preprocessing 
var mosaic = image.mosaic()
var clip = mosaic.clip(boundary);

// FCC creation and visualisation of AOI     
Map.addLayer(clip, {bands: ['B8','B4','B3'], min: 0, max: 0.3},'clip');

// NDVI  calculation
var ndvi = clip.expression(
    ' ((NIR - RED) / (NIR + RED))', {
      'NIR': clip.select('B8'),
      'RED': clip.select('B4'),


// Colour Palette
var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
               '74A901', '66A000', '529400', '3E8601', '207401', '056201',
               '004C00', '023B01', '012E01', '011D01', '011301'];

// Map display
Map.addLayer(ndvi, {min:0, max:1, palette: palette},"NDVI");

// Training Classes 
var newfc = wb.merge(plantation)
var bands = ['nd'];
var training = clip.select(bands).sampleRegions({
  collection: newfc, 
  properties: ['class'], 
  scale: 20,

var classifier = ee.Classifier.cart().train({
  features: training, 
  classProperty: 'class', 
  inputProperties: bands

var classified = clip.select(bands).classify(classifier);

  {min: 1, max: 7, palette: ['#0d1898', '#ff0841', '#138b11','#fff81c','#154c17','#529400','#F1B555']},

enter image description here


You forgot to add ndvi band to the image:

Do this:

// -----------  NDVI  calculation

var ndvi = clip.expression(
    ' ((NIR - RED) / (NIR + RED))', {
      'NIR': clip.select('B8'),
      'RED': clip.select('B4'),

clip = clip.addBands(ndvi)


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.