I am working with the Hansen Global Forest Change loss year layer. This raster tells us the year a 30m pixel transitioned to zero forest cover (2000-2014), if relevant (otherwise zero).

I am producing zonal statistics over tiles and am trying to get the within-tile count of pixels that transitioned in 2001, 2002, 2003, ...2014. These counts must be stored as separate properties (ex: count_2001 = x, count_2002=y.

I can execute this over my tiles year-by-year using a remap function and then counting the non-NA values using a reducer function. For example, for 2001:

var gfcLoss2001 = gfcLossYearMasked
.remap([1], [1], null);

//Need to change band name so future bands do not overwrite each other
var gfcLoss2001Clean = gfcLoss2001.select(
  ['remapped'], ['loss2001']);

var loss2001 = tiles.map(function(feature) {
  return feature.set(gfcLoss2001Clean.reduceRegion({
    reducer: 'count',
    geometry: feature.geometry(),
    scale: 100,

I am trying to transform this into a function that I can feed a vector of dates [2001, 2002, ...] and corresponding reference numbers [1, 2, 3] (where 1=2001 in the raster) that can execute this code over the 13 years. The main challenge I encounter is with variable naming. We need to give the generated properties (counts) unique names, but I don't know how to incorporate this into GEE.

If you have alternative strategies for producing these zonal stats, please let me know as well.

1 Answer 1


I would map a function over your feature collection that gets forest loss from your "Hansen Global Forest Change" (GFC) image for every year and then returns each feature with zonal statistics. This example is only partially complete because I am not sure how to mask the GFC image for each year.

// all years in GFC image 
var years = ee.List.sequence(2000,2019)

// indices for years you want to calculate stats for (2001 to 2014) 
var indices = ee.List.sequence(1,14)

// map over feature collection 
var tiles_new = tiles.map(function(feature){
  // iterate over years of interest. could modify this so you iterate over years
  // depending on how the GFC image is masked 
  var mdata = indices.iterate(function(i, dictin){
    // get year for this iteration 
    var thisyear = years.get(i)

    // create property name for this year (e.g. "loss2001" for first iteration)
    var bandname = ee.String('loss').cat(ee.Number(thisyear).int())
    var gfcLossthisyear // assign to this variable GFC image masked to this year 
    var dict = gfcLossthisyear
      // rename band such that the end dictionary key is unique to this year 
      // get zonal statistics 
      .reduceRegion({reducer:'count', geometry: feature.geometry(), scale: 100})
    // append zonal statistics for this year to dictionary 
    return ee.Dictionary(dictin).combine(dict)
  }, ee.Dictionary()) // begin iteration with an empty dictionary  

  // return feature with zonal stats accumulated from all years 
  return feature.set(mdata)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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