大疆导出三维模型里面有个models\pc\0\terra_grid\csv\terra_grid.csv文件,里面记录所有点的坐标和高程,但坐标是经纬度坐标,需要转化为大地2000坐标。
我参照了:经纬度坐标转换为CGCS2000大地坐标系对应XY值(PYTHON实现)_经纬度转换大地2000坐标工具-CSDN博客和百度ai提供的代码:
import ezdxf
doc = ezdxf.new() # 创建新 DXF 文档
msp = doc.modelspace() # 获取模型空间
# 添加点
msp.add_point((100, 200, 0), dxfattribs={'layer': 'Roads'})
# 添加线
points = [(150, 300), (200, 400)]
msp.add_lwpolyline(points, dxfattribs={'layer': 'Buildings'})
doc.saveas('output.dxf')
改写了一个能从csv文件转化为dxf文件的程序:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 19 10:38:48 2025
@author: YBK
"""
import math
import ezdxf
import csv
doc = ezdxf.new() # 创建新 DXF 文档
msp = doc.modelspace() # 获取模型空间
def GaussToBLToGauss(longitude, latitude):
output = [0.0, 0.0]
longitude1, latitude1, longitude0, X0, Y0, xval, yval = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
# Constants
iPI = 0.0174532925199433 # 3.1415926535898/180.0;
a = 6378137.0
f = 1 / 298.257222101 # CGCS2000坐标系参数
# a=6378137.0; f=1/298.2572236; #wgs84坐标系参数
longitude0 = 117.0 # 中央子午线 根据实际进行配置
longitude0 = longitude0 * iPI # 中央子午线转换为弧度
longitude1 = longitude * iPI # 经度转换为弧度
latitude1 = latitude * iPI # 纬度转换为弧度
e2 = 2 * f - f * f
ee = e2 * (1.0 - e2)
NN = a / math.sqrt(1.0 - e2 * math.sin(latitude1) * math.sin(latitude1))
T = math.tan(latitude1) * math.tan(latitude1)
C = ee * math.cos(latitude1) * math.cos(latitude1)
A = (longitude1 - longitude0) * math.cos(latitude1)
M = a * ((1 - e2 / 4 - 3 * e2 * e2 / 64 - 5 * e2 * e2 * e2 / 256) * latitude1 - (3 * e2 / 8 + 3 * e2 * e2 / 32 + 45 * e2 * e2
* e2 / 1024) * math.sin(2 * latitude1)
+ (15 * e2 * e2 / 256 + 45 * e2 * e2 * e2 / 1024) * math.sin(4 * latitude1) - (35 * e2 * e2 * e2 / 3072) * math.sin(6 * latitude1))
xval = NN * (A + (1 - T + C) * A * A * A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * ee) * A * A * A * A * A / 120)
yval = M + NN * math.tan(latitude1) * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24
+ (61 - 58 * T + T * T + 600 * C - 330 * ee) * A * A * A * A * A * A / 720)
X0 = 500000
Y0 = 0
xval = xval + X0
yval = yval + Y0
# 转换为投影
output[0] = xval
output[1] = yval
return output
try:
with open('terra_grid.csv', 'r', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
# Example usage:
longitude_input = float(row[0])
latitude_input = float(row[1])
hight_imput = float(row[2]) - 14.81798172 # 高程校对 - 14.81798172
result = GaussToBLToGauss(longitude_input, latitude_input)
msp.add_point((result[0], result[1], hight_imput), dxfattribs={'layer': 'dji_gcd'})
print(f"Converted Coordinates: X = {result[0]}, Y = {result[1]}")
except FileNotFoundError:
print("错误:文件不存在!")
except UnicodeDecodeError:
print("错误:文件编码非 UTF-8,请尝试 'encoding='gbk'' 或 'latin-1'")
except Exception as e:
print(f"未知错误: {e}")
doc.saveas('output0319.dxf')
用时记得修改中央子午线值和高程校对值。