Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.

Our new SQL Server Forums are live!
Come on over! We've restricted the ability to create new threads on these forums.

Im writing an application that needs to find the distance between two zip codes and return only records that are within a certain radius. I have the formula's I need. I can imagine that this is a pretty resource intensive operation especially on result sets. Is there a good method to optimize complex math operations itself. I was going to write a udf called GetZipDistance AND IsInRadius that took the lattitude and longitude coordinates as parameters. I was also considering creating a table that stored the distances but it would have ended up being over a billion records :).

Heres what I got working... Sql server laughed at it. Funny thing is I think it was losing the most speed from having to do a table scan. This will get you pretty accurate results as far as distance goes. for doing a radius you would just check radius <= distance. The thing i was worried about was the mathematical functions. But.. I did a display execution plan and it said the computer scalar cost was like.. 4% of the cost of the query. and that was with 100 records.. ill at most pull 20-30 at a time. This will show miles.

declare @zip1 int
declare @lat1 decimal(18,6)
declare @long1 decimal(18,6)
select @zip1 = 92591
select @lat1 = Lattitude, @long1 = longitude from T_ZipCodes where zip = @zip1
select top 100
zip,
(DEGREES(ACOS(
SIN(RADIANS(@lat1)) *
SIN(RADIANS(lattitude)) +
COS(RADIANS(@lat1)) *
COS(RADIANS(lattitude)) *
COS(RADIANS(@long1 - longitude))
)
)
) * 69.09
as distance
from T_ZipCodes

there was another big question on distances between points (long and lat points). And there was some question on the number of comparisons that had to be made.

This may be slightly off topic, but you can make some very easy restrictions to approximate mileage boundaries. If for example you want a 20 mile radius from a given point, simply calculate the distance in a cardinal direction (North, East...) so that if long or lat were the same the other would have to change by x degrees

lets say that it is .250 degrees (just for simplicity)

using the origin point, lets call this (a,b), we can build three simply conditions to approximate the mileage restrictions without applying the calculation to EVERY single row!

Set @approxDegrees = .250

Where newPoint_a between (origin_a - @approxDegrees) and (origin_a + @approxDegrees) and newPoint_b between (origin_b - @approxDegrees) and (origin_b + @approxDegrees) and abs(newPoint_a - origin_a) + abs(newPoint_b - origin_b) < @approxDegress*1.5

this builds a range that looks like a square with the four corners cut off:

___
/ | |
\___/

Just some thoughts I had as I was skimming through!!

I'm going to take a guess. If the earth was perfectly round, and a flat approximation was used, 100 miles would give a 1 mile max error? It probably gets quadratically worse as the radius increases?

take the (+,+) quadrant of a unit circle which is defined by the function: y = (1+x^2)^(1/2) where 0<=x<=1

also add in the approximation constraints: x <= 1 y <= 1 y <= (2)^(1/2) - x

these equations generate a tangent lines about the unit circle at the following 3 points (0,1); (1,0); ((2)^(1/2)/2,(2)^(1/2)/2)

Looking at this graph I determined that the furthest points from the origin occured on

y <= (2)^(1/2) - x when x=1 or y=1

Solving for the other you get the pairs: (1,(2)^(1/2) - 1) ~ (1,.414214) and ((2)^(1/2) - 1),1 ~ (.414214,1)

Using the formula for distance provided in the link

a = x/57.2958
b = y/57.2958
d = 3958.75*ArcTan( (1 - (sin(0)sin(a) + cos(0)cos(a)cos(b))²)^½
-----------------------------------------------
sin(0)sin(a) + cos(0)cos(a)cos(b) )
so
using the point ((2)^½-1,1): distance ~ 74.785381537492
using the point (0,1): distance ~ 69.093197057399
which gives an error of: distance ~ 5.692184480093
error ratio is then: 69.093197057399/5.692184480093 ~ 12.138256814943
this means that for every x miles of range there will be (x/12.138256814943) miles of error
or error = x/12.138256814943
the error % is constant and would be ~ 8.2384%

I'm surprised and suspicious that the error % above is a constant. I'd bet a lot that it's very much not a constant, because for small distances, spherical trig and flat-earth calculations would be almost exactly the same. For large distances, the errors become greater.

I've got to pack for a trip now, but I'll be back Monday nite. I hope Arnold Fribble takes a look at this to give us his 2cents.

quote:Originally posted by SamC I've got to pack for a trip now, but I'll be back Monday nite. I hope Arnold Fribble takes a look at this to give us his 2cents.

So... Arnold, from the second link I believe I read that you implemented something similar, but that you broke the world up into latitude rings and slightly altered the conditions for each ring.

So basically, the error is not constant, but I would still like to know if you think that the conditions are still a reasonable approximation for quick filtering?

x <= @approxDegrees y <= @approxDegrees y <= (2)^(1/2)*@approxDegrees - x

Where the @approxDegrees is an approximation based on the desired mileage range.

I think what I gather is that this would only work within areas that do not cross too many latitude lines.

I always thought it was the Great Circle Formula...wrote this in Access (which has very poor trig support)...guess I should convert it

Function Deg2Rad(NumberArg As Double) As Double
Deg2Rad = NumberArg * ((22 / 7) / 180) ' Return Radians
End Function
Function ArcCos(NumberArg As Double) As Double
ArcCos = Atn((-1 * NumberArg) / Sqr((-1 * NumberArg) * NumberArg + 1)) + 2 * Atn(1) ' Return Inverse Cosine
End Function
Function GreatCircle(X1 As Double, Y1 As Double, X2 As Double, Y2 As Double) As Double
GreatCircle = Kil2Mi((Rad2Deg(ArcCos((Sin(Deg2Rad([X1])) * Sin(Deg2Rad([X2]))) + (((Cos(Deg2Rad([X1])) * Cos(Deg2Rad([X2]))) * (Cos(Abs((Deg2Rad([Y2])) - (Deg2Rad([Y1])))))))))) * 111.23)
End Function
Function Kil2Mi(NumberArg As Double) As Double
Kil2Mi = 0.62 * NumberArg ' Convert Kilometers to Miles
End Function
Function Mi2Kil(NumberArg As Double) As Double
Mi2Kil = 1.6 * NumberArg ' Convert Miles to Kilometers
End Function
Function Rad2Deg(NumberArg As Double) As Double
Rad2Deg = NumberArg * (180 / (22 / 7)) ' Return Degrees.
End Function