views:

5445

answers:

3

I have a earth coordinates given as longitude and latitude (WSG-84).

How you i convert them to Cartesian coordinates (x,y,z) with the origin at the center of the earth?

+2  A: 

Here's the answer I found:

Just to make the definition complete, in the Cartesian coordinate system:

  • the x-axis goes through long,lat (0,0), so longitude 0 meets the equator;
  • the y-axis goes through (0,90);
  • and the z-axis goes through the poles.

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. 6371KM).

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,

Daphna Shezaf
-1 This is wrong. You are assuming the earth is a sphere, while WGS-84 assumes an ellipsoid.
starblue
+2  A: 

Why implement something which has already been implemented and test-proven?

C#, for one, has the NetTopologySuite which is the .NET port of the JTS Topology Suite.

Specifically, you have a severe flaw in your calculation. The earth is not a perfect sphere, and the approximation of the earth's radius might not cut it for precise measurements.

If in some cases it's acceptable to use homebrew functions, GIS is a good example of a field in which it is much preferred to use a reliable, test-proven library.

Yuval A
+1. Using a reliable library is more accurate than a homebrew function and also **easier**.
MarkJ
+4  A: 

I have recently done something similar to this using the "Haversine Formula" on WGS-84 data, which is a derivative of the "Law of Haversines" with very satisfying results.

Yes, WGS-84 assumes the Earth is an ellipsoid, but I believe you only get about a 0.5% average error using an approach like the "Haversine Formula", which may be an acceptable amount of error in your case. You will always have some amount of error unless you're talking about a distance of a few feet and even then there is theoretically curvature of the Earth... If you require more rigidly WGS-84 compatible approach checkout the "Vincenty Formula."

I understand where starblue is coming from, but good software engineering is often about trade offs, so it all depends on the accuracy you require for what you are doing. For example, the result calculated from "Manhattan Distance Formula" versus the result from the "Distance Formula" can be better for certain situations as it is computationally less expensive. Think "which point is closest?" scenarios where you don't need a precise distance measurement.

Regarding, the "Haversine Formula" it is easy to implement and is nice because it is uses "Spherical Trigonometry" instead of a "Law of Cosines" based approach which is based on two-dimensional trigonometry, therefore you get a nice balance of accuracy over complexity.

A gentlemen by the name of Chris Veness has a great website at http://www.movable-type.co.uk/scripts/latlong.html that explains some the concepts you are interested in and demonstrates various programmatic implementations; this should answer your x/y conversion question as well.

bn
0.5% error - 0.5% of what? In the context of this question it could be the radius of the earth, so 0.5% could be 30 km :)
MarkJ
Checked out your link. The 0.5% quote is for error in the great-circle distance between two points so not strictly relevant to this question. I would think when converting lat-long to Cartesian coordinates with the origin at the centre of the earth, the errors from assuming a spherical earth could be significant. It's not clear what the questionner wants to **do** with the Cartesian coordinates. Either it's just more convenient to work in them for some bizarre reason, or perhaps it's some requirement for data export? If the latter, accuracy would be important.
MarkJ

related questions