View Question
Q: Using latitude, longitude, and altitude to determine distance ( Answered ,   4 Comments )
 Question
 Subject: Using latitude, longitude, and altitude to determine distance Category: Science > Instruments and Methods Asked by: radres-ga List Price: \$10.00 Posted: 07 Apr 2004 10:15 PDT Expires: 07 May 2004 10:15 PDT Question ID: 326655
 ```Given the latitude, longitude, and altitude above sea level of two points on the globe, what formula can be used to accurately determine the distance between these two points?``` Request for Question Clarification by leapinglizard-ga on 08 Apr 2004 01:22 PDT ```Are you asking for the straight-line distance between two points in space? In other words, do you want to know the length of a line segment whose endpoints are specified in polar coordinates?``` Clarification of Question by radres-ga on 08 Apr 2004 05:48 PDT ```Here's exactly what I'm trying to do: I have a programmable GPS device that can be set to take GPS reads as often as once every 5 seconds. The GPS read returned has latitude, longitude, and altitude. I want to write a program that when run while driving somewhere, will take a GPS read every 5 seconds and use the sum of the deltas between each 5 second read in order to determine distance traveled. If you think that the length of a line segment between two endpoints specified in polar coordinates would work for this task, then yes. If you don't think it's possible to determine distance via lat, long, and altitude at all, then please explain why not. As I understand it, the Great Circle method won't work because the earth is not a perfect sphere. There are difficulties in mapping the latitude and longitude to specific distances because at different points on the globe, the land is higher or lower so the actual distances can increase or decrease between different points with the same delta. In my own research, I've found many sites that describe the innaccurate Great Circle approach to distance calculations, but in my experience the actual results returned are too inaccurate to be used for my program. I have not been able to find a formula that takes into account altitude which is another parameter my device gives me and I believe may or may not make this program possible.``` Clarification of Question by radres-ga on 08 Apr 2004 09:57 PDT ```Here's a device that claims to be able to determine distance from GPS lat, long, and altitude data: http://www.garmin.com/products/forerunner201/```
 Subject: Re: Using latitude, longitude, and altitude to determine distance Answered By: leapinglizard-ga on 08 Apr 2004 10:46 PDT Rated:
 ```As long as the points along your path are spaced at short intervals -- and five seconds does seem like a reasonable sampling rate for an automobile trip -- the sum of the lengths of the line segments will yield a good approximation of the total distance traveled. You can improve your precision by increasing the sampling rate or by applying a smoothing procedure such as spline interpolation to the resulting points. I really wouldn't worry about it myself. In any case, you were interested in calculating the distance between two points specified by longitude, latitude, and altitude. These are known as polar coordinates, which are convenient on a globe, but for our purposes we want to know the equivalent Cartesian coordinates. Given the polar coordinates of a point p = (long, lat, alt) we calculate the equivalent Cartesian coordinates p = (x, y, z) as follows, where the asterisk denotes multiplication. x = alt * cos(lat) * sin(long) y = alt * sin(lat) z = alt * cos(lat) * cos(long) If you're interested in a geometric analysis of the polar-Cartesian conversion, see the following page. TopCoder match summary http://www.topcoder.com/index?t=statistics&c=srm180_prob You'll have to scroll down to the section headed "Satellites", where the conversion is described in the context of a computational problem. If you're familiar with trigonometry, you might find the spherical diagram helpful. (At the very bottom of the page is a headshot of yours truly.) Now, given a pair of points p0 = (x0, y0, z0) p1 = (x1, y1, z1) the distance between them is given by the Pythagorean Theorem extended to three dimensions. In the equation below, the caret ("^") denotes exponentiation. dist = sqrt( (x1-x0)^2 + (y1-y0)^2 + (z1-z0)^2 ) And that's it! So you can solve the problem using nothing more complicated than the sine, cosine, and square-root functions. If you find my answer lacking in any way, please give me a chance to address your concerns before you assign a rating. Cheers, leapinglizard``` Request for Answer Clarification by radres-ga on 08 Apr 2004 14:24 PDT ```Your answer sounds promising, but in order for it to be useful, I will need to know what units the distance calculated will be in. My phone natively reads lat/long in 1/100,000ths of a degree, and altitude in meters.``` Clarification of Answer by leapinglizard-ga on 08 Apr 2004 23:56 PDT ```It doesn't matter what units the angles -- longitude and latitude -- are measured in, since you just apply the sine and cosine functions, each of which returns a real number between -1 and +1. What's decisive is the unit of measure used for the altitude. If the altitude is measured in meters, the resulting distance will also be expressed in meters. You can then convert this to kilometers or miles or what have you. Is there anything else I can help you with? leapinglizard``` Request for Answer Clarification by radres-ga on 15 Apr 2004 07:23 PDT ```according to http://mathworld.wolfram.com/SphericalCoordinates.html the conversion from spherical to cartesian coordinates is as follows: x = alt * cos(lat) * sin(long) y = alt * sin(lat) * sin(long) z = alt * cos(long) are both formulas equivalent or is there a difference here?``` Clarification of Answer by leapinglizard-ga on 15 Apr 2004 10:08 PDT ```When applied to your problem, my formula will always yield the same result as the one derived by Eric Weisstein in his MathWorld article. However, the formula you have given above is incorrect. I'm afraid you've misinterpreted the symbols used by Weisstein. His answer is as follows. x = alt * cos(long) * sin(90 degr - lat) y = alt * sin(long) * sin(90 degr - lat) z = alt * cos(90 degr - lat) I have transcribed this from his equations (4) through (6). Weisstein defines the meaning of each symbol in the first paragraph of the article. Also note that Weisstein's x, y, and z axes are not oriented in the same way as mine, but this is merely a difference of notation. Thus, his coordinates are not in the same order as mine: his x is my z; his y is my x; his z is my y. When used in the context of the distance formula dist = sqrt( (x1-x0)^2 + (y1-y0)^2 + (z1-z0)^2 ) the order doesn't matter, so you'll get the same answer whether you use Weisstein's notation or mine. Cheers, leapinglizard``` Request for Answer Clarification by radres-ga on 19 Apr 2004 07:09 PDT ```I tried testing out this formula today. I got in my car with my GPS device and set the trip odometer. At the start of my trip, I had the following read: lat: 41.756192 N long: 87.967360 W alt: 192m I drove for about 0.45 miles. At the end of the trip I had the following read: lat: 41.758701 N long: 87.973307 W alt: 198 m I plugged the numbers into the equation you gave me, and I got 137.973 meters, or 0.0857 miles. Any thoughts on why this isn't working?``` Clarification of Answer by leapinglizard-ga on 19 Apr 2004 08:35 PDT ```How embarrassing! I forget to mention that the formulas given by Eric Weisstein and myself use the distance from the center of the earth and not the elevation above sea level. Of course, Weisstein uses the word "radius" instead of "altitude", and I should have realized that a GPS device would report distance from sea level. This is easily corrected by taking into account the distance between sea level and the center of the earth, which is about 6370 kilometers. Thus, before plugging your altitude reading into the formula, you should add 6,370,000 meters to it. Using the readings you have supplied above, I obtain a straight-line distance of about 567 meters between the beginning and end of your car trip. This is reasonably close to your own estimate. Any other imprecision I would put down to the car odometer, since GPS readings are much more consistent than any odometer. Also bear in mind that you are not traveling in a straight line when you take a car trip. If your calculator doesn't give you a result of 567 meters, make sure that you're using the trigonometric functions in "degree mode" and not "radian mode". If the functions only have a radian mode, convert degrees into radians with the following formula. rad = deg * 3.14159 / 180.0 Let me know if you're still having trouble. Cheers, leapinglizard``` Request for Answer Clarification by radres-ga on 21 Apr 2004 07:07 PDT ```Unfortunately, introducing the radius of the earth to the equation takes us back to square one with the Great Circle approach, where because the radius of the earth is different at different locations it is impossible to accurately calculate distance. In fact, your equation now gives results equivalent to the Great Circle formula (190 meters of altitude is not significant enough to affect the radius of 6370km). Is there any other way to accurately determine the distance between the two points given altitude?``` Clarification of Answer by leapinglizard-ga on 21 Apr 2004 10:22 PDT ```The radius of the earth does not vary so significantly that it would affect the precision of an application such as yours, where you are using commercial GPS readings at relatively close intervals to compute the length of an automobile trip. As good as it is, a GPS reading simply isn't precise enough that it can record distortions of distance caused by the earth's curvature, which are very slight indeed on the scale of your application. The mathematical model I used to derive the formulae assumes that the earth is a sphere, meaning that it has a constant radius. In principle, one could use the actual radii of the earth at the locations given by GPS. Once again, however, the error introduced by the GPS reading is far greater than any gains in precision to be obtained by this method. There is no measurable advantage to doing so. If you sense that the formulae are not giving you good approximations of trip length, I propose that one or more of the following explanations may obtain. (a) You base your estimate on the car odometer, which is itself inaccurate. (b) You base your estimate on personal intuition, which is distorted by the psychological effects of automobile travel. (c) You are taking GPS intervals at such long intervals that the resulting line segments are a poor match for the route you have taken, in which case you should increase your sampling frequency. Have you tried applying the formula to a walk or a bicycle ride of known length, where you can easily stop at frequent intervals to record the GPS reading? I'm sure you'd be pleased with the results. leapinglizard```
 radres-ga rated this answer: and gave an additional tip of: \$5.00 ```Finally got an implementation of this completed, and it works great! Good job leapinglizard!```

 ```Usually people want to know the distance along a great circle path. A longer distance is getting there by road. The shortest distance is the cord which passes underground though rock between the points. Neil```
 ```"Usually people want to know the distance along a great circle path. A longer distance is getting there by road. The shortest distance is the cord which passes underground though rock between the points. Neil" I know that there is the "Great Circle" method of determining distance using latitude and longitude, however it is fairly inaccurate, largely due to the variations in altitude. I have not seen a formula that takes into account the altitude of the points. If the two points are fairly close together along a road, and enough points are taken, it should be possible to determine the distance between two places by road.```
 ```Specifing altitude at each end complicates the problem consciderably. If the distance is less than 1000 miles, and the altitude is a mile or more at both ends, it may be practical to use topagraphic maps to plot the shortest path in several instalments though the hills, mountains and ridges near the middle. You will have to specify a minimum safe altitude such as 1000 feet. An accueracy of plus or minus one part per million may be impossible even with military GPS which is more accurate than civillian GPS for any of the methods as the Earth is slightly oblate instead of a perfect sphere. Neil```
 ```Handheld GPS units usually won't give you fixes that are more accurate than a few meters in the horizontal. So if you simply connect the dots you will generally get a distance that is an overestimate of the actual distance traveled, especially if you take a fix as often as every 5 seconds (in this case, increasing the sampling rate actually DECREASES the accuracy). This is because the GPS path will tend to zigzag back and forth across the true path. This inaccuracy is likely far greater than that introduced by ignoring changes in altitude.```