How do I map over individual regions, over a several year timespan, in Google Earth Engine?

272 Views Asked by At

I'm new to GEE and am trying to get the average precipitation over a time period 1981-2019 for geometries along a transect. Here is my code:

def yearlyRainfall(year):
    startDate = ee.Date.fromYMD(year, 1, 1)
    endDate = startDate.advance(1, 'year')
    filtered = chirps.filter(ee.Filter.date(startDate, endDate))
    total = filtered.reduce(ee.Reducer.sum())
    stats = total.reduceRegion(
      reducer = ee.Reducer.mean(),
      geometry = circles_collection,
      scale = 5000,
    )
    f = ee.Feature(None,{
      'year': year,
      'precipitation': stats.get('precipitation_sum')
    })
    return f
years = ee.List.sequence(1981, 2019)
rainfallYears = ee.FeatureCollection(years.map(yearlyRainfall))
rainfalldf = geemap.ee_to_pandas(rainfallYears)
print(rainfalldf["precipitation"].mean())

This code can find the mean precipitation for all the geometries in circles_collection. However, I want to make a separate feature collection for each geometry within the feature collection so I can get the precipitation data for the individual geometries, rather than averaging all of them. Any help is appreciated.

Tried: adding "geo" parameter to yearlyRainfall(), produces error when trying to map over a function that takes in an alternate parameter

1

There are 1 best solutions below

0
On

Use reduceRegions instead. Also, create an image with bands for all years instead of mapping over the years so you only do the reduce call once.

def yearlyRainfall(year):
    startDate = ee.Date.fromYMD(year, 1, 1)
    endDate = startDate.advance(1, 'year')
    filtered = chirps.filter(ee.Filter.date(startDate, endDate))
    return filtered.reduce(ee.Reducer.sum()).rename(f'precipitation_{year}')

years = ee.List.sequence(1981, 2019)
rainfallYears = ee.Image(years.map(yearlyRainfall))

stats = rainfallYears.reduceRegions(
  collection = circles_collection,
  reducer = ee.Reducer.mean(),
  scale = 5000
)

print(stats.getInfo())