Convert latitude and longitude to ECEF coordinates system

Crazy picture Crazy · Oct 20, 2013 · Viewed 20.8k times · Source

I am studying pArk Apple sample code, and how it is works. anyone knows how convert the latitude and longitude to ECEF coordinates, and Covert ECEF to ENU coordinates centered at given lat, lon functions are work? I just want to understand what is going on in this function!

thanks.

void latLonToEcef(double lat, double lon, double alt, double *x, double *y, double *z)
{   
    double clat = cos(lat * DEGREES_TO_RADIANS);
    double slat = sin(lat * DEGREES_TO_RADIANS);
    double clon = cos(lon * DEGREES_TO_RADIANS);
    double slon = sin(lon * DEGREES_TO_RADIANS);

    double N = WGS84_A / sqrt(1.0 - WGS84_E * WGS84_E * slat * slat);

    *x = (N + alt) * clat * clon;
    *y = (N + alt) * clat * slon;
    *z = (N * (1.0 - WGS84_E * WGS84_E) + alt) * slat;
}
// Coverts ECEF to ENU coordinates centered at given lat, lon
void ecefToEnu(double lat, double lon, double x, double y, double z, double xr, double yr, double zr, double *e, double *n, double *u)
{
    double clat = cos(lat * DEGREES_TO_RADIANS);
    double slat = sin(lat * DEGREES_TO_RADIANS);
    double clon = cos(lon * DEGREES_TO_RADIANS);
    double slon = sin(lon * DEGREES_TO_RADIANS);
    double dx = x - xr;
    double dy = y - yr;
    double dz = z - zr;

    *e = -slon*dx  + clon*dy;
    *n = -slat*clon*dx - slat*slon*dy + clat*dz;
    *u = clat*clon*dx + clat*slon*dy + slat*dz;
}

Answer

Rob picture Rob · Oct 20, 2013

The latLonToEcef method is an implementation of the algorithm outlined in the Geographic coordinate conversion - From geodetic to ECEF coordinates wikipedia page:

geodetic to ecef

where

Φ is latitude, λ is longitude, and

n-latitude

Likewise the ecefToEnu method is an implementation of the ECEF to ENU algorithm:

ECEF to ENU

If you need further references, they can be found at the bottom of that Wikipedia page. You might also refer to the World Geodetic System 1984 spec.