I am trying to download a point sample of climate information from the TerraClimate collection in GEE. I want the column names to be the concatenation of image ID and band name (a wide table).
I tried with this, but this is for one band, and I want the 14 bands of TerraClimate.
Error: Collection.iterate: Error in map(ID=200005): Image.rename: The number of names (1) must match the number of bands (14). (Error code: 3)
I get this:
I want something like this:
aet_201801
This is the script
var points = ee.FeatureCollection("projects/ee-jemora3/assets/points_all");
var drought_M = ee.ImageCollection("IDAHO_EPSCOR/TERRACLIMATE")//.select("pdsi")
.filter(ee.Filter.date('2018-01-01', '2018-12-31'));
//This function creates a stack of images from the image collection
function newCollectionToImage(collection){
var stack = ee.Image(collection.iterate(function(img, prev) {
return ee.Image(prev).addBands(img);
}, ee.Image(1)));
stack = stack.select(ee.List.sequence(1, stack.bandNames().size().subtract(1)));
return stack;
}
//This function renames the image collection bands
//var drought_M_rename = drought_M.map(function(img){
//var y = ee.String(ee.Date(img.get('system:time_start')).get("year"));
//return img.set("year",y).rename(y);
//});
//Apply the function to create a stack of images
var droughtImageStack = newCollectionToImage(drought_M);
var table_masked_index = droughtImageStack
.sampleRegions({
// Get the sample from the points FeatureCollection.
collection: points,
// Properties from the points collection to pass on to the sampled info
properties: ['id'],
// Set the scale to get Sentinel pixels in the FeatureCollection.
scale: 30,
tileScale: 8,
geometries: true
});
// Export table as csv
Export.table.toDrive({
collection: table_masked_index,
description: 'TerraClimate',
fileFormat: 'CSV',
});