0

the process to iterate over imageCollection is well documented, but that is not the case for the featureCollection. When trying to apply it, several errors appear. I show here one of them. If anybody has succeeded over this procedure help is appreciated.

var dict1 = ee.Dictionary({'Name': null, 'cultivo': null, 'imageId':null,'sum':null})

var n_list = ee.List(ee.Feature(null,dict1));
print(n_list)

var sum2 = function(feature,list){
  var actual = feature.get('sum')
  var previous = ee.Feature(ee.List(list).get(-1))
  var added = actual.add(previous)
  return ee.List(list).add(added)
    };

var summarize = ee.FeatureCollection(ee.List(sumChange.iterate(sum2,n_list)));
print('summarise',summarize)

Error actual.add is not a function

javier Moreira
  • 65
  • 2
  • 10
  • Did you check if `actual` is a `ee.Number` object? I suggest to explicitly cast it to `ee.Number` with `var actual = ee.Number(feature.get('sum'))` to avoid those type-related errors. In principle, there's no difference between iterating over `featureCollection` and over `imageCollection`. – Kevin Apr 03 '19 at 02:41
  • I have tried this one too. Now the error is: `FeatureCollection (Error) List.get, argument 'list': Invalid type. Expected: List. Actual: Feature.` – javier Moreira Apr 03 '19 at 14:58

1 Answers1

0

the correct way is as follows,

var dict1 = ee.Dictionary({'Name': null, 'cultivo': null, 'imageId':null,'sum':0})
//
var n_list = ee.List([ee.Feature(sumChange.first().geometry(),dict1)]);
//print(n_list)

var sum2 = function(feature,list){
  var actual = ee.Number(feature.get('sum'))
  var previous = ee.Number(ee.Feature(ee.List(list).get(-1)).get('sum'))
  var keepProperties = ['Name', 'cultivo', 'imageId','sum'];
  var added = actual.add(previous)
  return ee.List(list).add(feature.set('sumatoria', added).copyProperties(feature,keepProperties))
    };

var summarize = ee.FeatureCollection(ee.List(sumChange.iterate(sum2,n_list)));
print('summarise',summarize)
javier Moreira
  • 65
  • 2
  • 10