关于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

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了