1

I am very new to GEE. I have been trying to figure out how to assign different colours to different NDVI values. Let's say, in following code, I want to assign 'red' for NDVI value of 0.2-0.4, 'orange' for 0.4-0.6, 'yellow' for 0.6-0.8, and 'green' for 0.8-1. When I use following code, I feel that I do not have full control over the colours that are being assigned to the NDVI values. The colours I get are gradual based on the NDVI value at certain pixel. What I am trying to do is displaying all pixels that fall in the respective NDVI range in the same colour (something that is done in symbology of ArcMap or QGIS). link to the code: https://code.earthengine.google.com/1f92036224ea8e7e193820ca30c1cef7

Code I have:

// Point for filterbound
var roi = ee.Geometry.Point([92.1339935642684,21.19472665398992])

// Center the display to ROI.
 Map.centerObject(roi, 11);
// Function to get NDVI.
var getNDVI = function(image){
  var NDVI = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
  var clouds = image.select('QA60').gte(1024).unmask().not().rename('CloudMask')
  return image.addBands(NDVI).select('NDVI');
  //.addBands(clouds).mask(clouds) 
  };

// Gets the current time.  
var eeNow = ee.Date(Date.now());

// SET OBSERVATION PERIOD
var startDate = '2018-02-15';
var endDate = eeNow;

// Gets image collection of Observation Period
var series = ee.ImageCollection('COPERNICUS/S2').filterDate(startDate, endDate)
          .filterBounds(roi)
          .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))

// APPLY NDVI FUNCTION TO OBSERVATIONS
series = series.map(getNDVI)
 Map.addLayer(series.first(), {bands: ['NDVI'], min:0.2, max: 1, palette: ['red', 'orange', 'yellow', 'green']}, 'first')
1

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

  • It worked for me but, I had to convert my image to an ee.ImageCollection first (equivalent to series variable). How can I use an ee.Image directly? – xunilk Mar 4 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.