使用shp数据批量裁剪栅格数据并统计均值

# -*- coding: UTF-8 -*-

#scrip

#使用shp数据批量裁剪栅格数据并统计均值

print"使用shp数据批量裁剪栅格数据并统计结果均值"

###########修改部分###########

ras_file= r"D:\python\nc\GLEAM\Transpiration_TIF_OUT"  #待裁剪的原始栅格数据存储文件夹路径

suffix= 'tif'  #待裁剪的原始栅格数据后缀

bvalue=-999.0  #待裁剪的原始栅格数据背景值

shp_file= r"D:\python\shp"  #裁剪所需shp模板数据存储文件夹路径

ras_file_cut= r"D:\python\nc\GLEAM\Transpiration_YR_SWAT_mask"  #裁剪后栅格数据结果存储文件夹路径

txtname=r"D:\python\nc\GLEAM\Transpiration_YR_SWAT_mask"    #输出统计文本路径

#############################

#计算部分

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))+"个栅格数据"

#

print "Processing......"

for shin shps:

    shtmp=sh.encode('cp936')

shpfile=shp_file+"\\"+shtmp

print "共有"+str(len(shps))+"个shp数据,正在处理第"+str(shps.index(sh)+1)+"个:"+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!"

你可能感兴趣的:(使用shp数据批量裁剪栅格数据并统计均值)