I Find This Hard To Believe

Recommended Posts

Huggybearman
Posted
Posted

t's all to do with Rhumb Lines and Great Circles.

 

That triggered a memory of a program that I wrote several years back for a trucking company.  Part of the program used the GPS position of trucks sent via satellite to find the truck closest to a given customer latitude/longitude.  In case anyone needs  a SQL Stored Procedure to do the same here it is.  No charge for my friends to use this "bad boy" even though it almost caused me to have a brain aneurism by the time I figured it out and got it debugged.  :1 (103):

 

set ANSI_NULLS OFF

set QUOTED_IDENTIFIER OFF

GO

ALTER PROCEDURE [dbo].[Haney_GreatCircleMilesBetweenPoints]

 (

@Latitude1  float,

@Longitude1 float,

@Latitude2  float,

@Longitude2 float,

@Distance float OUTPUT

)

 

-- calc the distance between to lat longs using the great circle formula

-- points are passed either as long int seconds or as decimal degrees

-- example a lat long could be expressed as 47.2531 &  122.2558 OR 170111 &  440121

 

AS

 

-- if lat long is passed in seconds divide by 3600

IF @Latitude1 > 360

BEGIN

   set @Latitude1 = @Latitude1/3600

END

IF @Longitude1 > 360

BEGIN

   set @Longitude1 = @Longitude1/3600

END

 

IF @Latitude2 > 360

BEGIN

   set @Latitude2 = @Latitude2/3600

END

IF @Longitude2 > 360

BEGIN

   set @Longitude2 = @Longitude2/3600

END

 

declare @radius float

 

declare @lon1  float

declare @lon2  float

declare @lat1  float

declare @lat2  float

 

declare @a float

 

-- Sets average radius of Earth in Miles

set @radius = 3956.0E

 

-- Convert degrees to radians

set @lon1 = radians( @Longitude1 )

set @lon2 = radians( @Longitude2 )

set @lat1 = radians( @Latitude1 )

set @lat2 = radians( @Latitude2 )

 

set @a = sqrt(square(sin((@lat2-@lat1)/2.0E)) + 

(cos(@lat1) * cos(@lat2) * square(sin((@lon2-@lon1)/2.0E))) )

 

set @distance =

@radius * ( 2.0E *asin(case when 1.0E < @a then 1.0E else @a end ))

That's an impressive piece of coding. I wonder if the likes of Garmin have 'borrowed' it for their SatNavs!

Ken

Link to comment
Share on other sites

sonjack2847
Posted
Posted

Ok as a straight line I cannot see it but if it is configured another way so be it

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...