写一个arcgis工具箱脚本:连接要素中多个图斑叠加目标要素其中一个图斑时,取连接要素中多个图斑面积大于多少值的图斑利用空间连接赋值到目标要素图斑里,多个图斑值赋值一个字段时用"/"隔开,最终导出成果
时间: 2023-12-03 22:44:05 浏览: 82
arcgis中根据excel坐标表批量生产shp图形
3星 · 编辑精心推荐
下面是一个简单的 ArcGIS 工具箱脚本,可以实现你所描述的功能:
```python
import arcpy
# 定义工具箱参数
input_features = arcpy.Parameter(
displayName="输入要素",
name="input_features",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
overlay_features = arcpy.Parameter(
displayName="叠加要素",
name="overlay_features",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
overlay_area_threshold = arcpy.Parameter(
displayName="叠加面积阈值",
name="overlay_area_threshold",
datatype="GPLong",
parameterType="Required",
direction="Input")
output_feature_class = arcpy.Parameter(
displayName="输出要素类",
name="output_feature_class",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
value_field = arcpy.Parameter(
displayName="值字段",
name="value_field",
datatype="Field",
parameterType="Required",
direction="Input")
# 设置参数属性
value_field.parameterDependencies = [overlay_features.name]
# 定义工具函数
def process_overlay(input_features, overlay_features, overlay_area_threshold, output_feature_class, value_field):
# 创建空间连接对象
spatial_ref = arcpy.Describe(input_features).spatialReference
spatial_rel = "INTERSECT"
join_type = "KEEP_ALL"
field_mapping = arcpy.FieldMappings()
for field in arcpy.ListFields(input_features):
if field.name != value_field:
field_mapping.addTable(input_features)
fm = arcpy.FieldMap()
fm.addInputField(input_features, field.name)
field_mapping.addFieldMap(fm)
join_operation = "JOIN_ONE_TO_MANY"
match_option = "COMPLETELY_CONTAINS"
search_radius = None
distance_field_name = None
join_data = arcpy.SpatialJoin_analysis(
target_features=overlay_features,
join_features=input_features,
out_feature_class="in_memory/join_data",
join_operation=join_operation,
join_type=join_type,
field_mapping=field_mapping,
match_option=match_option,
search_radius=search_radius,
distance_field_name=distance_field_name)
# 根据面积阈值选择叠加要素
overlay_sel = arcpy.SelectLayerByAttribute_management(
join_data,
"NEW_SELECTION",
"{} >= {}".format(arcpy.Describe(join_data).shapeFieldName, overlay_area_threshold))
# 聚合值字段
value_dict = {}
with arcpy.da.SearchCursor(overlay_sel, [arcpy.Describe(overlay_features).oidFieldName, value_field]) as cursor:
for row in cursor:
key = row[0]
value = row[1]
if key in value_dict:
value_dict[key].append(value)
else:
value_dict[key] = [value]
# 更新目标要素的值字段
with arcpy.da.UpdateCursor(input_features, [arcpy.Describe(input_features).oidFieldName, value_field]) as cursor:
for row in cursor:
key = row[0]
if key in value_dict:
values = value_dict[key]
row[1] = "/".join(values)
cursor.updateRow(row)
# 导出结果
arcpy.CopyFeatures_management(
input_features,
output_feature_class,
spatial_grid_1="0.001 DecimalDegrees",
spatial_grid_2="0.001 DecimalDegrees",
spatial_grid_3="0.001 DecimalDegrees")
# 运行工具
if __name__ == '__main__':
process_overlay(
input_features.valueAsText,
overlay_features.valueAsText,
overlay_area_threshold.value,
output_feature_class.valueAsText,
value_field.valueAsText)
```
这个脚本包含了以下几个步骤:
1. 将输入要素和目标要素进行空间连接,得到连接要素。
2. 根据面积阈值选择连接要素中的图斑。
3. 聚合连接要素中每个图斑的值字段。
4. 将聚合后的值字段更新到目标要素中。
5. 导出最终结果。
你可以将这段代码保存为一个 .py 文件,然后在 ArcGIS 中创建一个新的工具箱,在其中添加一个 Python 工具,并将这个 .py 文件作为工具的脚本文件。然后,为工具箱中的参数设置正确的属性,就可以使用这个工具进行处理了。
阅读全文