import syssys.tracebacklimit = 0import osos.environ['PYTHONUNBUFFERED'] = '1'import arcpy# 获取参数input_features = arcpy.GetParameterAsText(0)join_field = arcpy.GetParameterAsText(1)target_feature = arcpy.GetParameterAsText(2)target_field = arcpy.GetParameterAsText(3)area_threshold = arcpy.GetParameterAsText(4)# 创建空间连接join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT")# 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0)join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field)# 使用SelectLayerByAttribute选择重叠面积大于阈值的要素arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold))# 使用SummaryStatistics工具进行面积求和summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [[join_field_name, "SUM_Shape_Area"]], [join_field_name])# 使用TableToNumPyArray将结果转换为字典sum_dict = {}with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr: for row in arr: sum_dict[row[0]] = row[1]# 使用UpdateCursor更新目标要素类的目标字段with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor: for row in cursor: join_value = row[1] if join_value in sum_dict: area_sum = sum_dict[join_value] row[0] = area_sum cursor.updateRow(row)# 导出结果output_feature = arcpy.GetParameterAsText(5)arcpy.CopyFeatures_management(target_feature, output_feature)# 删除游标对象和要素图层对象del cursor, join_layer运行错误Traceback (most recent call last): ExecuteError: 执行失败。参数无效。 WARNING 000725: 输出表: 数据集 in_memory\summary_table 已存在。 ERROR 000800: 该值不是 SUM | MEAN | MIN | MAX | RANGE | STD | COUNT | FIRST | LAST 的成员。 ERROR 000728: 表中不存在字段 "数据" 执行(Statistics)失败。请改正代码
时间: 2023-12-06 07:40:06 浏览: 120
这段代码可能存在以下问题:
1. 参数无效:请检查输入参数是否正确,并确保路径和名称正确无误。
2. 表已存在:请确保“in_memory/summary_table”表不存在,或者在运行前删除该表。
3. 错误的统计方法:请确保统计方法是 SUM,MEAN,MIN,MAX,RANGE,STD,COUNT,FIRST,LAST 中的一种,且正确拼写。
4. 不存在字段:请检查代码中的字段名称是否正确,确保与表中存在的字段名称一致。
5. 缺少必要的导入语句:请确保导入必要的模块和函数,例如“arcpy.da”。
建议逐行检查代码并核对参数和语法,以排除以上问题。
相关问题
import sys sys.tracebacklimit = 0 import os os.environ['PYTHONUNBUFFERED'] = '1'import arcpy # 获取参数 input_features = arcpy.GetParameterAsText(0) join_field = arcpy.GetParameterAsText(1) target_feature = arcpy.GetParameterAsText(2) target_field = arcpy.GetParameterAsText(3) area_threshold = arcpy.GetParameterAsText(4) # 创建空间连接 join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT") # 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称 join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0) join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field) # 使用SelectLayerByAttribute选择重叠面积大于阈值的要素 arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold)) # 使用SummaryStatistics工具进行面积求和 summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [["Shape_Area", "SUM"]], join_field_name) # 使用TableToNumPyArray将结果转换为字典 sum_dict = {} with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr: for row in arr: sum_dict[row[0]] = row[1] # 使用UpdateCursor更新目标要素类的目标字段 with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor: for row in cursor: join_value = row[1] if join_value in sum_dict: area_sum = sum_dict[join_value] row[0] = area_sum cursor.updateRow(row) # 导出结果 output_feature = arcpy.GetParameterAsText(5) arcpy.CopyFeatures_management(target_feature, output_feature) # 删除游标对象和要素图层对象 del cursor, join_layer运行错误SyntaxError: invalid syntax (空间连接.py, line 4) 执行(ccc)失败。请改正代码
import sys
sys.tracebacklimit = 0
import os
os.environ['PYTHONUNBUFFERED'] = '1'
import arcpy
# 获取参数
input_features = arcpy.GetParameterAsText(0)
join_field = arcpy.GetParameterAsText(1)
target_feature = arcpy.GetParameterAsText(2)
target_field = arcpy.GetParameterAsText(3)
area_threshold = arcpy.GetParameterAsText(4)
# 创建空间连接
join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT")
# 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称
join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0)
join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field)
# 使用SelectLayerByAttribute选择重叠面积大于阈值的要素
arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold))
# 使用SummaryStatistics工具进行面积求和
summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [["Shape_Area", "SUM"]], join_field_name)
# 使用TableToNumPyArray将结果转换为字典
sum_dict = {}
with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr:
for row in arr:
sum_dict[row[0]] = row[1]
# 使用UpdateCursor更新目标要素类的目标字段
with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor:
for row in cursor:
join_value = row[1]
if join_value in sum_dict:
area_sum = sum_dict[join_value]
row[0] = area_sum
cursor.updateRow(row)
# 导出结果
output_feature = arcpy.GetParameterAsText(5)
arcpy.CopyFeatures_management(target_feature, output_feature)
# 删除游标对象和要素图层对象
del cursor, join_layer
import sys sys.tracebacklimit = 0 import os os.environ['PYTHONUNBUFFERED'] = '1' import arcpy 获取参数 input_features = arcpy.GetParameterAsText(0) join_field = arcpy.GetParameterAsText(1) target_feature = arcpy.GetParameterAsText(2) target_field = arcpy.GetParameterAsText(3) area_threshold = arcpy.GetParameterAsText(4) 创建空间连接 join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT") 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称 join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0) join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field) 使用SelectLayerByAttribute选择重叠面积大于阈值的要素 arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold)) 使用SummaryStatistics工具进行面积求和 summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [["Shape_Area", "SUM"]], join_field_name) 使用TableToNumPyArray将结果转换为字典 sum_dict = {} with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr: for row in arr: sum_dict[row[0]] = row 使用UpdateCursor更新目标要素类的目标字段 with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor: for row in cursor: join_value = row[1] if join_value in sum_dict: area_sum = sum_dict[join_value] row[0] = area_sum cursor.updateRow(row) 导出结果 output_feature = arcpy.GetParameterAsText(5) arcpy.CopyFeatures_management(target_feature, output_feature) 删除游标对象和要素图层对象 del cursor, join_layer请改正为可复制代码
import sys
import os
import arcpy
# 禁用traceback,避免出现异常时输出堆栈信息
sys.tracebacklimit = 0
# 设置PYTHONUNBUFFERED环境变量,避免使用arcpy输出信息时出现延迟
os.environ['PYTHONUNBUFFERED'] = '1'
# 获取参数
input_features = arcpy.GetParameterAsText(0)
join_field = arcpy.GetParameterAsText(1)
target_feature = arcpy.GetParameterAsText(2)
target_field = arcpy.GetParameterAsText(3)
area_threshold = arcpy.GetParameterAsText(4)
# 创建空间连接
join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT")
# 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称
join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0)
join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field)
# 使用SelectLayerByAttribute选择重叠面积大于阈值的要素
arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold))
# 使用SummaryStatistics工具进行面积求和
summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [["Shape_Area", "SUM"]], join_field_name)
# 使用TableToNumPyArray将结果转换为字典
sum_dict = {}
with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr:
for row in arr:
sum_dict[row[0]] = row
# 使用UpdateCursor更新目标要素类的目标字段
with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor:
for row in cursor:
join_value = row[1]
if join_value in sum_dict:
area_sum = sum_dict[join_value]["SUM_Shape_Area"]
row[0] = area_sum
cursor.updateRow(row)
# 导出结果
output_feature = arcpy.GetParameterAsText(5)
arcpy.CopyFeatures_management(target_feature, output_feature)
# 删除游标对象和要素图层对象
del cursor, join_layer
阅读全文