import arcpy # 定义空间连接函数 def spatial_join(input_features, target_features, overlap_threshold, output_features): # 创建空间连接对象 join_operation = "JOIN_ONE_TO_ONE" join_type = "KEEP_ALL" field_mapping = "" match_option = "INTERSECT" search_radius = "" distance_field_name = "" spatial_join = arcpy.SpatialJoin_analysis(input_features, target_features, output_features, join_operation, join_type, field_mapping, match_option, search_radius, distance_field_name) # 过滤结果 overlap_field = "SHAPE@AREA" with arcpy.da.UpdateCursor(spatial_join, overlap_field) as cursor: for row in cursor: if row[0] < overlap_threshold: cursor.deleteRow() del cursor # 设置工具箱参数 input_features = arcpy.GetParameterAsText(0) target_features = arcpy.GetParameterAsText(1) overlap_threshold = arcpy.GetParameterAsText(2) output_features = arcpy.GetParameterAsText(3) # 运行空间连接函数 spatial_join(input_features, target_features, overlap_threshold, output_features) # 完成工具箱输出 arcpy.SetParameterAsText(3, output_features)运行代码输出成果要素图层数据为空,请改正代码
时间: 2023-12-03 20:46:53 浏览: 156
根据你提供的代码,可能存在以下问题:
1. `arcpy.SpatialJoin_analysis()`函数返回的是一个字符串类型的路径,而不是要素图层对象。因此,在过滤结果时,应该使用`spatial_join`字符串作为输入,而不是要素图层对象。
2. `overlap_threshold`参数应该是一个数字类型的阈值,而不是字符串类型。因此,在调用`spatial_join()`函数时,应该将其转换为浮点数类型。
修改后的代码如下:
```python
import arcpy
def spatial_join(input_features, target_features, overlap_threshold, output_features):
# 创建空间连接对象
join_operation = "JOIN_ONE_TO_ONE"
join_type = "KEEP_ALL"
field_mapping = ""
match_option = "INTERSECT"
search_radius = ""
distance_field_name = ""
spatial_join = arcpy.SpatialJoin_analysis(input_features, target_features, output_features, join_operation, join_type, field_mapping, match_option, search_radius, distance_field_name)
# 过滤结果
overlap_field = "SHAPE@AREA"
with arcpy.da.UpdateCursor(spatial_join, overlap_field) as cursor:
for row in cursor:
if row[0] < float(overlap_threshold):
cursor.deleteRow()
del cursor
# 设置工具箱参数
input_features = arcpy.GetParameterAsText(0)
target_features = arcpy.GetParameterAsText(1)
overlap_threshold = float(arcpy.GetParameterAsText(2))
output_features = arcpy.GetParameterAsText(3)
# 运行空间连接函数
spatial_join(input_features, target_features, overlap_threshold, output_features)
# 完成工具箱输出
arcpy.SetParameterAsText(3, output_features)
```
注意,在调用`arcpy.GetParameterAsText()`函数获取参数时,返回的是字符串类型,需要根据参数的实际类型进行转换。
阅读全文