0

The print() on the Google Earth Engine cloud editor just processes the information and prints a value in the console. What's the equivalent on a standalone application JavaScript/HTML application using the google earth api?

I've tried using console.log(JSON.stingify()) but all that I obtain is a description of the object.

The code I'm trying to compute is:

var chirps = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD');

var year = 2017;
var startDate = ee.Date.fromYMD(year, 1, 1);
var endDate = startDate.advance(1, 'year');
var filtered = chirps
  .filter(ee.Filter.date(startDate, endDate));
var total = filtered.reduce(ee.Reducer.sum());

var bangalore = ee.FeatureCollection("users/ujavalgandhi/public/bangalore_boundary");
var stats = total.reduceRegion({
  reducer: ee.Reducer.mean(),
  geometry: bangalore,
  scale: 5000,
  })

console.log(stats);
console.log(JSON.stringify(stats.get('precipitation_sum'))); // 1336.52 mm

Thanks

VLAZ
  • 26,331
  • 9
  • 49
  • 67

1 Answers1

0

You are correct in that console.log() is the way to go. Though both this method and JSON.stringify() are client-side operations, so you cannot pass a server-side object to it. Read up on in here.

You can turn a server-side object, like stats and stats.get('precipitation_sum') to a client-side object with getInfo().

console.log(stats.getInfo());
console.log(JSON.stringify(stats.get('precipitation_sum').getInfo())); // 1336.52 mm

The problem with this is that it's a synchronous/blocking operations. It typically give you a bad user experience. An alternative to this is to use the asynchronous evaluate(), which takes a callback.

stats.evaluate(function (clientStats) {
  console.log(clientStats)
  console.log(clientStats.precipitation_sum)
})
Daniel Wiell
  • 253
  • 2
  • 5