当前位置:首页>python>ArcGIS Pro 四棱锥生成python代码

ArcGIS Pro 四棱锥生成python代码

  • 2026-10-21 13:28:08
ArcGIS Pro 四棱锥生成python代码

一个正四棱锥(四面金字塔),

  • 底面是正方形,边长400

  • 高度500

  • 共有4个侧面三角形+1个底面正方形(每个三角面是带Z的Polygon)。

下面给你自动创建数据库、要素类、插入4个侧面三角面和底面正方形的完整代码(EPSG:3857,底面中心(0,0)):

import arcpyimport os# 1. 路径参数gdb_path = r"C:\Your\Workspace\Path"   # 修改为你的文件夹gdb_name = "pyramid.gdb"fc_name = "PyramidPolyZ"gdb_full = os.path.join(gdb_path, gdb_name)fc_full = os.path.join(gdb_full, fc_name)# 2. 新建数据库和带Z的Polygon要素类if not arcpy.Exists(gdb_full):    arcpy.CreateFileGDB_management(gdb_path, gdb_name)sr = arcpy.SpatialReference(3857)if not arcpy.Exists(fc_full):    arcpy.CreateFeatureclass_management(        out_path=gdb_full, out_name=fc_name, geometry_type="POLYGON",        spatial_reference=sr, has_z="ENABLED")# 3. 四棱锥参数half = 400 / 2.0h = 500# 底面4点(顺时针)A = arcpy.Point(-half, -half, 0)B = arcpy.Point(half, -half, 0)C = arcpy.Point(half, half, 0)D = arcpy.Point(-half, half, 0)# 顶点(z=500,x=0,y=0)P = arcpy.Point(0, 0, h)# 4. 插入四个侧面三角形和底面正方形with arcpy.da.InsertCursor(fc_full, ["SHAPE@"]) as cursor:# 侧面ABC, BCP, CDP, DAPfaces = [        [A, B, P, A],        [B, C, P, B],        [C, D, P, C],        [D, A, P, D]    ]for f in faces:        arr = arcpy.Array(f)        poly = arcpy.Polygon(arr, sr, has_z=True)        cursor.insertRow([poly])# 底面ABCDarr = arcpy.Array([A, B, C, D, A])    poly = arcpy.Polygon(arr, sr, has_z=True)    cursor.insertRow([poly])print(f"已完成 {fc_full} 的四棱锥:4个侧面、1个底面Polygon(三维面)!")

用法说明

  • 底面中心:(0,0)

  • 底面边长:400,顶点高程z=500

  • 4个三角形侧面+1个底面正方形

  • 可直接在ArcGIS Pro三维窗口查看


如需调整棱锥位置、底面方向、高度等,或要其它多面体结构,随时补充!


最新文章

随机文章