操作方法
在 ArcGIS Pro 中,“计算字段”工具中的 sum() 函数可用于计算数值字段列表中每条记录的总和。 然而,当要素类或表中包含大量字段时,手动选择所有数值字段是不切实际的。
本文提供的 ArcPy 脚本可用于计算要素类或表中所有数值字段的值之和。
在此示例中,'Point1' 要素类包含五个数值字段,如下图所示。

import arcpy
# Set the path to the feature class or the table
table = r""
# Add a new field to the table
arcpy.management.AddField(table, "SumField", "DOUBLE")
# Use UpdateCursor to iterate through the rows of the table
with arcpy.da.UpdateCursor(table, "*") as cursor:
for row in cursor:
# Initialize a variable to store the sum
total = 0
# Iterate through each field in the row
for i in range(len(row)):
# Check if the field is numeric and not OBJECTID, Shape_Length, and Shape_Area
if isinstance(row[i], (int, float, complex)) and cursor.fields[i] not in ['OBJECTID', 'Shape_Length', 'Shape_Area']:
# Add the field's value to the total
total += row[i]
# Update the new field with the total
row[cursor.fields.index('SumField')] = total
cursor.updateRow(row)
现在,每个要素的数值字段之和将填入新字段 'SumField' 中。

文章 ID: 000029132
获取来自 ArcGIS 专家的帮助
立即开始聊天