Thursday, 13 September 2018

"Print" and "ui.Label" return different strings in console and in the UI in Google Earth Engine


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)

Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...