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);