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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lat Long Distance Calculations

Status
Not open for further replies.

puforee

Technical User
Oct 6, 2006
741
US
In MS Access I want to calculate the distance between two Lat Long points. I was told there is a FAQ about this but I cannot find it.

I would like to mechanize it by having a From Lat field and Long fields and a To Lat field and Long fields. When these are populated I would like to have a calculation field show the distance between the From and the To points. I would like this to be fairly accurate. It should include the entire world so + and - would have to be part of the equation. I already have a table of 1000's of lattitude and longitudes for various cities in the world.

Thanks,
 
Very interesting but I am not sure how to translate the formulas to VB for Applications.

Maybe I am bitting off more than I can chew.

Thanks for the link,
 





Well you take a stab at coding it.

Then you run the code and evaluate the results.

THen you post back if you have specific questions.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
First, I don't do much with Acess but the functionality you want should be basic VBA.
Second, how accurate does it have to be? Is a spherical Earth (mean radius = 6371 km) sufficient?

You basically need to do 4 things (for the spherical case):
1 convert hours,minutes,seconds to decimal degrees
2 convert a lat/long pair (in dec. degrees) to x,y,z
3 compute the included angle (in radians) between 2 xyz's
4 compute the distance between points on the Earth's surface that correspond to the included angle.

Code:
Function hms2deg(h, m, s)
    h = h + m / 60# + s / 3600#
    hms2deg = 360# * h / 24#
End Function
Function latlon2xyz(lat, lon)
    'lat,lon in dec. degrees
    'assume length units of (spherical) Earth radii
    x3 = Cos((90 - lat) * Pi / 180#)
    x1 = Cos(lat * Pi / 180#) * Cos(lon * Pi / 180#)
    x2 = Cos(lat * Pi / 180#) * Sin(lon * Pi / 180#)
    latlon2xyz = Array(x1, x2, x3)
End Function
Function innerangle(v1, v2)
    innerangle = Acos(v1(1) * v2(1) + v1(2) * v2(2) + v1(3) * v2(3))
End Function
Function dist(theta)
    dist = 6371# * theta  'using mean radius in km
End Function

_________________
Bob Rashkin
 
I forgot to unitize the xyz vector:
Code:
Function latlon2xyz(lat, lon)
    'lat,lon in dec. degrees
    'assume length units of (spherical) Earth radii
    x3 = Cos((90 - lat) * Pi / 180#)
    x1 = Cos(lat * Pi / 180#) * Cos(lon * Pi / 180#)
    x2 = Cos(lat * Pi / 180#) * Sin(lon * Pi / 180#)
    [red]mag=sqrt(x1*x1+x2*x2+x3*x3)
    latlon2xyz = Array(x1/mag, x2/mag, x3/mag)[/red]
End Function

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top