1

I want to print the number that corresponds to the list of classes I defined in the variable pixelClass, but when I click on my button, it only prints the label name, not the number. How can I make it to print only the number?

My code:

var pixelClass = {
  'All' : 0,
  'Soil' : 2,
  'Water' : 3,
  'Dead Grass' : 5,
  'Marsh' : 6,
  'Scrub' : 7,
  'Grass' : 8,
  'Forested Upland' : 9,
  'Forested Wetland' : 10,
  'Developed' : 11,
  };

var classSelector = ui.Select({
  items: Object.keys(pixelClass),
  value: null,
  style: {width: '35%', stretch: 'horizontal',padding:'0px 0px 0px 10px'}
});

var Button = ui.Button({
  label: 'Print Value', 
  style: {width: '45%', stretch: 'horizontal',padding:'0px 0px 0px 10px'},
  onClick: function getValue(){
    return print(classSelector.getValue())
  }
})

var panel = ui.Panel({
  layout: ui.Panel.Layout.flow('vertical'),
  style: {width: '350px'}   });
panel.add(classSelector)
panel.add(Button)
ui.root.insert(0, panel);

Link: https://code.earthengine.google.com/91c0a53623b8ea0a901d2f7751884e3c

1 Answer 1

1

Because you specified

items: Object.keys(pixelClass),

the values that the ui.Select has are the names in pixelClass. In order to get the values of pixelClass, you need to look up the name in pixelClass.

  onClick: function getValue(){
    print(pixelClass[classSelector.getValue()]);
  }
0

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.