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?
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?
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.
If you care about getting coordinates based on an ellipsoid rather than a sphere, take a look at http://en.wikipedia.org/wiki/Geodetic_system#From_geodetic_to_ECEF - it gives the formulae as well as the WGS84 constants you need for the conversion.
The formulae there also take into account the altitude relative to the reference ellipsoid surface (useful if you are getting altitude data from a GPS device).
You can do it this way on Java.
Theory for convert
GPS(WGS84)
to Cartesian coordinates https://en.wikipedia.org/wiki/Geographic_coordinate_conversion#From_geodetic_to_ECEF_coordinatesThe following is what I am using:
I attached a VB code I wrote:
Please notice that the
h
is altitude above theWGS 84 ellipsoid
.Usually
GPS
will give usH
of aboveMSL
height. TheMSL
height has to be converted to heighth
above theWGS 84 ellipsoid
by using the geopotential modelEGM96
(Lemoine et al, 1998).This is done by interpolating a grid of the geoid height file with a spatial resolution of 15 arc-minutes.
Or if you have some level professional
GPS
has AltitudeH
(msl,heigh above mean sea level) andUNDULATION
,the relationship between thegeoid
and theellipsoid (m)
of the chosen datum output from internal table. you can geth = H(msl) + undulation
To XYZ by Cartesian coordinates:
Here's the answer I found:
Just to make the definition complete, in the Cartesian coordinate system:
The conversion is:
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:
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,