1

I tried to set new properties in a map function, but I failed to get these properties outside the map function. The error is:

    ComputedObject (Error)
    Collection.first: Error in map(ID=0):
    Feature.intersection: Parameter 'right' is required.

And the code is:

// double map over the polygons
var mapped = vectors.map(function(feat1){
  feat1 = ee.Feature(feat1);
  var mapped1 = polygons.map(function(feat2){
    feat2 = ee.Feature(feat2);
    var intersection = feat2.intersection(feat1, ee.ErrorMargin(1));
    return ee.Feature(intersection).set({'Intersect': intersection.area().divide(1000 * 1000),'date':'2016-01-01'})
  })
  return mapped1
}).flatten().filter(ee.Filter.gt('Intersect', 0));

var test = mapped.first().get('date');
print(test);

As I set new properties "Intersect" and "date", but I cannot get any of them when I print it.

The full code can be found at: https://code.earthengine.google.com/e31d1380d8499df42194fa2a5afa08f1

The test code is written by Rodrigo E. Principe https://gis.stackexchange.com/users/23470/rodrigo-e-principe

3 Answers 3

1

Don't turn things into lists unnecessarily. I also don't think this is a bug. One of your intersections is null, so that's why you can't set properties on it. Here's a good way to solve that, by allowing map() to return nulls (and drop them):

var mapped = vectors.map(function(feat1){
  feat1 = ee.Feature(feat1);
  var mapped1 = polygons.map(function(feat2){
    feat2 = ee.Feature(feat2);
    var intersection = feat2.intersection(feat1, ee.ErrorMargin(1));
    return intersection
  }, true) // Note the boolean flag.  See map() docs for details.
  return mapped1
}).flatten();

// Now set properties on non-null features.
mapped = mapped.map(function(intersection) {
  return intersection.set({
    'Intersect': intersection.area().divide(1000 * 1000),
    'date': '2016-01-01'})
});

print(mapped.first());
var aaa = mapped.first().get('date');
print(aaa);

https://code.earthengine.google.com/6162e6ea95efa8b00b29b2e8514e5735

0

The fact that print(mapped.first()) works but print(mapped.first().get('date')) fails even though the former shows a date property, and the error message is about steps that precede fetching the date property, suggests that this is a bug. I will report it.

1
  • Thank you so much for your reply. May I ask is there any other method that can reach the same effect so far?
    – Mr_C
    Commented Aug 4, 2019 at 1:41
0

I find a temporally useful way to solve this problem. You can transform the Feature type to List type and use the get function of List type to obtain what you want. The code looks like:

// double map over the polygons
var mapped = vectors.map(function(feat1){
  feat1 = ee.Feature(feat1);
  var mapped1 = polygons.map(function(feat2){
    feat2 = ee.Feature(feat2);
    var intersection = feat2.intersection(feat1, ee.ErrorMargin(1));
    return ee.Feature(intersection).set({'Intersect': intersection.area().divide(1000 * 1000),'date':'2016-01-01'})
  })
  return mapped1
}).flatten().filter(ee.Filter.gt('Intersect', 0));

var lists = mapped.toList(3); // 3 is temporally set, and it should be the number of your features
var ff = ee.Feature(lists.get(0));  // get the first feature
print(ff.get('Intersect'));      // This will work.

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.