将LLA转换为XYZ

时间:2013-09-12 08:49:09

标签: coordinates wgs84

你能帮我把lla转换成xyz coordnates。

我正在使用

earthRadius = 6378.137;
var x = earthRadius * Math.cos(lat)*Math.cos(lon);
var y = earthRadius * Math.cos(lat)*Math.sin(lon);
var z = earthRadius * Math.sin(lat);

此方法转换为xyz坐标。但它没有给出我想要的正确结果。 地球被定义为wgs84对象。

2 个答案:

答案 0 :(得分:1)

我解决了我的问题

Lat Long to X Y Z position in JS .. not working

这个公式是正确的。感谢Stephen Quan回答这个问题。

答案 1 :(得分:1)

完成坐标转换(python代码):

geodetic -> ECEF(固定地心),距地心的距离

ECEF -> ENU(东北),以获取局部典型的x-y轨迹:

from __future__ import print_function
import math
import pyproj
R = 6378137
f_inv = 298.257224
f = 1.0 / f_inv
e2 = 1 - (1 - f) * (1 - f)

coords = [
  (0,  45,  1000),
  (45,  90,  2000),
  (48.8567,  2.3508,  80),
  (61.4140105652, 23.7281341313,149.821),
]

def gps_to_ecef_pyproj(lat, lon, alt):
    ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84')
    lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84')
    x, y, z = pyproj.transform(lla, ecef, lon, lat, alt, radians=False)

    return x, y, z

def gps_to_ecef(latitude, longitude, altitude):
    # (lat, lon) in WSG-84 degrees
    # h in meters
    cosLat = math.cos(latitude * math.pi / 180)
    sinLat = math.sin(latitude * math.pi / 180)

    cosLong = math.cos(longitude * math.pi / 180)
    sinLong = math.sin(longitude * math.pi / 180)

    c = 1 / math.sqrt(cosLat * cosLat + (1 - f) * (1 - f) * sinLat * sinLat)
    s = (1 - f) * (1 - f) * c

    x = (R*c + altitude) * cosLat * cosLong
    y = (R*c + altitude) * cosLat * sinLong
    z = (R*s + altitude) * sinLat

    return x, y, z

# ecef2enu
def ecef_to_enu(x, y, z, latRef, longRef, altRef):

    cosLatRef = math.cos(latRef * math.pi / 180)
    sinLatRef = math.sin(latRef * math.pi / 180)

    cosLongRef = math.cos(longRef * math.pi / 180)
    sinLongRef = math.sin(longRef * math.pi / 180)

    cRef = 1 / math.sqrt(cosLatRef * cosLatRef + (1 - f) * (1 - f) * sinLatRef * sinLatRef)

    x0 = (R*cRef + altRef) * cosLatRef * cosLongRef
    y0 = (R*cRef + altRef) * cosLatRef * sinLongRef
    z0 = (R*cRef*(1-e2) + altRef) * sinLatRef

    xEast = (-(x-x0) * sinLongRef) + ((y-y0)*cosLongRef)

    yNorth = (-cosLongRef*sinLatRef*(x-x0)) - (sinLatRef*sinLongRef*(y-y0)) + (cosLatRef*(z-z0))

    zUp = (cosLatRef*cosLongRef*(x-x0)) + (cosLatRef*sinLongRef*(y-y0)) + (sinLatRef*(z-z0))

    return xEast, yNorth, zUp

def geodetic_to_enu(lat, lon, h, lat_ref, lon_ref, h_ref):
    x, y, z = gps_to_ecef(lat, lon, h)

    return ecef_to_enu(x, y, z, lat_ref, lon_ref, h_ref)


def run_test():

    for pt in coords:

    xPy,yPy,zPy = gps_to_ecef_pyproj(pt[0], pt[1], pt[2])   
    xF,yF,zF = gps_to_ecef(pt[0], pt[1], pt[2])

        print("pyproj (XYZ)\t = ", xPy, yPy, zPy)
        print("ECEF (XYZ)\t = ", xF, yF, zF)

    xE, yN, zU = ecef_to_enu(xF,yF,zF,pt[0], pt[1], pt[2])
        print('ENU (XYZ) \t = ', xE, yN, zU)

    print("-------------------------------------------------")


run_test()

geodetic -> ECEFECEF -> ENU之间的区别如下图所示:

enter image description here

以及当地的东,北坐标系: enter image description here