社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

ArcGIS Pro 四棱锥生成python代码

GISAI • 10 月前 • 349 次点击  

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

  • 底面是正方形,边长400

  • 高度500

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

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

import arcpy
import 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.0
h = 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, DAP
faces = [
        [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])
# 底面ABCD
arr = 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三维窗口查看


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





Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/191727