手动下载全球数据比较困难,本文提供批量化下载这些土地覆盖产品的Python代码。包含GLC_FCS30 、Esri_GLC10、 ESA_GLC10 、FROM_GLC10
FCS_GLC30
刘良云、张肖老师团队制作,提供了非常便捷的下载方式。
https://data.casearth.cn/dataset/6123651428a58f70c2a51e49#filesArea
API | Url | 格式 | HTTP请求 |
---|---|---|---|
通过ID获取文件列表 | /api/dataset/getAllFileListBySdoId?sdoId=6123651428a58f70c2a51e49 | JSON | GET |
单文件下载 | /api/file/downloadOneFile?fileId=文件Id&username=用户名 | 文件 | GET |
获取元数据信息 | /api/dataset/sdoMetadata?itemId=2500 | JSON | GET |
获取文件元数据信息 | /api/file/fileMetadata?fileId=文件Id | JSON | GET |
import os
import requests
from tqdm import tqdm
def get_all_image_list():
get_image_list_url = r'https://data.casearth.cn/api/dataset/getAllFileListBySdoId?sdoId=6123651428a58f70c2a51e49'
response = requests.get(
url=get_image_list_url
)
data = response.json()['data']
return data
def download_one_file(url, filename, save_dir="./downloads"):
os.makedirs(save_dir, exist_ok=True)
try:
# 获取文件名
file_path = os.path.join(save_dir, filename)
# 下载图片
response = requests.get(url, stream=True)
response.raise_for_status() # 检查请求是否成功
# 保存图片到本地
with open(file_path, "wb") as file:
for chunk in response.iter_content(chunk_size=8192): # 分块写入文件
file.write(chunk)
return os.path.abspath(file_path)
except requests.exceptions.RequestException as e:
raise Exception(f"下载失败: {e}") from e
except Exception as e:
raise Exception(f"保存文件失败: {e}") from e
if __name__ == '__main__':
# TODO: 修改为自己的用户名
username = 'user'
image_list = get_all_image_list()
for it in tqdm(image_list):
file_name = it['file_name']
id = it['id']
down_url = fr'https://data.casearth.cn/api/file/downloadOneFile?fileId={id}&username={username}'
download_one_file(down_url, file_name, save_dir="./downloads")
–未完待续–