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?