1

I'm calculating the ndti for a water body over a time period and have successfully made the ndti calculation and can view the results for each time period. In the layer settings I have to set results to greyscale and then set the color palette. I would like to do this programmatically, but am new to GEE and having difficulties. When I set the color palette in Map.addLayer I get an error "ndti: Layer error: Image.visualize: Cannot provide a palette when visualizing more than one band." I understand the error. It seems like I need to convert the multiband to single band first and have tried various additions of

var grayscale = image.expression(
  '(0.299 * R) + (0.587 * G) + (0.114 * B)', {
    'R': image.select('B4'),
    'G': image.select('B3'),
    'B': image.select('B2')
  }
);

to the code but run into other errors with applying to image collection. Not sure how to resolve. Any suggestions would be appreciated.

// Add Sentinel 2 Images and filter
var sentinelImage = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
   .select(['B3','B4','B8'])
  .filterDate('2025-08-06','2025-08-06')
  .filterBounds(AOI)
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',45))
  
.map(function(img){
  var bands = img.select('B3','B4','B8').multiply(0.0001)
  //Create watermask
  var ndwi = bands.normalizedDifference(['B3','B8']).rename('ndwi')
  var watermask = ndwi.gt(0.1)
  //Calculate ndti
  var ndti = bands.normalizedDifference(['B4','B3']).rename('ndti')
  //Mask non-water areas
  return ndti.updateMask(watermask)
  .copyProperties(img,['system:time_start','system:time_end'])
  })

var palette = ['blue', 'green', 'yellow', 'red'];
var vis = {
     min: -1,
     max: 1,
     palette: palette
   };

Map.addLayer(sentinelImage.toBands().clip(AOI), vis, 'ndti', false);
print(sentinelImage);

1 Answer 1

1

toBands() is converting your ImageColection with N images to a single Image with N bands which is probably not what you want for display. You can display a mosaiced version of the image collection or reduce it to a single image, see Map.addLayer and various reducers such as ImageCollection.mean

// Add Sentinel 2 Images and filter
var sentinelImage = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
   .select(['B3','B4','B8'])
  .filterDate('2025-08-06','2025-08-06')
  .filterBounds(AOI)
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',45))
  .map(function(img){
    var bands = img
      .select('B3','B4','B8')
      .clip(AOI)
      .multiply(0.0001)
    //Create watermask
    var ndwi = bands.normalizedDifference(['B3','B8']).rename('ndwi');
    var watermask = ndwi.gt(0.1);
    //Calculate ndti
    var ndti = bands.normalizedDifference(['B4','B3']).rename('ndti');
    //Mask non-water areas
    return ndti
      .updateMask(watermask)
      .copyProperties(img,['system:time_start','system:time_end']);
  })

var palette = ['blue', 'green', 'yellow', 'red'];
var vis = {
     min: -1,
     max: 1,
     palette: palette
   };

// Images within ImageCollections are automatically mosaicked according to mask
// status and image order. The last image in the collection takes priority,
// invalid pixels are filled by valid pixels in preceding images.
Map.addLayer(sentinelImage, vis, 'ndti mosaiced', false);

// Reduce the collection to a single image using a variety of methods.
var mean = sentinelImage.median();
Map.addLayer(sentinelImage, vis, 'ndti median', false);

var mean = sentinelImage.mean();
Map.addLayer(sentinelImage, vis, 'ndti mean', false);

var min = sentinelImage.min();
Map.addLayer(min, vis, 'ndti min');

var max = sentinelImage.max();
Map.addLayer(max, vis, 'ndti max');

// Or add them all individually
var image_count = sentinelImage.size().getInfo();
var image_list = sentinelImage.toList(image_count); 
print(image_count);
for(var i = 0; i < image_count; i++){  //Warning client side loop, will be slow!
  var image = ee.Image(image_list.get(i));
  var date = image.date().format('YYYY-MM-dd').getInfo();
  Map.addLayer(image, vis, date, false);
  
}

enter image description here

3
  • Thank you so much for the code and answer. This works great for completing statistical results that cover the multiple days over the April 2025 through July 2025 time period. Something I was hoping to do as well. The original code completed the ndti for multiple days from April 2025 through July 2025 and I could view the ndti for each day separately. Is there away to add the color palette for each day that the ndti was calculated rather than a statistical calculation for all the days over the time period.? Thank you.
    – Rathpr
    Commented 2 days ago
  • 1
    @Rathpr see edit
    – user2856
    Commented yesterday
  • Thanks! so much for the help!
    – Rathpr
    Commented yesterday

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.