import arcpy # 获取用户输入参数 input_feature = arcpy.GetParameterAsText(0) # 输入要素 query_expression = arcpy.GetParameterAsText(1) # 查询表达式 output_feature = arcpy.GetParameterAsText(2) # 输出要素 # 构建查询语句 query = "\"{}\"".format(query_expression) # 使用查询语句选择要素 arcpy.MakeFeatureLayer_management(input_feature, "lyr") arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query) # 将选择的要素导出到输出位置 arcpy.CopyFeatures_management("lyr", output_feature)运行错误:执行: 删除小面 Export_Output_21111 "面积 >= '497477.651982771'" D:\多对一空间连接\多对一空间连接.gdb\突突突 开始时间: Wed May 24 22:17:33 2023 正在运行脚本 删除小面... Failed script 删除小面... Traceback (most recent call last): File "D:\多对一空间连接\批量删除小面积.py", line 12, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: 无效的表达式 执行(SelectLayerByAttribute)失败。 执行(删除小面)失败。 失败 在 Wed May 24 22:17:33 2023 (经历的时间: 0.25 秒)请改正代码
时间: 2024-01-22 17:21:17 浏览: 13
问题可能出在构建查询语句的这一步,可以尝试将代码修改为:
```python
# 构建查询语句
query = "{}".format(arcpy.AddFieldDelimiters(input_feature, "面积") + " >= {}".format(query_expression))
```
这样可以确保构建出来的查询语句符合 ArcGIS 的要求。另外,如果输入要素是一个图层,建议在选择要素之前先进行清空选择:
```python
# 清空选择
arcpy.SelectLayerByAttribute_management("lyr", "CLEAR_SELECTION")
# 使用查询语句选择要素
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query)
```
这样可以避免上一次选择的要素对本次选择造成影响。
相关问题
import arcpy # 获取用户输入参数 input_feature = arcpy.GetParameterAsText(0) # 输入要素 query_expression = arcpy.GetParameterAsText(1) # 查询表达式 output_feature = arcpy.GetParameterAsText(2) # 输出要素 # 构建查询语句 query = "".format(arcpy.AddFieldDelimiters(input_feature, "面积"), query_expression) # 使用查询语句选择要素 arcpy.MakeFeatureLayer_management(input_feature, "lyr") arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query) # 将选择的要素导出到输出位置 arcpy.CopyFeatures_management("lyr", output_feature) # 清除选择 arcpy.SelectLayerByAttribute_management("lyr", "CLEAR_SELECTION")运行错误:Traceback (most recent call last): File "D:\多对一空间连接\批量删除小面积.py", line 15, in <module> NameError: name 'output_feature' is not defined 执行(删除小面)失败。请改正代码
代码中定义了变量 `output_feature` 作为输出要素的路径,但是在构建查询语句和导出要素时都没有使用该变量,导致在执行 `arcpy.CopyFeatures_management("lyr", output_feature)` 时出现了 `NameError: name 'output_feature' is not defined` 的错误。
要解决这个问题,需要将 `output_feature` 变量传递给相应的函数,修改后的代码如下:
```python
import arcpy
# 获取用户输入参数
input_feature = arcpy.GetParameterAsText(0) # 输入要素
query_expression = arcpy.GetParameterAsText(1) # 查询表达式
output_feature = arcpy.GetParameterAsText(2) # 输出要素
# 构建查询语句
query = "{} < {}".format(arcpy.AddFieldDelimiters(input_feature, "面积"), query_expression)
# 使用查询语句选择要素
arcpy.MakeFeatureLayer_management(input_feature, "lyr")
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query)
# 将选择的要素导出到输出位置
arcpy.CopyFeatures_management("lyr", output_feature)
# 清除选择
arcpy.SelectLayerByAttribute_management("lyr", "CLEAR_SELECTION")
```
修改后的代码中,将查询语句中的占位符 `{}` 替换为了正确的字段和查询表达式,同时在导出要素时使用了 `output_feature` 变量。
import arcpy arcpy.SelectLayerByAttribute_management("lyr", "CLEAR_SELECTION") # 使用查询语句选择要素 arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query) # 获取用户输入参数 input_feature = arcpy.GetParameterAsText(0) # 输入要素 query_expression = arcpy.GetParameterAsText(1) # 查询表达式 output_feature = arcpy.GetParameterAsText(2) # 输出要素 # 构建查询语句 query = "{}".format(arcpy.AddFieldDelimiters(input_feature, "面积") + " >= {}".format(query_expression)) # 使用查询语句选择要素 arcpy.MakeFeatureLayer_management(input_feature, "lyr") arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query) # 将选择的要素导出到输出位置 arcpy.CopyFeatures_management("lyr", output_feature)运行错误:执行: 删除小面 Export_Output_21111 "面积 >= '497477.651982771'" D:\多对一空间连接\多对一空间连接.gdb\突突突 开始时间: Wed May 24 22:20:05 2023 正在运行脚本 删除小面... Failed script 删除小面... Traceback (most recent call last): File "D:\多对一空间连接\批量删除小面积.py", line 3, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: 执行失败。参数无效。 ERROR 000732: 图层名称或表视图: 数据集 lyr 不存在或不受支持 执行(SelectLayerByAttribute)失败。 执行(删除小面)失败。 失败 在 Wed May 24 22:20:06 2023 (经历的时间: 0.28 秒)请改正代码
可以尝试修改代码如下:
```
import arcpy
# 获取用户输入参数
input_feature = arcpy.GetParameterAsText(0) # 输入要素
query_expression = arcpy.GetParameterAsText(1) # 查询表达式
output_feature = arcpy.GetParameterAsText(2) # 输出要素
# 构建查询语句
query = "{}".format(arcpy.AddFieldDelimiters(input_feature, "面积") + " >= {}".format(query_expression))
# 使用查询语句选择要素
arcpy.MakeFeatureLayer_management(input_feature, "lyr")
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", query)
# 将选择的要素导出到输出位置
arcpy.CopyFeatures_management("lyr", output_feature)
# 清除选择
arcpy.SelectLayerByAttribute_management("lyr", "CLEAR_SELECTION")
```
修改后的代码中,先获取了用户输入的参数,然后构建查询语句,并使用`MakeFeatureLayer_management`函数创建名为"lyr"的要素图层。接着使用`SelectLayerByAttribute_management`函数选择符合条件的要素,并将其导出到输出位置。最后使用`SelectLayerByAttribute_management`函数清除选择。