Geographic Information Systems Asked on September 3, 2021
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!
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'])
.get('mean');
print('Area mean:', meanArea);
Answered by rasenior on September 3, 2021
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)
print(results.get('sum'))
print(results.get('mean'))
Answered by Qiusheng Wu on September 3, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP