Use visualize
function
// var landcover_roi = composite.clip(ft);
var landcover_roi = ee.Image('COPERNICUS/S2/20151001T142056_20161104T062106_T18GYP')
var ndvi =landcover_roi.normalizedDifference(['B8', 'B4']);
// Make a palette: a list of hex strings.
var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
'74A901', '66A000', '529400', '3E8601', '207401', '056201',
'004C00', '023B01', '012E01', '011D01', '011301'];
// make a visualizing variable
var vis = {min: 0, max: 1, palette: palette, bands:['nd']};
// create a new image that will have 'vis-red', 'vis-green' and 'vis-blue' bands and add original value of ndvi
var toexport = ndvi.visualize(vis).addBands(ndvi)
Map.addLayer(ndvi, vis,'Sentinel-2 NDVI')
Export.image.toDrive({
image:toexport,
description: 'ndvi',
scale: 30,
maxPixels:1e13
});
In Python:
import ee
ee.Initialize()
landcover_roi = ee.Image('COPERNICUS/S2/20151001T142056_20161104T062106_T18GYP')
ndvi =landcover_roi.normalizedDifference(['B8', 'B4'])
# Make a palette: a list of hex strings.
palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
'74A901', '66A000', '529400', '3E8601', '207401', '056201',
'004C00', '023B01', '012E01', '011D01', '011301']
palette = ','.join(palette)
# make a visualizing variable
vis = {'min': 0, 'max': 1, 'bands':'nd', 'palette': palette}
# create a new image that will have 'vis-red', 'vis-green' and 'vis-blue' bands and add original value of ndvi
toexport = ndvi.visualize(**vis).addBands(ndvi)
params = {
'fileNamePrefix':'NAME',
'folder':'FOLDER',
'image':toexport.toFloat(), // cast bands
'description': 'ndvi',
'scale': 30,
'maxPixels':1e13
}
ee.batch.Export.image.toDrive(**params)
I think for this particular job you will need your band values to be intgers from 0 to 4. The most straightforward manner to do so is as follows I guess:
var setPalletes = function(image){
image = image.select('NDVI');
var image02 = image.gte(0.2);
var image04 = image.gte(0.4);
var image06 = image.gte(0.6);
var image08 = image.gte(0.8);
return image02.add(image04).add(image06).add(image08);
};
var newImages = series.map(setPalletes);
Map.addLayer(newImages.first(), {bands: ['NDVI'], min: 0, max: 4, palette: ['black','red', 'orange', 'yellow', 'green']}, 'first');
As you can see, now NDVI values will be above 0.8 so maybe you want to rewrite some values to make it look better. I added black as a color for values below 0.2.
Here is a link to the full code:
https://code.earthengine.google.com/05d07373354e0de70684dc7dbb4efab6
Best Answer
If you know the min/max values it's easy enough of course. But if you don't, and want to have the whole thing automated, you'll first need to calculate the min/max values of the image you're trying to display, and then pass those values to the
visParams
argument ofMap.addLayer()
.Here's a solution combining
.reduceRegion()
and.evaluate()
. As your question doesn't have a reproducible example, I've used a DEM image of Switzerland as an example. Just replace those with whatever you're trying to display.This returns: