Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-22-2008, 01:59 AM   #1
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default Converting Latitude and Longitude to X, Y, Z

Any Geodata visualization experts out here?

I'm working on an open-source 3D Flash mapping application. You can see and download all the source code here (including the LAT/LONG KML XML data): http://code.google.com/p/meadanglobe

I found the calculations for converting LATITUDE and LONGITUDE to X, Y, Z coordinates, yet I am just not getting accurate results! There are actually 2 issues:

1) the nodes do not appear to be in correct locations relative to each other (ie., Rome and Johannesburg are right next to each other, and San Francisco and New York City are about half way around the world from eachother!)

2) also, the nodes are not not in the right places (ie, New York City is at the South Pole!)

I assume that the second issue will be easier to solve (relative x, y, z adjustment) once the first issue is resolved -- which appears much more variant.

Anyone have any idea what is wrong here?

Here is the ActionScript that I am using:

xPos = (app.radius) * Math.cos(longitude) * Math.cos(latitude);
yPos = (app.radius) * Math.sin(longitude) * Math.cos(latitude);
zPos = (app.radius) * Math.sin(latitude);

Please help if you can ... !
miquael is offline   Reply With Quote
Old 03-22-2008, 02:22 AM   #2
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

there are three basic things you should be concerned about here.

1) the Math trig methods require Radians, not degrees. So convert your longitudes and latitudes to Radians (eq: PI * degree / 180)

2) the formula is actually taken from the spherical to cartesian conversion

x = ro * sin(phi) * cos(theta)
y = ro * sin(phi) * sin(theta)
z = ro * cos(theta)

phi can be considered latitude, and theta can be considered longitude, finally ro is radius.

3) lastly you have to consider what cartesion coordinate system you are in. There are multiple kinds, z forward, x forward, etc, And they can be left or right handed as well.

The previous method of conversion is for the standard conversion of spherical 0 180 phi pole to pole, over to the basic math z up right handed coordinate system. Well for starters the spherical system doesn't match earth, they stick the phi 0 degrees at the equator... and they go positive each direction (with designating north and south).

a pic of the standard continuous math cartesian coordinate system "Right handed Z up"
mapped to a lon/lat map of the earth, x forward is relatively where Nigeria's shore line is located.


so to convert to the z up right handed system you'd actually have to say...

What direction from 0 am I going... in the formula you have there you can only draw a quarter sphere (mainly the area in which Asia exists). from greenwich both Louisiana and Eastern India are 90 degrees away... just one is east, the other is west. They both happen to fall at 30 degrees north... Both locations will yield India and never yield Louisiana, unless you announce you've gone east or west. Same goes with north and south. Notice how Johannesburg South Africa and Rome were really close, well they both fall about the same distance from the equator, but one is north, the other south.

So lets first convert the longitude and latitude to radians, and then define it positive or negative by direction.
North is positive, south is negative
East is positive, west is negative

all longitudes are based where 0 degrees passes through England (like all maps have it), and latitude 90 north is the north pole, and latitude 90 south is the south pole.

now we can do this...
start with the z,
z = ro * sin(phi) //remember phi is latitude

there if you plug in all values of phi you range up and down the from north pole to south pole. (+1 to -1, for 90 degrees to -90 degrees)

x and y at 0 lat and 0 lon should yield x = 1, and y = 0. well sin(0) = 0, cos(0) = 1. Let's go with the circle/ellipse formula from polar coords to make this (trig * trig) which is a triginometric extension of the pythagorean theorem; so x is gonna consist of two cos's obviousily so that it remains 1.
x = ro * cos(phi) * cos(theta)

looking a lot like the formula you got... but we are considering the issue of north south east and west now. And we've converted them to radians.

what is y though? 0Lat and 0lon told us nothing. Lets try 0 lat and 90 lon, this should yield x = 0, y = 1. cos(0) = 1, sin(0) = 0, cos(90) = 0, sin(90) = 1. Y's formula has to yield a 1 * ro at the end. So lat must take the cos to stay 1, and lon must take the sin to stay 1.
y = ro * cos(phi) * sin(theta)

cross check amongst them all, make sure it works on all scenarios, like India and Louisiana from before (I assure you it does).

Your formula was right as long as you are converting to the coord system shown in the image above, you were putting in bad values.




... and hopefully now you understand how/why this all works.
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!

Last edited by lordofduct; 03-22-2008 at 03:07 AM.
lordofduct is offline   Reply With Quote
Old 03-22-2008, 03:08 AM   #3
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

excuse the amount of times I edited this. I wanted to the most important info to you early, but still explain it. I'm busy with other stuff as well though, so it took a little while.
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!
lordofduct is offline   Reply With Quote
Old 03-25-2008, 11:49 PM   #4
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

This helps alot, yet I am still not understanding all of this ... especially in the context I am working with.

I have made a step closer to success by converting the latitude and longitude to radians with this:

latitude = Math.PI * latitude / 180;
longitude = Math.PI * longitude / 180;

Now at least the nodes appear to be in an appropriate relative position to each other. For example, Portland, San Francisco, and New York City all seem about the right relative positions to each other. Yet they are all plotting over the ocean just east of South America--so the relative positioning is still off.

I will see what I can do with the rest of this information (having a hard time wrapping my head around these types of conversions). Concrete examples help me understand the most.



Which type of cartesian space is natural to Flash? From my experience, I believe that Flash operates with 0 degrees (x) on the right side of any circle (which in this case would also influence z). This is not "standard" orientation I believe, so some type of conversion is probably necessary.
miquael is offline   Reply With Quote
Old 03-25-2008, 11:51 PM   #5
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

Again, all the source code can be found here: http://code.google.com/p/meadanglobe
miquael is offline   Reply With Quote
Old 03-26-2008, 12:08 AM   #6
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

As you described above, I attempted:

y = ro * cos(phi) * sin(theta)

by this in ActionScript:

yPos = (app.radius) * Math.cos(longitude) * Math.sin(latitude);

(where before I had: yPos = (app.radius) * Math.sin(longitude) * Math.cos(latitude))

But this produced even worse results. (?)
miquael is offline   Reply With Quote
Old 03-26-2008, 12:19 AM   #7
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

In this case, Z is not up, it is forward. And I believe that 0 X is on the right. Is this then "Right handed Z forward"?
miquael is offline   Reply With Quote
Old 03-26-2008, 02:15 AM   #8
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

Since 0,0 is plotting in the middle of the Pacific (when it should be just off the coast of the Congo), it must be off by about 180.
miquael is offline   Reply With Quote
Old 03-26-2008, 02:27 AM   #9
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

FYI, for any geo experts, there is this related post on how to plot curve lines over a sphere: http://www.actionscript.org/forums/s...961#post722961
miquael is offline   Reply With Quote
Old 03-28-2008, 02:15 AM   #10
miquael
New Member
 
miquael's Avatar
 
Join Date: Apr 2004
Posts: 24
Default

okay, i solved it with this:

// convert lat/long to radians
latitude = Math.PI * latitude / 180;
longitude = Math.PI * longitude / 180;

// adjust position by radians
latitude -= 1.570795765134; // subtract 90 degrees (in radians)

// and switch z and y (since z is forward)
xPos = (app.radius) * Math.sin(latitude) * Math.cos(longitude);
zPos = (app.radius) * Math.sin(latitude) * Math.sin(longitude);
yPos = (app.radius) * Math.cos(latitude);
miquael is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using accurate Longitude and Latitude LiQuidShade ActionScript 2.0 0 02-14-2008 12:33 PM
How to plot locations base on longitude and Latitude? D-flyer ActionScript 2.0 4 10-29-2006 08:20 PM
Can we use latitude and longitude in flash mx 2004 @bc ActionScript 1.0 (and below) 4 05-20-2006 12:39 AM
Display Longitude Latitude of mouse position sdw135 ActionScript 1.0 (and below) 2 05-23-2004 04:41 PM
Latitude and Longitude... image ActionScript 1.0 (and below) 5 01-09-2003 06:33 PM


All times are GMT. The time now is 11:54 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.