2

How can I use ui.select for Administrative levels dropdown in Google Earth Engine? I would like to use ui.select to first select the state and then select the county.

Here is the GEE code:

var countries = ee.FeatureCollection("TIGER/2016/States")
var Counties = ee.FeatureCollection("TIGER/2016/Counties")

var countryNames = countries.aggregate_array('NAME')
var countyNames = Counties.aggregate_array('NAME')

var countryNames = countries.aggregate_array('NAME').getInfo()
function countryNames(names) {      return names}

var countyNames = Counties.aggregate_array('NAME').getInfo()
function countyNames(names) {      return names}

var state = {
country: ui.Select({
items: countryNames,
}),

county: ui.Select({
items: countyNames,
})
};

var update = function(NAME) {
var roi = Map.layers().get(0)
[countries.filterMetadata("NAME", "equals", NAME).getValue()]
[counties.filterMetadata("NAME", "equals", NAME).getValue()]

}

state.country.onChange(update);

state.county.onChange(update);

ui.root.insert(0, ui.Panel([state.country, state.county]));

https://code.earthengine.google.com/7d0286ea2eaf4de2a1a8915b9c0d71d6

2

Ok, this is a bit tricky, I assume you want to filter counties by state, so I found a linking property: STATEFP

var states = ee.FeatureCollection("TIGER/2016/States")
var counties = ee.FeatureCollection("TIGER/2016/Counties")

var statesNames = states.aggregate_array('NAME')

var getCounties = function(state) {
  // Given a state get all counties
  var feat = ee.Feature(states.filterMetadata('NAME', 'equals', state).first())
  var statefp = ee.String(feat.get('STATEFP'))
  var filteredCounties = counties.filterMetadata('STATEFP', 'equals', statefp)
  var filteredCountiesNames = filteredCounties.aggregate_array('NAME')
  return ee.List(filteredCountiesNames)
}

// Empty Dropdowns
var statesDD = ui.Select([], 'Loading..')
var countiesDD = ui.Select([], 'Waiting for a state..')

// Load states
statesNames.evaluate(function(states){
  statesDD.items().reset(states)
  statesDD.setPlaceholder('Select a state')
  statesDD.onChange(function(state){
    // once you select a state (onChange) get all counties and fill the dropdown
    countiesDD.setPlaceholder('Loading...')
    var counties = getCounties(state)
    counties.evaluate(function(countiesNames){
      countiesDD.items().reset(countiesNames)
      countiesDD.setPlaceholder('Select a county')
    })
  })
})

ui.root.insert(0, ui.Panel([statesDD, countiesDD]));

link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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