1

I am calculating zonal statistics for 0.01-degree tiles that fall within buffer areas across sub-Saharan Africa. I want to determine the estimated population counts within the tiles for years 2001-2012 using the Worldpop dataset. Earth Engine stores this image collection so that each feature (image) represents a country-year. When I am deriving Worldpop zonal stats. for just one country, I have used the following function before (in this example, for tiles ("grid") across Syria):

var pop = ee.ImageCollection("WorldPop/GP/100m/pop")
  .filterDate('2000', '2012')
  .filterMetadata('country', 'equals', 'SYR');

function newCollectionToImage(collection){
var stack = ee.Image(collection.iterate(function(img, prev) {
return ee.Image(prev).addBands(img);
}, ee.Image(1)));
 
stack = stack.select(ee.List.sequence(1, stack.bandNames().size().subtract(1)));
return stack;
}

var tch = pop.map(function(img){
var y = ee.String(ee.Date(img.get('system:time_start')).get("year"));
return img.set("year",y).rename(y);
});

var tchImg = newCollectionToImage(tch);

var pop_Syria = 
tchImg.select(['2000', '2001', '2002', '2003', '2004', '2005', '2006',
'2007', '2008', '2009', '2010', '2011'],
['pop_00', 'pop_01', 'pop_02', 'pop_03', 'pop_04', 'pop_05', 'pop_06',
'pop_07', 'pop_08', 'pop_09', 'pop_10', 'pop_11']);

var Syr_gridpop = pop_Syria.reduceRegions(grid,ee.Reducer.sum());

But what if instead, I wanted zonal statistics from polygons in multiple countries? When I tried removing the metadata filter (that designates country), the resulting stack of images that this function produces is all zeroes. Here is my code.

Is there a way in which I can combine the country-level features in the Worldpop data by year (resulting in a global image) and then perform the zonal statistics?

1 Answer 1

2

You can just mosaic() the images from each year together by mapping over a list of year numbers.

var years = ee.List.sequence(2000, 2012)
// Make some band names from the years numbers.
var yearBandNames = years.map(function(n) { return ee.Number(n).format("pop_%d") })

var yearImages = ee.ImageCollection(
  years.map(function(year) {
    var oneYear = ee.ImageCollection("WorldPop/GP/100m/pop")
      .filter(ee.Filter.calendarRange(year, year, 'year'))
      .filterBounds(geometry)
    return oneYear.mosaic()
  }))
  .toBands()
  .rename(yearBandNames)

https://code.earthengine.google.com/ce059e2a1c82c2a3b9403ab86e0eb693

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.