[GIS] Summing of individual image’s statistic over ImageCollection Google Earth Engine

countgoogle-earth-enginegoogle-earth-engine-javascript-apiimage

I have an ImageCollection containing FIRMS fire image in 2014. I want to count the number of fires in the year for a predefined region. I can find a way to count the number of fires for an individual image through the use of ee.Image.reduceRegion.

However, I cannot figure out a way to summing up individual image's fire count to derive total fire count for 2014.
Here's my code:

        // Load Vietnam shapefile 
    var vn_shape = ee.FeatureCollection('users/chucmd/VNM_adm0');
    print (vn_shape);
    
    // Load fire counts image
    var fire = ee.ImageCollection('FIRMS').filterBounds(vn_shape).filterDate('2014-01-01', '2014-03-30');
    
    // Clip image to Vietnam
    var fire_vn = fire.map(function(image) {return image.clip(vn_shape);});
    print (fire_vn);
    
    // Filter fire with more than 50% confidence and add a new band representing areas where confidence of fire > 50%
    var filterConfidence = function(image) {
      var line_number = image.select('line_number');
      var confidence = image.select('confidence');
      var conf_50 = confidence.gt(50).rename('confidence_50');
      var count_band = line_number.multiply(conf_50).rename('count');
      return image.addBands(count_band);
    };
    var fire_conf_vn = fire_vn.map(filterConfidence);
    print (fire_conf_vn);
    
    // fire count for individual image
    var img1 = ee.Image(fire_conf_vn.first());
    print (img1);
    var countObject = img1.reduceRegion({
        reducer: ee.Reducer.countDistinct(),
        scale: 1000,
        geometry: vn_shape
      });
    print (countObject);

And the result:
cc

Best Answer

Summing up attributes of images in an ImageCollection can be done using ee.ImageCollection.aggregate_sum().

A few other changes I made:

  • Switched to using a publicly accessible Feature Collection LSIB so that others can replicate.
  • Used the "50% confidence" test to mask out pixels of low confidence.
  • Updated the date range to include the entire year.

Here is the resulting script:

// Load Vietnam shapefile 
var lsib = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017");
var vn_shape = lsib.filterMetadata('country_na', 'equals', 'Vietnam');
print (vn_shape);

// Load fire counts image
var fire = ee.ImageCollection('FIRMS')
             .filterBounds(vn_shape)
             .filterDate('2010', '2011');

// Filter fire with more than 50% confidence and add a new band representing areas where confidence of fire > 50%
var filterConfidence = function(image) {
  var line_number = image.select('line_number');
  var confidence = image.select('confidence');
  var conf_50 = confidence.gt(50).rename('confidence_50');
  var count_band = line_number.updateMask(conf_50).rename('count');
  return image.addBands(count_band);
};
var fire_conf = fire.map(filterConfidence);
print('fire_conf', fire_conf);

// Count for individual image.
var countIndividualImg = function(image) {
  var countObject = image.reduceRegion({
    reducer: ee.Reducer.countDistinct(),
    scale: 1000,
    geometry: vn_shape
  });
  return image.set(countObject);
};
var fire_ind_count = fire_conf.map(countIndividualImg);
print('fire_ind_count', fire_ind_count);

print('Total fire count', fire_ind_count.aggregate_sum('count'));
Related Question