0

I'm building a function that updates a dictionary (in a feature of a FeatureCollection) with values calculated within the function.

I use the variables 'HighDeg', 'MildDeg' and 'NoDeg' to store the counts of pixels in each of these three categories in the given feature. 'PercHigh', 'PercMild', and 'PercNo' are used to store the percentage area of each category in the given feature. Code below:

var PQPercAbsFreq = function(feature) {

    var HighDeg = ee.Number(ee.Dictionary(feature.get('HiDeg1')));
    var MildDeg = ee.Number(ee.Dictionary(feature.get('MiDeg2')));
    var NoDeg = ee.Number(ee.Dictionary(feature.get('NoDeg3')));
    var PercHigh = ee.Number(HighDeg.multiply(100).divide(MildDeg.add(HighDeg).add(NoDeg)).format('%.2f'));
    var PercMild = ee.Number(MildDeg.multiply(100).divide(MildDeg.add(HighDeg).add(NoDeg)).format('%.2f'));
    var PercNo = ee.Number(NoDeg.multiply(100).divide(MildDeg.add(HighDeg).add(NoDeg)).format('%.2f'));


    var newfeature = feature.set({'AbsHi1':HighDeg, 'AbsMi2': MildDeg, 'AbsNo3': NoDeg, 
  'PercHi1': PercHigh, 'PercMi2':PercMild, 'PercNo3': PercNo});

  return newfeature;
};

When I map this function over a FeatureCollection (in which every feature has a value for 'HiDeg1', 'MiDeg2', and 'NoDeg3'), I get the message:

"FeatureCollection (Error) Error in map(ID=00000000000000000029): Dictionary: Unrecognized arguments to Dictionary constructor."

ID=00000000000000000029 is the first feature in my FeatureCollection, which means that the function doesn't work from the beginning.

When I substitute the variables (HighDeg, MildDeg, NoDeg, ...) in the feature.set(...) by numbers (0, 1, ...) the code runs perfectly.

I think I'm having a problem with the client x server objects thing, but I'm very new to ee and I have no idea how to fix this. I thought that a variable created within a function should be 'valid' within that same function. But I'm not getting to access it within the function, in my code.

I tried using getInfo() and then parseInt() before using the variables in the feature.set(...), but it didn't work either.

How can I use a variable created within a function to save its stored value into a dictionary within that same function?

2 Answers 2

0

Your code is calling HiDeg1 a dictionary. It's (apparently) not, it just seems to be a number. So take out the ee.Dictionary casts.

In fact, you can get rid of the ee.Number casts too, and just make the first 3 lines:

var HighDeg = feature.getNumber('HiDeg1')
var MildDeg = feature.getNumber('MiDeg2')
var NoDeg = feature.getNumber('NoDeg3')
1
  • Great! This simplifies the code and helps me to understand it better. Thank you so much!
    – Camila
    Commented Nov 7, 2022 at 5:54
0

You have several issues in your code. I changed my test feature collection to list because it is easier to debug your code. So, your function in my code looks as follows. I assumed as 'HiDeg1', 'MiDeg2' and 'NoDeg3' properties in your Feature Collection, respectively, as 'perimeter', 'min' and 'max' in my collection.

Map.centerObject(polygons);
Map.addLayer(polygons);

var polygons_list = polygons.toList(polygons.size());

print(polygons_list);

var PQPercAbsFreq = polygons_list.map(function (feature) {

  var HighDeg = ee.Feature(feature).get('perimeter');
  var MildDeg = ee.Feature(feature).get('min');
  var NoDeg = ee.Feature(feature).get('max');

  var PercHigh = ee.Number(ee.Number(HighDeg).multiply(100)
                                   .divide(ee.Number(MildDeg)
                                   .add(HighDeg)
                                   .add(NoDeg))).format('%.2f');

  var PercMild = ee.Number(ee.Number(MildDeg).multiply(100)
                                             .divide(ee.Number(MildDeg)
                                             .add(HighDeg)
                                             .add(NoDeg)).format('%.2f'));
  
  var PercNo = ee.Number(ee.Number(NoDeg).multiply(100)
                                         .divide(ee.Number(MildDeg)
                                         .add(ee.Number(HighDeg))
                                         .add(ee.Number(NoDeg))).format('%.2f'));


  var newfeature = ee.Feature(feature).set({'AbsHi1':HighDeg, 'AbsMi2': MildDeg, 'AbsNo3': NoDeg, 
                                            'PercHi1': PercHigh, 'PercMi2':PercMild, 'PercNo3': PercNo});


  return newfeature;

});

print(PQPercAbsFreq);

After running above code in GEE editor, I got following result. It can be observed that additional properties were added as expected.

enter image description here

1
  • Yes, you assumed correctly and that's great, it worked! Thank you so much!
    – Camila
    Commented Nov 7, 2022 at 5:53

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.