[GIS] Image Export fails in Google Earth Engine because “export region contains no valid (un-masked) pixels

exportgoogle-earth-enginemosaicsentinel-2

I want to export Sentinel-2 mosaics for rectangular ROIs. For this, I intend to use a script following this example: https://code.earthengine.google.com/ce1a151ce06497b20cf1793715cb0120

However, whenever I want to run the export task in the "Tasks" manager, I receive the following error after some time: "Error: Export region contains no valid (un-masked) pixels."

This leaves two questions:

1) Why are there no images for the right half of the ROI? The ImageCollection actually contains 9 images. Is that a Sentinel-2 inherent problem?

2) How can I deal with this situation when automated exporting is the goal? I can imagine two general options:
a) Export the image anyway (e.g. with the non-covered pixels set to NaN) – but how will GEE let me do this? b) Apply a check if such "empty" pixels are contained in my mosaic image. (Unfortunately, I have no idea how to achieve this.)

Best Answer

1st: The exact code you posted (https://code.earthengine.google.com/ce1a151ce06497b20cf1793715cb0120) did export the image correctly. So the error cannot be reproduced. May be, you changed the 'ROI' to a place where the filtered collection had no images.

2nd: There are no images because you filtered by cloud percentage, and so, it found images that suited the condition only in the left part of the ROI. If you comment like:

//.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE', 5))

you'll see that there are more images and you get the whole ROI (of course you get a part of the image plenty of clouds).

3rd: if automated exporting is your goal, maybe you should migrate to Python, but if you don't mind clicking the run button over and over, this would be one approach to download images only if the filtered collection has images (don't blame me on the getInfo =):

//Define date range
var startDate = ee.Date.fromYMD(2017,7,1);
var endDate = ee.Date.fromYMD(2017,7,31);

//Load Sentinel-2 image collections
var coll_s2 = ee.ImageCollection("COPERNICUS/S2");

//Filter Sentinel-2 collection for ROI and cloud-coverage.
//Keep only images with less than 5% clouds
var coll_s2_filtered = coll_s2.filterDate(startDate, endDate)
                              .filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE', 5))
                              .sort('system:time_start',false);

// Define the FeatureCollection!
var fc = ee.FeatureCollection(LIST_OF_POINT_FEATURES)

// Iterate over the FeatureCollection to create a List
// with images where there is data avialable
var listn = ee.List(fc.iterate(function(elem, ini){
  var rectangle = ee.Feature(elem.buffer(20000).bounds());
  var roi = rectangle.geometry();
  var col = coll_s2_filtered.filterBounds(roi);
  return ee.Algorithms.If(col.size(), 
            ee.List(ini).add(coll_s2_filtered.select(['B4','B3','B2']).mosaic().clip(roi)), 
            ee.List(ini))
}, ee.List([])))

listn = ee.List(listn)

print(listn)

// As Export is a Client-side function, you have to iterate the list
// in a client-side way

for (var n = 0; n<listn.size().getInfo(); n++) {
  var i = ee.Image(listn.get(n))
  Export.image.toDrive({
    image: i,
    description: 'image_'+n.toString(),
    scale: 10,
    folder: "tests",
    region: i.geometry()
    });
}

But if you want to change the filter regarding on whether it finds or not images, that would be different.