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.
.map(function(feature){
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(sheds.limit(1));
// 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:
print(areaStats.get('mean'));
print(areaStats.mean);
print(areaStats['mean']);
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!
Best Answer
Here is a workaround for accessing the results from
aggregate_stats()
using the Python API.