1

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:

Currently Names

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',
});

1 Answer 1

1

The ImageCollection.toBands function will create an image stack from a collection of images where the new band names are the concatenation of image ID (ee.Image.id) and original band names, separated by an underscore. After you apply ee.Image.sample the resulting table will have columns names like 201801_aet, 201801_def, etc (for TerraClimate data).

var imgCol = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE')
    .filter(ee.Filter.date('2018-01-01', '2018-12-31'));

var imgColStack = imgCol.toBands();

var pointsFc = ee.FeatureCollection([
    ee.Feature(ee.Geometry.Point([-121.25, 39.55]), {'id': 0}),
    ee.Feature(ee.Geometry.Point([-120.29, 38.53]), {'id': 1})
]);

var imgSamp = imgColStack.sampleRegions({
    collection: pointsFc,
    properties: ['id'],
    scale: imgCol.first().projection().nominalScale(),
    geometries: true
});

print(imgSamp.first());
1
  • Thank you, this solution is so elegant, perfect. Aug 29, 2022 at 18:13

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.