gpalmer711
IS-IT--Management
Hi All,
I'm developing a site in ASP.Net using SQL Server 2005.
It is basically a site for hosting the details of certain venues which people can search for. I'm currently implementing a feature that will allow people to search for venues that are within a certain distance of their location.
I essentially have to code working for this feature, I have the longitudes and latitudes of all the venues in the database and also the longitudes and latitudes of all the postcodes in the uk in another table. A user puts in their postcode and it will first pick out the lat and long of the postcode and then calculate the distance between the venue and the postcode (as the crow flys).
Now there may be as many as 3000 venues in the system within the next few months so what would be the best way of going through all of the records to calculate the distances?
What I have come up with so far is using the BETWEEN operator. This means that when the user enters their post code I calculate what the max and min Lang and Lat are and then only return records that match this criteria. While this works it is not ideal, the reason being as this works on a square grid, i.e. 5 km by 5km. What I would ideally do is have a 5KM radius.
I have the calculation functions already in the code but wondered if there is any way to perform some more advanced functions in the SQL code?
The code that I use to calculate the radius is
If you have any ideas or could point me to some relevant reading I would appreciate it.
Greg Palmer
Freeware Utilities for Windows Administrators.
I'm developing a site in ASP.Net using SQL Server 2005.
It is basically a site for hosting the details of certain venues which people can search for. I'm currently implementing a feature that will allow people to search for venues that are within a certain distance of their location.
I essentially have to code working for this feature, I have the longitudes and latitudes of all the venues in the database and also the longitudes and latitudes of all the postcodes in the uk in another table. A user puts in their postcode and it will first pick out the lat and long of the postcode and then calculate the distance between the venue and the postcode (as the crow flys).
Now there may be as many as 3000 venues in the system within the next few months so what would be the best way of going through all of the records to calculate the distances?
What I have come up with so far is using the BETWEEN operator. This means that when the user enters their post code I calculate what the max and min Lang and Lat are and then only return records that match this criteria. While this works it is not ideal, the reason being as this works on a square grid, i.e. 5 km by 5km. What I would ideally do is have a 5KM radius.
I have the calculation functions already in the code but wondered if there is any way to perform some more advanced functions in the SQL code?
The code that I use to calculate the radius is
Code:
Const pi = 3.1415926535897931
Function distance(ByVal lat1, ByVal lon1, ByVal lat2, ByVal lon2, ByVal unit)
Dim theta, dist
theta = lon1 - lon2
dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta))
dist = acos(dist)
dist = rad2deg(dist)
distance = dist * 60 * 1.1515
Select Case UCase(unit)
Case "K"
distance = distance * 1.609344
Case "N"
distance = distance * 0.8684
End Select
distance = Format(distance, "Fixed")
End Function
'
' This function get the arccos function from arctan function
'
Function acos(ByVal rad)
If Math.Abs(rad) <> 1 Then
acos = pi / 2 - Math.Atan(rad / Math.Sqrt(1 - rad * rad))
ElseIf rad = -1 Then
acos = pi
End If
End Function
'
' This function converts decimal degrees to radians
'
Function deg2rad(ByVal Deg)
deg2rad = CDbl(Deg * pi / 180)
End Function
'
' This function converts radians to decimal degrees
'
Function rad2deg(ByVal Rad)
rad2deg = CDbl(Rad * 180 / pi)
End Function
If you have any ideas or could point me to some relevant reading I would appreciate it.
Greg Palmer
Freeware Utilities for Windows Administrators.