0

I would like to print names and value from classified images with this code

//**Set center **//
Map.setCenter(103.667, 11.5019, 9);

//**Import Copernicus Global Landcover dataset
var dataset = ee.Image("COPERNICUS/Landcover/100m/Proba-V-C3/Global/2019")
              .clip (roi);
              
//**Selection attribute value
var classified = dataset.select(['discrete_classification']);

//**Visualize land cover map on canvas
Map.addLayer(classified, {}, "Land Cover Combodja");

//**grouping the images
var landcover_class = ee.Image.pixelArea().addBands(classified);
print (landcover_class)

//**Calculation area
var reduction_results = landcover_class.reduceRegion({
  reducer: ee.Reducer.sum().group({
    groupField: 1,
    groupName: 'Landcover ID',
  }),
  geometry: roi,
  scale: 100,
  maxPixels: 1e10
});
print('reduction_results', reduction_results);
var roi_stats = ee.List(reduction_results.get('groups'));
print('roi_stats', roi_stats)

///////////////////////////////////////////////////////////////////
var roi_stats = ee.List(reduction_results.get('groups'));
// Create a dictionary for looking up names of transition classes.
var lookup_names = ee.Dictionary.fromLists(
    ee.List(gsw.get('classification_values')).map(ee.String),
    gsw.get('classification_names')
);
print (lookup_names)
// Create a dictionary for looking up colors of transition classes.
var lookup_palette = ee.Dictionary.fromLists(
    ee.List(gsw.get('transition_class_values')).map(ee.String),
    gsw.get('transition_class_palette')
);
print (lookup_palette)

however, these code doesnt work and something appears in console "Dictionary (Error) List.map: Parameter 'list' is required. "

How can I fix this?

0
2

If you want to have simultaneously class names and sums in your final result, you have to map roi_stats with a function able to retrieve class names from corresponding list by using indices obtained from values class list. Following script can do that where I created an arbitrary roi area (you don't provide one) that encloses your point (103.667, 11.5019).

//**Set center **//
var pt = ee.Geometry.Point([103.667, 11.5019]);

var roi =ee.Geometry.Polygon(
        [[[103.5626298828125, 11.418453478561592],
          [103.677986328125, 11.375374688788868],
          [103.89496630859375, 11.46690929767244],
          [103.83454150390625, 11.590702996961742],
          [103.68897265625, 11.58801241064796],
          [103.6505205078125, 11.628368481791046],
          [103.55439013671875, 11.628368481791046],
          [103.51319140625, 11.534195251171665]]]);

Map.setCenter(103.667, 11.5019, 9);
//Map.addLayer(pt);


//**Import Copernicus Global Landcover dataset
var dataset = ee.Image("COPERNICUS/Landcover/100m/Proba-V-C3/Global/2019")
              .clip (roi);

print(dataset.propertyNames());

var ds_names = dataset.get('discrete_classification_class_names');
print("class names", ds_names);


var ds_value = dataset.get('discrete_classification_class_values');
print("class values", ds_value);

var ds_palette = dataset.get('discrete_classification_class_palette');
print("palette names", ds_palette);

//**Selection attribute value
var classified = dataset.select(['discrete_classification']);

//**Visualize land cover map on canvas
Map.addLayer(classified, {}, "Land Cover Combodja");

//**grouping the images
var landcover_class = ee.Image.pixelArea().addBands(classified);

print (landcover_class);

//**Calculation area
var reduction_results = landcover_class.reduceRegion({
  reducer: ee.Reducer.sum().group({
    groupField: 1,
    groupName: 'Landcover ID',
  }),
  geometry: roi,
  scale: 100,
  maxPixels: 1e10
});

print('reduction_results', reduction_results);

var roi_stats = ee.List(reduction_results.get('groups'));

print('roi_stats', roi_stats);

var pairNameSum = roi_stats.map(function (ele) {
  
  var val = ee.Dictionary(ele).get('Landcover ID');
  var sum = ee.Dictionary(ele).get('sum');
    
  var idx = ee.List(ds_value).indexOf(ee.Number(val));
  
  return [ee.List(ds_names).get(idx), sum];
  
});


print('roi_stats with class names', pairNameSum);

After running above script in GEE code editor, I got result of following image. There are printed class names paired with respective sums.

enter image description here

0

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.