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, "your_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 17, in <module> RuntimeError: 使用了无效的 SQL 语句。 [SELECT * FROM 相交77777777 WHERE "your_field_name" = SHAPE_Area <" 497477.65198277065 "] 执行(删除小面)失败。请改正代码
时间: 2023-12-28 09:04:15 浏览: 67
GRB2网格预报数据发布影像服务.zip_.grb数据_Arcpy_GRB2格式数据_arcgis 气象_气象数据
根据错误信息,你的 SQL 语句存在问题。在构建查询语句时,你需要将查询条件括在引号中,例如:
```python
expression = arcpy.AddFieldDelimiters(feature_class, "your_field_name") + " = '" + query + "'"
```
同时,如果你要删除小于某个面积的要素,应该这样写:
```python
expression = arcpy.AddFieldDelimiters(feature_class, "SHAPE_Area") + " < " + query
```
最终代码应该是这样:
```python
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
```
阅读全文