Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GEE code editor to create unique values list from existing list pulled from feature

I'm working in the Google Earth Engine code editor. I have a feature collection containing fires in multiple states and need to generate a unique list of states that will be used in a selection widget. I'm trying to write a function that takes the list of state values for all fires, creates a new list, and then adds new state values to the new unique list. I have run the code below and am not getting any error messages, but the output is still statesUnique = []. Can anyone point me in the right direction to get the new list to populate with unique values for states?

My Code:

// List of state property value for each fire
var states = fire_perim.toList(fire_perim.size()).map(function(f) {
  return ee.Feature(f).get('STATE');
}).sort();
print('States: ', states);

// Create unique list function
var uniqueList = function(list) {
  var newList = []
  var len = list.length;
  for (var i = 0; i < len; i++) {
    var j = newList.contains(list[i]);
    if (j === false) {
      newList.add(list[i])
    }
  }
  return newList
};

// List of unique states
var statesUnique = uniqueList(states);
print('States short list: ', statesUnique)
like image 777
Andrew Stratton Avatar asked Aug 05 '26 15:08

Andrew Stratton


1 Answers

Okay, I did not come up with this answer, some folks at work helped me, but I wanted to post the answer so here is one solution:

var state_field = 'STATE'
var all_text = 'All states'
// Function to build states list
var build_select = function(feature_collection, field_name, all_text) {
  var field_list = ee.Dictionary(feature_collection.aggregate_histogram(field_name))
                     .keys().insert(0, all_text);
  return field_list.map(function(name) {
    return ee.Dictionary({'label': name, 'value': name})
  }).getInfo();
};

var states_list = build_select(fire_perim, state_field, all_text)
print(states_list)
like image 76
Andrew Stratton Avatar answered Aug 11 '26 10:08

Andrew Stratton