Post · 2 min read· pp. 62–63

ecef2geodetic

ahrs.common.frames.ecef2geodetic(x: float, y: float, z: float, a: float = 6378137.0, b: float = 6356752.3142) → ndarray : Transform cartesian coordinates in ECEF-frame to Geodetic Coordinates [ZHP11].

code
Given the Cartesian coordinates (xyz)\begin{pmatrix}x & y & z\end{pmatrix}
in the Earth-Centered Earth-Fixed (ECEF) frame, we can start by computing
the geodetic longitude λ\lambda:

λ=arctan2(x, y)\lambda = \mathrm{arctan2}\left(x, \,y\right)
Then, we iteratively compute the geodetic latitude ϕ\phi using the initial estimation:
ϕ0=arctan2(z, (1−e2)p)\phi_0 = \mathrm{arctan2}\big(z, \,(1-e^2)p \big)
with p=x2+y2p = \sqrt{x^2 + y^2}. Now we iterate until the difference between two consecutive latitudes (ϕi\phi_i and ϕi−1\phi_{i-1}) is smaller than a given threshold δ\delta. Each iteration updates the values as follows:
N←a1−e2sin⁡2(ϕi−1)ϕi←arctan2(z+e2Nsin⁡(ϕi−1), p)\begin{split}\begin{array}{rcl} N & \leftarrow & \frac{a}{\sqrt{1 - e^2 \sin^2(\phi_{i-1})}} \\ \phi_i & \leftarrow & \mathrm{arctan2}\big(z+e^2N\sin(\phi_{i-1}), \,p\big) \end{array}\end{split}
where NN is the radius of curvature in the vertical prime, and e2e^2 is the square of the first eccentricity of the ellipsoid.
e2=a2−b2a2e^2 = \frac{a^2-b^2}{a^2}
The value of δ\delta is empirically found to perform well when set to 10−810^{-8} in this implementation. The altitude (height) hh is computed as:
h=pcos⁡ϕ−Nh = \frac{p}{\cos\phi} - N
Note The altitude hh has an accuracy up to 0.1 m (10 cm), which is enough for most applications. The latitude and longitude are returned in degrees. The altitude is returned in meters. Parameters: : - **x** (*float*) – ECEF x-coordinate, in meters. - **y** (*float*) – ECEF y-coordinate, in meters. - **z** (*float*) – ECEF z-coordinate, in meters. - **a** (*float, default: 6378137.0*) – Ellipsoid’s equatorial radius, in meters. Defaults to Earth’s. - **b** (*float, default: 6356752.3142*) – Ellipsoid’s polar radius, in meters. Defaults to Earth’s. Returns: : **lla** – Geodetic coordinates [latitude, longitude, altitude]. Return type: : numpy.ndarray Examples ```default

from ahrs.common.frames import ecef2geodetic x = 4_201_000 y = 172_460 z = 4_780_100 ecef2geodetic(x, y, z) array([48.85616162, 2.35079383, 67.37006803]) ```