4

I've been breaking my head the greater part of the day on getting correct output from longitude and latitude data. I know this data is correct by plotting it in google maps. For example I will show the data for these lat and long coordinates: Longitude: 4.905396 Latitude: 52.364643

When plotted in google maps it shows the correct location which is Amsterdam in The Netherlands: PLotting location of amsterdam on gmaps

Now when I plot this in my application it looks as follows: selfmade app showing location

And yes google maps is a 2D representation and mine is 3D but this was just to prove that the longitude and latitude values are correct so if my formula of converting them to 3D space is correct they should appear on the same spot.

I've been reading through a lot of articles on the web as well as stackoverflow itself. I've been through these articles and applied all of the different types of formula's I saw:

Convert Latitude and Longitude to point in 3D space

and

3D coordinates on a sphere to Latitude and Longitude

These two seemed to have the cleared explanation with good examples but nothing seems to work. What I am doing right now is calculating the position in 3D space and then normalizing it and using it as a direction to draw a line with a dot on the end as you can see in the screenshot of my application. This doesn't differ of just drawing a line from the center to the calculated position when using the radius of the earth which is used in most examples.

My current formula is:

X = r * cos(latitude) * cos(lontitude);
Y = r * cos(latitude) * sin(longitude);
Z = r * sin(latitude);

Which is wrong I know but Ive went through all the examples I saw on the web and none showed me correct data. By testing it with data from google maps I know that at least the input is correct.

Update1:

I checked the topology of my texture, this is what happens when its latitude and longitude are 0.0000 and 0.0000: Latitude and longitude check with 0.0000 and 0.0000

So this means that this is wrong already but its off with an odd offset, as google maps shows it it should be like this: google maps latitude and longitude 0.0000, 0.0000

The green arrow is the 0.0000, 0.0000 mark. So mine is off that's obvious, its off to the side. I'm thinking it might ( dont think/hope so ) be the texture, I'm using this one: http://naturalearth.springercarto.com/ne3_data/16200/textures/2_no_clouds_16k.jpg

Which I obtained from: Shadedrelief Natural Earth textures

Update 2: I've checked the topology of my sphere with texture, it seems to be off for 0.0 and 0.0 but I'm also doubting my formula. I've tried a lot of different ones from the web and some of them get the same 0.0 0.0 as my first formula but a lot don't. My longitude and latitude are in degrees with the -180~180 and -90~90.

Update 3: This is the view of a plot on amsterdam, its off a little bit, just like the 0.0 0.0 lat/long piont. Somewhere either the map or calculations are wrong I guess. I'll try adapting the texture to be shifted a little bit. almost fixed

Community
  • 1
  • 1
Yonathan
  • 1,253
  • 1
  • 17
  • 29
  • 7
    Have you checked the texture coordinates to make sure your texture is in the right place? – Reed Copsey Jul 16 '12 at 20:28
  • 2
    To align the two - use lat/long = 0,0 and make the texture coincide with the expected point inside your modeling tool (or UV mapping code if you're creating the sphere via code). After that all your other lat/long's should be good. – Ani Jul 16 '12 at 20:32
  • We need to see how you're texturing your sphere, as well as the orientation of your texture and your texture loader. – Nicol Bolas Jul 16 '12 at 20:56
  • Added screenshots of the 0.0, 0.0 spot. It is indeed off but by some weird margins. The texturing is straight out of maya, I generate a sphere add a lambert material with the texture. I also rotated it 180° because the 0.0, 0.0 spot was on the other side of the planet, 180° helped a bit but its still off as you can see. – Yonathan Jul 17 '12 at 05:22
  • My 0, 0 is a bit off with the original green arrow. When I add the amsterdam location however it appears on the other side of the globe so my calculation is still wrong. – Yonathan Jul 17 '12 at 12:13
  • 2
    How can google show `0, 0` next to brazil? `0, 0` is obviously on the equator and right under England. Your 3d picture with `0, 0` looks pretty correct to me! – Shahbaz Jul 17 '12 at 13:37
  • Well when I add another location, say amsterdam, it appears on a weird position. Nowhere close to amsterdam. – Yonathan Jul 17 '12 at 14:02
  • 1
    Try googling "Geodetic to ECEF" – TreyA Jul 17 '12 at 15:44
  • please update the fact that the (0,0) point in google map is the green arrow... it nows looks as if they did this wrong (the A mark is obviously wrong) – meduz Jul 17 '12 at 20:12

2 Answers2

7

You must first have your angles in radians, not degrees.

Then check: which axis is up? Your formulas are consistent with Z being up (north pole) and X out of the screen (you have Earth rotated to see 0,0)

However it's more common to have a view rendered with Y up and Z out of the screen.

Check how the texture coordinates are generated to see which axis is used with sin.

Erik Olson
  • 1,154
  • 8
  • 18
  • I'll be damned, I just drew it out on paper and you are 100% correct. Seems I had them wrong indeed, I fixed them now. I had to adjust the rotation of my globe a bit in Maya but that's fine, now everything aligns correctly. Many thanks! – Yonathan Jul 17 '12 at 19:41
1

not been mentioned so far but make sure your angles passed to cos and sin are in radians and not degrees http://en.cppreference.com/w/cpp/numeric/math/cos

JonMacey
  • 11
  • 2