1

I want to join two featureCollections and remove the ID field in order to run linearRegression in GEE. I found codes to clean the featureCollection after innerJoin, and snippet to remove properties from FC. However, when I try on my own data, all the properties from FC were removed!

Here is my code

// Create the primary collection.
var primaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {foo: 0, label: 'a'}),
  ee.Feature(null, {foo: 1, label: 'b'}),
  ee.Feature(null, {foo: 1, label: 'c'}),
  ee.Feature(null, {foo: 2, label: 'd'}),
]);

print('primary fc',primaryFeatures);

// Create the secondary collection. var secondaryFeatures = ee.FeatureCollection([ ee.Feature(null, {bar: 1, label: 'e'}), ee.Feature(null, {bar: 1, label: 'f'}), ee.Feature(null, {bar: 2, label: 'g'}), ee.Feature(null, {bar: 3, label: 'h'}), ]);

print('2nd fc',secondaryFeatures);

// Use an equals filter to specify how the collections match. var toyFilter = ee.Filter.equals({ leftField: 'foo', rightField: 'bar' });

// Define the join. var innerJoin = ee.Join.inner('primary', 'secondary');

// Apply the join. var toyJoin = innerJoin.apply(primaryFeatures, secondaryFeatures, toyFilter);

// Print the result. print('Inner join toy example:', toyJoin);

// I tried // clean the join // this snippet is from stackexchange function cleanJoin(feature){ return ee.Feature(feature.get('primary')).copyProperties(feature.get('secondary')); } var Joined = toyJoin.map(cleanJoin); print('joined',Joined)

// remove one properties // this snippet is also from https://gis.stackexchange.com/questions/321724/removing-property-from-feature-or-featurecollection-using-google-earth-engine

var removeProperty = function(feat, property) { var properties = feat.propertyNames() print(properties); var selectProperties = properties.filter(ee.Filter.neq('item', property)) return feat.select(selectProperties) }

var removeJoined = removeProperty(Joined,'foo'); print('remove joined',removeJoined) // all properties were removed!!

enter image description here

hydro9
  • 71
  • 5

1 Answers1

1
var removeProperty = function(feat, property) {
  var properties = feat.propertyNames()
  print(properties);
  var selectProperties = properties.filter(ee.Filter.neq('item', property))
  return feat.select(selectProperties)
}

This does not work for what you want because .propertyNames() returns the names of properties of the collection, not its elements — since this collection has no properties, it removes every property. This function was intended to work on a feature, not a collection; you need to map it over the collection.

However, it's also unnecessarily complicated. The simplest way to remove properties is to set them to null:

var removeJoined = Joined.map(function (feature) {
  return feature.set('foo', null);
});
Kevin Reid
  • 10,379
  • 14
  • 18