I am a new in GEE, trying to generate the MSAVI trends and image output for How can I fix the errors? My main objective is generate monthly MSAVI trend over the years.
This is my code:
// Import the shp
//Print the shp
var shp = ee.FeatureCollection (polygon)
Map.addLayer(shp, {},('Laikipia'))
// Landsat7 data
var dataset = ee.ImageCollection('LANDSAT/LE07/C01/T1_RT_TOA')
// Study period
var years = [ 2016, 2017, 2018, 2019, 2020, 2021]
// mask landsat 7 clouds
function maskL7clouds(image) {
var qa = image.select('BQA');
// Bits 4 are clouds.
var cloudBitMask = 1 << 4;
// flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
return image.updateMask(mask);
}
// calculate year mean msavi2
var datasets = dataset.filterDate('2016-01-01','2021-25-11')
var startDate = '2016-01-01';
var endDate = '2021-11-01';
// Pre-filter to get less cloudy granules.
// .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',50))
// Compute the MSAVI.
var MSAVI = function(image) {
return image.expression(
'(2 * NIR + 1 - sqrt(pow((2 * NIR + 1), 2) - 8 * (NIR - RED)) ) / 2',
{
'NIR': image.select('B4'),
'RED': image.select('B3'),
}).rename('MSAVI');
};
var MSAVIvis= {min: -1, max: 1, palette: ['blue', 'white', 'green']};
//calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();
// get a list of time strings to pass into a dictionary later on
var monList = ee.List.sequence(0, nMonths).map(function (n) {
return startDate.advance(n, 'month').format('YYYMMdd');
})
print(monList)
// map over each month
var timeSeries = ee.List.sequence(0,nMonths).map(function (n){
}
// calculate the offset from startDate
var ini = startDate.advance(n,'month');
// advance just one month
var end = ini.advance(1,'month');
var msavi2Images = ee.ImageCollection(months.map(function(month) {
var filtered = monthlyCol.filter(ee.Filter.eq('month', month));
var monthlyMean = filtered.mean();
return monthlyMean.set('month', month);
}));
print(msavi2Images)
// Plot a time series of
var plotMSAVI = ui.Chart.image.seriesByRegion({
imageCollection:MSAVIcol,
regions: shp,
reducer:ee.Reducer.mean(),
scale:30,
xProperty: 'system:time_start',
seriesProperty: 'system:index'})
.setChartType('LineChart').setOptions({
title: 'MSAVI laikipia',
hAxis: {title: 'Date'},
vAxis: {title: 'MSAVI'},
});
// Display the results.
print(plotMSAVI);
Map.addLayer(MSAVIcol.clipToCollection(shp), MSAVIvis, 'MSAVI');