zonal static as table重复区域无法统计的问题。

xiaoxiao2025-11-02  5

两个项目用到arcgis的zonal static as table功能,发现如果矢量有重叠的话,会导致其像元值只会被统计一次。对于此种情况,就采用遍历矢量要素,生成内存图层,然后对每一个矢量进行区域统计,得到结果再汇总。

在前人摸索的基础上,原教程是通过arcpy.list来汇总结果,但是发现会特别慢,因此本文通过py的dbfread模块来统计栅格值。

import arcpy,arcgisscripting,os from arcpy.sa import * from arcpy import env # Local variables: env.workspace=r"D:\cs.gdb" 

#矢量图层 CY50 = "CY50"

#栅格图层 rk="rk"

#得到cursor

inRows = arcpy.SearchCursor(CY50) inRows.reset() inRow = inRows.next()

#开始遍历,根据其OBJECTID来依次遍历(gdb中的数据不存在FID字段,任一唯一字段即可)

count=0 while inRow:

count+=1     print "{0} is being processed".format(inRow.OBJECTID)

  #内存图层的名称     lyr = "Zone {0}".format(inRow.OBJECTID)

#创建内存图层,详情见toolbox中的创建内存图层帮助     arcpy.MakeFeatureLayer_management(  CY50, lyr, '"OBJECTID" = {0}'.format(inRow.OBJECTID))

#生成结果表的路径     tempTable = "{0}/DBF_{1}.dbf".format(dbfPath, inRow.OBJECTID)     try :

#对该内存图层进行区域统         ZonalStatisticsAsTable(lyr, "OBJECTID", raster, tempTable, "DATA")     except BaseException as  e :         print ("")     inRow = inRows.next()      from dbfread import DBF

#然后读取dbf汇总#切换到dbf生成的表格,将结果读到字典中

#读取dbf的函数,参数1为区域统计dbf生成的路径,第二个为矢量的个数

def readDBF(dbfpath,count):     os.chdir(dbfpath)     my_dic=dict()     for i in range(count+1) :         s= "DBF_" + str(i) + ".dbf"         print("查找路径为"+dbfpath+"的表"+s)         if (os.path.exists(s)):             table = DBF(s, load=True)             try:                 my_dic[str(i)]=table.records[0]['SUM']             except BaseException as e:                 my_dic[str(i)]=0         else :             my_dic[str(i)]=str(0)     return my_dic     my_dicRK=readDBF(dbfpath,count)

#将结果字典写入到目标图层中

for s in my_dicRK.keys():         print(s+":"+str(my_dicRK[s]))     #将字典的值,依次写入到结果图层中     os.chdir(dbpath)     with arcpy.da.UpdateCursor(CY50,["rk_v","OBJECTID","rkbd_v","jz_v"]) as cursor2:         for inRow2 in cursor2:                 try:                     inRow2[0]=my_dicRK[str(inRow2[1])]                     inRow2[2]=my_dicRKBD[str(inRow2[1])]                     inRow2[3]=my_dicJZ[str(inRow2[1])]                     cursor2.updateRow(inRow2)                 except BaseException as e:                     print(e)

转载请注明原文地址: https://www.6miu.com/read-5038963.html

最新回复(0)