Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating straight line distances (reopen)

Status
Not open for further replies.

nzoli

Programmer
Feb 17, 2011
22
0
0
HU
thread184-496015

Hi!
I have used successfully GriffMG solution!

Code:
CLEAR
Lat1=52.47758
Lon1=1.90983
Lat2=50.80029
Lon2=1.08825
Dlat=Lat2-Lat1
Dlon=Lon2-lon1
a=(sin(dtor(Dlat/2)))^2 + (cos(dtor(lat1)) * cos(dtor(lat2)) * (sin(dtor(dlon/2)))^2)

Radius = 4214.618316927
c=2*asin(sqrt(a))
d=Radius*c

I understand everything except one thing...
What kind of number that radius?
As I know the earth radius is about 3,959 miles. So what is that ~4214 miles. As I search in other site and distance calculators it uses this number (google and other uses too).

For example Wien and Budapest distance is ~133 miles with the ~4214 miles, but with the ~3,959 miles it's only ~127 miles.

So which one is the correct radius?

Thanks
 
As I know the earth radius is about 3,959 miles

Nothing's that simple.

The answer is that there is no single figure that represents the radius of the earth, because the earth is not a perfect sphere. The figure of 3,959 miles (6,371 km) is the mean radius. But the figure in GriffM's code (4214.618316927 miles) seems to be the one that's most often used in this formula.

The point is that, whatever figure you plug into the formula, there will always be a small possibility of error. Presumably, there's some way of refining the formula to deal with that, but I don't what it is.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
I don't know, Mike

4214.618316927 is a too precise number, maybe that is a radius of another planet.
It's too precise to mean a mean radius, and that rather is, as you found 3959 miles or 6371 km.

I actually googled 4214.618316927 and only found this thread. So I wonder where that number comes from. Nzoli, could you link to source? Am I assuming correctly, that "(reopen)" means you're reopening a topic you discussed here earlier? Refresh our memory, please.

Bye, Olaf
 
In the original thread, Griff says that R = 4,000 gives a better result than R = 4,214. Since 4,000 is closer to 3,959 than 4,214 is, that could mean that 3,959 will give an even better result.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Colleagues,

if you re-run the search I have conducted,


you will find that the given numbers vary quite widely!

Even stipulating that our planet is not a sphere but a spheroid, and that the polar and equatorial radiuses differ because of that.

Moreover: radius measured from the center to the pick of Chomolungma (+8.848 km) - or Everest, if you prefer - should differ from that measured to the bottom of the Mariana Trench (-10.91 km) by almost 20 km!
Even if we stipulate that the Earth's radius should be measured from the center to the sea level - that latter also differs (not mentioning the tides)!

So, colleagues, what bloody radius we're talkin' 'bout, eh? ;-)

Regards,

Ilya
 
Sorry, nzoli, I just now see you referenced thread184-496015 as the first thing in your post. I saw that as a caption...

Ok, now the connection is done. Mike is right. Only Griff knows, where that original 4214 came from, but he said himself, that 4000 would be a better fit, this really should be the earth radius.

Maybe someone calculated 4214 from the km figure, using seamiles rather than land miles.

see or nautical miles / land mile is a factor of about 1.15, but even that won't explain it as 4214/3959 rather is 1.06 than 1.15

Bye, Olaf.
 
Right, Ilya.

Wikipeadia [link en.wikipedia.org/wiki/Earth_radius][/url] says earth radius varies from 6,353 km to 6,384 km or 3,947–3,968 mi.

But the 20km from everest to Mariana trench you mention don't matter much. Even less the few feet of tidal sea level differences.

It's ok to get a precise enough air distance of places to assume a sphere or spheroid. If you want travel distances, rather use a maps route finder service. That measure - the route length - can differ very much from an air distance, of course.

Bye, Olaf.
 
So, can we agree that, whether you use R = 3,959 or R = 4,000, or even R = 4,214, it's not going to make a significant difference? Or, rather, that any error will be small compared to all the other factors that can affect the result.

Nzoli, it would be interesting to know why you need to do this calculation. Obviously, it can't be to work out travel distances or times, and certainly not flight times. In calculating the distance from Vienna to Budapest, is an error of about 10 km acceptable?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Mike,

If you just want to find out which distances are shorter, you can also calculate with R=1, if an arc on the unit sphere is longer, it's also longer on earth with earth radius. In that aspect, yes, Mike.

But if you want the air distance, the radius best fitting would be the average earth radius, or perhaps a radius valid at the lattitudes of main interest. The radius is a multiplicator, so it has linear effect on the result. Could be worse, but I wouldn't call it an insignificant influence.

Bye, Olaf.
 
Huh!
I didn't expect that so much comment :)
But I'm glad that forum always help me about my vfp problem :)
Thanks 4 you.

So we would like to use this calculations to our gps/tracking server.

MikeLewis: Yes the 10-15 km calculating error is accepted, because we don't have such huge distance. Our sampling time is 5 minutes when moving and 60 minutes in standing position. Because of the 5 minutes we have max ~10 km or less distances (most of the moving in city only).

So as I wrote my first comment, that solution is simple and good for us, only that radius distracted me :)

thanks for you again
 
Fine, Nzoli!

I second Mike, we've all learned something from this.

Just one recommendation: If you only calculate routes in one city, forget about the real earth radius and simply put in a figure that is a best fit for your area of interest.

To make that fit I would compare to a distance you compute using something like Google Earth perhaps for several pairs of coordinates at the suburbs or something like that.

Besides, a map service might be of more help, determining realistic distances of the routes really driven.

Bye, Olaf.
 
Hello All,

To be fair, I had NO IDEA where I got the 4214... radius from!

It was quite a while ago (9 years!).

The actual radius depends on the lattitude and you can use
this formula to determine that...

radius = 6372 * cos(lattitude/180*pi)

The thing is - the radius gets a bit smaller as you approach the poles...

B-)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
One other thought ....

If the points for which you want to calculate the distance are all in a country or region that employs a cartesian grid system, then you should use that system rather than lattitude and longitude.

For example, here in the British mainland (Scotland, England and Wales) we have a system called the National Grid, which identifies any point in the country to whatever precision you want. To find the distance between any two points, you only need to apply a simple formula, based on Pythagoras' Theorem. (This also has the advantage that the National Grid co-ordinates are generally better known that the long/lats.)

If anyone is interested, I've written an article showing some example code (see [URL unfurl="true"]http://www.hexcentral.com/articles/postcodes-distances.htm[/url]). Although the code is specific to Great Britain, the same general technique can be applied elsewhere.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
It seems to be calculating orbits of satellites, something about 100 or so miles above the Earth.
 
There's an easy way to calculate the literal distance between two points on a sphere. Given latitude and longitude, rotate both points through a series of transformations in 3D space to position one of the points on the north pole. Then, rotate the last point so it winds up at the meridian line, at 0 degrees / radians.

Example:
pt1 = [lat1,lon1] = [x1,y1,z1]
pt2 = [lat2,lon2] = [x2,y2,z2]

Rotate pt1 and pt2 by the pt1.lon1 value to derive x1',y1',z1', x2',y2',z2' values. Rotate both points by pt1.lat1 value to move pt1 to north pole. At this point, pt2 is somewhere on the sphere. Rotate it by pt2.lon2 to bring it to the 0 degree / radian mark.

The distance between the two points will be the length of the arc segment from the north pole to whatever latitude it's at (by angle) multiplied by the radius.

This allows you to process through great circles in a sphere. You could change the point's distance at that point by rotating it around the sphere, then preform the same angle calculations you originally did in the reverse order to re-position pt1 at its original location, with pt2 being now literally exactly closer or further (on the great circle straight line around the sphere) from where it was.

The calculation is notably more difficult for the true ellipse, but for all practical purposes it's sufficient for a game, or general computation and use, especially on close distances of a 1000 miles or less.

Another somewhat easier thing to do for computing ellipses is to compute the inscribed sphere using lat and lon, and then project its location out to the location on the ellipse, and then compute the distance between two points on an ellipse using that methodology (established formulas for distance on an ellipse), but greatly simplified by always using only a sphere to compute movements.

Hope this helps.

Best regards,
Rick C. Hodgin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top