This post isn't a question but a solution to a problem I have been trying to solve for a while. Hopefully somebody else will find the code useful!
I wanted to export Sentinel-2 Satellite imagery (https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2) with a cloud masking filter applied from Google Earth Engine to my Google Drive using the Python API. However, not all images fully overlapped with the geometry I was interested in and the cloud mask made parts of some images invisible. I therefore needed to create a mosaic of the images closest to the date I was interested.
The solution which eventually worked is below:
# This is the cloud masking function provided by GEE but adapted for use in Python.
def maskS2clouds(image):
qa = image.select('QA60')
# Bits 10 and 11 are clouds and cirrus, respectively.
cloudBitMask = 1 << 10
cirrusBitMask = 1 << 11
# Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudBitMask).eq(0)
mask = mask.bitwiseAnd(cirrusBitMask).eq(0)
return image.updateMask(mask).divide(10000)
# Define the geometry of the area for which you would like images.
geom = ee.Geometry.Polygon([[33.8777, -13.4055],
[33.8777, -13.3157],
[33.9701, -13.3157],
[33.9701, -13.4055]])
# Call collection of satellite images.
collection = (ee.ImageCollection("COPERNICUS/S2")
# Select the Red, Green and Blue image bands, as well as the cloud masking layer.
.select(['B4', 'B3', 'B2', 'QA60'])
# Filter for images within a given date range.
.filter(ee.Filter.date('2017-01-01', '2017-03-31'))
# Filter for images that overlap with the assigned geometry.
.filterBounds(geom)
# Filter for images that have less then 20% cloud coverage.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
# Apply cloud mask.
.map(maskS2clouds)
)
# Sort images in the collection by index (which is equivalent to sorting by date),
# with the oldest images at the front of the collection.
# Convert collection into a single image mosaic where only images at the top of the collection are visible.
image = collection.sort('system:index', opt_ascending=False).mosaic()
# Assign visualization parameters to the image.
image = image.visualize(bands=['B4', 'B3', 'B2'],
min=[0.0, 0.0, 0.0],
max=[0.3, 0.3, 0.3]
)
# Assign export parameters.
task_config = {
'region': geom.coordinates().getInfo(),
'folder': 'Example_Folder_Name',
'scale': 10,
'crs': 'EPSG:4326',
'description': 'Example_File_Name'
}
# Export Image
task = ee.batch.Export.image.toDrive(image, **task_config)
task.start()
After using the maskS2clouds
function above, the images in my imageCollection
lose 'system:time_start'
.
I changed the function to the following and seems it is working. We may need the 'system:time_start'
for mosaicing later:
def maskS2clouds(image):
qa = image.select('QA60')
# Bits 10 and 11 are clouds and cirrus, respectively.
cloudBitMask = 1 << 10
cirrusBitMask = 1 << 11
# Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudBitMask).eq(0)
mask = mask.bitwiseAnd(cirrusBitMask).eq(0)
helper = image.updateMask(mask).divide(10000)
helper = ee.Image(helper.copyProperties(image, properties=["system:time_start"]))
return helper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With