Earth Engine Data Catalog:https://developers.google.com/earth-engine/datasets/catalog
搜索REMA:
import ee
import geemap
import os
import warnings
# 忽略所有警告
warnings.filterwarnings("ignore")
# 下载 REMA Strips 数据(2m 和 8m)
def download_rema_strips(shp_mask, output_path, resolution='2m', scale=2, bands=None):
"""
下载 REMA Strips 数据
shp_mask (ee.Geometry): 用作下载区域的几何对象(shapefile 或 GEE Geometry)。
output_path (str): 下载图像保存的目录路径。
resolution (str): 分辨率,可以是 '2m' 或 '8m'。默认为 '2m'。
scale (int): 分辨率(单位:米),2m 或 8m,默认为 2 米。
bands (list): 要下载的波段名称列表,默认为 ['elevation']。
"""
if bands is None:
bands = ['elevation']
# 根据分辨率选择数据ID
if resolution == '2m':
data_id = 'UMN/PGC/REMA/V1/2m'
elif resolution == '8m':
data_id = 'UMN/PGC/REMA/V1/8m'
else:
raise ValueError("无效的分辨率。请选择 '2m' 或 '8m'。")
try:
# 获取 REMA Strips 数据并筛选日期范围
collection = ee.ImageCollection(data_id).filterBounds(shp_mask).filterDate('2009-01-01', '2018-01-01')
image_list = collection.toList(collection.size())
num_images = image_list.size().getInfo()
for i in range(num_images):
image = ee.Image(image_list.get(i))
date = image.date().format('YYYYMMdd').getInfo() # 获取影像日期并格式化
output_file_name = f'rema_strips_{resolution}_{date}.tif'
# 强制下载每个影像,不跳过相同日期的影像
image_clip = image.clip(shp_mask) # 使用 clip 裁剪到指定区域
# 打印所有波段信息,以帮助调试
band_names = image_clip.bandNames().getInfo() # 获取波段名称列表
print("所有波段信息:", band_names) # 打印所有波段名称
# 如果指定的波段不在数据中,打印错误信息
if not all(band in band_names for band in bands):
print(f"警告: 指定的波段 {bands} 不存在。请检查数据集的波段。")
continue
# 选择指定的波段
image_selected = image_clip.select(bands)
# 下载图像
geemap.download_ee_image(image_selected, os.path.join(output_path, output_file_name), crs='EPSG:3031',
scale=scale)
print(f'{output_file_name} 下载成功。')
except Exception as e:
print(f"下载 {resolution} REMA Strips 数据时出错: {e}")
# 下载 REMA Mosaic 数据
def download_rema_mosaic(shp_mask, output_path, scale=8):
"""
下载 REMA Mosaic 数据
shp_mask (ee.Geometry): 用作下载区域的几何对象(shapefile 或 GEE Geometry)。
output_path (str): 下载图像保存的目录路径。
scale (int): 分辨率(单位:米),默认为 8 米。
"""
data_id = 'UMN/PGC/REMA/V1_1/8m' # Mosaic 数据集
try:
# 获取 REMA Mosaic 数据并筛选日期范围
image = ee.Image(data_id).clip(shp_mask)
date = image.date().format('YYYYMMdd').getInfo() # 获取影像日期并格式化
output_file_name = f'rema_mosaic_{date}.tif'
# 强制下载,不检查文件是否已存在
if os.path.exists(os.path.join(output_path, output_file_name)):
print(f'{output_file_name} 已经存在,跳过下载。')
return
# 下载图像
geemap.download_ee_image(image, os.path.join(output_path, output_file_name), crs='EPSG:3031',
scale=scale)
print(f'{output_file_name} 下载成功。')
except Exception as e:
print(f"下载 REMA Mosaic 数据时出错: {e}")
# 下载给定时间范围和shp范围的所有 Sentinel-2 数据
def download_sentinel2(shp_mask, output_path, start_date='2009-01-01', end_date='2018-01-01', scale=30,
cloud_coverage=30):
"""
下载指定时间范围内,给定Shapefile范围内的所有 Sentinel-2 光学影像,并将数据转换为 0-255 的整数。
shp_mask (ee.Geometry): 用作下载区域的几何对象(shapefile 或 GEE Geometry)。
output_path (str): 下载图像保存的目录路径。
start_date (str): 开始日期,格式为 'YYYY-MM-DD',默认为 '2009-01-01'。
end_date (str): 结束日期,格式为 'YYYY-MM-DD',默认为 '2018-01-01'。
scale (int): 分辨率(单位:米),默认为 30 米。
cloud_coverage (int): 云层覆盖率的阈值,默认为 30%。
"""
try:
# 选择 Sentinel-2 数据集
sentinel2 = ee.ImageCollection('COPERNICUS/S2') \
.filterBounds(shp_mask) \
.filterDate(start_date, end_date) \
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', cloud_coverage)) # 过滤云层覆盖
# 获取影像列表
image_list = sentinel2.toList(sentinel2.size())
num_images = image_list.size().getInfo()
if num_images == 0:
print(f"没有找到符合条件的 Sentinel-2 影像。")
return
print(f"找到 {num_images} 张符合条件的 Sentinel-2 影像。")
# 收集所有影像日期和影像
image_dict = {}
for i in range(num_images):
image = ee.Image(image_list.get(i))
date = image.date().format('YYYYMMdd').getInfo() # 获取影像日期并格式化
if date not in image_dict:
image_dict[date] = []
image_dict[date].append(image)
# 下载每个日期的所有影像
for date, images in image_dict.items():
for image in images:
output_file_name = f'sentinel2_{date}_{images.index(image)}.tif'
if os.path.exists(os.path.join(output_path, output_file_name)):
print(f'{output_file_name} 已经存在,跳过下载。')
continue
# 将影像从反射率转换为 0-255 范围的整数
image_scaled = image.multiply(255).toInt()
# 使用 clip 裁剪到指定区域
image_clip = image_scaled.clip(shp_mask)
# 下载图像
geemap.download_ee_image(image_clip, os.path.join(output_path, output_file_name),
crs='EPSG:3031', scale=scale)
print(f'{output_file_name} 下载成功。')
except Exception as e:
print(f"下载 Sentinel-2 数据时出错: {e}")
# 下载 Sentinel-1 数据
def download_sentinel1(shp_mask, output_path, start_date='2014-10-03', end_date='2024-12-22', polarization='VV',
scale=10):
"""
下载指定时间范围内,给定Shapefile范围内的所有 Sentinel-1 SAR 数据
shp_mask (ee.Geometry): 用作下载区域的几何对象(shapefile 或 GEE Geometry)。
output_path (str): 下载图像保存的目录路径。
start_date (str): 开始日期,格式为 'YYYY-MM-DD',默认为 '2009-01-01'。
end_date (str): 结束日期,格式为 'YYYY-MM-DD',默认为 '2018-01-01'。
polarization (str): 极化方式,默认为 'VV',可选值为 'VV' 或 'VH'。
scale (int): 分辨率(单位:米),默认为 10 米。
"""
try:
# 选择 Sentinel-1 数据集
sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD') \
.filterBounds(shp_mask) \
.filterDate(start_date, end_date) \
.filter(ee.Filter.listContains('transmitterReceiverPolarisation', polarization)) \
.filter(ee.Filter.eq('instrumentMode', 'GRD')) # 过滤 GRD 模式数据
# 获取影像列表
image_list = sentinel1.toList(sentinel1.size())
num_images = image_list.size().getInfo()
if num_images == 0:
print(f"没有找到符合条件的 Sentinel-1 影像。")
return
print(f"找到 {num_images} 张符合条件的 Sentinel-1 影像。")
for i in range(num_images):
image = ee.Image(image_list.get(i))
date = image.date().format('YYYYMMdd').getInfo()
output_file_name = f'sentinel1_{date}.tif'
# 强制下载每个影像,不跳过相同日期的影像
image_clip = image.clip(shp_mask)
# 下载图像
geemap.download_ee_image(image_clip, os.path.join(output_path, output_file_name), crs='EPSG:3031',
scale=scale)
print(f'{output_file_name} 下载成功。')
except Exception as e:
print(f"下载 Sentinel-1 数据时出错: {e}")
# 主函数:用于测试和调用下载函数
def main():
# 初始化 GEE
ee.Authenticate()
ee.Initialize(project='替换成自己的')
# 选择一个 Shapefile(作为下载区域)
shp_path = 'G:\\矢量\\Fleming冰川\\fleming.shp' # 请替换为你的 shapefile 路径
shp_mask = geemap.vector_to_ee(shp_path).geometry()
# 设置输出路径
output_path_strip_8m = r'G:\\Fleming\\strip\\8m' # 请替换为你的输出目录路径
output_path_strip_2m = r'D:\fleming\strip\2m'
output_path_mosaic_8m = r'D:\fleming\mosaic'
output_path_sentinel2_10m = r'D:\fleming\sentinel2\10m' # 请替换为你的输出目录路径
output_path_sentinel1_10m = r'D:\fleming\sentinel1\10m' # 请替换为你的输出目录路径
# 下载给定时间范围内的所有 Sentinel-1 数据
# download_sentinel1(shp_mask, output_path_sentinel1_10m, start_date='2009-01-01', end_date='2018-01-01',
# polarization='VV', scale=10)
# 下载给定时间范围内的所有 Sentinel-2 数据
download_sentinel2(shp_mask, output_path_sentinel2_10m, start_date='2009-01-01', end_date='2018-01-01', scale=10)
# 下载 REMA Strips 数据(8m 分辨率)
# download_rema_strips(shp_mask, output_path_strip_8m, resolution='8m', scale=8)
# 下载 REMA Strips 数据(2m 分辨率)
#download_rema_strips(shp_mask, output_path_strip_2m, resolution='2m', scale=2)
# 下载 REMA Mosaic 数据
#download_rema_mosaic(shp_mask, output_path_mosaic_8m, scale=8)
if __name__ == '__main__':
main()