2

I'm trying to reclass the values of a sentinel 2 NDVI creating a function but I don't know exactly how do to it. What I exactly want is: When NDVI values are between -1 and 0, return (-1) ; 0 and 0.3 return (0); 0.4 and 1 return (1).

Here's my idea of script:

//Imagen Sentinel 2A 
var IMGSentinel2A= ee.ImageCollection("COPERNICUS/S2")
.filterDate ('2016-10-01', '2017-03-31')
.filterBounds (geometry)
.filterMetadata ("CLOUDY_PIXEL_PERCENTAGE",'Less_Than', 5);
var filtropix = ee.Image(IMGSentinel2A.max());
var Sentinel2Clip = filtropix .clip (geometry);

//Bandas
var b4_1 = Sentinel2Clip.select('B4');
var b8_1 = Sentinel2Clip.select('B8');

// NDVI
var ndvi1 = b8_1.subtract(b4_1).divide((b8_1.add(b4_1)));


var recl = ndvi1.select('B8');
var repl1 = ee.Image(-1);
var repl2 = ee.Image(0);
var repl3 = ee.Image(1);

var zones = function (image) { 
  return image.where(image.gt(-1).and(image.lt(0)), repl1)
  .add(image.where(image.gt(0).and(image.lt(0.3)), repl2))
  .add(image.where(image.gt(0.3)).and(image.lt(1))) };

var output = dndvi.map(zones);

//palette
var bdif_palette = ['00FF00','FF0000'];

Map.addLayer(output,
             {min: 0, max: 2, palette: bdif_palette},
             'reclass NDVI');

2 Answers 2

0

Try to use the RasterSymbolizer element to display the data either discretely or along a color ramp according to the values such as this for discrete values:

 // Define an SLD style of discrete intervals to apply to the image.
var sld_intervals =
  '<RasterSymbolizer>' +
    '<ColorMap  type="intervals" extended="false" >' +
      '<ColorMapEntry color="#0000ff" quantity="0" label="-1-0"/>' +
      '<ColorMapEntry color="#00ff00" quantity="0.3" label="0-0.3" />' +
      '<ColorMapEntry color="#007f30" quantity="1" label="0.3-1" />' +
    '</ColorMap>' +
  '</RasterSymbolizer>';

Or this for a color ramp:

// Define an sld style color ramp to apply to the image.
var sld_ramp =
  '<RasterSymbolizer>' +
    '<ColorMap type="ramp" extended="false" >' +
      '<ColorMapEntry color="#0000ff" quantity="0" label="-1-0"/>' +
      '<ColorMapEntry color="#00ff00" quantity="0.3" label="0-0.3" />' +
      '<ColorMapEntry color="#007f30" quantity="1" label="0.3-1" />' +
    '</ColorMap>' +
  '</RasterSymbolizer>';
0

I think this will get you there. Make a series of boolean images based on the breakpoints, sum them and then subtract 2 to adjust the values to your final desired range [-1, 1] (you may need to first mask NDVI values greater than 1 or less than -1, if they happen to exist).

var zones = function(image) { 
  return ee.ImageCollection.fromImages([
    image.gt(-1),
    image.gt(0),
    image.gt(0.3)])
    .sum()
    .subtract(2);
};

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.