I was experimenting with GEE for the first time (no javascript experience) using ChatGPT as my teacher to achieve the following: I have a shapefile with multiple polygon features. I want to use the entire landsat 5 and 7 satellite image archive to create a time series of mean NDVI values (multispectral index using the red and near-infrared bands) for each of my polygons in the shapefile. In the end I want one CSV file for each polygon in the shapefile that has three columns (Name of the landsat scene, date of the scene, mean NDVI value) and export those to my google drive. I keep getting an error "User-defined methods must return a value".
From my understanding, this error comes up when there is a function that doesn't return anything. This is my code:
// Load the shapefile
var shp = ee.FeatureCollection("shapefile");
// Create a map centered on the shapefile
Map.centerObject(shp, 12);
// Add the shapefile to the map as a layer
Map.addLayer(shp, {}, 'My Shapefile');
// Function to calculate NDVI
var addNDVI = function(image) {
var ndvi = image.normalizedDifference(['B4', 'B3']).rename('ndvi');
return image.addBands(ndvi);
};
// Find Landsat 5 and Landsat 7 images that cover the shapefile and have cloud cover less than 10%
var l5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')
.filterBounds(shp)
.filterDate('1984-01-01', '2012-05-05')
.filter(ee.Filter.lt('CLOUD_COVER', 10))
.map(addNDVI);
var l7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
.filterBounds(shp)
.filterDate('1999-01-01', '2021-05-05')
.filter(ee.Filter.lt('CLOUD_COVER', 10))
.map(addNDVI);
// Merge the collections
var collection = ee.ImageCollection(l5.merge(l7));
// Function to calculate mean NDVI for each image and each polygon
var calculateMeanNDVI = function(image) {
var results = ee.FeatureCollection(
shp.map(function(feature) {
var mean = ee.Image(image.select('ndvi')).reduceRegion({
reducer: ee.Reducer.mean(),
geometry: feature.geometry(),
scale: 30,
maxPixels: 1e9
});
return ee.Feature(feature.geometry(), {
'mean_ndvi': mean.get('ndvi'),
'image_id': image.id(),
'date': image.date().format('YYYY-MM-dd')
});
})
);
return results;
};
// Map over the image collection to calculate mean NDVI for each image
var featureList = collection.map(calculateMeanNDVI).toList(collection.size());
// Convert the featureList to a list of dictionaries
var dictList = ee.List(featureList.map(function(feature) {
feature = ee.Feature(feature);
return ee.Dictionary({
'image_id': feature.get('image_id'),
'date': feature.get('date'),
'mean_ndvi': feature.get('mean_ndvi')
});
}));
// Export results to CSV files in Google Drive
var exportList = dictList.map(function(dict) {
dict = ee.Dictionary(dict);
var name = ee.String('NDVI_').cat(ee.String(dict.get('image_id')));
return Export.table.toDrive({ // This is where I get the error: User-defined methods must return a value
collection: ee.FeatureCollection([ee.Feature(null, dict)]),
description: name,
fileFormat: 'CSV',
selectors: ['image_id', 'date', 'mean_ndvi']
});
});
// Print the export list to the console
print('Export list:', exportList);```