关于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远程控制MySQL的完整指南

《Python远程控制MySQL的完整指南》MySQL是最流行的关系型数据库之一,Python通过多种方式可以与MySQL进行交互,下面小编就为大家详细介绍一下Python操作MySQL的常用方法和最... 目录1. 准备工作2. 连接mysql数据库使用mysql-connector使用PyMySQL3.

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

在Java中将XLS转换为XLSX的实现方案

《在Java中将XLS转换为XLSX的实现方案》在本文中,我们将探讨传统ExcelXLS格式与现代XLSX格式的结构差异,并为Java开发者提供转换方案,通过了解底层原理、性能优势及实用工具,您将掌握... 目录为什么升级XLS到XLSX值得投入?实际转换过程解析推荐技术方案对比Apache POI实现编程

Python FastMCP构建MCP服务端与客户端的详细步骤

《PythonFastMCP构建MCP服务端与客户端的详细步骤》MCP(Multi-ClientProtocol)是一种用于构建可扩展服务的通信协议框架,本文将使用FastMCP搭建一个支持St... 目录简介环境准备服务端实现(server.py)客户端实现(client.py)运行效果扩展方向常见问题结

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061