# -*- coding: UTF-8 -*-
#scrip
#使用shp數(shù)據(jù)批量裁剪柵格數(shù)據(jù)并統(tǒng)計(jì)均值
print"使用shp數(shù)據(jù)批量裁剪柵格數(shù)據(jù)并統(tǒng)計(jì)結(jié)果均值"
###########修改部分###########
ras_file= r"D:\python\nc\GLEAM\Transpiration_TIF_OUT"? #待裁剪的原始柵格數(shù)據(jù)存儲(chǔ)文件夾路徑
suffix= 'tif'? #待裁剪的原始柵格數(shù)據(jù)后綴
bvalue=-999.0? #待裁剪的原始柵格數(shù)據(jù)背景值
shp_file= r"D:\python\shp"? #裁剪所需shp模板數(shù)據(jù)存儲(chǔ)文件夾路徑
ras_file_cut= r"D:\python\nc\GLEAM\Transpiration_YR_SWAT_mask"? #裁剪后柵格數(shù)據(jù)結(jié)果存儲(chǔ)文件夾路徑
txtname=r"D:\python\nc\GLEAM\Transpiration_YR_SWAT_mask"? ? #輸出統(tǒng)計(jì)文本路徑
#############################
#計(jì)算部分
import fenqutongji_arcpy
fenqutongji_arcpy.env.workspace=shp_file
shps=fenqutongji_arcpy.ListFeatureClasses()
fenqutongji_arcpy.env.workspace=ras_file
ras=fenqutongji_arcpy.ListRasters('*', suffix)
print "共有"+str(len(ras))+"個(gè)柵格數(shù)據(jù)"
#
print "Processing......"
for shin shps:
? ? shtmp=sh.encode('cp936')
shpfile=shp_file+"\\"+shtmp
print "共有"+str(len(shps))+"個(gè)shp數(shù)據(jù),正在處理第"+str(shps.index(sh)+1)+"個(gè):"+shtmp
result=[]
for rsin ras:
? ? ? ? rstmp=rs.encode('cp936')
outname=ras_file_cut+"\\"+rstmp[0:len(rstmp)-4]+shtmp[0:len(shtmp)-4]+".tif"
? ? ? ? #arcpy.Clip_management(rstmp,"#",outname,shpfile,"#","ClippingGeometry")
? ? ? ? fenqutongji_arcpy.Clip_management(rstmp, "#", outname, shpfile, str(bvalue), "ClippingGeometry")#忽略無效值
? ? ? ? stats=fenqutongji_arcpy.GetRasterProperties_management(outname, "MEAN")
result.append(rstmp+'? '+str(stats)+"\n")
# try:
? ? ? ? #? ? arcpy.Delete_management(outname,"")#注釋則裁剪的柵格不刪除,不注釋刪掉。
? ? ? ? # except:
? ? ? ? print(outname+'cannot delete')
file(txtname+"\\"+shtmp[0:len(shtmp)-4]+".txt",'w').writelines(result)
print "Finish!"