# 导入必要的模块 import arcpy # 定义要素类和筛选条件 feature_class = arcpy.GetParameterAsText(0) query = arcpy.GetParameterAsText(1) # 构建查询语句 expression = arcpy.AddFieldDelimiters(feature_class, "FIELD_NAME") + " = " + query # 使用 arcpy.da.UpdateCursor 删除要素 with arcpy.da.UpdateCursor(feature_class, "*", where_clause=expression) as cursor: for row in cursor: cursor.deleteRow() # 完成操作并清理内存 del cursor运行错误: Traceback (most recent call last): File "D:\多对一空间连接\批量删除小面积.py", line 7, in <module> NameError: name 'feature_class' is not defined 执行(删除小面)失败。请改正代码
时间: 2023-12-28 21:04:15 浏览: 50
出现这个错误是因为在构建查询语句时,变量 `feature_class` 没有被定义。请确保在运行脚本之前,正确设置了输入参数。可以通过在脚本开头添加以下代码来检查输入参数是否正确:
```python
if not arcpy.Exists(feature_class):
arcpy.AddError("输入的要素类不存在!")
sys.exit()
```
另外,在构建查询语句时,应该使用正确的字段名来替换 `FIELD_NAME`,例如:
```python
expression = arcpy.AddFieldDelimiters(feature_class, "your_field_name") + " = " + query
```
相关问题
import arcpy import sys # 获取要素类路径和查询条件 feature_class = arcpy.GetParameterAsText(0) query = arcpy.GetParameterAsText(1) if not arcpy.Exists(feature_class): arcpy.AddError("输入的要素类不存在!") sys.exit() # 构建查询语句 expression = arcpy.AddFieldDelimiters(feature_class, "SHAPE_Area") + " < " + query # 使用 arcpy.da.UpdateCursor 删除要素 with arcpy.da.UpdateCursor(feature_class, "*", where_clause=expression) as cursor: for row in cursor: cursor.deleteRow() # 完成操作并清理内存 del cursor请将代码加一个输出成果
import arcpy
import sys
# 获取要素类路径和查询条件
feature_class = arcpy.GetParameterAsText(0)
query = arcpy.GetParameterAsText(1)
if not arcpy.Exists(feature_class):
arcpy.AddError("输入的要素类不存在!")
sys.exit()
# 构建查询语句
expression = arcpy.AddFieldDelimiters(feature_class, "SHAPE_Area") + " < " + query
# 使用 arcpy.da.UpdateCursor 删除要素
with arcpy.da.UpdateCursor(feature_class, "*", where_clause=expression) as cursor:
for row in cursor:
cursor.deleteRow()
# 完成操作并清理内存
del cursor
# 输出操作完成的信息
arcpy.AddMessage("要素删除成功!")
import arcpy import os import sys feature_class = arcpy.GetParameterAsText(0) query = arcpy.GetParameterAsText(1) output_path = arcpy.GetParameterAsText(2) if not arcpy.Exists(output_path): arcpy.CreateFolder_management(os.path.dirname(output_path), os.path.basename(output_path)) gdb_path = os.path.join(output_path, "output.gdb") if not arcpy.Exists(gdb_path): arcpy.CreateFileGDB_management(output_path, "output.gdb", "CURRENT") expression = arcpy.AddFieldDelimiters(feature_class, "SHAPE_Area") + " < " + query if arcpy.Exists(feature_class): with arcpy.da.UpdateCursor(feature_class, "*", where_clause=expression) as cursor: for row in cursor: cursor.deleteRow() arcpy.FeatureClassToFeatureClass_conversion(feature_class, gdb_path, "output_feature_class") del cursor print("操作完成") else: print("要素类不存在")运行代码没有导出数据。请改正代码
根据你提供的代码,可能是因为在删除要素之后,没有将剩下的要素导出到输出要素类中。你可以尝试将以下代码添加到删除要素的代码后面:
```
arcpy.FeatureClassToFeatureClass_conversion(feature_class, gdb_path, "output_feature_class", where_clause=expression)
```
这将把符合查询条件的要素导出到输出要素类中。另外,你还需要注意一下以下几点:
1. 确保输入的要素类和输出路径是正确的。
2. 确保输入的查询表达式是正确的。
3. 确保输出路径中文件夹和文件名没有特殊字符或空格。
阅读全文