You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArcGIS Calculate Field工具使用arcpy.da.SearchCursor报错求助

ArcGIS Calculate Field工具报Arcade $featureset错误的解决方法

问题场景

我在ArcGIS文件地理数据库的Parkmobile表中存储了停车交易数据,包含Zone(区域)、Parking_Start_date(停车开始时间)、Parking_End_date(停车结束时间)字段。想通过Calculate Field工具,用Python代码计算每条交易开始时刻对应区域的车辆占用量。

使用的表达式:calculate_occupancy(!Zone!, !Parking_Start_date!, !Parking_End_date!)
修复后的Python代码块:

def calculate_occupancy(zone, start_date, end_date):
    count = 0
    table_name = r"./Default.gdb/Parkmobile"
    field_names = ["Zone", "Parking_Start_date", "Parking_End_date"]
    with arcpy.da.SearchCursor(table_name, field_names) as cursor:
        for row in cursor:
            if row[0] == zone and row[1] <= start_date and row[2] >= start_date:
                count += 1
    return count

运行时报错:

ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support to Report a Bug, and refer to the error help for potential solutions or workarounds.
Failed to evaluate Arcade expression. [
Rule name: Zone_Occ,
Triggering event: Update,
Class name: Parkmobile,
GlobalID: ,
Arcade error: Object not found $featureset,
Script line: 9]
Failed to execute (CalculateField).

明明选了Python作为脚本语言,代码里也没用到Arcade的$featureset,却报Arcade相关错误,求解决方法。


核心原因

报错和你当前用的Python代码无关,是因为你的Parkmobile要素类(或目标字段Zone_Occ)绑定了Arcade字段规则(比如自动计算规则、约束规则)。当Calculate Field工具修改字段值时,触发了后台的Arcade规则,而该规则中引用的$featureset找不到,导致报错。


解决步骤

1. 禁用/删除冲突的Arcade字段规则

  • 右键Parkmobile要素类 → 打开属性窗口
  • 切换到「字段」选项卡,找到你要更新的Zone_Occ字段
  • 查看「规则」列,若显示绑定了Arcade规则(名称就是报错里的Zone_Occ):
    • 点击规则对应的编辑按钮,选择禁用该规则,或者直接删除规则
  • 保存属性设置后,重新运行Calculate Field工具

2. 优化Python代码(解决效率问题)

原代码每行都全表扫描,数据量大时会极慢甚至崩溃,建议改用预统计+批量更新的方式:

import arcpy

# 设置工作空间
arcpy.env.workspace = r"./Default.gdb"
fc = "Parkmobile"
target_field = "Zone_Occ"  # 存储占用量的目标字段

# 预计算所有(区域, 开始时间)对应的占用量,存入字典
occupancy_cache = {}
with arcpy.da.SearchCursor(fc, ["Zone", "Parking_Start_date"]) as s_cursor:
    for zone, start_time in s_cursor:
        # 用SQL查询过滤同区域且覆盖当前开始时间的记录
        sql = f"""
        Zone = '{zone}' 
        AND Parking_Start_date <= timestamp '{start_time}' 
        AND Parking_End_date >= timestamp '{start_time}'
        """
        # 创建临时图层并统计数量
        temp_layer = arcpy.MakeFeatureLayer_management(fc, "temp_layer", sql)
        count = int(arcpy.GetCount_management(temp_layer).getOutput(0))
        occupancy_cache[(zone, start_time)] = count

# 批量更新目标字段
with arcpy.da.UpdateCursor(fc, ["Zone", "Parking_Start_date", target_field]) as u_cursor:
    for zone, start_time, _ in u_cursor:
        u_cursor.updateRow([zone, start_time, occupancy_cache.get((zone, start_time), 0)])

3. 额外注意事项

  • 确保日期字段在SQL查询中用timestamp前缀包裹,避免格式解析错误
  • 如果数据库路径包含空格,SQL语句中的路径或字段名要用双引号包裹
  • 运行工具前关闭所有编辑会话,避免要素类被锁定

内容的提问来源于stack exchange,提问作者Arthur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:10:28