Falk Schuetzenmeister
Falk Schuetzenmeister

Reputation: 1597

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.

Upvotes: 1

Views: 2778

Answers (1)

Nicholas Clinton
Nicholas Clinton

Reputation: 893

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()

Upvotes: 6

Related Questions