Earth Engine Band Value Property – How to Set a Property as a Band Value in Google Earth Engine

bandfunctiongoogle-earth-engine

I have an image collection of MODIS monthly burn (MODIS/006/MCD64A1), in which BurnDate band is selected. I then have a function that calculates total burned area for each image (monthly). However, that area is stored as a property. To chart correctly, it needs to be a band.

Could I change my function to convert a property into a band value across my image collection?

Code below, the chart is rough code and just a test to see about plotting.

var dataset = ee.ImageCollection('MODIS/006/MCD64A1')
                  .filter(ee.Filter.date('2017-01-01', '2021-12-31'));
                  
var burnedArea = dataset.select('BurnDate');
//print(burnedArea)

var addArea = function(img) {
  var area = ee.Image.pixelArea()
            .divide(1e6)
            .clip(geometry)
            .reduceRegion({
              reducer: ee.Reducer.sum(),
              geometry: geometry,
              scale: 500,
              bestEffort: true
            });
  return img.set(area);
}

// function addArea(img) {
//   var area = ee.Image.pixelArea()
//             .divide(1e6)
//             .clip(geometry)
//             .reduceRegion({
//               reducer: ee.Reducer.sum(),
//               geometry: geometry,
//               scale: 500,
//               bestEffort: true
//             });
//   return img.set(area);
// }

var area_collection = burnedArea.map(addArea);
print(area_collection)

var chart =
    ui.Chart.feature
        .byFeature({
          features: area_collection.select('area'),
          xProperty: 'system:time_start:',
        })
        .setSeriesNames([
          'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
          'Nov', 'Dec'
        ])
        .setChartType('LineChart')
        .setOptions({
          title: 'Burning',
          hAxis:
              {title: 'Date', titleTextStyle: {italic: false, bold: true}},
          vAxis: {
            title: 'Total area burned',
            titleTextStyle: {italic: false, bold: true}
          },
          colors: [
            '604791', '1d6b99', '39a8a7', '0f8755', '76b349', 'f0af07',
            'e37d05', 'cf513e', '96356f', '724173', '9c4f97', '696969'
          ]
        });
print(chart);

Best Answer

There's a few errors in your code. First, you are not actually calculating the burned area, but the area of geometry over and over again. You need to updateMask() on your ee.Image.pixelArea() in order to limit your area calculation to areas that actually have burned data. As for adding the area as an image, it's pretty simple once you have the format right. The output of reduceRegion() is an object with a number within it, so you need to retrieve the area from said object using getNumber(). Making that number into an image is done by using ee.Image(). Lastly all you need to do is take the image you are mapping on and addBands(). Below is the full code.

var dataset = ee.ImageCollection('MODIS/006/MCD64A1')
  .filter(ee.Filter.date('2017-01-01', '2021-12-31'));
var burnedArea = dataset.select('BurnDate');

var addArea = function(img) {
  var area = ee.Image.pixelArea()
    .updateMask(img)
    .divide(1e6)
    .clip(geometry)
    .reduceRegion({
      reducer: ee.Reducer.sum(),
      geometry: geometry,
      scale: 500,
      bestEffort: true
    }).getNumber('area');
  return img.addBands(ee.Image(area).rename('area'));
}
var area_collection = burnedArea.map(addArea);