I have some troubles with the sample method from Google Earth Engine. On some images, it returns an empty feature collection (0 element).

Why this is happening and how do I deal with that ?

It's problematic to train a model on an empty dataset...

Here one illustration:

// Example on two images : one working, not the other
var image_ok = ee.Image("COPERNICUS/S2/20150627T102531_20160606T223605_T31RFN");
var image_empty = ee.Image("COPERNICUS/S2/20151228T002843_20151228T085259_T54HYD");
var VisImg = {"opacity":1,"bands":["B4","B3","B2"],"min":293.6805388296989,"max":2220.5270083401124,"gamma":1};

// Sampling
var training_ok = image_ok.sample({numPixels:1000});
var training_empty = image_empty.sample({numPixels:1000});

// Display
print("Ok: ", training_ok);
print("Empty: ", training_empty);

Map.addLayer(image_empty, VisImg, "Image empty");
Map.addLayer(image_ok, VisImg, "Image ok");

1 Answer 1


The "empty" image contains QA bands that are completely masked out. You can avoid the empty sample by first selecting the bands that have valid data. For example:

var training_empty = image_empty
    // 'QA10',
    // 'QA20',
    // 'QA60',

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.