2

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

3
  • Hi, the code is not easily reproducible. There is no modis, geometry, forest, water, bare. Mar 7, 2019 at 11:48
  • @RodrigoE.Principe I have added a link to code. Mar 7, 2019 at 15:46
  • @RodrigoE.Principe I made it reproducible now. Hope that helps as well. Thanks Rodrigo! Mar 7, 2019 at 15:51

1 Answer 1

4

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

3
  • Thanks Rodrigo! This is great. I am currently trying to add standard deviations as well to the same chart. Any thoughts on that? I am going to use ee.Reducer.stdDev and then run the above code, so I will have two featureCollections - One for means and one for stdDev and I would like to add the stdDev as confidence intervals around my mean in the chart - like this: developers.google.com/chart/interactive/docs/gallery/intervals; Any thoughts? Should I post a new question? Mar 8, 2019 at 16:31
  • You should split the process. In the fist part, a function should return the dict that will be the properties of the feature. So, in the second process add the stdDev to that dict, and finally make the FeatureCollection Mar 8, 2019 at 16:34
  • Hmm, I tried the above, but am getting this error: Error generating chart: No features contain non-null values of "class".; Link to code: code.earthengine.google.com/3c76cec705b43880bdeb89d288219576 Mar 8, 2019 at 16:42

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.