首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python将Google引擎RGB Sentinel-2图像导出到Google

使用Python将Google引擎RGB Sentinel-2图像导出到Google
EN

Stack Overflow用户
提问于 2019-09-08 10:57:38
回答 2查看 2.8K关注 0票数 6

这篇文章不是一个问题,而是一个我已经尝试解决了一段时间的问题的解决方案。希望其他人会发现代码有用!

我想导出哨兵-2卫星图像(S2)与云掩蔽过滤器应用从谷歌地球引擎到我的谷歌驱动器使用Python。然而,并不是所有的图像都与我感兴趣的几何图形完全重叠,云掩膜使得部分图像不可见。因此,我需要创建一个图像马赛克最近的日期,我感兴趣。

最终奏效的解决办法如下:

代码语言:javascript
复制
# 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()
EN

回答 2

Stack Overflow用户

发布于 2022-03-28 04:06:02

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

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

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2022-08-20 13:43:13

考虑到cirrusBitMask,也要做更多的修正。(为了考虑cirrusBitMask,我们需要使用"qa“变量而不是”掩码“):

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57841300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档