1

I would like to print computed values in a side bar using a Google Earth Engine app.

Currently the output is a dictionary for example if I want the feature name that has been clicked on:

var computeValues = function (coords) {

  // select built-up area from map
  var point = ee.Geometry.Point(coords.lon, coords.lat);

  // select the polygon and colour red
  var selected = bu.filterBounds(point);
  var id = ee.String(selected.first().get('bua11cd'));

  // display in side panel
  bu_name.setValue(['location:', id]);

// Create panels to print values.
var inspectorPanel = ui.Panel({style: {width: '30%'}});
var bu_name = ui.Label();
inspectorPanel.add(ui.Panel([bu_name], ui.Panel.Layout.flow('vertical')));

Returns this is the side panel:

location:,ee.String({ "type": "Invocation", "arguments": { "input": { "type": "Invocation", "arguments": { "object": { "type": "Invocation", "arguments": { "collection": { "type": "Invocation", "arguments": { "collection": { "type": "Invocation", "arguments": { "tableId": "users/philtown81/Builtup_Areas" }, "functionName": "Collection.loadTable" }, "filter": { "type": "Invocation", "arguments": { "leftField": ".all", "rightValue": { "type": "Invocation", "arguments": { "geometry": { "type": "Point", "coordinates": [ -0.4458729125976788, 51.7557470078457 ] } }, "functionName": "Feature" } }, "functionName": "Filter.intersects" } }, "functionName": "Collection.filter" } }, "functionName": "Collection.first" }, "property": "bua11cd" }, "functionName": "Element.get" } }, "functionName": "String" })

Here is a link to a working example.

marked as duplicate by Erik, whyzar, Jochen Schwarze, ThingumaBob, nmtoken May 2 at 13:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

You are trying to print objects that make sense in the server side, but in the client side they are just API calls (what you see printed). You have to retrieve their value somehow. There are 2 ways:

  1. Using the function getInfo(callback)

Retrieves the value of this object from the server. If no callback function is provided, the request is made synchronously. If a callback is provided, the request is made asynchronously. The asynchronous mode is preferred because the synchronous mode stops all other code (for example, the EE Code Editor UI) while waiting for the server. To make an asynchronous request, evaluate() is preferred over getInfo().

Returns the computed value of this object.

Arguments:

this:computedobject (ComputedObject):

The ComputedObject instance.

callback (Function, optional):

An optional callback. If not supplied, the call is made synchronously.

Returns: Object

// determine tree density
var NDVI = ndvi.select('NDVI').median().clip(selected).divide(10000)
mapPanel.addLayer(NDVI, {min:0, max:1, palette:['white', 'green']}, 'ndvi')

// values in side panel
bu_name.setValue('Loading name...')
bu_name.setValue('location: '+id.getInfo())

var meanNDVI = NDVI.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: selected,
  scale: 250,
  maxPixels: 1e9
  });

bu_value.setValue('Loading value...')
bu_value.setValue('Mean NDVI: '+JSON.stringify(meanNDVI.getInfo()))
  1. Using function evaluate(callback) (I think this is the best option in this case)

Asynchronously retrieves the value of this object from the server and passes it to the provided callback function.

Arguments:

this:computedobject (ComputedObject):

The ComputedObject instance.

callback (Function):

A function of the form function(success, failure), called when the server returns an answer. If the request succeeded, the success argument contains the evaluated result. If the request failed, the failure argument will contains an error message.

// determine tree density
var NDVI = ndvi.select('NDVI').median().clip(selected).divide(10000)
mapPanel.addLayer(NDVI, {min:0, max:1, palette:['white', 'green']}, 'ndvi')

// values in side panel
bu_name.setValue('Loading name...')
id.evaluate(function(value){bu_name.setValue('location: '+value)});

var meanNDVI = NDVI.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: selected,
  scale: 250,
  maxPixels: 1e9
  });

bu_value.setValue('Loading value...')
meanNDVI.evaluate(function(value){bu_value.setValue('Mean NDVI: '+JSON.stringify(value))})

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