# 导入 ArcPy 模块 import arcpy # 获取输入参数 input_layer = arcpy.GetParameterAsText(0) field_name = arcpy.GetParameterAsText(1) field_value = arcpy.GetParameterAsText(2) output_folder = arcpy.GetParameterAsText(3) # 定义筛选条件 where_clause = "{} = '{}'".format(arcpy.AddFieldDelimiters(input_layer, field_name), field_value) # 使用游标进行筛选并导出要素图层 with arcpy.da.SearchCursor(input_layer, ["OID@", "SHAPE@"], where_clause) as cursor: for row in cursor: output_name = "feature_{}.shp".format(row[0]) output_path = output_folder + "\\" + output_name arcpy.CopyFeatures_management(row[1], output_path)运行错误:Traceback (most recent call last): File "D:\多对一空间连接\批量删除小面积.py", line 13, in <module> NameError: name 'where_clause' is not defined 执行(删除小面)失败。请改正代码
时间: 2023-12-25 10:06:37 浏览: 102
看起来出错的原因是 `where_clause` 变量在定义之前被调用了。你可以将 `where_clause` 的定义放在获取输入参数之后,如下所示:
```python
import arcpy
# 获取输入参数
input_layer = arcpy.GetParameterAsText(0)
field_name = arcpy.GetParameterAsText(1)
field_value = arcpy.GetParameterAsText(2)
output_folder = arcpy.GetParameterAsText(3)
# 定义筛选条件
where_clause = "{} = '{}'".format(arcpy.AddFieldDelimiters(input_layer, field_name), field_value)
# 使用游标进行筛选并导出要素图层
with arcpy.da.SearchCursor(input_layer, ["OID@", "SHAPE@"], where_clause) as cursor:
for row in cursor:
output_name = "feature_{}.shp".format(row[0])
output_path = output_folder + "\\" + output_name
arcpy.CopyFeatures_management(row[1], output_path)
```
这样应该就能够正常运行了。
阅读全文