# -*- coding: utf-8 -*-"""将矩形方框转换成中心线上均匀分布的 5 个圆圆半径:0.175"""import arcpyimport os# 配置INPUT_GDB = r'C:\Users\yl\Documents\ArcGIS\Projects\MyProject18\MyProject18.gdb'INPUT_FC = os.path.join(INPUT_GDB, 'line')OUTPUT_FC = os.path.join(INPUT_GDB, 'c')CIRCLE_RADIUS = 0.175NUM_CIRCLES = 5def create_circle_at_point(x, y, radius, spatial_ref):"""在指定位置创建圆""" # 使用 buffer 创建圆point = arcpy.PointGeometry(arcpy.Point(x, y), spatial_ref) circle = point.buffer(radius)return circledef process_feature(geom, oid):"""处理单个要素,返回圆的几何列表"""circles = []# 获取边界范围extent = geom.extent xmin, xmax = extent.XMin, extent.XMax ymin, ymax = extent.YMin, extent.YMax# 计算中心线 Y 坐标center_y = (ymin + ymax) / 2# 计算 5 个圆的 X 坐标# 第 1 个圆:在左边缘中心 (xmin) # 第 5 个圆:在右边缘中心 (xmax) # 第 2-4 个圆:在中间均匀分布(将剩余距离 4 等分)x_positions = []# 5 个圆,将矩形分成 4 等份step = (xmax - xmin) / 4for i in range(NUM_CIRCLES): x = xmin + (step * i) x_positions.append(x)for x in x_positions: circles.append((x, center_y))return circlesdef main():"""主函数"""print(f"输入要素类:{INPUT_FC}") print(f"输出要素类:{OUTPUT_FC}") print(f"圆半径:{CIRCLE_RADIUS}") print(f"每行圆数量:{NUM_CIRCLES}") print()# 获取空间参考desc = arcpy.Describe(INPUT_FC) spatial_ref = desc.spatialReference print(f"空间参考:{spatial_ref.name}")# 读取所有输入要素print("\n读取输入要素...") all_circles = []with arcpy.da.SearchCursor(INPUT_FC, ['SHAPE@', 'OID@']) as cursor:for row in cursor: geom, oid = row circle_positions = process_feature(geom, oid)for x, y in circle_positions: all_circles.append((x, y)) print(f"输入要素数:{len(list(arcpy.da.SearchCursor(INPUT_FC, ['OID@'])))}") print(f"将创建圆数量:{len(all_circles)}")# 创建输出要素类print(f"\n创建输出要素类:{OUTPUT_FC}")# 如果已存在则删除if arcpy.Exists(OUTPUT_FC): arcpy.Delete_management(OUTPUT_FC) print(f" 已删除已存在的要素类")# 创建多边形要素类arcpy.CreateFeatureclass_management( out_path=INPUT_GDB, out_name='c', geometry_type='POLYGON', spatial_reference=spatial_ref ) print(f" 要素类已创建")# 添加字段arcpy.AddField_management(OUTPUT_FC, 'ParentOID', 'LONG') arcpy.AddField_management(OUTPUT_FC, 'CircleID', 'LONG') arcpy.AddField_management(OUTPUT_FC, 'Radius', 'DOUBLE') print(f" 字段已添加")# 插入圆print("\n插入圆...")with arcpy.da.InsertCursor(OUTPUT_FC, ['SHAPE@', 'ParentOID', 'CircleID', 'Radius']) as cursor: circle_id = 0with arcpy.da.SearchCursor(INPUT_FC, ['SHAPE@', 'OID@']) as input_cursor:for row in input_cursor: geom, oid = row circle_positions = process_feature(geom, oid)for i, (x, y) in enumerate(circle_positions, 1): circle_id += 1circle_geom = create_circle_at_point(x, y, CIRCLE_RADIUS, spatial_ref) cursor.insertRow([circle_geom, oid, i, CIRCLE_RADIUS])if circle_id % 10 == 0: print(f" 已创建 {circle_id} 个圆...")# 统计结果output_count = int(arcpy.GetCount_management(OUTPUT_FC)[0]) print(f"\n[OK] 完成!") print(f" 输出要素数:{output_count}") print(f" 输出文件:{OUTPUT_FC}")if __name__ == '__main__': main()

夜雨聆风