# java - converting polar to cartesian

###
#1
Posted 06 April 2008 - 09:00 AM

I am new to GIS, but I am programming an application in java for which I need to convert polar coordinates to cartesian.

I have implemented the equation found here http://www.ordnances...ordinates3D.pdf

I am confused as to how I should be entering the longitude, latitude coordinates into the equation. For example should they be decimal, dm, dms etc? do they need to be converted to radians? From there I need to convert from WGS84 to OSGB36 and then to eastings northings.

Apologies for the silly, newbie question.

Jessica

###
#2
Posted 06 April 2008 - 09:08 AM

I am new to GIS, but I am programming an application in java for which I need to convert polar coordinates to cartesian.

I have implemented the equation found here http://www.ordnances...ordinates3D.pdf

I am confused as to how I should be entering the longitude, latitude coordinates into the equation. For example should they be decimal, dm, dms etc? do they need to be converted to radians? From there I need to convert from WGS84 to OSGB36 and then to eastings northings.

For ease of calculation, I would recommend decimal degrees.

Hope this helps!

**Hans van der Maarel - Cartotalk Editor**

**Red Geographics**

**Email: hans@redgeographics.com / Twitter: @redgeographics**

###
#3
Posted 06 April 2008 - 09:44 AM

I am new to GIS, but I am programming an application in java for which I need to convert polar coordinates to cartesian.

I have implemented the equation found here http://www.ordnances...ordinates3D.pdf

I am confused as to how I should be entering the longitude, latitude coordinates into the equation. For example should they be decimal, dm, dms etc? do they need to be converted to radians? From there I need to convert from WGS84 to OSGB36 and then to eastings northings.

For ease of calculation, I would recommend decimal degrees.

Hope this helps!

Thanks for your quick reply. I don't need to convert to radians then?

I can't understand why it isnt working... I am using lat: 52.653929 lon: 1.722809 height: 24.700 and I get

x: 1033695.60797645

y: -6747602.88998933

z: 6356769.207352724

but according to an online converter I should get XYZ: 3875613.7744, 116569.7603, 5047273.7641

I can't see anything wrong with my code... maybe you can see something?

double phi = X; //latitude double lambda = Y; //longitude double H = Z; //height in m double a = 6378137.000; //semi major axis length for WGS84 double b = 6356752.3141; //semi minor axis length double e2 = ((a * a) - (b * b)) / (a * a); //eccentricity System.out.println("e2: " + e2); double v = a / Math.sqrt((1 - e2) * sin2(phi)); System.out.println(v); double x = (v + H) * Math.cos(phi) * Math.cos(lambda); //latitude double y = (v + H) * Math.cos(phi) * Math.sin(lambda); //longitude double z = ((1 - e2) * v + H) * Math.sin(phi); //height

Jessica

###
#4
Posted 06 April 2008 - 12:13 PM

Thanks for your quick reply. I don't need to convert to radians then?

I can't understand why it isnt working... I am using lat: 52.653929 lon: 1.722809 height: 24.700 and I get

x: 1033695.60797645

y: -6747602.88998933

z: 6356769.207352724

but according to an online converter I should get XYZ: 3875613.7744, 116569.7603, 5047273.7641

I can't see anything wrong with my code... maybe you can see something?double phi = X; //latitude double lambda = Y; //longitude double H = Z; //height in m double a = 6378137.000; //semi major axis length for WGS84 double b = 6356752.3141; //semi minor axis length double e2 = ((a * a) - (b * b)) / (a * a); //eccentricity System.out.println("e2: " + e2); double v = a / Math.sqrt((1 - e2) * sin2(phi)); System.out.println(v); double x = (v + H) * Math.cos(phi) * Math.cos(lambda); //latitude double y = (v + H) * Math.cos(phi) * Math.sin(lambda); //longitude double z = ((1 - e2) * v + H) * Math.sin(phi); //height

Jessica

Hmmm...

Trigonometry was never my strongest subject in school... I manually calculated the X and got to something that was 'close' to what your online converter said. Close as in: I got the first 4 significant digits right (and in the right order even!). Don't know how much of that can be attributed to rounding errors.

2 things I'd check:

- The sample in the PDF mentions using the Airy ellipsoid, whereas you're using WGS84. That may be causing a difference

- The code looks okay, but do check whether the math functions use radians or degrees.

Other than that, all I can say is get out your calculator and manually calculate each step as you're going along, comparing it with the output of your code.

**Hans van der Maarel - Cartotalk Editor**

**Red Geographics**

**Email: hans@redgeographics.com / Twitter: @redgeographics**

###
#5
Posted 06 April 2008 - 01:43 PM

Thanks for your quick reply. I don't need to convert to radians then?

I can't understand why it isnt working... I am using lat: 52.653929 lon: 1.722809 height: 24.700 and I get

x: 1033695.60797645

y: -6747602.88998933

z: 6356769.207352724

but according to an online converter I should get XYZ: 3875613.7744, 116569.7603, 5047273.7641

I can't see anything wrong with my code... maybe you can see something?double phi = X; //latitude double lambda = Y; //longitude double H = Z; //height in m double a = 6378137.000; //semi major axis length for WGS84 double b = 6356752.3141; //semi minor axis length double e2 = ((a * a) - (b * b)) / (a * a); //eccentricity System.out.println("e2: " + e2); double v = a / Math.sqrt((1 - e2) * sin2(phi)); System.out.println(v); double x = (v + H) * Math.cos(phi) * Math.cos(lambda); //latitude double y = (v + H) * Math.cos(phi) * Math.sin(lambda); //longitude double z = ((1 - e2) * v + H) * Math.sin(phi); //height

Jessica

Hmmm...

Trigonometry was never my strongest subject in school... I manually calculated the X and got to something that was 'close' to what your online converter said. Close as in: I got the first 4 significant digits right (and in the right order even!). Don't know how much of that can be attributed to rounding errors.

2 things I'd check:

- The sample in the PDF mentions using the Airy ellipsoid, whereas you're using WGS84. That may be causing a difference

- The code looks okay, but do check whether the math functions use radians or degrees.

Other than that, all I can say is get out your calculator and manually calculate each step as you're going along, comparing it with the output of your code.

It turns out sin() and cos() expect radians! It now *sort of* works, like your result, it is correct to 4 significant figures.

And you are correct about the two ellipsoids. Although their example uses Airy, the coordinates that I will be performing the calculation on use the WGS84 datum. For the purposes of the example though I changed it to Airy, but I think it should work fine on WGS84 coordinates if I just change the ellipsoid...

Thank you for your advice.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users