[SQL] Distanza tra due punti geografici

sp TSQL che dati 2 punti geografici (latitudine e longitutide) ritorna la loro distanza in miglia:


ALTER FUNCTION dbo.udfComputeDistance(
 @lat1 float,
@lon1 float,
@lat2 float,
@lon2 float)

RETURNS float
AS

begin
declare @rt float
if @lat2=0 and @lon2=0

begin

set @rt=1000000

end

else

begin

 -- dLong represents the differences in longitudes

-- while dLat is the difference in latitudes

declare @dLong float
declare @dLat float
declare @temp float

-- Convert the decimal degrees to radians

set @lat2 = radians(@lat2)

set @lon1 = radians(@lon1)

set @lat1 = radians(@lat1)

set @lon2 = radians(@lon2)

-- Compute the degree differences

set @dLong = @lon2 - @lon1

set @dLat = @lat1 - @lat2

-- Compute the first part of the equation

set @temp = (square(sin(@dLat/2.0))) + cos(@lat2) * cos(@lat1) * (square(sin(@dLong/2.0)))

-- Return the approximate distance in miles

-- Note that 3956 is the approximate median radius of the Earth.

set @rt=(2.0 * atn2(sqrt(@temp), sqrt(1.0-@temp)))*3956.0

end

return @rt

end

 

 

 

 

 

 

posted @ martedì 23 ottobre 2007 04:38

Print

Comments on this entry:

# re: [SQL] Distanza tra due punti geografici

Left by Pietro Toniolo at 31/10/2007 08:13
Gravatar
In realtà per il codice centrale del calcolo della distanza sulla sfera io ho sempre utilizzato quest'altra formula, che è decisamente più semplice da utilizzare, e da memorizzare:

acos(sin(@lat1)*sin(@lat2) + cos(@lat1)*cos(@lat2)*cos(@lon2-@lon1))

Come quella riportata da te, a partire dai valori in radianti dà il risultato in radianti, quindi da moltiplicare per il raggio della terra (circa 6373km) per ricavare la distanza in chilometri.

# re: [SQL] Distanza tra due punti geografici

Left by plagiarism detection at 21/02/2013 15:22
Gravatar
Plagiarizing men loose their thoughts. The creators did properly when developed plagiarism detection (theplagiarism.com). It saves their property.

# re: [SQL] Distanza tra due punti geografici

Left by resume writing at 06/03/2013 19:52
Gravatar
Go to Exclusive-resume company exclusiveresume.com if you need professional resume writers review. Having delt with this reputable writing centre, you will not have to look for how to write a resume and where to glance over sample of cover letter. Don’t hesitate, buy resumes of superior quality from expert resume writers.

# re: [SQL] Distanza tra due punti geografici

Left by make at 07/11/2013 05:51
Gravatar

# re: [SQL] Distanza tra due punti geografici

Left by he at 13/11/2014 17:57
Gravatar
latest competitive data check your credit score economy policy adapt credit report scores important rolling out 4197

# re: [SQL] Distanza tra due punti geografici

Left by http://www.designmust.fr at 21/10/2015 15:09
Gravatar
declare @dLong float
declare @dLat float
declare @temp float
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011