1

I just perform a NDVI classification, and I want to know if I can plot or display the values of the NDVI in different ranges in the same image. For example, from 0.0 to 0.3 is 'blue', from 0.3 to 0.5 'green', etc.

Here is part of my code:

//cargar coleccion de landsat
///////////////////////////////////////////////////////////////
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')


//geometry with the AOI 
var roi = geometry;

//filtrar la imagen de acuerdo a la cobertura de nubes en el poligono
////////////////////////////////////////////////////////////////////
var l8f = l8.filterBounds(roi);

var withCloudiness = l8f.map(function(img_zone) {
  var cloud = ee.Algorithms.Landsat.simpleCloudScore(img_zone).select('cloud');
  var cloudiness = cloud.reduceRegion({
    reducer: 'mean', 
    geometry: roi, 
    scale: 30,
  });
  return img_zone.set(cloudiness);
});

var filteredCollection = withCloudiness.filter(ee.Filter.lt('cloud', 10));


var image = ee.Image(filteredCollection.filterBounds(roi)
    .filterDate('2017-03-30', '2017-07-30')
    .sort('CLOUD_COVER')
    .first());



    var red = image.select('B4');

    //infrarrojo cercano
    var NIR = image.select('B5')


    var nvdi = (NIR.subtract(red)).divide(NIR.add(red)).rename('NDVI');


var filtered_1 = nvdi.gt(0.3); // boolean (0,1) where nvdi > 0.3
var filtered_2 = nvdi.gt(0.5); // boolean (0,1) where nvdi > 0.5
var filtered_3 = nvdi.gt(0.7); // boolean (0,1) where nvdi > 0.7
var filtered_4 = nvdi.gt(0.8); // boolean (0,1) where nvdi > 0.8


var multiplied_1 = nvdi.multiply(filtered_1); // multiply to set <= 0.3 to 0
var multiplied_2 = nvdi.multiply(filtered_2); // multiply to set <= 0.5 to 0
var multiplied_3 = nvdi.multiply(filtered_3); // multiply to set <= 0.7 to 0
var multiplied_4 = nvdi.multiply(filtered_4); // multiply to set <= 0.8 to 0

Map.addLayer(multiplied,{min: -1, max: 1},'ndvi_rice_1'); // plot

I know that there are many ways of select range of values, i´m using less than [lt], greater than [gt].

1 Answer 1

1

I think you're looking for something like this. You can use SLD color ramps to define bounds on what colors are assigned to what values. I added an arbitrary geometry because you didn't provide a shape. Also note that you can use the normalizedDifference() function to calculate NDVI.

// Define region of interest
var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
  .filter(ee.Filter.eq('NAME', 'Waldo'));
print(maineCounties);
var geometry = maineCounties;
Map.centerObject(geometry);

//cargar coleccion de landsat
///////////////////////////////////////////////////////////////
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA');


//geometry with the AOI 
var roi = geometry;

//filtrar la imagen de acuerdo a la cobertura de nubes en el poligono
////////////////////////////////////////////////////////////////////
var l8f = l8.filterBounds(roi);

var withCloudiness = l8f.map(function(img_zone) {
  var cloud = ee.Algorithms.Landsat.simpleCloudScore(img_zone).select('cloud');
  var cloudiness = cloud.reduceRegion({
    reducer: 'mean', 
    geometry: roi, 
    scale: 30,
  });
  return img_zone.set(cloudiness);
});

var filteredCollection = withCloudiness.filter(ee.Filter.lt('cloud', 10));


var image = ee.Image(filteredCollection.filterBounds(roi)
    .filterDate('2017-03-30', '2017-07-30')
    .sort('CLOUD_COVER')
    .first());

var ndvi = image.normalizedDifference(['B5', 'B4']);
print("NDVI image", ndvi);

// Create color ramp based on bounds and colors you're interested in
var NDVI_ramp =
  '<RasterSymbolizer>' +
    '<ColorMap type="ramp" extended="false" >' +
      '<ColorMapEntry color="#0000ff" quantity="0" label="0"/>' +
      '<ColorMapEntry color="#00ff00" quantity="0.3" label="0.3" />' +
      '<ColorMapEntry color="#007f30" quantity="0.5" label="0.5" />' +
      '<ColorMapEntry color="#30b855" quantity="0.7" label="0.7" />' +
    '</ColorMap>' +
  '</RasterSymbolizer>';

Map.addLayer(ndvi, {}, "NDVI");
// Apply sldStyle color ramp to your ndvi image.
Map.addLayer(ndvi.sldStyle(NDVI_ramp), {}, "NDVI with ramp");

Your Answer

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

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