I am learning Google Earth Engine with no prior knowledge of javascript (coming from R and python). Following the FeatureCollection tutorial with the following script:

// Load watersheds from a data table.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')
  // Filter to the continental US.
  .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29))
  // Convert 'areasqkm' property from string to number.
    var num = ee.Number.parse(feature.get('areasqkm'));
    return feature.set('areasqkm', num);

// Display the table and print its first element.
Map.addLayer(sheds, {}, 'watersheds');

// Print the number of watersheds.
print('Count:', sheds.size());

// Print stats for an area property.
print('Area stats:', sheds.aggregate_stats('areasqkm'));

The last step creates something that the console calls a DataDictionary. This doesn't seem to behave like a normal dictionary, & I can't figure out how to access the individual elements within. I assigned the results with

var areaStats = sheds.aggregate_stats('areasqkm')

And then tried all of the following:


Which resulted in the error areaStats.get is not a function for the first and simply printing 'undefined' for the others. What is going on? I can clearly see a value called 'mean' if I print areaStats in the console.

I might not ever need to use this code on my own data, but it bothers me that I can't figure out something as apparently simple as how to access elements within a dictionary!

  • 1
    I believe that aggregate_stats() will not allow you to access the stats for use as variables in your code--it's just for inspection in the console. You would instead want to use a Reducer to make a FeatureCollection which you can indeed access. I didn't write an answer because I'm not certain this is correct. – Jon Jul 24 '19 at 23:40
  • I agree with @Jon. One thing that can be done is using evaluate or getInfo functions to get the Object to client side which can then be manipulated as standard JS object like a dictionary. – Nishanta Khanal Jul 25 '19 at 6:22
  • Yes you're right @Jon, I tried that approach and it does indeed work. I'll post the answer – rasenior Jul 25 '19 at 15:18

It does appear that you can't access the results from aggregate_stats(), but you can use Reducer to get stats as variables. The code below calculates the mean only, which matches the result from aggregate_stats (25513.32264880953):

var meanArea = 
  // Calculate mean area across polygons
  sheds.reduceColumns(ee.Reducer.mean(), ['areasqkm'])
print('Area mean:', meanArea);

Here is a workaround for accessing the results from aggregate_stats() using the Python API.

def summary_statistics(collection, column):

    stats = collection.aggregate_stats(column).getInfo()
    return eval(str(stats)).get('values')

results = summary_statistics(your_collection, column_name)

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.