1

When trying to run this code I get an error:

Expected Type: Image(unknown bands) actual type:ImageCollections

I would also like to maintain the ID column to be the ID column in my shapefile(labelled "Table") geometry: AOI table: shapefile with many polygons, want to extract by each polygon

var s2 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED');

//Sentinel -1 seasonal images

var s2April = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-04-01', '2020-4-25'))
  .filter(ee.Filter.bounds(geometry));

var s2May = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-05-01', '2020-05-25'))
  .filter(ee.Filter.bounds(geometry));

var s2June = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-06-01', '2020-06-25'))
  .filter(ee.Filter.bounds(geometry));

var s2July =s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-07-01', '2020-07-25'))
  .filter(ee.Filter.bounds(geometry));

var s2August = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-08-01', '2020-08-25'))
  .filter(ee.Filter.bounds(geometry));

var s2September = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-09-01', '2020-09-25'))
  .filter(ee.Filter.bounds(geometry));

var s2October = s2.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2020-10-01', '2020-10-25'))
  .filter(ee.Filter.bounds(geometry));
  
var s2November = s2
  .filter(ee.Filter.date('2020-11-01', '2020-11-25'))
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.bounds(geometry));

function addNDVI(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('ndvi');
  return image.addBands(ndvi);
}
//NDVI: image.normalizedDifference(['B8', 'B4']).rename('ndvi')
//GRVI: image.normalizedDifference(['B3', 'B4']).rename('grvi')
//GBVI: image.normalizedDifference(['B2', 'B3']).rename('gbvi')
//NDMI: image.normalizedDifference(['B8', 'B11']).rename('ndmi')
// Map the function over the collection

var april = s2April.map(addNDVI);
var may = s2May.map(addNDVI);
var june = s2June.map(addNDVI);
var july = s2July.map(addNDVI);
var august = s2August.map(addNDVI);
var september = s2September.map(addNDVI);
var october = s2October.map(addNDVI);
var november = s2November.map(addNDVI);


Map.centerObject(geometry);


//add optical map layers
Map.addLayer(geometry);
Map.addLayer(geometry,{color:"blue"});

//create collection of S1 values
var Change = ee.Image.cat(
        april,
        may,
        june,
        july,
        august,
        september,
        october,
        november);
//export to table
//export to table
var s2Means = Change.reduceRegions({
  collection: table,
  reducer: ee.Reducer.mean(),
  scale: 10
});
//ee.Reducer.mean().setOutputs(['export'])
print(s2Means);

Export.table.toDrive({
  collection:s2Means,
  description:'Region_MonthlyMeanNDVI',
  fileFormat:'csv',
})

I want to do this for NDVI and other indices, but also bands: 9 and 10. All as separate tables

1 Answer 1

1

The problem is with concatinating images. You need to select images while you are providing image collections for the 'Change' variable. Now you have to select the monthly mean NDVI for all the images of a certain month. Then you can add them as a band to create a new image. You can select a particular band e.g. 'ndvi' and rename it with the month. Otherwise, it can be hard to identify the period of ndvi. Similarly you can do for other indices and bands by modifying the select() parameter and renaming it.

var Change_NDVI = ee.Image.cat(
        [april.select('ndvi').mean().rename('ndvi_April'),
        may.select('ndvi').mean().rename('ndvi_may'),
        june.select('ndvi').mean().rename('ndvi_june'),
        july.select('ndvi').mean().rename('ndvi_july'),
        august.select('ndvi').mean().rename('ndvi_august'),
        september.select('ndvi').mean().rename('ndvi_september'),
        october.select('ndvi').mean().rename('ndvi_october'),
        november.select('ndvi').mean().rename('ndvi_november')]);
//export to table
var s2Means = Change_NDVI.reduceRegions({
  collection: table,
  reducer: ee.Reducer.mean(),
  scale: 10
  })

Export.table.toDrive({
  collection:s2Means,
  description:'Region_MonthlyMeanNDVI',
  fileFormat:'csv',
})
1
  • beautiful. thank you for your help. This worked very well
    – Sara
    Commented Aug 11, 2023 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.