0

I would like to get mean monthly NDVI values for multiple regions that have a 2.5km buffer. The regions are lat/long co-ordinates imported as an assest. I keep getting an error that stating NDVI pattern is not found. In the output the NDVI values are not mean monthly values. Code:

//get NDVI for multiple sites
// Map.centerObject(dungregions);
// Map.addLayer(dungregions);
var dungpointsgeo = dungregions.geometry();
// print(dungpointsgeo)

///create 2500m radius buffer around points
var dungbuffer = dungpointsgeo.buffer(2500);
// Map.addLayer(dungbuffer);
// print(dungbuffer);

// Make a feature collection from the geometry, add one to demonstrate mapping
// the multi-geometry explosion operation.
var fc = ee.FeatureCollection([
  ee.Feature(dungbuffer),
]);
// print(fc)

// Make feature collection a list and map over the elements (features)
var multiGeomExplodeList = fc.toList(fc.size()).map(function(feature) {
  // Cast the input as a feature since it is coming from a list, get its
  // geometry, then split geometries into a list.
  var geomList = ee.Feature(feature).geometry().geometries();
  // Map over the geometry list.
  var featureList = geomList.map(function(geom) {
    // Return a feature representation of the geometry.
    return ee.Feature(ee.Geometry(geom));
  });
  // Return a list of all the features making up a potential multi-geometry,
  // this is a list. 
  return featureList;
})

// The result is a list of feature lists - flatten to a single list of features.
.flatten();

// Convert the list of features to a featureCollection.
var SSplots = ee.FeatureCollection(multiGeomExplodeList);
// print(SSplots);

///Create image collection of S-2 imagery and filter by area of interest
var sentdat = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
.map(function(image){return image.clip(SSplots)})
.filterMetadata('CLOUDY_PIXEL_PERCENTAGE','less_than',10);

// Define date range for Sentinel 2 images
var start = '2022-10-01';
var end = '2022-11-01';

//Function to calculate and add an NDVI band
var addNDVI = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
};

//Add NDVI band to image collection
var SSNDVI = sentdat.map(addNDVI)
.select('NDVI');
// print(SSNDVI);

// Define function to extract monthly NDVI values.
function getMonthlyNDVI(image, location) {
var ndvi_image = ee.Image(image.select('NDVI'));
var ndvi_value = ndvi_image.reduceRegion({
reducer: ee.Reducer.mean(),
geometry: location,
scale: 10,
maxPixels: 1e9
});
return ndvi_value;
}

var monthly_ndvi = ee.List.sequence(1,6).map(function(month) {
var start = ee.Date.fromYMD(2022, month, 1);
var end = start.advance(1, 'month').advance(-1, 'day');
var image = SSNDVI.filterDate(start, end).mosaic();
var ndvi_value = getMonthlyNDVI(image, SSNDVI.geometry());
return ndvi_value.get('NDVI');
});
// print('Monthly NDVI values:', monthly_ndvi);


// //generate chart
// var timeseries= ui.Chart.image.seriesByRegion(
//   SSNDVI, SSplots, ee.Reducer.mean())
//   .setChartType('LineChart')
//   .setOptions({
//     interpolateNulls: true,
//     lineWidth: 1,
//     pointSize: 3,
//     title: 'Annual NDVI',
//     hAxis: {title: 'Date'},
//     vAxis: {title: 'NDVI'},
//     series: {0:{color: 'red'}
//     }
//   });
// print(timeseries);`

I have tried the above code and need to tweak it to get mean monthly NDVI values for each region.

depperm
  • 10,606
  • 4
  • 43
  • 67

0 Answers0