Google Earth Engine – Removing Properties from Feature or FeatureCollection

google-earth-engine

How do I remove a property from a Feature (or FeatureCollection), by only specifying the name of the properties I want to remove? I know I can "remove" by selecting only properties I want, but in my case it is quite cumbersome…

For the minimal example in code below (see also link), how to remove property color?

var features = [
  ee.Feature(ee.Geometry.Point(-73.96, 40.781), {name: 'Thiessen', color: "a"}),
  ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {name: 'Dirichlet', color: "a"})
];

// Create a FeatureCollection from the list and print it.
var FC = ee.FeatureCollection(features);
print(FC);


// Now I want to remove property color in each feature?

Best Answer

You have to make a function to do that and then apply it to the collection:

var features = [
  ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {name: 'Voronoi', color: "a"}),
  ee.Feature(ee.Geometry.Point(-73.96, 40.781), {
    name: 'Thiessen', 
    color: "a"
  }),
  ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {
    name: 'Dirichlet', 
    color: "a"
  })
];

// Create a FeatureCollection from the list and print it.
var FC = ee.FeatureCollection(features);
print(FC);

// Generic Function to remove a property from a feature
var removeProperty = function(feat, property) {
  var properties = feat.propertyNames()
  var selectProperties = properties.filter(ee.Filter.neq('item', property))
  return feat.select(selectProperties)
}

// remove property color in each feature
var newFC = FC.map(function(feat) {
  return removeProperty(feat, 'color')
})

print(newFC)

link

Related Question