1

I'm a beginner at Google Earth Engine. I've seen on previous topics that resampling can be made on single images, like in this topic on downscaling raster images. I have an ImageCollection of Sentinel-2 data and would like to resample at least the bands with 20 m spatial resolution to a 10 m. This is the part of the code working on data collection.

var area = ee.Geometry.Polygon([[8.647458, 44.659079],[8.588429, 44.658882],[8.587969, 44.723788],[8.647064, 44.723985]]);

/**FUNCTION to MASK CLOUDS using the Sentinel-2 QA band
@param {ee.Image} image Sentinel-2 image
@return {ee.Image} cloud masked Sentinel-2 image**/
function maskS2clouds(image) { 
    var qa = image.select('QA60');
    //Bits 10 and 11 are clouds and cirrus, respectively
    var cloudBitMask = 1 << 10;
    var cirrusBitMask = 1 << 11; 
    //Both flags should be set to zero, indicating clear conditions
    var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(qa.bitwiseAnd(cirrusBitMask).eq(0));
    //Return masked and scaled data, without the QA bands
    return image.updateMask(mask).divide(10000).select("B.*").copyProperties(image, ["system:time_start"]);
}

//define collection parameters 
var start = '2017-11-01';
var end = '2018-10-31';
var cld_max = 20;   //max % cloud cover allowed in image collection; for data until now used: 20
    
//collect S2 data
var s2 = ee.ImageCollection('COPERNICUS/S2_SR')
              .filterBounds(aoi)
              .filterDate(start,end)
              // Pre-filter to get less cloudy granules
              .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',cld_max))
              .map(maskS2clouds);

But, when it comes to the resampling procedure, doesn't seem to work

var b8 = s2.select('B8'); // 10-m resolution
var b8a = s2.select('B8A'); // 20-m resolution

// Get information about the S2 projection at different resolutions
var s2_10m = ee.Image(s2.first().select('B8')); 
var proj = s2_10m.projection(); 
print('S2 projection (10m-res):', proj);

var s2_20m = ee.Image(s2.first().select('B8A')); 
var proj = s2_20m.projection(); 
print('S2 projection (20m-res):', proj);

var transform = [10, proj.transform[1], proj.transform[2], proj.transform[3], -10, proj.transform[5]]; 
print('new CRS transform:', transform);

// Display resampled image collection
var b8a_res = b8a.first().resample('bilinear').reproject({crs: proj.crs, crsTransform: transform});
Map.addLayer(b8a_res, {'min': 0, 'max': 0.5}, 'nNIR_resampled');

And reports the

nNIR_resampled: Layer error: Can't encode object: function standin(_0) { return newF.apply(this, arguments); }

Is there a better way to resample the image collection 'B8A' (narrow NIR) to a 10-m spatial resolution (just like NIR and RGB bands)?

1 Answer 1

2

I had a similar problem and I solved it with a function to be applied to the image collection. I adapted it to your code, I hope it works for you too.

var area = ee.Geometry.Polygon([[8.647458, 44.659079],[8.588429, 44.658882],[8.587969, 44.723788],[8.647064, 44.723985]]);

/**FUNCTION to MASK CLOUDS using the Sentinel-2 QA band
@param {ee.Image} image Sentinel-2 image
@return {ee.Image} cloud masked Sentinel-2 image**/
function maskS2clouds(image) { 
    var qa = image.select('QA60');
    //Bits 10 and 11 are clouds and cirrus, respectively
    var cloudBitMask = 1 << 10;
    var cirrusBitMask = 1 << 11; 
    //Both flags should be set to zero, indicating clear conditions
    var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(qa.bitwiseAnd(cirrusBitMask).eq(0));
    //Return masked and scaled data, without the QA bands
    return image.updateMask(mask).divide(10000).select("B.*").copyProperties(image, ["system:time_start"]);
}

//define collection parameters 
var start = '2017-11-01';
var end = '2018-10-31';
var cld_max = 20;   //max % cloud cover allowed in image collection; for data until now used: 20
    
var s2 = ee.ImageCollection('COPERNICUS/S2_SR')
    .filterBounds(area)
    .filterDate(start,end)
    // Pre-filter to get less cloudy granules
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',cld_max))
    .map(maskS2clouds);
    
var b8 = s2.select('B8'); // 10-m resolution
var b8a = s2.select('B8A'); // 20-m resolution
    
//resampling procedure option

var proj_10m = b8.first()//Access to the first image of the colection 
              .projection();//this is an object with all the information about the projection needed for a 10m resolution

function resample(image) { 
    var b8a = image.select('B8A');
    var b8a_res = b8a.resample('bilinear').reproject(proj_10m);
    return b8a_res;
}


var b8a_res =b8a.map(resample)

//print ("projection of the first image from collection b8a (20m) "+b8a.first().projection())
//print ("projection of the first image from collection b8a_res (10m) "+b8a_res.first().projection())



var  paletaImagenes = {"bands":"B8A","min":-1,"max":1,"palette":["blue","white","green"]}


Map.addLayer(b8a,paletaImagenes,'b8a_20m');
Map.addLayer(b8a_res,paletaImagenes,'b8a_10m');

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.