Sample an image collection

Add all images as a band to a single image before sampling

use the code below or click this link 

// Helper function to convert image collection into stack of image bands
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;
}

// import the collection
var tch = ee.ImageCollection("projects/servir-mekong/UMD/tree_height");
var region = ee.Geometry.Polygon(
[[[104.74660727416631, 13.397896152929446],
[104.74660727416631, 11.902553533323454],
[106.49892661010381, 11.902553533323454],
[106.49892661010381, 13.397896152929446]]], null, false);

// create random points
var myPoints = ee.FeatureCollection.randomPoints(region);
Map.addLayer(myPoints,{},"random points");

// rename the bands
var tch = tch.map(function(img){
var y = ee.String(ee.Date(img.get('system:time_start')).get("year"));
return img.set("year",y).rename(y);
});

// create single image from collection
var tchImg = newCollectionToImage(tch);

// sample the features
var result = tchImg.reduceRegions(myPoints,ee.Reducer.mean());

// print the output
print(result);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s