/**** Start of imports. If edited, may not auto-convert in the playground. ****/
var cbay_20132014 = ee.Image("projects/sat-io/open-datasets/HRLC/Baywide_13Class_20132014");
/***** End of imports. If edited, may not auto-convert in the playground. *****/
// Define a dictionary which will be used to make legend and visualize image on map
var dict = {
  "names": [
    "Water",
    "Wetlands",
    "Tree Canopy",
    "Shrubland",
    "Herbaceous Vegetation",
    "Barren",
    "Structures",
    "Impervious Surfaces",
    "Impervious Roads",
    "Tree Canopy over Structures",
    "Tree Canopy over Impervious Surfaces",
    "Tree Canopy over Impervious Roads",
    "Aberdeen Proving Ground"
  ],
  "colors": [
    "00C5FF",
    "00A884",
    "267300",
    "4CE600",
    "A3FF73",
    "FFAA00",
    "FF0000",
    "9C9C9C",
    "000000",
    "737300",
    "E6E600",
    "FFFF73",
    "C500FF",
  ]};

// Create a panel to hold the legend widget
var legend = ui.Panel({
  style: {
    position: 'bottom-left',
    padding: '8px 15px'
  }
});

// Function to generate the legend
function addCategoricalLegend(panel, dict, title) {
  
  // Create and add the legend title.
  var legendTitle = ui.Label({
    value: title,
    style: {
      fontWeight: 'bold',
      fontSize: '18px',
      margin: '0 0 4px 0',
      padding: '0'
    }
  });
  panel.add(legendTitle);
  
  var loading = ui.Label('Loading legend...', {margin: '2px 0 4px 0'});
  panel.add(loading);
  
  // Creates and styles 1 row of the legend.
  var makeRow = function(color, name) {
    // Create the label that is actually the colored box.
    var colorBox = ui.Label({
      style: {
        backgroundColor: color,
        // Use padding to give the box height and width.
        padding: '8px',
        margin: '0 0 4px 0'
      }
    });
  
    // Create the label filled with the description text.
    var description = ui.Label({
      value: name,
      style: {margin: '0 0 4px 6px'}
    });
  
    return ui.Panel({
      widgets: [colorBox, description],
      layout: ui.Panel.Layout.Flow('horizontal')
    });
  };
  
  // Get the list of palette colors and class names from the image.
  var palette = dict['colors'];
  var names = dict['names'];
  loading.style().set('shown', false);
  
  for (var i = 1; i < names.length; i++) {
    panel.add(makeRow(palette[i], names[i]));
  }
  
  Map.add(panel);
  
}


/*
  // Display map and legend ///////////////////////////////////////////////////////////////////////////////
*/

// Add the legend to the map
addCategoricalLegend(legend, dict, 'Land cover');

// Add LC image to the map
Map.addLayer(cbay_20132014, {min:1, max:13, palette:dict['colors']}, 'Chesapeake Bay 2013-2014 LC')

// Center the map on image
//Map.setCenter(2.047, 45.346, 5)

Map.centerObject(cbay_20132014,8)