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

1 Answer 1


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:


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')))
  • Thanks that works. I forgot the system:index column exists ;)
    – Curlew
    Sep 16, 2020 at 20:51

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.