import geemap #需安装geemap
import ee
# ee.Authenticate()
=33210) #根据自己电脑配置修改
geemap.set_proxy(port
ee.Initialize()import math
from utils import *
练习1 基于GEE的哨兵1号数据获取及水体提取取
1. 数据获取
本案例需要科学上网,成功安装geemap后方可使用,slope_correction函数封装在utils中,有需要的话请联系作者yujunchuan@mail.cgs.gov.cn
定义必要的函数
def clip_img(img):
return img.clip(region)
def filtter_speckles(img):
=img.select('VV')
vv= img.select('VH')
vh= vv.focal_mean(50,'circle','meters').rename('VV_Filtered')
vv_smoothed = vh.focal_mean(50,'circle','meters').rename('VH_Filtered')
vh_smoothed return img.addBands(vv_smoothed).addBands(vh_smoothed)
def SDWI(img):
= img.select("VH_mean")
VH = img.select("VV_mean")
VV = VV.multiply(VH).multiply(ee.Number(10))
x =(x).log().rename('sdwi')
y# sdwi=y.subtract(8)
file=img.addBands(y)
return file.select("sdwi")
def addDate(image):
= ee.Date(image.get('system:time_start')).format('yyyyMMdd')
date return image.set("image_date",date)
设定鄱阳湖区域范围
=ee.Geometry.Polygon([[[115.795898, 28.623104],[116.779175, 28.623104],[116.779175, 29.441989],[115.795898, 29.441989],[115.795898, 28.623104]]])
region=geemap.Map()
Map
Map.center_object(region)'region')
Map.addLayer(region,{}, Map
2. SDWI指数计算
编写sdwi计算函数,如非山区可以不进行地形校正
def sdwi_cal(data):
= slope_correction(data).main().map(clip_img)
corrected = corrected.mean().select('VV')
VV = corrected.mean().select('VH')
VH # Map.addLayer(VV, {'bands':['VV'], 'min':[-21], 'max':[0.5], 'gamma': 0.65}, 'SAR_RGB_VV')
# Map.addLayer(VH, {'bands':['VH'], 'min':[-28], 'max':[4], 'gamma': 0.65}, 'SAR_RGB_VH')
= corrected.map(filtter_speckles)
S1 =S1.select("VV_Filtered").first().get("image_date")
name= S1.select("VV_Filtered").reduce(ee.Reducer.mean()).rename("VV_mean").set('image_date',name)
SVV = S1.select("VH_Filtered").reduce(ee.Reducer.mean()).rename("VH_mean").set('image_date',name)
SVH =SVV.addBands(SVH)
Sen1= SDWI(Sen1).select('sdwi')
sdwi # Map.addLayer(sdwi,{'bands':['sdwi'], 'min':[8], 'max':[10], 'gamma': 0.65}, 'sdwi')
return sdwi
根据阅读计算sdwi
def monthly_sdwi(mon):
=ee.Date.fromYMD(year=2022,month=mon,day=1)
start=start.advance(1.0,'month')
end= (ee.ImageCollection('COPERNICUS/S1_GRD')
data
.filterBounds(region)'transmitterReceiverPolarisation','equals',["VV", "VH"])
.filterMetadata('instrumentMode','equals','IW')
.filterMetadata(
.filterDate(start, end)map(addDate))
.=sdwi_cal(data)
sdwi=sdwi.get("image_date")
namereturn sdwi
=ee.ImageCollection(ee.List.sequence(6,9).map(monthly_sdwi))
monthly=monthly.aggregate_array('image_date')
bandname=monthly.toBands().rename(bandname)
singlesdwifor i in range(4):
8.5)), {"palette":['6a5acd']}, "swdi"+str(i))
Map.addLayer(singlesdwi.select(i).updateMask(singlesdwi.select(i).gte( Map
3. 水面变化分析
通过多视角查看前后水体变化
=geemap.Map()
Map
Map.center_object(region)='SATELLITE', right_layer='SATELLITE')
Map.split_map(left_layer= {"palette":['6a5acd']}
vis=geemap.ee_tile_layer(singlesdwi.select(0).updateMask(singlesdwi.select(0).gte(8.5)), vis,name='2022-06')
left_layer=geemap.ee_tile_layer(singlesdwi.select(3).updateMask(singlesdwi.select(3).gte(8.5)), vis,name='2022-09')
right_layer
Map.split_map(left_layer, right_layer) Map
年度12个月变化监测,提取永久水体
=ee.ImageCollection(ee.List.sequence(1,12).map(monthly_sdwi))
monthly=monthly.aggregate_array('image_date')
bandname=monthly.toBands().rename(bandname)
singlesdwifor i in range(0,12):
if i ==0:
=singlesdwi.select(0).where(singlesdwi.select(i).lte(8.5),0).where(singlesdwi.select(i).gt(8.5),1)
initialelse:
=singlesdwi.select(i).where(singlesdwi.select(i).lte(8.5),0).where(singlesdwi.select(i).gt(8.5),1)
final=initial.add(final) initial
"min":[1],"max":[12], "palette":['ffffff', '99d9ea', '0000ff']}, "water")
Map.addLayer(initial, { Map
4. 下载提取结果
for i in range(8):
=singlesdwi.select(i)
img=img.setDefaultProjection('epsg:4326',None,10)
img=singlesdwi.bandNames().getInfo()
name= './sentinel_'+name[i]+str(i)+".tif"
outname =outname,scale=10,region=region,crs='EPSG:4326') geemap.download_ee_image(img, filename
想了解更多请关注[45度科研人]公众号,欢迎给我留言!