繁体   English   中英

使用 Python API 将 Google Earth Engine RGB Sentinel-2 图像导出到 Google Drive

[英]Export Google Earth Engine RGB Sentinel-2 Imagery to Google Drive using Python API

这篇文章不是一个问题,而是一个我一直试图解决的问题的解决方案。 希望其他人会发现代码有用!

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. 但是,并非所有图像都与我感兴趣的几何图形完全重叠,并且云遮罩使某些图像的某些部分不可见。 因此,我需要创建最接近我感兴趣的日期的图像的马赛克。

最终奏效的解决方案如下:

# 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()

使用上面的maskS2clouds function 后,我的imageCollection中的图像丢失了'system:time_start'

我将 function 更改为以下内容,似乎它正在工作。 稍后我们可能需要'system:time_start'进行镶嵌:

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

还要考虑 cirrusBitMask 进行更多修正。 (为了考虑 cirrusBitMask,我们需要使用“qa”变量而不是“mask”):

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.
    mask1 = qa.bitwiseAnd(cloudBitMask).eq(0)
    mask2 = qa.bitwiseAnd(cirrusBitMask).eq(0)
    
    helper = image.updateMask(mask1).updateMask(mask2).divide(10000)
    helper = ee.Image(helper.copyProperties(image, properties=["system:time_start"]))

    return helper

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM