MapMath

CHAPTER 27

ECEF and 3D coordinate systems

Earth-Centered Earth-Fixed coordinates, converting lat/lon/altitude to XYZ, the ENU local frame, and 3D distances.

3 min read

Most of the formulas in this guide project the Earth onto a flat surface. ECEF doesn't. It's a 3D Cartesian system with the origin at Earth's centre — the coordinate system GPS hardware and 3D rendering engines actually use.

Geodetic → ECEF coordinates

XZorigin31.5°N, 74.4°E
X1,467,186.248 m
Y5,240,742.265 m
Z3,315,282.16 m

ECEF — Earth-Centered, Earth-Fixed

ECEF places the origin at Earth's centre of mass. The axes are fixed to the Earth and rotate with it:

  • X-axis: points to the intersection of the equator and prime meridian (0°N, 0°E)
  • Y-axis: points to 0°N, 90°E
  • Z-axis: points to the North Pole

Units are metres. At Lahore (31.52°N, 74.36°E, ≈220 m):

X1,186,000 m,Y4,249,000 m,Z3,309,000 mX \approx 1{,}186{,}000 \text{ m}, \quad Y \approx 4{,}249{,}000 \text{ m}, \quad Z \approx 3{,}309{,}000 \text{ m}

Geodetic → ECEF

Given latitude φ\varphi, longitude λ\lambda, and altitude hh (metres above the ellipsoid):

N(φ)=a1e2sin2φN(\varphi) = \frac{a}{\sqrt{1 - e^2 \sin^2 \varphi}} X=(N+h)cosφcosλX = (N + h) \cos\varphi \cos\lambda Y=(N+h)cosφsinλY = (N + h) \cos\varphi \sin\lambda Z=(N(1e2)+h)sinφZ = \bigl(N(1 - e^2) + h\bigr) \sin\varphi

where a=6,378,137a = 6{,}378{,}137 m, e2=0.00669437999014e^2 = 0.00669437999014 (WGS84).

const A  = 6_378_137.0;
const E2 = 0.00669437999014;

function geodeticToECEF(latDeg, lonDeg, altM = 0) {
  const lat = latDeg * Math.PI / 180;
  const lon = lonDeg * Math.PI / 180;
  const N = A / Math.sqrt(1 - E2 * Math.sin(lat) ** 2);
  return {
    x: (N + altM) * Math.cos(lat) * Math.cos(lon),
    y: (N + altM) * Math.cos(lat) * Math.sin(lon),
    z: (N * (1 - E2) + altM) * Math.sin(lat),
  };
}

ECEF → Geodetic

The inverse has no closed form — use Bowring's iterative method or Zhu's formula:

function ecefToGeodetic(x, y, z) {
  const p = Math.sqrt(x * x + y * y);
  const lon = Math.atan2(y, x);

  // Bowring iteration
  let lat = Math.atan2(z, p * (1 - E2));
  for (let i = 0; i < 5; i++) {
    const N  = A / Math.sqrt(1 - E2 * Math.sin(lat) ** 2);
    lat = Math.atan2(z + E2 * N * Math.sin(lat), p);
  }
  const N = A / Math.sqrt(1 - E2 * Math.sin(lat) ** 2);
  const alt = p / Math.cos(lat) - N;

  return { lat: lat * 180 / Math.PI, lon: lon * 180 / Math.PI, alt };
}

ENU — the local frame

optional — skip if familiarrefresher

ENU stands for East-North-Up. It's a local Cartesian frame centred on a reference point. East (+X), North (+Y), and Up (+Z) are defined relative to that point on Earth's surface — making it intuitive for local navigation, drone control, and AR overlays. Unlike ECEF, ENU axes change as you move to a different reference point.

ECEF is a global system, but for local navigation you want a frame with East, North, Up axes anchored to a specific point. ENU gives you that.

Chapter 27 · Paid content

Continue reading "ECEF and 3D coordinate systems"

You've reached the end of the free preview. Unlock all 22 paid chapters, including distance math, bearings, polygons, spatial indexing, and 3D map rendering — plus a downloadable PDF and the companion code repo.

  • All 22 paid chapters with worked examples
  • Downloadable PDF for offline reading
  • Companion GitHub repo (JavaScript + Python)
  • Free updates for life

Multiple payment options including Wise, PayPal, and bank transfer.