前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python爬取Landsat云掩膜数据

Python爬取Landsat云掩膜数据

作者头像
GIS与遥感开发平台
发布2022-12-03 10:02:47
5010
发布2022-12-03 10:02:47
举报

Landsat云标识数据

最近想尝试一下用深度学习对云进行分割,看到USGS官网上有一套做好的云标识数据集,包含多种地物类型,一种96景数据。其中,数据源采用的是Landsat-8,且未经过大气校正的L1级别数据。 数据下载网址为:https://landsat.usgs.gov/landsat-8-cloud-cover-assessment-validation-data 数据比较多,一景一景的下载比较麻烦,我就用Python的requests库简单爬取了一下,这个数据下载也无需注册账号,代码就比较简单。

下载代码

代码语言:javascript
复制
from bs4 import BeautifulSoup
import pandas as pd
# from multiprocessing import Pool,Manager
from tqdm import tqdm
import requests
import os

def download_file(url,save_path):
    
    # 用流stream的方式获取url的数据
    resp = requests.get(url, stream=True)
    # 拿到文件的长度,并把total初始化为0
    total = int(resp.headers.get('content-length', 0))
    # 打开当前目录的fname文件(名字你来传入)
    # 初始化tqdm,传入总数,文件名等数据,接着就是写入,更新等操作了
    with open(save_path, 'wb') as file, tqdm(
        desc=save_path,
        total=total,
        unit='iB',
        unit_scale=True,
        unit_divisor=1024,
    ) as bar:
        for data in resp.iter_content(chunk_size=1024):
            size = file.write(data)
            bar.update(size)

url="https://landsat.usgs.gov/landsat-8-cloud-cover-assessment-validation-data"

response=requests.get(url).content
parse_content=BeautifulSoup(response)
table=parse_content.select('table')[2::]

root_path='/content/drive/MyDrive/landsat-8-cloud-cover-assessment-validation-data'

for land_cover in table:
    
    land_cover_name=land_cover.select('th')[0].select('strong')[0].string.replace('/','')
    land_cover_path=os.path.join(root_path,land_cover_name)
    if not os.path.exists(land_cover_path):
      os.makedirs(land_cover_path)

        
    tar_file_urls=land_cover.select('a')
    
    for file_url in tar_file_urls:
        url=file_url.get('href')
        file_name=url.split('/')[-1]
        if os.path.exists(os.path.join(land_cover_path,file_name)):
          continue
        download_file(url,os.path.join(land_cover_path,file_name))

解压数据

下载之后将数据进行解压

代码语言:javascript
复制
import tarfile
from glob import glob
import os

input_path='/content/drive/MyDrive/dataset/landsat-8-cloud-cover-assessment-validation-data'
out_root_path='/content/drive/MyDrive/dataset/landsat_cloud_cover_unzip'

tar_files=glob(input_path+'/*/*.tar.gz')
for i in tar_files:
  tf = tarfile.open(i)
  out_path=os.path.join(out_root_path,i.split('/')[-2])
  if not os.path.exists(out_path):
    os.makedirs(out_path)
  tf.extractall(out_path)
  print("解压成功:{}".format(i))

这里我使用的colab进行下载,数据直接保存到了谷歌云盘上(花了9.9美元买的2T空间),因为谷歌服务器也是在国外,所以下载速度还是比较可以的。 如果是下载到本地的话应该就需要挂代理了。这里我把数据的谷歌云盘链接分享一下,大家谷歌云盘空间够的话也可以保存备用。 原始数据:https://drive.google.com/drive/folders/1R--pQxMoQ5vbkk7-L4suokvMKCGKSbuc?usp=sharing 解压后的数据:https://drive.google.com/drive/folders/1Cn5Sl8pdjQwcLGXLrNae4PZ8FthW4QVn?usp=sharing

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 GIS与遥感开发平台 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Landsat云标识数据
    • 下载代码
      • 解压数据
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档