2

I calculate the number of fires for a region and export the result to Google Drive as a CSV file, but I can't add the date to the image collection to export, it gives the following error:

ImageCollection (Error) Error in map(ID=2019001): Image.addBands, argument 'srcImg': Invalid type. Expected: Image. Actual: Date.

Here is the code:

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


// Load fire counts image
var fire = ee.ImageCollection('FIRMS')
             .filterBounds(vn_shape)
             .filterDate('2019', '2020')
print(fire)

var scale = fire.first().projection().nominalScale();

// 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');
  // HERE IS THE PROBLEM!
  var date = image.date();          
  return image.addBands(count_band)
              .addBands(date);
};


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: scale,
    geometry: vn_shape,
    maxPixels: 1e9
  });
  return image.set(countObject);
};
var fire_ind_count = fire_conf.map(countIndividualImg);
print('fire_ind_count', fire_ind_count);
Export.table.toDrive({
  collection: fire_ind_count,
  description: 'FireCounts_gt50_conf',
  selectors:['count','date'],
  fileFormat: 'CSV'
});
print('Total fire count', fire_ind_count.aggregate_sum('count'));

1 Answer 1

1

One way to do it is to move the date calculation from the function filterConfidence to after the reducer function countIndividualImg, here is the code:

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


// Load fire counts image
var fire = ee.ImageCollection('FIRMS')
             .filterBounds(vn_shape)
             .filterDate('2019', '2020')
print(fire)

var scale = fire.first().projection().nominalScale();

// 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');
  // 
  // I don't calculate the date here !!!
  // 
  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: scale,
    geometry: vn_shape,
    maxPixels: 1e9
  });
  // 
  // Add the date here
  // 
  return image.setMulti(countObject).set('date', image.date());
};

var fire_ind_count = fire_conf.map(countIndividualImg);
print('fire_ind_count', fire_ind_count);
Export.table.toDrive({
  collection: fire_ind_count,
  description: 'FireCounts_gt50_conf',
  selectors:['count','date'],
  fileFormat: 'CSV'
});
print('Total fire count', fire_ind_count.aggregate_sum('count'));

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.