1

I am trying to map over a feature collection and set a new property called 'id' for each element in the collection. I made a list 'feature_id' which contains the id names. The code runs and the id property is set but they're all zero.

enter image description here

I am trying to figure out a way to iterate through my list within my map statement, so that the first feature id is 0 , second is 1, etc... I thought using a counter would work but it resets every iteration. There also might be an easier way to do this but I had trouble finding one.

//combSlabs is a feature collection containing 5000 fields
var sizeSlabs = combSlabs.size().getInfo()

//create Id (System:Index) for each of the different fields
var feature_id = []
for (var i = 0; i <= sizeSlabs; i++) {
    feature_id.push(i)
}

var getId = combSlabs.map(function(feature) {
  var counter = 0
  var add_id = feature.set({id: feature_id[counter]})
  counter += 1
  return add_id
})
2
  • That's weird, I ran it with a fake featureCollection and had no issues with the id numbers code.earthengine.google.com/11b73b26a1724476475087f5753c46e9
    – M. Nicolas
    Nov 18, 2021 at 19:28
  • @MadeleineNicolas Yeah I tried it with a different feature collection and you were right. It seems the Feature Collection I was working was acting weird and ran fine with different feature collections. Thank you!
    – rgb_123
    Dec 13, 2021 at 18:16

1 Answer 1

1

You probably want to stick to server side processing as much as possible, so here is an example with everything server side. The answer comes from another similar question here in stackExchange, there you can also find a client-side version. There was no working example so I had to come up with something for myself, but it boils down to this:

var newList = idList.map(function(x){
  
  var index = idList.get(x)
  var feat = toList.get(x)
  
  return ee.Feature(feat).set('id', index).copyProperties(feat)
})

print(newList, 'newList')

You iterate over a featureCollection that you turn in list (such that you can use .get()) and return it as a feature in the new collection. https://code.earthengine.google.com/8cb66ee3b3840ee7f059e75573c2228b

1
  • thanks for the detailed answer. I read the similar post you linked above and it makes more sense to stick with the server-side analysis like you mentioned.
    – rgb_123
    Dec 13, 2021 at 18:20

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.