240404

# 使用代理
import os
os.environ['HTTP_PROXY'] = 'http://127.0.0.1:10809'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:10809'

# 登录
import ee
ee.Authenticate()

# 选择项目
ee.Initialize(project='ee-dsm-watershed-240315')

# 测试是否成功
import geemap
Map = geemap.Map()
Map

# 设置研究区域 大兴安岭5.6过火区
region = ee.Geometry.Rectangle([121.80,52.40, 125.10,53.50])

col = (
    # Tier 1 (T1) - Data that meets geometric and radiometric quality requirements
    # https://developers.google.com/earth-engine/datasets/catalog/LANDSAT_LT05_C02_T1_L2
    ee.ImageCollection('LANDSAT/LT05/C02/T1_L2')
    .filterBounds(region)
    .filterDate('1987-06-01', '1987-08-01')
    .filter(ee.Filter.lt('CLOUD_COVER_LAND',10))
)
col

# Google Earth Engine中数据的scale问题
# Applies scaling factors.
def apply_scale_factors(image):
  optical_bands = image.select('SR_B.').multiply(0.0000275).add(-0.2)
  thermal_bands = image.select('ST_B6').multiply(0.00341802).add(149.0)
  return image.addBands(optical_bands, None, True).addBands(
      thermal_bands, None, True
  )

col_asf = col.map(apply_scale_factors)

# 计算数量
print(col.size().getInfo())

# 定义归一化火烧指数的函数
# NBR = (NIR - SWIR) / (NIR + SWIR)
# 在LANDSAT/LT05/C02/T1_L2中定义的波段
# NIR SR_B4
# SWIR SR_B5
# https://github.com/gee-community/example-scripts/blob/master/SatelliteImagery/image.normalizedDifference.md
def NBR(image): #定义第一种NDVI函数
    nbr = image.normalizedDifference(['SR_B4', 'SR_B5']).rename('NBR')
    return image.addBands(nbr)

col_asf_nbr = col_asf.map(NBR).select(['NBR'])
col_asf_nbr

# 以列表的形式返回图像集合
col_list = col_asf_nbr.toList(col_asf_nbr.size())
col_list

# 返回图像的数量
col_ListSize = col_list.size().getInfo()
col_ListSize

# 筛选第一幅影像
image = ee.Image(col_list.get(0))
image

type(image.get("system:index").getInfo())

name = image.get("system:index").getInfo() + '.tif'
name

# 使用循环批量下载
for i in range(col_ListSize):  #循环加载单景影像
    image = ee.Image(col_list.get(i)) 
    name = image.get("system:index").getInfo() + '.tif'
    geemap.download_ee_image(image, filename = name, scale=30)