import arcpy # 设置输入参数 input_feature_class = arcpy.GetParameterAsText(0) where_clause = arcpy.GetParameterAsText(1) # 构建删除操作 with arcpy.da.UpdateCursor(input_feature_class, where_clause) as cursor: for row in cursor: cursor.deleteRow() # 完成操作,输出结果 arcpy.SetParameter(2, "删除成功!")运行错误:Traceback (most recent call last): File "D:\多对一空间连接\批量删除小面积.py", line 9, in <module> RuntimeError: 使用了无效的 SQL 语句。 [SELECT FID_大面大面 ="19", OBJECTID, SHAPE_Area, SHAPE_Length FROM 相交77777777] 执行(删除小面)失败。请改正代码
时间: 2023-12-28 15:04:05 浏览: 185
错误提示显示 SQL 语句无效,可能是因为你的 where_clause 参数传递进来的条件语句有误。请检查一下你传递的 where_clause 参数是否正确。
另外,根据你的代码,where_clause 参数应该是一个 SQL 查询语句,而你传递进来的似乎只是一个条件,可能导致 SQL 语句不完整。建议你将条件转换为 SQL 语句,例如:
```python
where_clause = "FID_大面大面 = '19'"
```
这样就可以构建出完整的 SQL 语句了。
相关问题
import arcpy # 设置工具箱参数 input_features = arcpy.GetParameterAsText(0) # 输入要素图层 join_features = arcpy.GetParameterAsText(1) # 连接要素图层 output_features = arcpy.GetParameterAsText(2) # 输出要素图层 join_fields = arcpy.GetParameterAsText(3).split(';') # 连接要素图层中需要赋值的字段,用“;”隔开 area_threshold = arcpy.GetParameterAsText(4) # 面积阈值,只有面积大于该值的要素才会被连接 # 创建空间连接对象 spatial_join = arcpy.SpatialJoin_analysis(target_features=input_features, join_features=join_features, out_feature_class=output_features, join_operation="JOIN_ONE_TO_MANY", join_type="KEEP_ALL", match_option="INTERSECT") # 根据面积阈值进行筛选 if len(area_threshold) > 0: arcpy.SelectLayerByAttribute_management(in_layer_or_view=spatial_join, where_clause="Shape_Area >= {}".format(area_threshold)) # 设置字段映射 field_mapping = arcpy.FieldMappings() for field in join_fields: field_map = arcpy.FieldMap() field_map.addInputField(spatial_join, field) output_field = field_map.outputField output_field.name = "{}_{}".format(join_features, field) field_map.outputField = output_field field_mapping.addFieldMap(field_map) # 对要素进行赋值 arcpy.FeatureClassToFeatureClass_conversion(spatial_join, output_features, "", field_mapping)运行错误:IndentationError: unexpected indent (空间连接.py, line 17) 执行(空间连接多对一)失败。请改正代码
根据错误提示,第17行出现了意外的缩进。请检查该行以及前面的几行代码是否存在多余的空格或制表符。正确的代码应该是:
```python
# 创建空间连接对象
spatial_join = arcpy.SpatialJoin_analysis(target_features=input_features,
join_features=join_features,
out_feature_class=output_features,
join_operation="JOIN_ONE_TO_MANY",
join_type="KEEP_ALL",
match_option="INTERSECT")
# 根据面积阈值进行筛选
if len(area_threshold) > 0:
arcpy.SelectLayerByAttribute_management(in_layer_or_view=spatial_join,
where_clause="Shape_Area >= {}".format(area_threshold))
# 设置字段映射
field_mapping = arcpy.FieldMappings()
for field in join_fields:
field_map = arcpy.FieldMap()
field_map.addInputField(spatial_join, field)
output_field = field_map.outputField
output_field.name = "{}_{}".format(join_features, field)
field_map.outputField = output_field
field_mapping.addFieldMap(field_map)
# 对要素进行赋值
arcpy.FeatureClassToFeatureClass_conversion(spatial_join,
output_features,
"",
field_mapping)
```
# 导入 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 执行(删除小面)失败。请改正代码
看起来出错的原因是 `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)
```
这样应该就能够正常运行了。
阅读全文