Python批量下载Landsat-8数据(I)

Python批量下载Landsat-8数据

参考国外的一篇文章:Automated Bulk Downloads of Landsat-8 Data Products in Python,略作修改,从Amazon S3批量下载Landsat-8数据。

1. 获取研究区域内Landsat-8的条带号

LANDSAT_PATH = './data/external/Landsat8' #文件存放路径
wrs_path = './data/external/Landsat8/wrs2/WRS2_descending.shp' #WRS2文件
bounds_path = './data/processed/research_area.shp' #研究区shp文件
bounds = gpd.GeoDataFrame.from_file(bounds_path)
wrs = gpd.GeoDataFrame.from_file(wrs_path)
wrs_intersection = wrs[wrs.intersects(bounds.geometry[0])]
paths,rows = wrs_intersection['PATH'].values,wrs_intersection['ROW'].values
for i, (path, row) in enumerate(zip(paths, rows)):
    print('Image', i+1, ' - path:', path, 'row:', row)

2. 根据条带号获取文件信息

亚马逊提供的检索目录获取所需要的文件信息。筛选条件是云量小于某一值,_T2 & _RT文件需要定标和预处理,同样排除。

def get_bulk_list(path,row):
    #Checking Available Images on Amazon S3 & Google Storage
    s3_scenes = pd.read_csv('./data/external/scene_list')
    # Empty list to add the images
    bulk_list = []
    print('Path:',path, 'Row:', row)
    # Filter the Landsat Amazon S3 table for images matching path, row, cloudcover and processing state.
    scenes = s3_scenes[(s3_scenes.path == path) & (s3_scenes.row == row) & 
                       (s3_scenes.cloudCover <= CLOUD_MAX) & 
                       (~s3_scenes.productId.str.contains('_T2')) &
                       (~s3_scenes.productId.str.contains('_RT'))]
    print(' Found {} images\n'.format(len(scenes)))
    return scenes

3. 保存下载链接

先获取下载链接,并以json格式保存到本地文件,之后再下载。

def get_urls(row):
    import requests
    from bs4 import BeautifulSoup
    url_list = []    
    print('\n', 'EntityId:', row.productId, '\n')
    print(' Checking content: ', '\n')
    response = requests.get(row.download_url)
    # If the response status code is fine (200)
    if response.status_code == 200:
        # Import the html to beautiful soup
        html = BeautifulSoup(response.content, 'html.parser')
        # Second loop: for each band of this image that we find using the html <li> tag
        for li in html.find_all('li'):
            # Get the href tag
            file = li.find_next('a').get('href')
            url = row.download_url.replace('index.html', file)
            url_list.append(url)
    return url_list
    

    
if __name__=='__main__':
    bulk_frame = get_bulk_list(118,39)
    #print(bulk_frame)
    down_url={}
    for i, row in bulk_frame.iterrows():
        EntityID = row.productId       
        # Print some the product ID
        print('\n', 'EntityId:', row.productId, '\n')
        down_url[EntityID] = get_urls(row)
    with open('11839.txt','w') as f:
        f.write(str(down_url))

4. 下载

从json中读取下载链接,下载。每一景影像保存到单独的文件夹下。

import wget,os

file_path = './11839/11839.txt' #下载链接
base_path = os.path.dirname(os.path.abspath(file_path))
with open(file_path,'r') as f:
    file = f.read()
file_list = eval(file) #str转dict
for key in file_list.keys():
    entity_dir = os.path.join(base_path, key)  
    os.makedirs(entity_dir, exist_ok=True) #生成目录
    os.chdir(entity_dir)  #转到目录下
    #print(os.getcwd())
    value = file_list[key] #文件下载链接
    for url in value:
        name = url.split('/')[-1]   #文件名      
        if os.path.exists(name): #检查是否存在
            print('\nDownloaded: ',name)
            continue
        print('\nDownloading: ',name) #下载
        try:  #若下载失败,则跳过(应该加一个日志文件)
            wget.download(url)
        except:
            continue
  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要使用Python下载Landsat数据,我建议使用landsat-util工具。landsat-util是一个用于管理和下载Landsat数据Python命令行工具。下面是使用landsat-util工具的步骤: 1. 首先,你需要在计算机上安装Pythonlandsat-util包。你可以在Python官方网站上下载并安装Python,然后使用pip命令安装landsat-util包。 2. 在命令行中输入`landsat configure`命令,按照提示输入USGS EarthExplorer网站的用户名和密码,以获取访问权限。 3. 使用`landsat search`命令来搜索Landsat数据。你可以根据地理区域、日期范围、云量等条件来筛选数据。 4. 选择符合要求的数据,并使用`landsat download [sceneID]`命令下载数据。sceneID可以在搜索结果中找到。 5. 下载数据将储存在你所指定的目录中。你可以使用`landsat process`命令对数据进行预处理和校正,例如大气校正、反射率计算等。 使用landsat-util工具,你可以轻松地通过命令行下载和处理Landsat数据,而无需手动搜索和下载。这使得批量下载Landsat数据变得非常方便,并且可以自动化整个流程。 请注意,使用landsat-util下载Landsat数据需要网络连接,并且你需要在USGS EarthExplorer网站上创建一个帐户并允许下载权限。同时,你也应该在搜索和下载数据时遵守相关的数据使用和许可条款。 ### 回答2: Python有多种库和模块可用于下载Landsat卫星图像数据,其中最常用的是提供遥感数据下载功能的地理空间数据处理库,如gdal、geopandas、rasterio等。这些库能够通过代码方式访问和下载Landsat数据。 其中,使用rasterio库可以实现Landsat图像数据下载。首先,需要在代码中导入rasterio库,并设置Landsat图像数据下载链接。例如,获取Landsat 8 OLI/TIRS数据下载链接是通过USGS EarthExplorer网站进行的,可以通过调用rasterio.rio.download函数来进行下载。以下是一个示例代码: ```python import rasterio url = "https://earthexplorer.usgs.gov/download/12864/LC08_L1TP_024032_20200718_20200730_01_T1.tar.gz" rasterio.rio.download(url, "./LC08_L1TP_024032_20200718_20200730_01_T1.tar.gz") ``` 上述代码中,`url`变量设定了数据下载链接,`rasterio.rio.download`函数将数据下载到当前工作目录下,保存为`LC08_L1TP_024032_20200718_20200730_01_T1.tar.gz`文件。 另外,对于需要大规模下载Landsat数据的情况,还可以使用Python的多线程或并行处理库来提高下载速度。常用的有concurrent.futures、multiprocessing等。 总的来说,Python提供了多种库和模块用于Landsat数据下载,通过调用相应函数和设置下载链接,可以方便地实现Landsat图像数据的获取。为了保证数据下载的可靠性和完整性,建议在下载前仔细阅读相应文档并设置好文件保存路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值