Google Earth Engine – Downloading and Exporting Image to Google Drive with Color Palette

color rampgoogle-earth-engineimagery

When I download Analysis results from Google Earth Engine, the image is exported in to google drive but it does not have a color palette. How do i add color palette with the exported image using earth engine.

var landcover_roi = composite.clip(ft);
var ndvi =landcover_roi.normalizedDifference(['B8', 'B4']) ;
Map.addLayer(landcover_roi,{bands: ['B6', 'B4', 'B3'], min: 0, max: 0.3}, 'From Fusion Table');



// 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'];

// Center the map
Map.setCenter(87.32,22.40, 6);


Map.addLayer(ndvi,{min: 0, max: 1, palette: palette},'Sentinel-2 NDVI')
Export.image.toDrive({
  image:ndvi,
  description: 'ndvi',
  scale: 30,
 // region: geometry,
  maxPixels:34089663741
  });

Best Answer

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)