1

I plotted a raster onto Google Earth Engine, with a colour palette, and created a legend as shown.

// Pull the package from the GitHub repo which stores colour palettes to be used in GEE
// See https://github.com/gee-community/ee-palettes for more info
var palettes = require('users/gena/packages:palettes');

// Choose the yellow, orange, red colour palette with 9 classes
var palette = palettes.colorbrewer.YlOrRd[9];
var vis = {min: 0, max: 335, palette: palette};

// Add a raster layer and enable the colour palette
Map.addLayer(image, {min: 0, max: 335, palette: palette});

// Creates a color bar thumbnail image for use in legend from the given color palette
function makeColorBarParams(palette) {
  return {
    bbox: [0, 0, 1, 0.1],
    dimensions: '100x10',
    format: 'png',
    min: 0,
    max: 1,
    palette: palette,
  };
}

// Create the colour bar for the legend
var colorBar = ui.Thumbnail({
  image: ee.Image.pixelLonLat().select(0),
  params: makeColorBarParams(vis.palette),
  style: {stretch: 'horizontal', margin: '0px 8px', maxHeight: '24px'},
});

// Create a panel with three numbers for the legend
var legendLabels = ui.Panel({
  widgets: [
    ui.Label(vis.min, {margin: '4px 8px'}),
    ui.Label(
        ((vis.max-vis.min) / 2+vis.min),
        {margin: '4px 8px', textAlign: 'center', stretch: 'horizontal'}),
    ui.Label(vis.max, {margin: '4px 8px'})
  ],
  layout: ui.Panel.Layout.flow('horizontal')
});

// Legend title
var legendTitle = ui.Label({
  value: 'Carbon (tons C)',
  style: {fontWeight: 'bold'}
});

// Add the legendPanel to the map
var legendPanel = ui.Panel([legendTitle, colorBar, legendLabels]);
Map.add(legendPanel);

The issue with this solution is that 0 values are included as the lightest shade in my colour palette, but I'd like them excluded (either as transparent or white). Is there a way this can be done?

Example of map as it stands currently

2 Answers 2

2

You can mask out those pixels using updateMask, so the pixels with zeros appear transparent in the visualization. Try inserting the following line before the Map.addLayer(...).

image = image.updateMask(image.neq(0));
2

Just for completeness - in addition to the previous answer (which works fine), you can also use .selfMask():

var maskedImg = image.selfMask()

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.