[GIS] Google Earth Engine Python API clipped export bad behavior

exportgoogle earthgoogle-earth-enginepython

I am trying to export a multi-polygon clipped image with the python API. The problem is that the code in python does not clipped the image correctly (I have just one polygon saved instead of two like the example given below). Probably I am doing something wrong but I do not know where my mistake is. I applied the same code on the Google earth Engine editor with a similar Javascript code and everything is correctly done.

Javascript code:

 var geometry = ee.Geometry.Polygon([[[37.24154695,  8.01665693],
 [37.26876315,  8.01665693],
 [37.26876315,  8.04378808],
 [37.24154695 , 8.04378808],
 [37.24154695,  8.01665693]],
 [[37.34154695,  8.11665693],
 [37.36876315,  8.11665693],
 [37.36876315,  8.14378808],
 [37.34154695 , 8.14378808],
 [37.34154695,  8.11665693]]]);

/**
 * Function to mask clouds based on the pixel_qa band of Landsat 8 SR data.
 * @param {ee.Image} image input Landsat 8 SR image
 * @return {ee.Image} cloudmasked Landsat 8 image
 */
function maskL8sr(image) {
  // Bits 3 and 5 are cloud shadow and cloud, respectively.
  var cloudShadowBitMask = (1 << 3);
  var cloudsBitMask = (1 << 5);
  // Get the pixel QA band.
  var qa = image.select('pixel_qa');
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
                 .and(qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image.updateMask(mask);
}

var dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
                  .filterBounds(geometry)
                  .filterDate('2016-01-01', '2016-12-31')
                  .map(maskL8sr);
var image = dataset.median()
Export.image.toDrive({
  image: image.clip(geometry),
  folder: "test",
  description: 'imageToCOGeoTiffExample2',
  scale: 30,
  region: geometry,
  fileFormat: 'geotiff',
  skipEmptyTiles: true
});

Python code:

t = [[[37.24154695, 8.01665693],
          [37.26876315, 8.01665693],
          [37.26876315, 8.04378808],
          [37.24154695, 8.04378808],
          [37.24154695, 8.01665693]],
         [[37.34154695, 8.11665693],
          [37.36876315, 8.11665693],
          [37.36876315, 8.14378808],
          [37.34154695, 8.14378808],
          [37.34154695, 8.11665693]]]

    region = ee.Geometry.Polygon(t)
    dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') \
        .filterBounds(region) \
        .filterDate('2016-01-01', '2016-12-31') \
        .map(_mask_l8_sr)
    dataset = dataset.reduce('median')
    task = ee.batch.Export.image.toDrive(image=dataset.clip(region),
                                         description="test",
                                         folder="testing",
                                         region=region['coordinates'],
                                         scale=30,
                                         fileFormat='GeoTIFF',
                                         skipEmptyTiles=True)
    task.start()

Best Answer

The difference is that you're passing a geometry in the javascript and a list of coordinates in the python, and those are interpreted differently because there's no enclosing object to identify how to interpret the coordinates, and it's more complex than a single list of coordinates.

Getting a region for the export in python is always a little tricky; I suggest this:

region=region.bounds().getInfo()['coordinates'],
Related Question