Google Earth Engine Chart – Create a Line Chart from a Dictionary of Values in Google Earth Engine

chart;dictionarygoogle-earth-engine

I am currently trying to create a line chart for each land-cover type across three bands I selected from MODIS.

Problem is most of the ui.Chart functions require a feature or an image. Please help?

// Create clipping function
var clipper = function(image){
  return image.clip(geometry);
};

// Load the MODIS data that is being processed

// Clip the image collection
var filtered = ee.ImageCollection('MODIS/006/MOD09A1').map(clipper)
                  // Sort dates in descending order
                  .sort('system:time_start',false);
print(filtered);

// Create a visualization color scheme
var medVis = {
  min: 0, 
  max: 8000,
  // You have to specify one band to use a palette
  bands:['sur_refl_b04'],
  palette: [
    'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
    '66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
    '012E01', '011D01', '011301'
  ],
};

// Load the first image
Map.centerObject(geometry,8);
Map.addLayer(filtered.first(),medVis,"Loading the most recent Image");
Map.addLayer(geometry, {}, 'Shapefile Outline');

// Get the recent image alone
var singleImage = filtered.first();
print(singleImage);

// Merge the featureCollections that have been created
// Create your own polygons here to make the code reproducible. 
// It can be polygons or points that refer to a particular land cover type
var training = forest.merge(water).merge(bare);
print(training);

var mean = singleImage.select(['sur_refl_b02','sur_refl_b03','sur_refl_b04'])
                      .reduceRegions({
                        collection: training,
                        reducer: ee.Reducer.mean(),
                        scale: 500
                      })
                      .reduceColumns({
                        selectors: ['sur_refl_b02','sur_refl_b03','sur_refl_b04','class'],
                        reducer: ee.Reducer.mean().repeat(3).group({
                              groupField: 3,
                              groupName: 'class'})
                        });

print(mean, 'Band Means');

Is there also any way in which I can retain the names of the selectors in the dictionary created as well?

I have added a link to code based on Rodrigo's comment: https://code.earthengine.google.com/a84485d5cba393122e2a1c4c5ae17690

Best Answer

I missed the bounty, but anyhow I got an answer. See if this is what you needed..

var createFC = function(element, list) {
  list = ee.List(list) // cast the 'accumulating' object

  element = ee.Dictionary(element)  // its an dict with 'class' and 'mean' as keys
  var clss = ee.Number(element.get('class'))
  var means = ee.List(element.get('mean')) // means is a list in which index 0 is 'sur_refl_b02', 1 is 'sur_refl_b03' and 2 is 'sur_refl_b04'
  var sr2 = ee.Number(means.get(0))
  var sr3 = ee.Number(means.get(1))
  var sr4 = ee.Number(means.get(2))

  var feat = ee.Feature(null, {'class': clss, 
                               'sur_refl_b02': sr2,
                               'sur_refl_b03': sr3,
                               'sur_refl_b04': sr4})

  return list.add(feat)
}

var featList = ee.List(ee.List(mean).iterate(createFC, ee.List([])))

var collection = ee.FeatureCollection(featList)

var chart = ui.Chart.feature.byFeature(collection, 'class')
print(chart)

link: https://code.earthengine.google.com/1bcc1a12e1771da8dc428333064e6422

Related Question