3

I have an ImageCollection containing FIRMS fire image in 2014. I want to count number of fires in the year for a predefined region. I can find a way to count number of fires for 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

2

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'));
1

Thank you very much. Your answer seems to be different from my question. It is my mistake not to state the problem clearly. However, I found a work around for that problem using your code. Here's my new code:

// 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-01-01', '2010-12-31');

// 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(40).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);

// Set properties to band
var setPropertiesToBand = function(image) {
  var countProperty = image.get('count');
  return image.addBands(image.metadata('count'));
};
var fire_ind_count_band = fire_ind_count.map(setPropertiesToBand);
print('fire_ind_count_band', fire_ind_count_band);

// Reduce the collection with a sum reducer.
var sum = fire_ind_count_band.reduce(ee.Reducer.sum());
print (sum);

// Get image containing total fire counts
Export.image.toDrive({
  image: sum.select('count_1_sum'),
  description: 'count_1_sum_2010_40',
});

Here, I count number of fires for each individual image in the collection using the countDistinct over "count" band (The reason is each different values inside that band correspond to a different fire). Then write it back to the same image as a new property. After that, make a new band out of the property. Finally, use the reduce sum function over the whole collection. I think this way is not good but I cannot find a better way to do that.

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.