I am trying to represent a raster giving to the palette the "max" and "min" of the raster according to the actual raster values. Instead of giving a minimum value of -1 and maximum value of 1 for the NDVI, I want that the minimum real value of the image represents the minimum value of the palette and the same for the maximum value. For instance, if the minimum value of a NDVI image is -0.7 and the maximum is 0.8 I want those values as "min" and "max" as parameters for the palette.
I thought that looking for the minimum/maximum value of the image with a reducer and transforming them to a Number I would be able to do it. However, when I run the code, I get the following error:
NDVI: Layer error: Can't encode object: abs() Computes the absolute value of the input. Args:this:input (Number): The input value.
The code is the following:
// Load image.
var img = ee.Image('MODIS/006/MOD09GA/2012_03_09');
// Use the normalizedDifference(A, B) to compute (A - B) / (A + B)
var ndvi = img.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']);
var Min = ee.Number(ndvi.reduceRegion({
reducer: ee.Reducer.min(),
//scale: 250,
maxPixels: 1e9,
geometry: geometry,
}));
print(Min, 'NDVImin');
var Max = ee.Number(ndvi.reduceRegion({
reducer: ee.Reducer.max(),
//scale: 250,
maxPixels: 1e9,
geometry: geometry,
}));
print(Max, 'NDVImax');
var BandCompViz = {min:Min, max:Max, palette:
['ffffff','b7f0ae','21f600','0000FF','FDFF92','FF2700','d600ff']};
// Center the map
Map.setCenter(-94.84497, 39.01918, 8);
Map.addLayer(ndvi, BandCompViz, 'NDVI');
https://code.earthengine.google.com/384e02f60104985848eb90fac875dfa6