有DLTB图层,有面积字段SHAPE_Area,要求面积小于5000,连续消除,生成新的数据,写python?

文摘   2024-09-05 07:55   新加坡  

DLTB图层,有面积字段SHAPE_Area,要求面积小于5000,和周围的数据合并,如果处理后还有小于5000,继续,生成新的数据,写python

import arcpy
arcpy.env.overwriteOutput = True
# 定义输入和输出的要素类路径
input_fc = r"DLTB"  # 你的 DLTB 图层路径
output_fc = r"merged_DLTB"  # 输出的新的图层路径

# 创建一个副本用于处理
arcpy.management.CopyFeatures(input_fc, output_fc)

# 定义消除的阈值面积(单位为平方米)
threshold = 5000

# 创建一个函数来递归消除小于5000平方米的多边形
def eliminate_small_polygons(fc, area_field, threshold):
    while True:
        # 使用 SelectLayerByAttribute 创建一个选择集,选择面积小于阈值的多边形
        arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", f"{area_field} < {threshold}")
        
        # 检查是否有要消除的小多边形
        count = int(arcpy.management.GetCount(fc)[0])
        if count == 0:
            break  # 如果没有小于阈值的多边形,结束循环
        
        # 使用 Eliminate 工具消除这些小多边形
        arcpy.management.Eliminate(fc, "my","LENGTH")
        
        arcpy.management.CopyFeatures("my", output_fc)

# 调用函数,处理面积小于5000平方米的多边形
eliminate_small_polygons(output_fc, "SHAPE_Area", threshold)


GISAI
ArcGIS培训和二次开发
 最新文章