这篇文章不是一个问题,而是一个我一直试图解决的问题的解决方案。希望其他人会发现代码有用!
我想使用 Python API 将 Sentinel-2 卫星图像 ( https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2 ) 与从 Google Earth Engine 应用到我的 Google Drive 的云屏蔽过滤器导出。但是,并非所有图像都与我感兴趣的几何图形完全重叠,并且云遮罩使某些图像的某些部分不可见。因此,我需要创建最接近我感兴趣的日期的图像的马赛克。
最终奏效的解决方案如下:
# 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()