2

Bit stuck at the moment on this. I need to add an incremental number (1:total length) to each feature in a FeatureCollection in Google Earth Engine.

So consider for instance the GAUL dataset as example. How would I add each of these newids to the dataset (one unique id per feature) ?

dataset = ee.FeatureCollection("FAO/GAUL/2015/level0")
newid = ee.List( list(range(1,dataset.size().getInfo() )) )

I can map a function to add a field, but this in itself needs to be mapped over the newid list I imagine?

def addField(feature, id):
  return feature.set({'sum': id})
dataset = dataset.map(addField, id)

Happy for any hints in javascript or python

3

Doing these kind of things - creating lists of collections - is typically not a good idea. But if you really need to, I suppose you cold do like this:

https://code.earthengine.google.com/460a24fc7139d2cedafb72a052ab4fef

var dataset = ee.FeatureCollection("FAO/GAUL/2015/level0")
var indexes = ee.List(dataset.aggregate_array('system:index'))
var ids = ee.List.sequence(1, indexes.size())
var idByIndex = ee.Dictionary.fromLists(indexes, ids)
var datasetWithId = dataset.map(function (feature) {
  return feature.set('id', idByIndex.get(feature.get('system:index')))
})
print(datasetWithId.first().get('id'))
1
  • Thanks that works. I forgot the system:index column exists ;)
    – Curlew
    Sep 16 '20 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.