9

On my previous question, it was advised I convert lat/lon/altitude to spherical or Cartesian coordinates. I'm not working near the poles and it would be safe to assume a spherical earth. What would be the best way to go about this with the minimal amount of operations? Would it be best to use x/y/z or phi/theta/rho? I'm working on a small microcontroller, with no hardware FPU (software FPU only!) so every cycle counts.

Thomas O
  • 2,053
  • 3
  • 15
  • 11
  • Using a spherical model introduces huge errors in the Cartesian coordinates (relative to the range of topographic elevations on the earth), because the ellipsoid has about a 23 km vertical deviation from the spheroid in places. Make sure your application can tolerate such errors. – whuber Dec 04 '10 at 19:01
  • @whuber: These errors are tolerable. – Thomas O Dec 04 '10 at 19:14

1 Answers1

17

Note that "Lat/Lon/Alt" is just another name for spherical coordinates, and phi/theta/rho are just another name for latitude, longitude, and altitude. :) (A minor difference: altitude is usually measured from the surface of the sphere; rho is measured from the center -- to convert, just add/subtract the radius of the sphere.)

To convert phi/theta/rho to cartesian x/y/z where (0,0,0) is the center of the earth and rho is the distance from the center:

    # python
    x = math.cos(phi) * math.cos(theta) * rho
    y = math.cos(phi) * math.sin(theta) * rho
    z = math.sin(phi) * rho # z is 'up'

(Note there's some slightly arbitrary choices here in what each axis means... you might want 'y' to point at the north pole instead of 'z', for example.)

The inverse, and a lot more information, can be found at Wikipedia: http://en.wikipedia.org/wiki/Spherical_coordinate_system

Dan S.
  • 3,549
  • 17
  • 20
  • Quick note that (a) I obviously copy'n'pasted this from some python code I had sitting around, and (b) should have named 'alt' to be 'rho' for consistency. – Dan S. Dec 03 '10 at 23:10
  • 1
    you may want to edit your post for clarity instead of including it in a comment if you think it helps understanding. – scw Dec 04 '10 at 00:53
  • @scw: good call, and done. – Dan S. Dec 04 '10 at 05:07
  • 2
    @Dan On a spherical earth, 'rho' is another name for radius of the earth PLUS altitude, not the altitude alone. – whuber Dec 04 '10 at 19:00
  • @whuber True, which is why I said "as measured from (0,0,0)" Think I should edit to make that more explicit? – Dan S. Dec 07 '10 at 17:43
  • @Dan I see that now. It didn't register with me because I fixated on "altitude", which almost always means height above a reference surface. That's my mistake but others might make the same mistake too. If you're willing, you might consider modifying the code snippet to replace 'rho' by '(rho + height)'. – whuber Dec 07 '10 at 18:46
  • @whuber: Added some clarification as well. – Dan S. Dec 07 '10 at 19:53