2

I'm new to javascript & GEE, trying to reduce a global FeatureCollection by computing sums of two specific properties, grouped by country (using this helpful link). The output is a Dictionary Object that contains all the info I need.

I'd like to reformat the data into rows and columns and then export it as a CSV.

What is the simplest way to do this?

// Call up WDPA data, filter

var dataset = ee.FeatureCollection('WCMC/WDPA/current/polygons')
              .filter(ee.Filter.or(
              ee.Filter.and(ee.Filter.eq('MARINE','0'),
                            ee.Filter.eq('IUCN_CAT','Ia')),
              ee.Filter.and(ee.Filter.eq('MARINE','0'),
                            ee.Filter.eq('IUCN_CAT','Ib'))));

// Compute sums of protected area, grouped by country

var sums = dataset
  .filter(ee.Filter.and(
    ee.Filter.neq('GIS_AREA', null),
    ee.Filter.neq('REP_AREA', null)))
  .reduceColumns({
    selectors: ['REP_AREA', 'GIS_AREA', 'PARENT_ISO'],
    reducer: ee.Reducer.sum().repeat(2).group({
      groupField: 2,
      groupName: 'country',
    })
});

print(sums);

I can export as a CSV from there, but it obviously comes out in Dictionary form (i.e. not terribly useful in a spreadsheet). This post almost addresses the question, but their initial output is a FeatureCollection, and mine is an Object. I couldn't get the chosen answer to work for me, and I'm just going in circles.

1 Answer 1

3

This may not be the only way to do it, but it works:

/** YOUR CODE **/
// Call up WDPA data, filter

var dataset = ee.FeatureCollection('WCMC/WDPA/current/polygons')
              .filter(ee.Filter.or(
              ee.Filter.and(ee.Filter.eq('MARINE','0'),
                            ee.Filter.eq('IUCN_CAT','Ia')),
              ee.Filter.and(ee.Filter.eq('MARINE','0'),
                            ee.Filter.eq('IUCN_CAT','Ib'))));

// Compute sums of protected area, grouped by country

var sums = dataset
  .filter(ee.Filter.and(
    ee.Filter.neq('GIS_AREA', null),
    ee.Filter.neq('REP_AREA', null)))
  .reduceColumns({
    selectors: ['REP_AREA', 'GIS_AREA', 'PARENT_ISO'],
    reducer: ee.Reducer.sum().repeat(2).group({
      groupField: 2,
      groupName: 'country',
    })
});

print(sums);

/** MY ANSWER **/
var groups = ee.List(sums.get('groups'));

var fc = ee.FeatureCollection(groups.map(function(obj) {
  var object = ee.Dictionary(obj)
  var name = ee.String(object.get('country'))
  var sum = ee.List(object.get('sum'))
  var rep = ee.Number(sum.get(0))
  var gis = ee.Number(sum.get(1))
  return ee.Feature(null, {'name':name, 'rep_area':rep, 'gis_area':gis})
}))

print(fc.limit(1))

Export.table.toDrive({
  collection: fc, 
  description: 'description', 
  folder: 'FOLDER', 
  fileNamePrefix: 'NAME', 
  fileFormat: 'CSV',
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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