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 toupdateMask()
on youree.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 ofreduceRegion()
is an object with a number within it, so you need to retrieve the area from said object usinggetNumber()
. Making that number into an image is done by usingee.Image()
. Lastly all you need to do is take the image you are mapping on andaddBands()
. Below is the full code.