rendered paste bodyCREATE FUNCTION [dbo].[gis_great_circle_distance](@latitude_1 float, @longitude_1 float, @latitude_2 float, @longitude_2 float)
RETURNS float
AS
BEGIN
DECLARE @distance float
DECLARE @EARTH_RADIUS float
DECLARE @rad_lat_1 float
DECLARE @rad_lat_2 float
DECLARE @rad_lon_1 float
DECLARE @rad_lon_2 float
DECLARE @delta_rad_lat float
DECLARE @delta_rad_lon float
DECLARE @temp float
SET @EARTH_RADIUS = 3956
SET @rad_lat_1 = dbo.gis_degrees_to_radians(@latitude_1)
SET @rad_lat_2 = dbo.gis_degrees_to_radians(@latitude_2)
SET @rad_lon_1 = dbo.gis_degrees_to_radians(@longitude_1)
SET @rad_lon_2 = dbo.gis_degrees_to_radians(@longitude_2)
SET @delta_rad_lat = @rad_lat_2 - @rad_lat_1
SET @delta_rad_lon = @rad_lon_2 - @rad_lon_1
SET @temp = POWER(SIN(@delta_rad_lat/2.0), 2) + COS(@rad_lat_1) * COS(@rad_lat_2) * POWER(SIN(@delta_rad_lon / 2.0), 2)
SET @distance = @EARTH_RADIUS * 2 * ATN2(SQRT(@temp), SQRT(1 - @temp))
SET @distance = ABS(@distance)
RETURN (@distance)
END
CREATE FUNCTION [dbo].[gis_degrees_to_radians](@degrees float)
RETURNS float AS
BEGIN
DECLARE @radians float
SET @radians = 0
SET @radians = @degrees * PI() / 180.0
RETURN (@radians)
END