Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Distance Algorithm


Message #1 by "Leo Scott" <leoscott@c...> on Thu, 18 Jul 2002 14:01:29 -0700
This is a multi-part message in MIME format.

------=_NextPart_000_0028_01C22E63.9D86EAA0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I am looking for a formula or algorithm to calculate the distance between two points given their lattitude and
longitude.  Since the points will be fairly close curvature of the earth is not significant.  Can anyone point me in the
right direction to find this?


Message #2 by "Foote, Chris" <Chris.Foote@u...> on Fri, 19 Jul 2002 10:34:05 +0100
Hi Leo!


cos D = sin A sin B + cos A cos B cos L

Where A = latitude of location 1 in degrees
      B = latitude of location 2 in degrees
      L = long of location 1 _minus_ long of location 2. (If result is
greater than +180 deg or less than -180 deg, add or subtract 360 deg to give
a result between -180 & +180 deg)
      D = distance between location 1 and location 2 in degrees of arc (each
degree of arc equals 60 nautical miles)

Hope this is of some help!
Spike

-----Original Message-----
From: Leo Scott [mailto:leoscott@c...]
Sent: Thursday, July 18, 2002 10:01 PM
To: Access
Subject: [access] Distance Algorithm


I am looking for a formula or algorithm to calculate the distance between
two points given their lattitude and longitude.  Since the points will be
fairly close curvature of the earth is not significant.  Can anyone point me
in the right direction to find this?
--- Change your mail options at http://p2p.wrox.com/manager.asp or to
unsubscribe send a blank email to  
Message #3 by PStreeter@C... on Fri, 19 Jul 2002 9:08:38 CST
I didn't thinl of this when I read Leo's original message, but there 
are map packages available. Some are expensive and elaborate. 
I suspect there are some in the right range of prowess and price 
for you, Leo.

Paul

On Fri, 19 Jul 2002 10:34:05 +0100 "Foote, Chris" wrote:
 
> cos D = sin A sin B + cos A cos B cos L
> 
> Where A = latitude of location 1 in degrees
>       B = latitude of location 2 in degrees
>       L = long of location 1 _minus_ long of location 2. (If result is
> greater than +180 deg or less than -180 deg, add or subtract 360 deg
> to give
> a result between -180 & +180 deg)
>       D = distance between location 1 and location 2 in degrees of
> arc (each degree of arc equals 60 nautical miles)

> 
> -----Original Message-----
> From: Leo Scott [mailto:leoscott@c...]

> I am looking for a formula or algorithm to calculate the distance between
> two points given their lattitude and longitude.  Since the points will be
> fairly close curvature of the earth is not significant.  Can anyone
> point me in the right direction to find this?


Message #4 by "Leo Scott" <leoscott@c...> on Fri, 19 Jul 2002 10:36:10 -0700
Thanks, but I needed the algorithm.  I have 80 points of service based on 5
digit zip codes and 40,000 customers, with zip codes.  I have zip codes with
Lat & Long in a table.  I have to calculate the crow flies shortest distance
to each point of service for each customer.  I'm just going to turn a
machine loose on it and let it build a table.

|-----Original Message-----
|From: PStreeter@C...
|[mailto:PStreeter@C...]
|Sent: Friday, July 19, 2002 8:09 AM
|To: Access
|Subject: [access] RE: Distance Algorithm
|
|
|I didn't thinl of this when I read Leo's original message, but there
|are map packages available. Some are expensive and elaborate.
|I suspect there are some in the right range of prowess and price
|for you, Leo.
|
|Paul
|
|On Fri, 19 Jul 2002 10:34:05 +0100 "Foote, Chris" wrote:
|
|> cos D = sin A sin B + cos A cos B cos L
|>
|> Where A = latitude of location 1 in degrees
|>       B = latitude of location 2 in degrees
|>       L = long of location 1 _minus_ long of location 2. (If result is
|> greater than +180 deg or less than -180 deg, add or subtract 360 deg
|> to give
|> a result between -180 & +180 deg)
|>       D = distance between location 1 and location 2 in degrees of
|> arc (each degree of arc equals 60 nautical miles)
|
|>
|> -----Original Message-----
|> From: Leo Scott [mailto:leoscott@c...]
|
|> I am looking for a formula or algorithm to calculate the distance between
|> two points given their lattitude and longitude.  Since the points will be
|> fairly close curvature of the earth is not significant.  Can anyone
|> point me in the right direction to find this?
|
|
|

Message #5 by "Leo Scott" <leoscott@c...> on Thu, 25 Jul 2002 20:47:25 -0700
Thanks to those of you that responded to my question about calculating the distance between two points
given their Lat
and Long.  For those of you interested I found this piect of code that did the trick nicely:

Private Function DistCalc(Lat1 As Single, Lon1 As Single, _
                         Lat2 As Single, Lon2 As Single, _
                         Optional UnitFlag As String = "M")
    Dim LatRad1 As Single
    Dim LonRad1 As Single
    Dim LatRad2 As Single
    Dim LonRad2 As Single
    Dim LonRadDif As Single
    Dim RadDist As Single
    Dim X As Single
    Dim PI As Single
    Dim DistKM As Single
    Dim DistMI As Single

   On Error Resume Next
    PI = 3.141592654
    If IsNull(Lat1) Then
        Exit Function
    End If
    If Lat1 = 0 Or Lon1 = 0 Or Lat2 = 0 Or Lon2 = 0 Then
        DistCalc = Null
        Exit Function
    ElseIf Lat1 = Lat2 And Lon1 = Lon2 Then
        DistCalc = 0
        Exit Function
    End If

    LatRad1 = Lat1 * PI / 180
    LonRad1 = Lon1 * PI / 180
    LatRad2 = Lat2 * PI / 180
    LonRad2 = Lon2 * PI / 180
    LonRadDif = Abs(LonRad1 - LonRad2)
    X = Sin(LatRad1) * Sin(LatRad2) + Cos(LatRad1) * Cos(LatRad2) * Cos(LonRadDif)
    RadDist = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
    DistMI = RadDist * 3958.754
    DistKM = DistMI * 1.609344
    DistCalc = IIf(UnitFlag = "M", DistMI, DistKM)
End Function


Message #6 by "Amy Wyatt" <amyw@c...> on Fri, 26 Jul 2002 16:07:55
Thanks Leo,

I was actually looking for something like this.

Amy

> Thanks to those of you that responded to my question about calculating 
the distance between two points given their Lat
and Long.  For those of you interested I found this piect of code that did 
the trick nicely:

Private Function DistCalc(Lat1 As Single, Lon1 As Single, _
                         Lat2 As Single, Lon2 As Single, _
                         Optional UnitFlag As String = "M")
    Dim LatRad1 As Single
    Dim LonRad1 As Single
    Dim LatRad2 As Single
    Dim LonRad2 As Single
    Dim LonRadDif As Single
    Dim RadDist As Single
    Dim X As Single
    Dim PI As Single
    Dim DistKM As Single
    Dim DistMI As Single

   On Error Resume Next
    PI = 3.141592654
    If IsNull(Lat1) Then
        Exit Function
    End If
    If Lat1 = 0 Or Lon1 = 0 Or Lat2 = 0 Or Lon2 = 0 Then
        DistCalc = Null
        Exit Function
    ElseIf Lat1 = Lat2 And Lon1 = Lon2 Then
        DistCalc = 0
        Exit Function
    End If

    LatRad1 = Lat1 * PI / 180
    LonRad1 = Lon1 * PI / 180
    LatRad2 = Lat2 * PI / 180
    LonRad2 = Lon2 * PI / 180
    LonRadDif = Abs(LonRad1 - LonRad2)
    X = Sin(LatRad1) * Sin(LatRad2) + Cos(LatRad1) * Cos(LatRad2) * Cos
(LonRadDif)
    RadDist = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
    DistMI = RadDist * 3958.754
    DistKM = DistMI * 1.609344
    DistCalc = IIf(UnitFlag = "M", DistMI, DistKM)
End Function



  Return to Index