Google Earth Engine – How to Change Property of FeatureCollection in Google Earth Engine

google-earth-engine

I am working on land-use classification and I would like to merge some classes from my training dataset. I've a FeatureCollection with over a hundred polygons (features), with all of them having a property 'class' to them with a number from 1 to 17 representing the different classes. I would like to replace from all Features the property which have a class 8 to be replaced by class 9.

I've been trying different things, and I think it should look something like this:

var conditional = function(image){
  return ee.Algorithms.If(ee.Number(image.get('class').eq(8)),
  image.set({class: 9}),
  image)

var test = table.map(conditional)

In which:

  • table contains my FeatureCollection with polygons
  • class is the property on which the conditional function must be based
  • 8 and 9 are linked to the 'class' properties

Best Answer

You function is fine, you just need to move one parenthesis, from ee.Number(image.get('class').eq(8)) to ee.Number(image.get('class')).eq(8). Also, in the code you posted you are missing the closing bracket.

As a suggestion, I wouldn't use image for that function argument, it's confusing because when you loop (map) over a FeatureCollection each element is a Feature, so I'd use feat, but of course it works with any name you use, and that is up to you.

var conditional = function(feat) {
  return ee.Algorithms.If(ee.Number(feat.get('class')).eq(8),
  feat.set({class: 9}),
  feat)
}
var test = table.map(conditional)
Related Question