Geographic Information Systems Asked by Johann Kraft on January 10, 2021
I am calculating the mean precipitation value over a given area from CHIRPS precipitation data.
My goal is to get a list containing a date and precipitation value for every Image of the ImageCollection
.
Using .getInfo()
on the reduced ImageCollection
I can get a dict representing the ImageCollection
and from there select the values on the client side.
import ee
from datetime import datetime
ee.Initialize()
dataset = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filter(ee.Filter.date('2016-01-01','2016-12-31'))
area = ee.Geometry.Polygon([[[29.045341997822412, -2.1339228457039368],
[29.07984593458999, -2.1339228457039368],
[29.07984593458999, -2.0966977113182073],
[29.045341997822412, -2.0966977113182073],
[29.045341997822412, -2.1339228457039368]]])
# Image reduction applied to each image.
def reduce_dataset_region(image):
# Calculate mean of precipitation on defined area.
local_precipitation_image = image.reduceRegion(
reducer=ee.Reducer.mean(),
geometry=area,
scale=20
)
return image.set('mean', local_precipitation_image)
# Apply region reduction to ImageCollection
reduced_dataset = dataset.map(reduce_dataset_region, True)
# Request server-side ImageCollection as dict
reduced_dataset_dict = reduced_dataset.getInfo()
# Create list to hold daily precipitation values.
precipitation_list = []
# Loop through features in image collection dict. One feature represents one image.
for feature in reduced_dataset_dict['features']:
# Get date and turn into datetime
# Get precipitation value and add with datetime to list.
precipitation_list.append((timestamp, feature['properties']['mean']['precipitation']))
print(precipitation_list)
This seems slow and not the right way to go, especially since I want to use data from multiple years.
How can I create a list containing the precipitation value for each Image and send only that, and not the whole ImageCollection
, to the client? What is the better alternative to .getInfo
here?
I found a solution using the iterate()
function (https://developers.google.com/earth-engine/apidocs/ee-imagecollection-iterate). It calls the reduce_dataset_region
function and returns a list containing the precipitation values.
This example helped understanding how to use it.
import ee
ee.Initialize()
dataset = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filter(ee.Filter.date('2016-01-01', '2016-12-31'))
area = ee.Geometry.Polygon([[[29.045341997822412, -2.1339228457039368],
[29.07984593458999, -2.1339228457039368],
[29.07984593458999, -2.0966977113182073],
[29.045341997822412, -2.0966977113182073],
[29.045341997822412, -2.1339228457039368]]])
# Create list to hold precipitation values
precipitation_list = ee.List([])
# Image reduction applied to each image.
def reduce_dataset_region(image, list):
# Calculate mean of precipitation on defined area.
local_precipitation_image = image.reduceRegion(
reducer=ee.Reducer.mean(),
geometry=area,
scale=20
)
return ee.List(list).add(local_precipitation_image)
# Apply region reduction to ImageCollection and return result in List
reduced_dataset = dataset.iterate(reduce_dataset_region, precipitation_list)
# Request server-side List as dict
reduced_dataset_dict = reduced_dataset.getInfo()
Answered by Johann Kraft on January 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP