ArcGIS属性表中对某一字段进行分段求和

0
分享 2017-03-02

需求描述:现有一个矢量图层,其中ACRES字段记录了每一个面要素的面积,如何分段对该字段求和,如该字段数值在以下范围时0-50,50-300,300-1000,>4000,其面积分别是多少?


解决方案:
(1) 对于数据量少的,可以采用此方法;
可以通过属性查询(select by attributes),通过表达式:ACRES >0 AND ACRES <=50将分段范围的要素类选择,使用export data将选中的要素导出。


导出后在ACRES字段右键,使用statistics,出现如下图所示,即可得出ACRES >0 AND ACRES <=50的Sum值为203.951802;同样的方法,将剩余的分段依次求出。


(2) 对于数据量大,方案1就比较麻烦,可以使用python表达式来实现,由于下述方法直接改变字段的属性值,所以操作之前,新建一个ACRES_1字段,使其值等于ACRES。
在ACRES字段右键点击field Calculator,勾选show codeblock,在Pre-logic Script code框中输入下述表达式:
def Reclass(WellYield):
if (WellYield >= 0 and WellYield <= 50):
return 1
elif (WellYield > 50 and WellYield <= 300):
return 2
elif (WellYield > 300 and WellYield <=1000):
return 3
elif (WellYield > 4000):
return 4

具体参考如下截图:


运行完后,ACRES字段属性值将改变, 0-50被赋值为1,50-300被赋值为2,300-1000被赋值为3,>4000被赋值为4
然后使用Summary Statistics,设置如下图:


计算结果为:

(3) 直接使用arcpy脚本实现,具体代码如下:
import arcpy
arcpy.env.workspace = r"E:\ArcTutor\Editing\Zion.gdb"
# Create insert cursor for table
cursor = arcpy.da.SearchCursor("Research_areas",
["AREA","PERIMETER"])
sum1=0
sum2=0
sum3=0
sum4=0
for row in cursor:
print(row)
if (row[0] >= 10 and row[0] <= 100):
sum1=sum1+row[0]
elif (row[0] > 100 and row[0] <= 1000):
sum2=sum2+row[0]
elif (row[0] > 1000 and row[0] <= 10000):
sum3=sum3+row[0]
elif (row[0] > 10000):
sum4=sum4+row[0]

print sum1,sum2,sum3,sum4


文章来源:http://blog.sina.com.cn/s/blog_8026b6cf0102woml.html

1 个评论

楼上引的很好,这个很实用

要回复文章请先登录注册