关于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正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Python如何精准判断某个进程是否在运行

《Python如何精准判断某个进程是否在运行》这篇文章主要为大家详细介绍了Python如何精准判断某个进程是否在运行,本文为大家整理了3种方法并进行了对比,有需要的小伙伴可以跟随小编一起学习一下... 目录一、为什么需要判断进程是否存在二、方法1:用psutil库(推荐)三、方法2:用os.system调用

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

一文带你搞懂Python中__init__.py到底是什么

《一文带你搞懂Python中__init__.py到底是什么》朋友们,今天我们来聊聊Python里一个低调却至关重要的文件——__init__.py,有些人可能听说过它是“包的标志”,也有人觉得它“没... 目录先搞懂 python 模块(module)Python 包(package)是啥?那么 __in