Point_Bearing_Distance_To_Point

Posted by Philip Leitch Monday, March 8, 2010 10:42:41 PM Categories: Geospatial Scalar Function
CREATE     function [dbo].[Point_Bearing_Distance_To_Point]
( @Latitude float, @Longitude float, @Bearing float, @Distance as float)
/*
www.prlsoftware.com
Author: Philip Leitch
Date: 2010
Purpose: This function converts a latitude and longatude, bearing and distance to another latitude and longitude (concatenated together).
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:
*/
returns varchar(50)
as
begin
Declare @Lat2 float, @Long2  float
set @Latitude = cast(@latitude as numeric(9,6))
Declare @Radius float
    set @Radius = dbo.Earth_Radius(@Latitude)

    set @Bearing = @bearing * (pi()/180.0)   
    set @Latitude = @Latitude * (pi()/180.0)   
    set @Longitude = @Longitude * (pi()/180.0)   

set @lat2 = asin( sin(@Latitude)*cos(@Distance/@Radius) +
                      cos(@Latitude)*sin(@Distance/@Radius)*cos(@Bearing) );
set @long2 = @Longitude + atn2(sin(@Bearing)*sin(@Distance/@Radius)*cos(@Latitude),
                             cos(@Distance/@Radius)-sin(@Latitude)*sin(@lat2));

    return  cast(cast(@lat2 * (180/pi()) as numeric(10,6)) as varchar(50)) + ', ' + cast(cast( @long2 * (180/pi()) as numeric(10,6)) as varchar(50))
end
Copyright 2009 Philip Leitch

Comments

You must sign in to this site to post comments.
Already Registered?
Sign In
Not Yet Registered?