【问题标题】:Download Sentinel file from S3 using Python boto3使用 Python boto3 从 S3 下载 Sentinel 文件
【发布时间】:2020-08-09 07:02:12
【问题描述】:

我正在使用以下代码从 S3 下载完整大小的 Sentinel 文件

import boto3

s3_client = boto3.Session().client('s3')
response = s3_client.get_object(Bucket='sentinel-s2-l1c',
                        Key='tiles/7/W/FR/2018/3/31/0/B8A.jp2', 
                        RequestPayer='requester')
response_content = response['Body'].read()

with open('./B8A.jp2', 'wb') as file:
     file.write(response_content)

但我不想下载全尺寸图片。有没有办法下载基于 latMax、longMin、LatMin 和 LatMax 的图像?我正在使用以下命令,但自从在 S3 上将数据设为请求者 - 付款者后,它无法正常工作

gdal_translate --config CPL_TMPDIR temp -projwin_srs "EPSG:4326" -projwin 23.55 80.32 23.22 80.44 /vsicurl/http://sentinel-s2-l1c.s3-website.eu-central-1.amazonaws.com/tiles/43/R/EQ/2020/7/26/0/B02.jp2 /TestScript/B02.jp2

有没有什么方法可以使用 Python boto 实现这一点?

【问题讨论】:

    标签: python amazon-web-services amazon-s3 gis gdal


    【解决方案1】:

    您可以使用rasterio 访问图片的子窗口:

    (我假设 AWS 凭证已设置为与 boto3 一起使用,并且您拥有必要的权限)

    import boto3
    from matplotlib.pyplot import imshow
    import rasterio as rio
    from rasterio.session import AWSSession
    from rasterio.windows import Window
    
    # create AWS session object
    aws_session = AWSSession(boto3.Session(), requester_pays=True)
    
    with rio.Env(aws_session):
        with rio.open("s3://sentinel-s2-l1c/tiles/7/W/FR/2018/3/31/0/B8A.jp2") as src:
            profile = src.profile
            win = Window(0, 0, 1024, 1024)
            arr = src.read(1, window=win)
    
    imshow(arr)
    

    print(arr.shape)
    

    (1024, 1024)

    解释:

    如果为boto3 正确配置了AWS 凭证,您可以基于boto3.Session() 创建一个AWSSession 对象。这将为 S3 访问设置必要的凭据。添加标志requester_pays=True,以便您可以从请求者付款存储桶中读取。

    AWSSession 对象可以传递到rasterio.Env 上下文中,因此rasterio(更重要的是底层gdal 函数)可以访问凭据。

    使用rasterio.windows.Window,我将任意子窗口(0、0、1024、1024)读入内存,但您也可以使用坐标定义窗口,如documentation 中所述。

    您可以从那里处理阵列或将其保存到磁盘。

    【讨论】:

    • 对不起,我已经阅读了整个文档,但仍然无法根据纬度和经度找到 window()。
    • 或者有什么办法可以根据经纬度找出col_off row_off
    • 您需要将坐标重新投影到 S2 投影中,以便您可以使用 rasterio.windows.from_bounds - 有很多关于点重投影的文档:例如thisthisthat
    • 在这种情况下,我必须下载完整大小的 jp2 文件,然后我必须重新投影它,然后使用正确的纬度和经度
    • 如果您在投影坐标中没有您感兴趣的区域,而是在地理坐标中,您可以尝试仅重新投影这些坐标以在地图坐标中创建边界框。然后,此边界框可用于仅访问您想要的区域。无需下载和/或重新投影整个图像
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 2017-07-29
    • 2020-04-09
    • 2022-01-09
    • 2019-05-13
    • 2017-04-21
    • 2015-11-02
    • 2019-11-26
    相关资源
    最近更新 更多