1

I am working on a variable label in Google Earth Engine and noticed that the label as printed in the console is not the same as the label shown on the map. Why is that? Code below:

var aoi = ee.Geometry.Polygon([[[-106.75701284923326, 42.31332966408458],
          [-106.75701284923326, 42.003856624775146],
          [-106.31755972423326, 42.06913460511273]]]);

var chirpsSelect = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY")
            .filterDate('1985-01-01','2018-01-01')
            .filter(ee.Filter.dayOfYear(3,6))
            .filterBounds(aoi)

var chirpsStartImage = ee.String(chirpsSelect.first().get('system:index'))
var dateStartString = ((((chirpsStartImage.slice(6,8)).cat('/')).cat(chirpsStartImage.slice(4,6))).cat('/')).cat(chirpsStartImage.slice(2,4)) 

var sortChirpsSelect = chirpsSelect.sort('system:index',false)

var chirpsEndImage = ee.String(sortChirpsSelect.first().get('system:index'))
var dateEndString = ((((chirpsEndImage.slice(6,8)).cat('/')).cat(chirpsEndImage.slice(4,6))).cat('/')).cat(chirpsEndImage.slice(2,4))
var dateTitle = ee.String((dateStartString.cat(' to ')).cat(dateEndString))

print(dateTitle)

var dateLabel = ui.Label({
  value: dateTitle
})

var panel = ui.Panel()
panel.add(dateLabel)
Map.add(panel)
2

That is because the ui.Label needs to print a client side string. You can simply use getInfo() on the dataTitle, but in longer running scripts it's possibly better to use evaluate:

var dateLabel = ui.Label({
  value: 'Please wait...'
})
dateStartString.cat(' to ').cat(dateEndString).evaluate(function(val){dateLabel.setValue('From: ' + val)});

Link script

  • Yes, I noticed that after further research. Is "evaluate" client-side? – IskJon Mar 14 at 15:57
  • nice use of evaluate over getInfo @Kuik! – Rodrigo E. Principe Mar 14 at 16:55
  • 1
    just a minor detail, you can call evaluate in any ee.ComputedObject so you could've done: dateTitle.evaluate(function(val){dateLabel.setValue('From: ' + val)});.. but the concept of using evaluate in ui events for not blocking the app is great =) – Rodrigo E. Principe Mar 14 at 17:04

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.