关于ENU与LLA坐标系互相转换的python代码

2024-08-24 13:44

本文主要是介绍关于ENU与LLA坐标系互相转换的python代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于ENU与LLA坐标系互相转换的python代码

import math# WGS84 ellipsoid parameters
WGS_PARAMS = {'a': 6378137,'f': 1 / 298.257223563,'b': 6378137 * (1 - 1 / 298.257223563),
}
wgs84_a = 6378137
wgs84_b = 6378137 * (1 - 1 / 298.257223563)
wgs84_f = 1 / 298.257223563
# wgs84_f = 1.0 / 298.257223563
pow_e_2 = wgs84_f * (2.0 - wgs84_f)
def deg2radian(deg):return (deg * math.pi) / 180.0
def radian2deg(rad):return (rad * 180.0) / math.pi
def wgs2ecef(wgs):lng_r = deg2radian(wgs['lng'])lat_r = deg2radian(wgs['lat'])a = WGS_PARAMS['a']b = WGS_PARAMS['b']N = a ** 2 / math.sqrt(a ** 2 * math.cos(lat_r) ** 2 + b ** 2 * math.sin(lat_r) ** 2)x = (N + wgs['alt']) * math.cos(lat_r) * math.cos(lng_r)y = (N + wgs['alt']) * math.cos(lat_r) * math.sin(lng_r)z = (N * (b / a) ** 2 + wgs['alt']) * math.sin(lat_r)return {'x': x, 'y': y, 'z': z}def ecef2wgs(x, y, z):a = WGS_PARAMS['a']b = WGS_PARAMS['b']e = math.sqrt(a ** 2 - b ** 2)r = math.sqrt(x ** 2 + y ** 2 + z ** 2)u = math.sqrt(0.5 * (r ** 2 - e ** 2) + 0.5 * math.sqrt((r ** 2 - e ** 2) ** 2 + 4 * e ** 2 * z ** 2))Q = math.hypot(x, y)huE = math.hypot(u, e)Beta = math.atan((huE / u) * (z / Q))eps = ((b * u - a * huE + e ** 2) * math.sin(Beta)) / ((a * huE * 1) / math.cos(Beta) - e ** 2 * math.cos(Beta))Beta += epslat = math.atan((a / b) * math.tan(Beta))lng = math.atan2(y, x)alt = math.hypot(z - b * math.sin(Beta), Q - a * math.cos(Beta))inside = (x ** 2 / a ** 2 + y ** 2 / a ** 2 + z ** 2 / b ** 2) < 1if inside:alt = -altlat = radian2deg(lat)lng = radian2deg(lng)return lat, lng, altdef enu2uvw(east, north, up, lng0, lat0):lng_r = deg2radian(lng0)lat_r = deg2radian(lat0)t = math.cos(lat_r) * up - math.sin(lat_r) * northw = math.sin(lat_r) * up + math.cos(lat_r) * northu = math.cos(lng_r) * t - math.sin(lng_r) * eastv = math.sin(lng_r) * t + math.cos(lng_r) * eastreturn {'u': u, 'v': v, 'w': w}def enu2ecef(enu, wgs0):ecef0 = wgs2ecef(wgs0)uvw = enu2uvw(enu['x'], enu['y'], enu['z'], wgs0['lng'], wgs0['lat'])return {'x': ecef0['x'] + uvw['u'],'y': ecef0['y'] + uvw['v'],'z': ecef0['z'] + uvw['w'],}def enu2wgs(enu, wgs0):ecef = enu2ecef(enu, wgs0)return ecef2wgs(ecef['x'], ecef['y'], ecef['z'])def pos2ecef(lon0, lat0, alt0):# print('pos2ecef', lon, lat, alt)lon = math.radians(float(lon0))lat = math.radians(float(lat0))alt = float(alt0)cosLon = math.cos(lon)cosLat = math.cos(lat)sinLon = math.sin(lon)sinLat = math.sin(lat)N = wgs84_a / math.sqrt(1.0 - pow_e_2 * sinLat * sinLat)ecef_x = (N + alt) * cosLat * cosLonecef_y = (N + alt) * cosLat * sinLonecef_z = (N * (1.0 - pow_e_2) + alt) * sinLatprint('pos2ecef', lon0, lat0, alt0, '->', ecef_x, ecef_y, ecef_z)return (ecef_x, ecef_y, ecef_z)def pos2enu(lon, lat, alt, ref_lon, ref_lat, ref_alt):# lon0, lat0, alt0 = p0_lon, p0_lat, p0_altecef_x1, ecef_y1, ecef_z1 = pos2ecef(ref_lon, ref_lat, ref_alt)ecef_x0, ecef_y0, ecef_z0 = pos2ecef(lon, lat, alt)# offset_x, offset_y, offset_z = ecef_x1-ecef_x0, ecef_y1-ecef_y0, ecef_z1-ecef_z0offset_x, offset_y, offset_z = ecef_x0 - ecef_x1, ecef_y0 - ecef_y1, ecef_z0 - ecef_z1# array_delta = np.array( [[offset_x], [offset_y], [offset_z]], dtype=np.float64)# print(array_delta.reshape(1,3))# lon0 = math.radians(lon)# lat0 = math.radians(lat)lon0 = math.radians(ref_lon)lat0 = math.radians(ref_lat)cosLon = math.cos(lon0)cosLat = math.cos(lat0)sinLon = math.sin(lon0)sinLat = math.sin(lat0)x = -1 * sinLon * offset_x + cosLon * offset_yy = -1 * sinLat * cosLon * offset_x - 1 * sinLat * sinLon * offset_y + cosLat * offset_zz = cosLat * cosLon * offset_x + cosLat * sinLon * offset_y + sinLat * offset_zreturn (x, y, z)# array_S1 = np.array([-1 * sinLon,           cosLon,                 0], dtype=np.float64)# array_S2 = np.array([-1 * sinLat * cosLon,  -1 * sinLat *sinLon,    cosLat], dtype=np.float64)# array_S3 = np.array([cosLat * cosLon,       cosLat * sinLon,        sinLat], dtype=np.float64)# array_S = np.array([array_S1, array_S2, array_S3], dtype=np.float64)# v = np.dot(array_S, array_delta)# # print(v.reshape(1,3))# # print(v[0][0], v[1][0], v[2][0])# return (v[0][0], v[1][0], v[2][0])# Example usage
enu = {'x': -16.47, 'y': 8.667, 'z': 0}
wgs0 = {'lng': 121.5442525290947, 'lat': 31.229345976263158, 'alt': 0}
result = enu2wgs(enu, wgs0)
print(result)p0_lon, p0_lat, p0_alt = 121.5442525290947, 31.229345976263158, 0
p1_lon, p1_lat, p1_alt = result[1], result[0], result[2]
v1 = pos2enu(p1_lon, p1_lat, p1_alt, p0_lon, p0_lat, p0_alt)  # p0_lon, p0_lat, p0_alt为基准坐标
print(v1)

这篇关于关于ENU与LLA坐标系互相转换的python代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1102646

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚