I would like to convert polygon (as featurecollection) that has the attribute 'Name' which contains string value of plantation types, namely 'Rubber', 'Mango', 'Coffee'. However, the .reduceToImage function won't accept string values. Thus, I had to do some workaround by deriving unique numeric id but now I'm stuck.

// Workflow: 1) Import asset, plantation_trial1
// print(plantations);

var plant_field = 'Name'
var all_text = 'what is'
// Function to build states list
var build_select = function(feature_collection, field_name) {
  var field_list = ee.Dictionary(feature_collection.aggregate_histogram(field_name))
  return field_list.map(function(name) {
    return ee.Dictionary({'label': name})
var plant_list = build_select(plantations, plant_field);


The 'plant_list' already contains the unique id for each 'Name', but no array or list function worked with the object. So basically, I am trying to have the script automate the extraction of the unique names from the featureCollection and assign each with a unique id. I am also aware that there may be some more efficient alternatives to achieve this, like distinct. Currently, here is what I got from print():

enter image description here

Can anyone give any idea on identifying what object that I have here and what to do with it, please?

1 Answer 1


Here is a toy demonstration of adding an integer class ID property to each feature that corresponds to a particular string based class ID.

The lookup table that ties class name ID to integer ID is created programmatically, so the assignment of integer to class name is somewhat arbitrary. If you would like to explicitly assign integer IDs to class name IDs, then manually construct an ee.Dictionary where keys are class names and values are the integer you'd like associated with each class name.

Use the resulting 'NameCode' integer class ID property of the features in the collection to reduceToImage().

// Mock up feature collection with 'Name' property.
var ftCol = ee.FeatureCollection([
  ee.Feature(null, {'Name': 'Rubber'}),
  ee.Feature(null, {'Name': 'Mango'}),
  ee.Feature(null, {'Name': 'Coffee'})

// Get a list of unique class names.
var distinctClassNames = ee.List(ftCol.aggregate_array('Name')).distinct();

// Make a sequential list of integers with length equal to n classes.
var distinctClassCodes = ee.List.sequence(1, distinctClassNames.size());

// Make a lookup table that maps unique 'Name' entries to an integer code value.
var lookup = ee.Dictionary.fromLists(distinctClassNames, distinctClassCodes);

// Print the lookup table so you know how class name corresponds to integer IDs.
print('Lookup table', lookup);

// Define a function to add the integer code value as a property of each
// feature based on the given 'Name' property.
function addNameCode(feature) {
  var thisClassName = feature.get('Name');
  return feature.set('NameCode', lookup.get(thisClassName));

// Apply the addNameCode function to the feature collection.
ftCol = ftCol.map(addNameCode);


enter image description here

  • Thanks for the answer! However, I couldn't really apply this without manually defining the dictionary, which is something that I'd like to avoid.
    – adwiputra
    Commented Jan 19, 2020 at 20:13
  • Ah, I see. It was a little hard to figure out exactly what you were trying to achieve, besides the codification of strings. I updated my answer so that the lookup class dictionary that defines the relationship between class name string and integer ID is generated programmatically. If this suits your need, maybe we can try to rephrase your question to better describe the issue to make it easier for folks to search/find (I think it is a common/useful operation). Commented Jan 19, 2020 at 21:35
  • Will do. Apologies for the confusion!
    – adwiputra
    Commented Jan 19, 2020 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.