0

Starting with this, I have

// load
var landCover = ee.ImageCollection('MODIS/006/MCD12Q1');
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
// filter

// filter on image for 2017 and clip with country shape
var modis2017 = ee.Image(landCover.filterDate('2016-01-01', '2016-12-01')
                .select('LC_Prop1')
                .first()); //.clip(sel);
// ----------------------------- SENEGAL ----------------------------------------
var sel = country.filter(ee.Filter.eq('country_na', 'Senegal'));
print(sel);

// get the results in an array
var values = ee.List(modis2017.get('LC_Prop1_class_values'));
var array = values.map(function(ind){
  var cstImage = ee.Image.constant(ind);
  var equals = modis2017.eq(cstImage);
  return equals.reduceRegion(ee.Reducer.sum(), sel.geometry(), 500).values();
});

print(array);

But I'm unable to export those results to a CSV. I don't understand how can I convert a list into ee.FeatureCollection manageable by

// Export the FeatureCollection.
Export.table.toDrive({
  collection: array,
  description: 'senegal_landcover',
  fileFormat: 'CSV'
});
1

It's simpler to use a grouped reducer rather than running reduceRegion for each class. It's also more accurate to add pixel areas rather than just counting pixels. Putting those ideas together:

var landCover = ee.Image('MODIS/006/MCD12Q1/2016_01_01').select('LC_Prop1');
var countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');

print(landCover.addBands(ee.Image.pixelArea()).reduceRegion({
  reducer: ee.Reducer.sum().unweighted().group(),
  geometry: countries.filter(ee.Filter.eq('country_na', 'Senegal')),
}));

If you want something that's table-shaped for exporting you need to do a little conversion:

var landCover = ee.Image('MODIS/006/MCD12Q1/2016_01_01').select('LC_Prop1');
var countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');

var areas = landCover.addBands(ee.Image.pixelArea()).reduceRegion({
  reducer: ee.Reducer.sum().unweighted().group(),
  geometry: countries.filter(ee.Filter.eq('country_na', 'Senegal')),
}));
var asList = ee.List(areas.get('groups')).map(function (pair) {
  return ee.Feature(null, pair);
});
Export.table.toDrive(ee.FeatureCollection(asList));
0

I think the solution of Rodrigo works almost perfectly, only the name of the property is not set equally in all features inside the collection, so I think therefore you are only seeing the property of the first one when exporting. With a small adaptation of Rodrigo's answer, it will work, see below.

Use this to set all features with the same name and the CSV file is exported with all the property values (at least when I did it :)):

var array = ee.FeatureCollection(values.map(function(ind){
  ind = ee.Number(ind)
  var cstImage = ee.Image.constant(ind);
  var equals = modis2017.eq(cstImage);
  var count = equals.reduceRegion(ee.Reducer.sum(), sel.geometry(),     500).get('LC_Prop1');
  return ee.Feature(null, {'area': count})
}));

link to code

0

You can change the array function in order to return a FeatureCollection with empty Features.

var array = ee.FeatureCollection(values.map(function(ind){
  ind = ee.Number(ind)
  var indstr = ee.String(ind.format())
  var prop = ee.Dictionary({})
  var cstImage = ee.Image.constant(ind);
  var equals = modis2017.eq(cstImage);
  var count = equals.reduceRegion(ee.Reducer.sum(), sel.geometry(), 500).get('LC_Prop1');
  prop = prop.set(indstr, count)
  return ee.Feature(null, prop)
}));

// Export the FeatureCollection.
Export.table.toDrive({
  collection: array,
  description: 'senegal_landcover',
  fileFormat: 'CSV'
});

EDIT: As @delaye commented, this looks good, but cannot be exported. @Kuik observation is right, this is due to the Features having different property names (each class). In order to be exported, all Features must have the same property names with (of course) different values. I'd tell you to accept @Kuik modification, but I think it's missing one thing, the actual class, otherwise you couldn't know what class has that number of pixels. This new (little) modification will done it I hope:

var array = ee.FeatureCollection(values.map(function(ind){
  ind = ee.Number(ind)
  var indstr = ee.String(ind.format())
  var cstImage = ee.Image.constant(ind);
  var equals = modis2017.eq(cstImage);
  var count = equals.reduceRegion(ee.Reducer.sum(), sel.geometry(), 500).get('LC_Prop1');
  return ee.Feature(null, {'class':indstr, 'count':count})
}));
  • Results in array seem to be good! Great! But the Export.table.toDrive don't work. Most of the cells are empty. What going wrong? – delaye Dec 12 '18 at 15:03
  • you and @Kuik are right. I modified the answer to match your needs, in my test it worked. – Rodrigo E. Principe Dec 12 '18 at 17:03

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.