I have a legend in Google Earth Engine which pulls minimum and maximum values for my raster (imported as a cloud asset).
I would like to round those numbers in order to remove the decimals. It seems like ee.Number.round()
is what I want. It makes sense to me to input this command after I've retrieved my rater's max and min values as shown below.
// Calculate min/max values
var minMax = image.reduceRegion({reducer: ee.Reducer.minMax(),
geometry: image.geometry(),
scale: image.projection().nominalScale(),
bestEffort: true,
maxPixels: 1e9})
// Rename keys
var minMax = minMax.rename(minMax.keys(), ['max','min']);
// Retrieve dictionary values and pass to visParam settings
minMax.evaluate(function(val){
var min = val.min;
var max = val.max;
var visParam = {
min: min,
max: max,
palette: palettes.colorbrewer.YlOrRd[9]
};
var legendMin = ee.Number(val.min).round();
var legendMax = ee.Number(val.max).round();
var vis = {min: legendMin, max: legendMax, palette: visParam.palette}
// Create a mask to remove 0 values
image = image.updateMask(image.neq(0));
Map.addLayer(image, visParam, "Image");
However, that seems to break my legend.
What would be the proper way to use the Number.round()
command to get my legend numbers to exclude decimals?
Best Answer
First, visualization parameters must be client-side objects. Thus,
.getInfo()
is needed when defining the colormap limits invis
:With this, your legend will not be 'broken' and will show the specified label ticks, which would be the rounded minimum (0), the rounded maximum (335), and the middle value (167.5). Note that the middle value is not rounded. Following your full code sample, rounding could be done using
Math.round
(note client-side function) when specifying theui.Panel
parameters.