Google Earth Engine – Creating a Legend by Iterating Over ee.List()

google-earth-engineguilegend

The Goal is to adapt the Legend from the Example for my Data
I just Changed the Following:

In the original the names and their palettes of subclasses are separated in different subclasswise-properties, I guess that is why they need these lines to obtain palette and the names

// Get the list of palette colors and class names from the image.
image.toDictionary().select([BAND_NAME + ".*"]).evaluate(function(result) {
  var palette = result[BAND_NAME + "_class_palette"];
  var names = result[BAND_NAME + "_class_names"];
  loading.style().set('shown', false);

  for (var i = 0; i < names.length; i++) {
    legend.add(makeRow(palette[i], names[i]));
  }
  Map.addLayer(image, {min: 0, max: 17, palette: palette}, 'IGBP classification');
}); 

the names and palette in the case of CORINE by Copernicus are easier obtained

var names = ee.List(lc2012.get('landcover_class_names'))
var landcoverPalette = ee.List(lc2012.get('landcover_class_palette'))

extracting Values by indexing over theese ee.List() Objects by e.g. names[0] won´t work. Instead one has to use names.get(0)

print()names.get(0) retrieves a Computed Object that looks like a string –> 'Artificial surfaces; urban fabric; continuous urban fabric'

but used inside a function e.g. legend.add(makeRow('E6004D', names.get(0))), it doesnt get the string, but instead delivers this
enter image description here

Code

I do not understand how to access my List and retrieve the wanted element

for (var i = 0; i < names.length; i++) {
legend.add(makeRow(landcoverPalette[i], names[i]));
}

The legend won´t load fully

Best Answer

You are mixing server-side and client-side variables, that's why it does not work. The function for creating the legend was correct.

Try this: link