使用shp數(shù)據(jù)批量裁剪柵格數(shù)據(jù)并統(tǒng)計(jì)均值

# -*- 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!"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容