8

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?

Matifou
  • 2,011
  • 1
  • 21
  • 43

2 Answers2

14

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

Rodrigo E. Principe
  • 10,085
  • 1
  • 28
  • 35
  • great, exactly what I needed, thanks! And in case one wants to remove a list of properties, it seems like ee.Filter.inList('item', property).not() would do the trick!? – Matifou May 05 '19 at 17:23
  • also, any idea why in the JS editor, when printing result, we have now FeatureCollection (3 elements, 0 columns)? – Matifou May 05 '19 at 18:14
1

The copyProperties method have an "exclude" parameter which seems easier to use than the method proposed by @Rodrigo.

In a map function create a feature with all original properties but the one you want to remove:

FC = FC.map(function(f){
  return ee.Feature(f.geometry()).copyProperties({
    source: f, 
    exclude:["color"]
  })
})

here is a demo

Pierrick Rambaud
  • 2,788
  • 1
  • 14
  • 49