Search code examples
pythongoogle-earth-engine

Google EarthEngine: Time series of reduceRegion()


I am using the Google EarthEngine Python API. I have an image collection (MODIS) and would like to extract a time series containing mean NDVI for a region for each time step.

Currently, I am iterating over single images and extract the values for each. Like

feature_geometry = {
    'type': 'MultiPolygon',
    'coordinates': [[[
        [-120, 35],
        [-120.001, 35],
        [-120.001, 35.001],
        [-120, 35.001],
        [-120, 35]
    ]]]
}
ee.Initialize()
feature = ee.Feature(feature_geometry)
collection = ee.ImageCollection(
    'MODIS/006/MOD13Q1').filterDate('2017-01-01', '2017-05-01')
images = [
    item.get('id') for item in collection.getInfo().get('features')]
for image in images:
    print(ee.Image(image).reduceRegion(
        ee.Reducer.mean(), feature.geometry()).getInfo()['NDVI'])

The question: Is there a way to get the same result in a single request to EarthEngine, since I tend to run into request limits.


Solution

  • Here's an example of what I think you're asking:

    import ee
    ee.Initialize()
    
    feature_geometry = {
        'type': 'MultiPolygon',
        'coordinates': [[[
            [-120, 35],
            [-120.001, 35],
            [-120.001, 35.001],
            [-120, 35.001],
            [-120, 35]
        ]]]
    }
    
    collection = ee.ImageCollection(
        'MODIS/006/MOD13Q1').filterDate('2017-01-01', '2017-05-01')
    
    def setProperty(image):
        dict = image.reduceRegion(ee.Reducer.mean(), feature_geometry)
        return image.set(dict)
    
    withMean = collection.map(setProperty)
    
    print withMean.aggregate_array('NDVI').getInfo()