openclaw可以完成线在一定容差范围自动闭合生成面
#!/usr/bin/env python3"""线转面工具 - 端点自动连接后构面1. 提取所有线的端点(起点和终点)2. 查找距离小于容差的端点对3. 在端点对之间添加连接线4. 使用 FeatureToPolygon 将闭合的线转换为面"""import arcpyimport osimport sysimport mathdef connect_endpoints_and_polygon(input_lines, output_polygons, tolerance=30):"""端点自动连接后构面 参数: input_lines: 输入线要素类路径 output_polygons: 输出面要素类路径 tolerance: 端点连接容差(米) """ # 设置控制台编码if sys.platform == "win32":import io sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') print(f"[开始] 端点连接构面处理") print(f" 输入:{input_lines}") print(f" 输出:{output_polygons}") print(f" 容差:{tolerance} 米")# 设置环境arcpy.env.overwriteOutput = True# 检查输入是否存在if not arcpy.Exists(input_lines): print(f"[错误] 输入文件不存在:{input_lines}")return False# 获取空间参考desc = arcpy.Describe(input_lines) spatial_ref = desc.spatialReference print(f" 坐标系:{spatial_ref.name}")# 检查是否是地理坐标系if spatial_ref.type == "Geographic": print(f"[警告] 输入数据是地理坐标系(度单位),容差需要转换") tolerance = tolerance / 111000.0print(f" 转换后容差:{tolerance:.6f} 度")# 步骤 1:提取所有线的端点print(f"\n[步骤 1] 提取线端点...") endpoints = [] # [(x, y, line_oid, is_start), ...]with arcpy.da.SearchCursor(input_lines, ["OID@", "SHAPE@"]) as cursor:for row in cursor: oid = row[0] geom = row[1]if geom and geom.pointCount > 0: first_point = geom.firstPoint last_point = geom.lastPoint endpoints.append((first_point.X, first_point.Y, oid, True)) endpoints.append((last_point.X, last_point.Y, oid, False)) print(f" 共提取 {len(endpoints)} 个端点")# 步骤 2:查找距离小于容差的端点对print(f"\n[步骤 2] 查找距离小于 {tolerance} 的端点对...") connections = [] # [(x1, y1, x2, y2), ...]for i in range(len(endpoints)):for j in range(i + 1, len(endpoints)): x1, y1, oid1, _ = endpoints[i] x2, y2, oid2, _ = endpoints[j]# 计算距离dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)if dist < tolerance and dist > 0:# 检查是否已经连接过(避免重复)already_connected = False for conn in connections:if ((abs(conn[0] - x1) < 0.001 and abs(conn[1] - y1) < 0.001 andabs(conn[2] - x2) < 0.001 and abs(conn[3] - y2) < 0.001) or(abs(conn[0] - x2) < 0.001 and abs(conn[1] - y2) < 0.001 andabs(conn[2] - x1) < 0.001 and abs(conn[3] - y1) < 0.001)): already_connected = True break if not already_connected: connections.append((x1, y1, x2, y2)) print(f" 发现连接:端点{i}({oid1}) <-> 端点{j}({oid2}), 距离={dist:.2f}") print(f" 共找到 {len(connections)} 对需要连接的端点")# 步骤 3:创建连接线要素temp_connections = os.path.join(os.path.dirname(output_polygons), "temp_connections")if len(connections) > 0: print(f"\n[步骤 3] 创建连接线...") arcpy.management.CreateFeatureclass( os.path.dirname(output_polygons),"temp_connections","POLYLINE", spatial_reference=spatial_ref )with arcpy.da.InsertCursor(temp_connections, ["SHAPE@"]) as cursor:for x1, y1, x2, y2 in connections: line = arcpy.Polyline( arcpy.Array([arcpy.Point(x1, y1), arcpy.Point(x2, y2)]), spatial_ref ) cursor.insertRow([line]) print(f" 连接线已创建")else: temp_connections = Noneprint(f"\n[步骤 3] 无需创建连接线")# 步骤 4:合并原始线和连接线print(f"\n[步骤 4] 合并线要素...") temp_merged = os.path.join(os.path.dirname(output_polygons), "temp_merged")if temp_connections and arcpy.Exists(temp_connections): arcpy.management.Merge([input_lines, temp_connections], temp_merged) print(f" 已合并原始线和连接线")else: arcpy.CopyFeatures_management(input_lines, temp_merged) print(f" 仅使用原始线")# 步骤 5:线转面print(f"\n[步骤 5] 将闭合线转换为面...") arcpy.FeatureToPolygon_management(temp_merged, output_polygons)# 检查结果if arcpy.Exists(output_polygons): count = int(arcpy.GetCount_management(output_polygons)[0]) print(f"\n[成功] 生成 {count} 个面要素") print(f" 输出:{output_polygons}")# 清理临时文件print(f"\n[清理] 删除临时文件...")if temp_connections and arcpy.Exists(temp_connections): arcpy.Delete_management(temp_connections) print(f" 已删除:{temp_connections}")if arcpy.Exists(temp_merged): arcpy.Delete_management(temp_merged) print(f" 已删除:{temp_merged}")return True else: print(f"[错误] 输出文件未生成")return Falseif __name__ == "__main__":if len(sys.argv) >= 4: input_lines = sys.argv[1] output_polygons = sys.argv[2] tolerance = float(sys.argv[3])else: input_lines = r"C:\Users\yl\Documents\ArcGIS\Projects\MyProject12\MyProject12.gdb\line"output_polygons = r"C:\Users\yl\Documents\ArcGIS\Projects\MyProject12\MyProject12.gdb\pp"tolerance = 30.0success = connect_endpoints_and_polygon(input_lines, output_polygons, tolerance)if success: print(f"\n[完成] 端点连接构面完成!")else: print(f"\n[失败] 端点连接构面失败!") sys.exit(1)

夜雨聆风