0

I am trying to export the loss pixels from the Hansen/UMD dataset for each year from 2001-2014, for a region. I hope to obtain a stack of 13 rasters (one for each year), with a pixel having 0 or 1 in each of those rasters. My intent is then to run some sort of trend analysis of the loss 2001-2014.

As of now I have the following code, which loads the lossYear band and the geography in the form of a fusion table, reduces the lossYear image to the geography and iterates across the years 1-14.

// Load Hansen tree cover

var gfcImage = ee.Image('UMD/hansen/global_forest_change_2015');
var lossYear = gfcImage.select(['lossyear']); 

var scale = 100

//Sequence of years for which we need to extract loss
var years = ee.List.sequence(1, 14) //2001 to 2014 based on v1.2 of UMD dataset

// Load India border
var statesIndia = ee.FeatureCollection('ft:13fekwKFyNB2IIEgpdN156LdWpMcXohQzZ3kjzQ');
Map.setCenter(78.9629,20.5937)

var addVar = function(feature) {

  // function to iterate over the sequence of years
  var addVarYear = function(year, feat) {
  // cast var
year = ee.Number(year).toInt()
feat = ee.Feature(feat)

// actual year to write as property
var actual_year = ee.Number(2000).add(year)

// filter year:
// 1st: get mask
var filtered = lossYear.select("lossyear").eq(year)
// 2nd: apply mask
filtered = lossYear.updateMask(filtered)

// reduce variables over the feature
var reduc = filtered.reduceRegion({
  geometry: feature.geometry(),
  reducer: ee.Reducer.sum(),
  scale: scale
})

// get results
var loss = ee.Number(reduc.get(lossYear))

// set names
var nameloss = ee.String("loss_").cat(actual_year)

 // alternative 2: always set property
// set properties to the feature
return feat.set(nameloss, loss)
  }

  // iterate over the sequence
  var newfeat = ee.Feature(years.iterate(addVarYear, feature))

  // return feature with new properties
  return newfeat
}

// Map over the FeatureCollection
var lossIndiastack = statesIndia.map(addVar);

Map.addLayer(lossIndiastack, {}, "lossIndiastack")

But I get the error lossIndiastack: Layer error: String: Unable to convert object to string. And then I need to figure out how to export the stack.

Can anyone help me figure out the error and provide a solution?

2 Answers 2

1

The error

lossIndiastack: Layer error: String: Unable to convert object to string

is due to trying to concatenate a string with a number, and is solved unsing format()

var nameloss = ee.String("loss_").cat(actual_year.format())

But there are another errors. The function reduceRegion returns a Dictionary, so when you get something from it you have to use the name of a key, and you are using an Image. Replace

var loss = ee.Number(reduc.get(lossYear))

with

var loss = ee.Number(reduc.get('loss'))

And the final error is that you are reducing over points, and that is not helpful, and may not be what you want. The reducer you are using is meant for other geometries, like polygons.

For exporting, you have to use Export.image.toDrive as mentioned in Nicholas's answer.

I leave a working example: https://code.earthengine.google.com/435688b5d08788f749ce5c704497b5f4

1
  • can this be adapted to export a table instead, with polygon id and the annual area lost? Commented Jun 8, 2020 at 22:01
1

This will depend on whether you want a stack of rasters or something else. If it's the latter, then you need to clarify the question. Specifically, what you hope to get from reduceRegion(). If it's the former, then here's a way to do it:

// Load Hansen tree cover
var gfcImage = ee.Image('UMD/hansen/global_forest_change_2015');
var lossYear = gfcImage.select(['lossyear']); 

// Sequence of years for which we need to extract loss
var years = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]; 

// Example geometry.
var geometry = ee.Geometry.Polygon(
        [[[81.99512656249999, 25.52458880998577],
          [81.86329062499999, 24.168797998437054],
          [83.53321249999999, 24.569097959485987]]]);

var lossesStack = lossYear.gt(years);

Export.image.toDrive({
  image: lossesStack, 
  description: 'foo',
  fileNamePrefix: 'foo', 
  region: geometry, 
  scale: 30
});

(Also note that reduceRegion() is not at all what you want in this context.)

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.