0

This question refers to post: Calculating NDVI per region, month & year with Google Earth Engine?

I have modified the code posted by @Kel Markert https://code.earthengine.google.com/349615d7802d59f677181bef0badad9f to attempt to get a maximum monthly NDVI value from a small polygon from Landsat 8 in Google Earth Engine and export to CSV.

But I keep getting an error of

"Dictionary.get, argument 'key': Invalid type. Expected: String. Actual: List".

Any advice on how to fix this?

https://code.earthengine.google.com/bf6ea84442f33694b7f12247d1eabd3a

Table link https://code.earthengine.google.com/?asset=users/mangrovewatch/MCM1?

    var region = table,
L8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA");

var cloudlessNDVI = L8.map(function(image) {
  // Get a cloud score in [0, 100].
  var cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud');

  // Create a mask of cloudy pixels from an arbitrary threshold.
  var mask = cloud.lte(20);

  // Compute NDVI.
  var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');

  // Return the masked image with an NDVI band.
  return image.addBands(ndvi).updateMask(mask);
});

var startDate = ee.Date('2013-05-01'); // set analysis start time
var endDate = ee.Date('2019-12-31'); // set analysis end time

// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();

// get a list of time strings to pass into a dictionary later on
var monList = ee.List.sequence(0, nMonths).map(function (n) {
  return startDate.advance(n, 'month').format('YYYMMdd');
})
print(monList)

var result = region.map(function(feature){
  // map over each month
  var timeSeries = ee.List.sequence(0,nMonths).map(function (n){
    // calculate the offset from startDate
    var ini = startDate.advance(n,'month');
    // advance just one month
    var end = ini.advance(1,'month');
    // filter and reduce
    var data = cloudlessNDVI.filterDate(ini,end).max().reduceRegion({
      reducer: ee.Reducer.max(),
      geometry: feature.geometry(),
      scale: 30
    });
    // get the value and check that it has data
    var val = ee.Number(data.get(['ndvi']));
    val = ee.Number(ee.Algorithms.If(val,val,-999));
    // return max
    return val;
  });
  // create new dictionary with date strings and values
  var timeDict = ee.Dictionary.fromLists(monList,timeSeries);
  // return feature with a timeseries property and results
  return feature.set(timeDict);
});

// print to see if it is doing what we expect...
print(result);

// Export the data to a table for further analysis
Export.table.toDrive({
  collection:result,
  description:"MCM1_NDVI",
  fileFormat:"CSV",
  //selectors:["HRpcode","timeseries"]
})
Jock
  • 75
  • 1
  • 9
  • The link you've provided doesn't work. Just getting an empty JS console. – Val Jan 23 '20 at 08:12
  • @Val I have made the link public. Thanks for the heads up – Jock Jan 23 '20 at 22:32
  • Still getting an empty script - the link for the asset works. – Val Jan 24 '20 at 08:16
  • 2
    Note: you might consider directing future Earth Engine questions to [GIS Stack Exchange (google-earth-engine tag)](https://gis.stackexchange.com/questions/tagged/google-earth-engine) to help consolidate questions for easier discovery and engagement by users. [Further information](https://developers.google.com/earth-engine/help). – Justin Braaten Jan 24 '20 at 17:35
  • 1
    Cross-posted as https://gis.stackexchange.com/q/348887/115 – PolyGeo Jan 28 '20 at 23:29

0 Answers0