Falk Schuetzenmeister
Falk Schuetzenmeister

Reputation: 1597

Google EarthEngine: Getting time series for reduceRegion

I am working with Google EarthEngine (Python API, but that does not really matter). I have an ImageCollection that I need to reduce by Region for every single image in the collection.

Is there a way to get a time series for .reduceRegion with a single request to the EarthEngine. So far I figured out that .reduceRegion('mean', feature) works only on images. I need an equivalent to collection.reduceRegion('mean', feature) -- which does not exist -- with the goal to get a list of values for every time step.

The underlying problem is, I am running into the request limit of the EE (3 per seconds) while creating my time series. In addition, it is very slow to issue a request for every single value.

Is there a way to construct an appropriate reducer for collections. Since collection reducers need to return images (pls tell me if that is incorrect), I could imagine e.g. to create an image with one band per image in the input collection that has only one pixel with the desired value.

Thank you for your help

Upvotes: 2

Views: 3361

Answers (1)

Rodrigo E. Principe
Rodrigo E. Principe

Reputation: 1311

Here is an approach.

In this script you get a dictionary without null values

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 14 11:21:09 2017

@author: Gennadii
"""
import ee
ee.Initialize()

geometry = ee.Geometry.Polygon([[[-71.54365539550781, -43.07340216393553],
          [-71.5484619140625, -43.11050787253287],
          [-71.488037109375, -43.125043167401266],
          [-71.48460388183594, -43.0754084526532]]])

def calcMean(img):
  # gets the mean NDVI for the area in this img
  mean = img.reduceRegion(ee.Reducer.mean(), geometry, 30).get('NDVI')

  # sets the date and the mean NDVI as a property of the image
  return img.set('date', img.date().format()).set('mean', mean)

# Applies calcMean() in the collection
col = ee.ImageCollection("LANDSAT/LC8_L1T_8DAY_NDVI").filterDate("2014-01-01","2014-03-31").map(calcMean)

# Reduces the images properties to a list of lists
values = col.reduceColumns(ee.Reducer.toList(2), ['date', 'mean']).values().get(0)

# Type casts the result into a List
lista = ee.List(values)

# Converts the list of lists to a Dictionaty
means = ee.Dictionary(lista.flatten())
print "Dictionary of means:", means.getInfo()

and this other script you get also null values. They are filled with -10 in this script, but you can change that to whatever you need. It could be 0, or a string.

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 14 11:17:29 2017

@author: Rodrigo E. Principe
"""
import ee
ee.Initialize()

geometry = ee.Geometry.Polygon([[[-71.54365539550781, -43.07340216393553],
          [-71.5484619140625, -43.11050787253287],
          [-71.488037109375, -43.125043167401266],
          [-71.48460388183594, -43.0754084526532]]])

col = ee.ImageCollection("LANDSAT/LC8_L1T_8DAY_NDVI").filterDate("2014-01-01","2014-03-31")

# Initial empty Dictionary
meansIni = ee.Dictionary()

def calcMean(img, first):

  #gets the year of the image
  year = img.date().format()

  #gets the NDVI
  nd = ee.Image(img).reduceRegion(ee.Reducer.mean(),geometry,30).get("NDVI")

  #Checks for null values and fills them with whatever suits you (-10 is just an option)
  ndvi = ee.Algorithms.If(ee.Algorithms.IsEqual(nd, None), -10, nd)

  #fills the Dictionary
  return ee.Dictionary(first).set(year, ndvi)

# Apply calcMean() to the collection
means = ee.Dictionary(col.iterate(calcMean, meansIni))

print "Dictionary of means:", means.getInfo()

Upvotes: 1

Related Questions