写一个arcgis工具箱脚本:只取连接要素与目标要素图斑重叠面的面积大于多少参数值才能进行空间连接,并导出空间连接后成果
时间: 2023-12-03 22:47:15 浏览: 88
以下是一个参考示例,可以根据具体需求进行修改和调整:
```python
import arcpy
# 获取输入参数
input_features = arcpy.GetParameterAsText(0) # 输入的要素图层
join_features = arcpy.GetParameterAsText(1) # 连接的要素图层
output_features = arcpy.GetParameterAsText(2) # 输出的要素图层
min_overlap_area = float(arcpy.GetParameterAsText(3)) # 最小重叠面积,单位:平方米
# 创建空间查询对象
spatial_reference = arcpy.Describe(input_features).spatialReference
spatial_query = arcpy.SpatialReference(spatial_reference)
# 创建空间连接对象
join_operation = "JOIN_ONE_TO_ONE"
join_type = "KEEP_ALL"
field_mapping = arcpy.FieldMappings()
join_features_fields = arcpy.ListFields(join_features)
for field in join_features_fields:
if not field.required:
field_mapping.addInputField(join_features, field.name)
arcpy.AddMessage("Connecting features...")
connected_features = arcpy.SpatialJoin_analysis(input_features, join_features, output_features, join_operation, join_type, field_mapping, "INTERSECT", "", spatial_query)
# 过滤出重叠面积大于指定值的要素
arcpy.AddMessage("Filtering features by minimum overlap area...")
filtered_features = []
with arcpy.da.SearchCursor(connected_features, ["SHAPE@", "Join_Count"]) as cursor:
for row in cursor:
shape = row[0]
join_count = row[1]
if join_count > 0 and shape.area >= min_overlap_area:
filtered_features.append(row)
# 创建输出要素图层
arcpy.AddMessage("Creating output feature class...")
output_fields = arcpy.ListFields(connected_features)
output_field_names = [field.name for field in output_fields]
arcpy.CreateFeatureclass_management(arcpy.env.workspace, output_features, "POLYGON", spatial_reference)
arcpy.AddField_management(output_features, "Join_Count", "LONG")
arcpy.AddField_management(output_features, "Overlap_Area", "DOUBLE")
with arcpy.da.InsertCursor(output_features, output_field_names + ["Join_Count", "Overlap_Area"]) as cursor:
for row in filtered_features:
cursor.insertRow(row + (row[0].area,))
# 完成
arcpy.AddMessage("Done.")
```
该脚本中,首先获取输入的要素图层、连接的要素图层、输出的要素图层以及最小重叠面积参数值等输入参数。然后,创建空间查询对象和空间连接对象,对输入要素图层和连接要素图层进行空间连接,得到连接后的要素图层。接着,通过遍历连接后的要素图层中的每一个要素,过滤出重叠面积大于指定值的要素,并将它们插入到输出要素图层中。最后输出处理结果。
阅读全文