I want to randomly select a feature from each group in a FeatureCollection. My actual data has many groups, so filtering each group and choosing one from each group is not an option. I tried
using reduceColumns()
with a grouped reducer to choose a random item from each group. My idea was to extract the random values from that dictionary as a list, and then use that to filter the original feature collection, but got an error when trying to convert the dictionary to a list.
How can I get a list of the random values from my dictionary (see below)?
var fc = ee.FeatureCollection([
ee.Feature(null, {group: 'A'}),
ee.Feature(null, {group: 'A'}),
ee.Feature(null, {group: 'B'}),
ee.Feature(null, {group: 'B'}),
ee.Feature(null, {group: 'C'}),
ee.Feature(null, {group: 'C'})
]);
// Add random column.
var fc = fc
.randomColumn()
print(fc)
// Get a random value for each group
// (the random value is nested deep in a dictionary)
var randomDict = fc
.reduceColumns({
selectors: ['random', 'group'],
reducer: ee.Reducer.min().group({
groupField: 1
})
})
print(randomDict)
// Extract the random value from dictionary, return list
// BREAKS HERE:
var randomList = randomDict
.get('groups')
.map(function(e){
return e.get('min')
})
print(randomList) // randomDict.get(...).map is not a function
// Use list of random values to filter the original fc
var randomFC = fc
.filter(ee.Filter.inList('random', randomList))
print(randomFC)