I have some earth-centered coordinate points given as latitude and longitude (WGS-84).
How can i convert them to Cartesian coordinates (x,y,z) with the origin at the center of the earth?
Here's the answer I found:
Just to make the definition complete, in the Cartesian coordinate system:
The conversion is:
x = R * cos(lat) * cos(lon) y = R * cos(lat) * sin(lon) z = R *sin(lat)
Where R is the approximate radius of earth (e.g. 6371 km).
If your trigonometric functions expect radians (which they probably do), you will need to convert your longitude and latitude to radians first. You obviously need a decimal representation, not degrees\minutes\seconds (see e.g. here about conversion).
The formula for back conversion:
lat = asin(z / R) lon = atan2(y, x)
asin is of course arc sine. read about atan2 in wikipedia. Don’t forget to convert back from radians to degrees.
This page gives c# code for this (note that it is very different from the formulas), and also some explanation and nice diagram of why this is correct,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With