首页 > 编程语言 >GPS转化ECEF坐标系(0) python和c++代码

GPS转化ECEF坐标系(0) python和c++代码

时间:2023-01-10 19:56:24浏览次数:42  
标签:ECEF phi python cos c++ ref sin math lambda

https://www.cxyzjd.com/article/taiyang1987912/112982150

 

import math

a = 6378137
b = 6356752.3142
f = (a - b) / a
e_sq = f * (2-f)
pi = 3.14159265359


'''
功能: # gps转化到大地坐标系ECEF
输入:
等待转换的GPS   坐标 lat, lon, h
输出:
ecef  坐标 x, y, z 
'''

def GPSwgs84ToEcef(lat, lon, h):
    # (lat, lon) in degrees
    # h in meters
    lamb = math.radians(lat)
    phi = math.radians(lon)
    s = math.sin(lamb)
    N = a / math.sqrt(1 - e_sq * s * s)

    sin_lambda = math.sin(lamb)
    cos_lambda = math.cos(lamb)
    sin_phi = math.sin(phi)
    cos_phi = math.cos(phi)

    x = (h + N) * cos_lambda * cos_phi
    y = (h + N) * cos_lambda * sin_phi
    z = (h + (1 - e_sq) * N) * sin_lambda

    XYZ=[x,y,z]
    return XYZ


'''
功能: # 大地坐标系 转化到GPS第一帧为原点的本地ENU坐标系
输入:
等待转换的ecef       坐标 x, y, z 
作为原点的GPS第一帧   坐标lat0, lon0, h0
输出:
本地第一帧GPS为原点的 ENU 坐标系 xEast, yNorth, zUp

'''
def GPSecef_to_enu(x, y, z, lat0, lon0, h0):
    lamb = math.radians(lat0)
    phi = math.radians(lon0)
    s = math.sin(lamb)
    N = a / math.sqrt(1 - e_sq * s * s)

    sin_lambda = math.sin(lamb)
    cos_lambda = math.cos(lamb)
    sin_phi = math.sin(phi)
    cos_phi = math.cos(phi)

    x0 = (h0 + N) * cos_lambda * cos_phi
    y0 = (h0 + N) * cos_lambda * sin_phi
    z0 = (h0 + (1 - e_sq) * N) * sin_lambda

    xd = x - x0
    yd = y - y0
    zd = z - z0

    t = -cos_phi * xd -  sin_phi * yd

    xEast = -sin_phi * xd + cos_phi * yd
    yNorth = t * sin_lambda  + cos_lambda * zd
    zUp = cos_lambda * cos_phi * xd + cos_lambda * sin_phi * yd + sin_lambda * zd

    return xEast, yNorth, zUp

'''
功能: enu坐标转化到ecef坐标
输入:
等待转换的ENU坐标   坐标 xEast, yNorth, zUp
GPS第一帧原点      坐标 lat0, lon0, h0
输出:
ecef  坐标 x, y, z 
'''

def enu_to_ecef(xEast, yNorth, zUp, lat0, lon0, h0):
    lamb = math.radians(lat0)
    phi = math.radians(lon0)
    s = math.sin(lamb)
    N = a / math.sqrt(1 - e_sq * s * s)

    sin_lambda = math.sin(lamb)
    cos_lambda = math.cos(lamb)
    sin_phi = math.sin(phi)
    cos_phi = math.cos(phi)

    x0 = (h0 + N) * cos_lambda * cos_phi
    y0 = (h0 + N) * cos_lambda * sin_phi
    z0 = (h0 + (1 - e_sq) * N) * sin_lambda

    t = cos_lambda * zUp - sin_lambda * yNorth

    zd = sin_lambda * zUp + cos_lambda * yNorth
    xd = cos_phi * t - sin_phi * xEast 
    yd = sin_phi * t + cos_phi * xEast

    x = xd + x0 
    y = yd + y0 
    z = zd + z0 

    return x, y, z

'''
功能: # 大地坐标系ECEF转化到gps
输入:
等待转换的ecef  坐标 x, y, z 
输出:
GPS   坐标 lat, lon, h

'''
def ecef_to_geodetic(x, y, z):
   # Convert from ECEF cartesian coordinates to 
   # latitude, longitude and height.  WGS-84
    x2 = x ** 2 
    y2 = y ** 2 
    z2 = z ** 2 

    a = 6378137.0000    # earth radius in meters
    b = 6356752.3142    # earth semiminor in meters 
    e = math.sqrt (1-(b/a)**2) 
    b2 = b*b 
    e2 = e ** 2 
    ep = e*(a/b) 
    r = math.sqrt(x2+y2) 
    r2 = r*r 
    E2 = a ** 2 - b ** 2 
    F = 54*b2*z2 
    G = r2 + (1-e2)*z2 - e2*E2 
    c = (e2*e2*F*r2)/(G*G*G) 
    s = ( 1 + c + math.sqrt(c*c + 2*c) )**(1/3) 
    P = F / (3 * (s+1/s+1)**2 * G*G) 
    Q = math.sqrt(1+2*e2*e2*P) 
    ro = -(P*e2*r)/(1+Q) + math.sqrt((a*a/2)*(1+1/Q) - (P*(1-e2)*z2)/(Q*(1+Q)) - P*r2/2) 
    tmp = (r - e2*ro) ** 2 
    U = math.sqrt( tmp + z2 ) 
    V = math.sqrt( tmp + (1-e2)*z2 ) 
    zo = (b2*z)/(a*V) 

    height = U*( 1 - b2/(a*V) ) 
    
    lat = math.atan( (z + ep*ep*zo)/r ) 

    temp = math.atan(y/x) 
    if x >=0 :    
        long = temp 
    elif (x < 0) & (y >= 0):
        long = pi + temp 
    else :
        long = temp - pi 

    lat0 = lat/(pi/180) 
    lon0 = long/(pi/180) 
    h0 = height 

    return lat0, lon0, h0

'''
功能: # gps直接转化到enu坐标系
       相对于指定GPS_ref为原点(一般都是第一帧)的enu坐标系
输入:
等待转换的GPS   坐标 lat, lon, h
参考原点GPS     坐标 lat_ref, lon_ref, h_ref
输出:
enu坐标 x, y, z

'''
def geodetic_to_enu(lat, lon, h, lat_ref, lon_ref, h_ref):

    x, y, z = geodetic_to_ecef(lat, lon, h)
    
    return ecef_to_enu(x, y, z, lat_ref, lon_ref, h_ref)
'''
功能: # enu直接转化到gps坐标系
       相对于指定GPS_ref为原点(一般都是第一帧)的enu坐标系
输入:
等待转换的enu   坐标 xEast, yNorth, zUp
参考原点GPS     坐标 lat_ref, lon_ref, h_ref
输出:
GPS   坐标 lat, lon, h

'''
def enu_to_geodetic(xEast, yNorth, zUp, lat_ref, lon_ref, h_ref):

    x,y,z = enu_to_ecef(xEast, yNorth, zUp, lat_ref, lon_ref, h_ref)

    return ecef_to_geodetic(x,y,z)

  

标签:ECEF,phi,python,cos,c++,ref,sin,math,lambda
From: https://www.cnblogs.com/gooutlook/p/17041240.html

相关文章

  • python读取大文件
    """每次固定读取10000行"""withopen(filename)asf:whileTrue:next10k=list(islice(f,10000))#needlisttodolen,3linesdownfor......
  • 【python小课堂专栏】python小课堂24 - 正则表达式(二)
    python小课堂24-正则表达式(二)前言今天继续来介绍一下python的正则表达式,回顾一下上次介绍的re模块整篇文章围绕着re.findall()来进行实例的讲解,也就是所谓的查询操作。......
  • C++获取CPU信息
    #include"windows.h" #include"iostream"#include"string"usingnamespacestd;//用来存储信息DWORDdeax;DWORDdebx;DWORDdecx;DWORDdedx;voidExeCPUID(DWORD......
  • python:reshape()函数
    a.reshape(m,n)表示将原有数组a转化为一个m行n列的新数组,a自身不变。m与n的乘积等于数组中的元素总数reshape(m,n)中参数m或n其中一个可写为"-1","-1"的作用在于计算机根据......
  • python 使用函数名的字符串调用函数(4种方法)
    先看一个例子:>>>deffoo():print"foo">>>defbar():print"bar">>>func_list=["foo","bar"]>>>forfuncinfunc_list:func()TypeError......
  • pycharm:无法加载文件 C:\Users\admin\PycharmProjects\pythonProject1\venv\Scr
    以前一直在vmware虚机上用pycharm,这次想在win10pc上试试 安装pycharm后,打开终端直接报错:无法加载文件C:\Users\admin\PycharmProjects\pythonProject1\venv\Scripts......
  • 用python做个简单的监控
    今天在看博客园的时候看到一篇文章,忘记是什么地址了,之前我也做过一个类似的监控,不过不好控制。之前的模式是通过内网地址访问相应的php程序,php调用python程序,能捕获到摄像......
  • python里的__call__()方法
    解释__call__方法是Python中类的特殊方法,当一个类的实例被“调用”时,就会自动触发这个方法。“调用”一个类的实例就是使用小括号()操作符。举个例子:classAdder:......
  • C/C++ 异常处理机制(例:文件拷贝)
    异常是一种程序控制机制,与函数机制互补。函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它可以在出现“意外”时中断当前函数,并以某种机制......
  • python pip 安装报错 (公司网络需要设置代理)
    python pip安装失败 1.打开“文件资源管理器”,地址栏输入“%USERPROFILE%”,回车键打开当前登录用户目录2.新建文件夹“pip”3.打开新建的“pip”文件夹,新建文件......