1

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(
             qa.bitwiseAnd(cirrusBitMask).eq(0));

  // 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
  .filterDate("2017-12-01","2018-01-30")
  .filterBounds(boundary)
  .map(maskS2clouds)
  .sort("CLOUD_COVERAGE_ASSESSMENT")
  .median()
);

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

// 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'),
}).rename('nd');

print(ndvi);

// 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)
              .merge(agriland1)
              .merge(agriland2)
              .merge(habitation)
              .merge(sandyarea)
              .merge(deepplantation);
var bands = ['nd'];
var training = clip.select(bands).sampleRegions({
  collection: newfc, 
  properties: ['class'], 
  scale: 20,
  geometries:true
});

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

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

Map.addLayer(
  classified, 
  {min: 1, max: 7, palette: ['#0d1898', '#ff0841', '#138b11','#fff81c','#154c17','#529400','#F1B555']},
  'classification');

enter image description here

4

1 Answer 1

2

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'),
}).rename('nd');

clip = clip.addBands(ndvi)

https://code.earthengine.google.co.in/64019c5b9fe1023f8633d182d2388d91

0

Your Answer

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

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