I'm using a feature collection that's a grid over an area:

var BGD = ee.FeatureCollection("FAO/GAUL/2015/level0")
.filter(ee.Filter.eq("ADM0_NAME", "Bangladesh"));

// Specify grid size in projection, x and y units (based on projection).
var projection='EPSG:4326'; // WGS84 lat lon
var dx=0.1;
var dy=0.1;

// Make grid and visualize.
var proj=ee.Projection(projection).scale(dx, dy);
var grid=BGD.geometry().coveringGrid(proj);

I want to get the mean forest cover ("Percent_Tree_Cover"), nonforest vegetation ("Percent_NonTree_Vegetation"), and nonforest nonvegetative cover ("Percent_NonVegetated") for each year 2000-2020 from the MODIS 250m tree cover dataset:

var data = ee.ImageCollection('MODIS/006/MOD44B');

So far I have tried writing out separate functions for each year and variable as follows:

var name = "Percent_Tree_Cover"

var tree = data.filter(ee.Filter.date('2000-01-01', '2000-12-31'))
.map(function(image){return image.clip(BGD)}).select(name).toBands();

var tree = tree.rename("PctFor00");

var grid = grid.map(function(feature) {
  return feature.set(tree.reduceRegion({
    reducer: 'mean', geometry: feature.geometry(),

How do I write a function that generates these reductions for all variables (percent forest cover, percent nonforest vegetation, percent nonvegetated) and years that is more computationally efficient than repeating the same code over and over?

1 Answer 1


How about using ee.Image.reduceRegions using the grid as the collection, and then mapping this function to your image collection? Make sure to also retain some information about the image, e.g. the year:

function reduceImage(image){
  return image.reduceRegions({
    scale:250  // Adjust if needed
  return feature.set({

var results = ee.FeatureCollection(

A quick test printing the first few results (limited to 5 features from the grid and 2 images):


For the complete results I would recommend preparing an Export task.

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.