下载sentinelhub(python)提供的列表中的所有数据

问题描述 投票:0回答:1

我正在尝试下载一次查询的所有数据。我的代码如下:

from sentinelhub import AwsProductRequest, AwsTileRequest, AwsTile, BBox, CRS
import sentinelhub

betsiboka_coords_wgs84 = [38.788605,35.781057,39.314575,36.180008]
bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
date= '2016-09-05',('2018-03-08')
for i in data:
    print(i['properties']['title'])

此代码返回:S2A_OPER_MSI_L1C_TL_EPAE_20180307T093658_A014132_T37SBD_N02.06 S2A_OPER_MSI_L1C_TL_EPAE_20180307T093658_A014132_T36SYH_N02.06 S2A_OPER_MSI_L1C_TL_EPAE_20180307T093658_A014132_T36SYJ_N02.06 S2A_OPER_MSI_L1C_TL_EPAE_20180307T093658_A014132_T37SBC_N02.06 S2B_OPER_MSI_L1C_TL_MPS__20180305T103142_A005195_T36SYH_N02.06

在它返回tile_id的列表之后,我使用代码来提取tile_name和单个tile_id的日期(例如:S2B_OPER_MSI_L1C_TL_MPS__20170906T122033_A002621_T36STA_N02.05):

tile_id ='S2B_OPER_MSI_L1C_TL_MPS__20170906T122033_A002621_T36STA_N02.05'
tile_name, time, aws_index = AwsTile.tile_id_to_tile(tile_id)
tile_name, time, aws_index

返回:('36STA','2017-9-6',0)

最后一段代码告诉我们应该下载哪些band,它们应该被下载的目录等等:

bands = ['B02','B03','B04']
metafiles = [ 'preview']

data_folder = './ChangeDetection'

request = AwsTileRequest(tile=tile_name, time=time, aws_index=aws_index,
                         bands=bands, metafiles=metafiles, data_folder=data_folder)
request.save_data() 

因此,为了下载多个图像,我每次都要复制并粘贴Tile_id。我的问题是:我如何编写代码,以便它获取一个Tile_id下载,然后转到第二个Tile_id并下载它。并继续下载,直到列表完成。问候,本

python
1个回答
0
投票

要专门回答您的问题,您需要使用Sentinelhub Web要素服务获取符合您参数的所有切片列表。然后,您可以通过迭代器传递该列表,提取它们实现它的tile / product请求的输入信息。

这是我的代码,它不是很漂亮,但它可以解决问题。请注意,它当前设置为以.SAFE模式下载,如果您未在AWS环境中工作,则需要传入AWS安全凭证。

from sentinelhub import WebFeatureService, BBox, CRS, AwsProductRequest, AwsTile, DataSource, AwsTileRequest

# config for sentinelhub WFS requests
INSTANCE_ID = 'YOUR INSTANCE HERE' 

#boundslist format eg = [101.429, 0.4629, 101.64, 0.6737]
#searchdates format eg =('2018-08-01T00:00:00', '2018-08-31T23:59:59')
#cloudmax format eg = 0.25

def batchdownloader(boundslist,searchdates,cloudmax,downloadfolder,):
    search_bbox = BBox(bbox = boundslist, crs=CRS.WGS84)
    search_time_interval = searchdates
    wfs_iterator = WebFeatureService(search_bbox, search_time_interval,
                                 data_source=DataSource.SENTINEL2_L1C,
                                 maxcc=cloudmax, instance_id=INSTANCE_ID)


    outinfo=wfs_iterator.get_tiles()
    print('The tool will download',len(outinfo), 'images.')
    for i in outinfo:
        tilename = i[0]
        tiledate = i[1]
        tileindex = i[2]
        productid=(AwsTile(tile_name=tilename, time=tiledate, aws_index=tileindex, data_source=DataSource.SENTINEL2_L1C).get_product_id())
        print (productid)

        #Uncomment the below code if you want to download the whole product
        #product_request = AwsProductRequest(product_id=productid, data_folder=downloadfolder, safe_format=True)
        #product_request.save_data()
        #print (productid, '.SAFE folder for this image has been downloaded to', downloadfolder, )

        #uncomment the below code if you want to download just the tiles
        #tile_request = AwsTileRequest(tile=tilename, time=tiledate, aws_index=tileindex, data_folder=downloadfolder, safe_format=True)
        #tile_request.save_data()
        #print (productid, 'Tiles for this image have been downloaded to', downloadfolder)
        i =+1
    print('Completed downloads.')


batchdownloader([110.3055, -1.0574, 110.4125, -0.87],('2018-06-15T00:00:00', '2018-06-30T23:59:59'), 0.4, r'C:\YOURFOLDER') 
© www.soinside.com 2019 - 2024. All rights reserved.