Average NDVI for each year from 2013 to 2024

PHOTO EMBED

Sat Sep 21 2024 11:27:29 GMT+0000 (Coordinated Universal Time)

Saved by @Rehbar #javascript

// Define the region of interest (could be a point or polygon)
var point = ee.Geometry.Point(90.2611485521762, 23.44690280909043);

// Define the start and end years for the time series from 2013 to 2024
var startYear = 2013;
var endYear = 2024;

// Function to filter Landsat images for a specific year and calculate average NDVI
var calculateYearlyAverageNDVI = function(year) {
  // Define the date range for the current year
  var startDate = ee.Date.fromYMD(year, 1, 1);
  var endDate = ee.Date.fromYMD(year, 12, 31);
  
  // Filter the image collection for the year and apply cloud cover filter
  var yearlyCollection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterBounds(point)
    .filterDate(startDate, endDate)
    .filter(ee.Filter.lt('CLOUD_COVER', 20)) // Cloud cover less than 20%
    .map(function(image) {
      var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
      return image.addBands(ndvi);
    });

  // Compute the average NDVI for the year
  var averageNDVI = yearlyCollection.select('NDVI').mean();

  // Extract the average NDVI value at the specified point
  var averageNDVIValue = averageNDVI.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: point,
    scale: 30
  }).get('NDVI');

  // Return the result as a feature with the year and average NDVI value
  return ee.Feature(null, {
    'year': year,
    'average_NDVI': averageNDVIValue
  });
};

// Generate a list of years from 2013 to 2024
var years = ee.List.sequence(startYear, endYear);

// Apply the function to each year and create a feature collection with the results
var yearlyNDVICollection = ee.FeatureCollection(years.map(calculateYearlyAverageNDVI));

// Print the average NDVI values for each year
print('Yearly Average NDVI:', yearlyNDVICollection);

// Chart the yearly average NDVI values
var chart = ui.Chart.feature.byFeature(yearlyNDVICollection, 'year', 'average_NDVI')
  .setOptions({
    title: 'Yearly Average NDVI at Point (2013-2024)',
    hAxis: {title: 'Year'},
    vAxis: {title: 'Average NDVI'},
    lineWidth: 2,
    pointSize: 3
  });
print(chart);

// Map Visualization
Map.centerObject(point, 10);
Map.addLayer(point, {color: 'red'}, 'Selected Point');

// Optionally visualize NDVI of the last year's average
var lastYearNDVI = yearlyNDVICollection.sort('year', false).first();
var lastYearNDVIImage = ee.Image(ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
  .filterDate(ee.Date.fromYMD(endYear, 1, 1), ee.Date.fromYMD(endYear, 12, 31))
  .select('NDVI')
  .mean());
Map.addLayer(lastYearNDVIImage, {min: 0, max: 1, palette: ['white', 'green']}, 'NDVI (2024)');

// Optionally, add a legend for NDVI
var legend = ui.Panel({
  style: {
    position: 'bottom-right',
    padding: '8px 15px'
  }
});
legend.add(ui.Label({
  value: 'NDVI Values',
  style: {
    fontWeight: 'bold',
    fontSize: '14px',
    margin: '0 0 4px 0',
    padding: '0'
  }
}));

var palette = ['white', 'green'];
var ndviRanges = ['Low NDVI', 'High NDVI'];

for (var i = 0; i < palette.length; i++) {
  var colorBox = ui.Label({
    style: {
      backgroundColor: palette[i],
      padding: '8px',
      margin: '0 0 4px 0'
    }
  });
  var description = ui.Label({
    value: ndviRanges[i],
    style: {margin: '0 0 4px 6px'}
  });
  legend.add(
    ui.Panel({
      widgets: [colorBox, description],
      layout: ui.Panel.Layout.Flow('horizontal')
    })
  );
}

// Add the legend to the map
Map.add(legend);
content_copyCOPY