写一个arcgis工具箱脚本:连接要素中多个图斑叠加目标要素其中一个图斑时,取连接要素中多个图斑面积大于多少值的图斑利用空间连接赋值到目标要素图斑里,多个图斑相同字段值赋值到一个目标要素图斑字段时,用"/"隔开并赋值进去,最终导出成果
时间: 2023-12-03 13:44:26 浏览: 69
arcgis中Python脚本的使用【简明教程】.zip_arcgis_arcgis python_bridgeov5_draw
以下是一个基本的ArcGIS工具箱脚本,可以实现你描述的功能:
```python
import arcpy
# 定义输入参数
connect_features = arcpy.GetParameterAsText(0) # 连接要素
target_features = arcpy.GetParameterAsText(1) # 目标要素
target_field = arcpy.GetParameterAsText(2) # 目标要素中需要赋值的字段
area_threshold = arcpy.GetParameterAsText(3) # 连接要素中需要筛选的面积阈值
output_features = arcpy.GetParameterAsText(4) # 输出结果要素类
# 定义空间连接参数
spatial_relationship = "INTERSECT"
join_type = "KEEP_ALL"
field_mapping = arcpy.FieldMappings()
# 为目标要素中需要赋值的字段添加映射
field_to_map = arcpy.FieldMap()
field_to_map.addInputField(target_features, target_field)
field_to_map_name = field_to_map.outputField
field_to_map_name.name = target_field
field_to_map.outputField = field_to_map_name
field_mapping.addFieldMap(field_to_map)
# 获取连接要素中面积大于阈值的图斑
query = "{} > {}".format(arcpy.AddFieldDelimiters(connect_features, arcpy.Describe(connect_features).ShapeFieldName), area_threshold)
selected_connect_features = arcpy.SelectLayerByAttribute_management(connect_features, "NEW_SELECTION", query)
# 进行空间连接
result = arcpy.SpatialJoin_analysis(selected_connect_features, target_features, output_features, join_type, "KEEP_COMMON", field_mapping, spatial_relationship)
# 处理相同字段值赋值问题
if target_field != "":
# 创建一个字典来存储每个目标要素的值
target_values = {}
with arcpy.da.SearchCursor(target_features, ["OID@", target_field]) as cursor:
for row in cursor:
target_values[row[0]] = row[1]
# 遍历结果要素类中的每个目标要素
with arcpy.da.UpdateCursor(result, ["TARGET_FID", target_field]) as cursor:
for row in cursor:
target_fid = row[0]
value_to_add = target_values[target_fid]
if row[1] is not None and row[1] != "":
# 如果目标要素已经有值,则将新值添加到已有值的末尾
value_to_add = "{}/{}".format(row[1], value_to_add)
cursor.updateRow([target_fid, value_to_add])
# 完成工具的输出
arcpy.SetParameter(4, result)
```
你可以通过 ArcGIS 工具箱向该脚本中添加输入参数和输出参数,以便在 ArcGIS 中使用该工具。
阅读全文