I want to add the key property "class: 1" to my FeatureCollection.

This feature of adding a class / value key is done automatically when you create a FeatureCollection and add a property by using the "Geometry Import" tool in the Earth Engine map area.

var toList_pasto = ee.List([

for (var i = 0; i < 11; i++){
  if (i == 0){
    randomSamples = ee.FeatureCollection.randomPoints(toList_pasto.get(i),500)
  else {
    randomSamples = randomSamples.merge(ee.FeatureCollection.randomPoints(toList_pasto.get(i),500))

In my code, I want to do this in the line 28. Add Property in FeatureCollection


You probably want to use 'set'. It can set a property to the whole featurecollection:

for (var i = 0; i < 11; i++){
  if (i === 0){
    randomSamples = ee.FeatureCollection.randomPoints(toList_pasto.get(i),500).set('Class', i)
    print('Collection number: '+ i, randomSamples)
  else {
    var newCol = ee.FeatureCollection.randomPoints(toList_pasto.get(i),500).set('Class', i);
    print('Collection number: '+ i, newCol)
    randomSamples = randomSamples.merge(newCol) 

However, as you are merging all your featurecollection into one big collection, you probably want to set every single feature with a property 'class'. Therefore, you have to map over a (server-side) list with the length of the number of geometries you have. We then map inside the mapped list over each collection and set the property to every individual feature:

// Map over a list equal to the amount of geometries
var FeatCol = ee.FeatureCollection(ee.List.sequence(0, toList_pasto.length().subtract(1)).map(function(i){
  // get the geometry corresponding to the number
  var pasto = ee.Geometry(toList_pasto.get(ee.Number(i)))
  // make random samples. Map over the collection to give each feature a class name
  var randomSamples = ee.FeatureCollection.randomPoints(toList_pasto.get(i),500).map(function(feat){
      return feat.set('Class', i)
  return randomSamples;
// return the flattened collection of collections
// print limited to 5000 otherwise it won't print

Link script

| improve this answer | |

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.