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)运行错误:Traceback (most recent call last): File "D:\实验2\空间连接.py", line 15, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: 执行失败。参数无效。 The value cannot be a feature class ERROR 000840: 该值不是 栅格图层。 ERROR 000840: 该值不是 镶嵌图层。 执行(SelectLayerByAttribute)失败。 执行(空间连接多对一)失败。请改正代码
时间: 2023-12-06 14:44:42 浏览: 161
根据错误提示,SelectLayerByAttribute_management 函数的参数无效,要求输入的值不是一个要素类,可能是栅格图层或镶嵌图层。因此,需要检查代码中 SelectLayerByAttribute_management 函数的输入参数是否正确。
可以使用 arcpy.Describe 函数来获取 SpatialJoin_analysis 工具输出的结果的数据类型,确保 SelectLayerByAttribute_management 函数的输入参数是一个要素图层。
修改后的代码如下:
```
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))
# 检查 SelectLayerByAttribute_management 函数的输入参数是否正确
desc = arcpy.Describe(spatial_join)
if desc.dataType == 'FeatureClass':
# 设置字段映射
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)
else:
arcpy.AddError("SelectLayerByAttribute_management 函数的输入参数不是要素图层。")
```
阅读全文