0

I would like to plot the number of pixels or the area for each class contained in a polygon (shape of a country). I have found the ui.Chart.image.byClass function but I don't understand how to use it with only one raster

for example :

// load
var landCover = ee.ImageCollection('MODIS/006/MCD12Q1');
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
// filter
var sn = country.filter(ee.Filter.eq('country_na', 'Senegal'));
// 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(sn);



// display
Map.addLayer(sn, {}, 'country');
//Map.addLayer(country, {}, 'country');
Map.addLayer(modis2017, {}, 'raster');
  • Why would you like to calculate the amount of pixels inside each country? This requires a huge aggregation of pixels which would run out of memory easily. – Kuik Nov 23 '18 at 8:00
  • @Kuik , my purpose is to know for each class of the raster the area for a specific country (Senegal in my example). MODIS/006/MCD12Q1 data contain 16 class so I would like to have a table (or barplot) with the number of pixel for each class in Senegal – delaye Nov 23 '18 at 9:12
1

For a quick overview of the amount of pixels each class has inside your area of interest (sn), you should use the histogram chart:

  • Update: results in an array added with the full code
// load
var landCover = ee.ImageCollection('MODIS/006/MCD12Q1');
var country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
// filter
var sn = country.filter(ee.Filter.eq('country_na', 'Senegal'));
print(sn)

// 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(sn);
print(modis2017)

// quick plot the amount of pixels in a histogram
var graph = ui.Chart.image.histogram(modis2017, sn, 500).setOptions({
  title: 'Histogram ## pixels for each class',
});
print(graph);

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

print(array)

// display
Map.addLayer(sn, {}, 'country');
//Map.addLayer(country, {}, 'country');
Map.addLayer(modis2017, {}, 'raster');

https://code.earthengine.google.com/d9bf71c4aa94943d1fabd6cd659632a3

  • If I want those result in an array? How can I do that? – delaye Nov 23 '18 at 12:34
  • I added the full code to do so. You will see that small amounts of pixels are now also taken into consideration. Also note that values can slightly differ, possibly because the pixels are partly in the region of interest – Kuik Nov 23 '18 at 12:52
  • me again! I'm trying to save the array with Export.table.toDrive({ collection: array, description:'test1', fileFormat: 'CSV' }); but I don't understand the object array ! Why it's wrong ? – delaye Dec 10 '18 at 21:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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