I have two different landcover class first code give me NDVI with 4 class(low-ndvi, moderate-ndvi, high-ndvi, very-high-ndvi) and second code (supervised classification) that is give me 2 class (water and agriculture). I want to combined these two landcover together that at the end give me 6 class (low-ndvi, moderate-ndvi, high-ndvi, very-high-ndvi, water and agriculture) all of them in one image.


var point = ee.Geometry.Point(62.6755, 28.097);

 var bangalore = geometry
var s2 = ee.ImageCollection("COPERNICUS/S2_SR")
// The following collections were created using the 
// Drawing Tools in the code editor 

var filtered = s2
//.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2019-10-01', '2019-11-30'))

var composite = filtered.median().clip(bangalore) 

var ndvi = composite.normalizedDifference(['B8', 'B4']);
var ndviclass = ee.Image(1)
      .where(ndvi.gt(-1).and(ndvi.lte(0.1)), 1)
      .where(ndvi.gt(0.1).and(ndvi.lte(0.2)), 2)
      .where(ndvi.gt(0.2).and(ndvi.lte(0.3)), 3)
      .where(ndvi.gt(0.3).and(ndvi.lte(1)), 4)

// create colors for those classifications
var visparams = {
   "opacity": 1,
    "min": 1,
    "max": 9,
    "palette": ['f7e084','225129', '369b47', '30eb5b' ]
var ndviclass1 =ndviclass.clip(geometry)
Map.addLayer(ndviclass1, visparams,'ndvi_class');

// Display the input composite.
var rgbVis = {
  min: 0.0,
  max: 3000,
  bands: ['B8', 'B4', 'B3'],
//Map.addLayer(composite, rgbVis, 'image');
// be cationion about bands
var bands = ['B4', 'B8'];
//var gcps = urban.merge(bare).merge(water).merge(vegetation)
var polygons = ee.FeatureCollection([bareland,urban,water]);
var label = 'landcover';
// Overlay the point on the image to get training data.
var training = composite.sampleRegions({
  collection: polygons,
  properties: [label],
  scale: 30

var trained = ee.Classifier.smileCart().train(training, label);

//var classified = image.select(bands).clip(marz).classify(trained);

// Train a classifier.
var classifier = ee.Classifier.smileRandomForest(50).train({
  features: training,  
 classProperty: 'landcover', 
 inputProperties: composite.bandNames()
// // Classify the image

var classified = composite.classify(classifier);

// Remove the "none_agri" class from the classified image
var nonebarelandMask = classified.select('classification').neq(0);

classified = classified.updateMask(nonebarelandMask);

Map.addLayer(classified, {min: 1, max: 3, palette: [ 'green', 'blue']}, 'classes'); 

var gcp = polygons
var gcp = gcp.randomColumn()
var trainingGcp = gcp.filter(ee.Filter.lt('random', 0.6));
var validationGcp = gcp.filter(ee.Filter.gte('random', 0.6));


  • You can use the ee.Image.addBands() function to combine all bands into one image. var combined=classified.addBands(ndviclass1)
    – Padmanabha
    Commented Jun 9, 2023 at 8:31

1 Answer 1


You can remap classified values to the ones you want to use in your final, combined, classification, using remap(). Then you combine them with where(). The snipped below will keep the NDVI class values and remaps agriculture and water to 5 and 6.

var remappedClassification = classified
  .remap([2, 3], [5, 6])
var combinedClassification = ndviclass
  .where(remappedClassification.gte(5), remappedClassification)
Map.addLayer(combinedClassification, {min: 1, max: 6, palette: '#129450, #126e45, #1a482f, #122414, #e1cd73, #14439c'}, 'combined') 


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.