Monday, April 12, 2010 8:11:10 PM
Create function [dbo].[Bearing]
( @Lat1 float, @Lng1 float, @Lat2 float, @Lng2 float)
Author: Philip Leitch
Purpose: This function converts two sets of latitude and longatude points in to a bearing.
Copyright: Philip Leitch 2010
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.
Liability: The developer assumes all liability when using this code.
Notes: Bearing changes during travel, for instance, if you travel heading due north, after enough time you will end up heading due
south even though you haven’t changed direction. Therefore this is the outset direction (direction from point A to get to point B).
returns numeric(10,6) as
Declare @Bearing as numeric(10,6)
set @Lat1 = @Lat1 * (pi()/180.0)
set @Lng1 = @Lng1 * (pi()/180.0)
set @Lat2 = @Lat2 * (pi()/180.0)
set @Lng2 = @Lng2 * (pi()/180.0)
set @Bearing = atn2(sin(@Lng2-@Lng1)*cos(@Lat2),cos(@Lat1)*sin(@Lat2)-sin(@Lat1)*cos(@Lat2)*cos(@Lng2-@Lng1))/ (pi()/180.0)
if @Bearing < 0
set @Bearing = 360 + @Bearing
Copyright 2009 Philip Leitch