iBaker
08-22-2007, 07:04 AM
Hi
I have the code for a page that calculates the distance as the bird flies between two different places but it is from an asp page. It uses latitude and longitude.
Is there anyone who would like to take the code and convert it to an addon page for use in vBulletin - if so it would be greatly appreciated.
The code is:
ASP Code, commented:
You?ll need to retrieve your start/finish locations however you wish (recordset from DB), pull your D:M:S data for each location, and feed into variables listed below.
If using another language such as PHP, most of the calculations will remain the same, though there will be differences in syntax of course.
<%
'Calculate the distance between the two selected airports
Dim lat1, lat2, lon1, lon2
Dim lat1D, lat1M, lat1S 'D:M:S values need to be converted to decimal degrees
Dim lat2D, lat2M, lat2S
Dim lon1D, lon1M, lon1S
Dim lon2D, lon2M, lon2S
Dim Lat1Dir, Lat2Dir, Lon1Dir, Lon2Dir
?rsStart= my recordset which retrieves the start location/airport
If rsStart.Fields.Item("LatDir").Value = "N" then
'do nothing
else
Lat1Dir="-" 'south is negative
End If
If rsStart.Fields.Item("LonDir").Value = "E" then
'do nothing
else
Lon1Dir="-" 'west is negative
End If
lat1D=rsStart.Fields.Item("LatDeg").Value
lat1M=rsStart.Fields.Item("LatMin").Value
lat1S=rsStart.Fields.Item("LatSec").Value
'convert Lat1 to Decimal
If Lat1Dir>"" then 'if there is an operator, then insert it
Lat1= "-" & Lat1D + (Lat1M/60) + (Lat1S/3600) 'we should have a decimal number now
else
Lat1=Lat1D + (Lat1M/60) + (Lat1S/3600)
end if
'now get Lon1
lon1D=rsStart.Fields.Item("LonDeg").Value
lon1M=rsStart.Fields.Item("LonMin").Value
lon1S=rsStart.Fields.Item("LonSec").Value
'convert Lat1 to Decimal
If Lon1Dir > "" then 'if there is an operator, then insert it
Lon1= "-" & Lon1D + (Lon1M/60) + (Lon1S/3600) 'we should have a decimal number now
else
Lon1=Lon1D + (Lon1M/60) + (Lon1S/3600)
end if
?rsDest is my recordset for the destination location/airport
'We have Lat1 and Lon1 now repeat for 2
If rsDest.Fields.Item("LatDir").Value = "N" then
'do nothing
else
Lat2Dir="-" 'south is negative
End If
If rsDest.Fields.Item("LonDir").Value = "E" then
'do nothing
else
Lon2Dir="-" 'west is negative
End If
lat2D=rsDest.Fields.Item("LatDeg").Value
lat2M=rsDest.Fields.Item("LatMin").Value
lat2S=rsDest.Fields.Item("LatSec").Value
'convert Lat2 to Decimal
If Lat2Dir>"" then 'if there is an operator, then insert it
Lat2= "-" & Lat2D + (Lat2M/60) + (Lat2S/3600) 'we should have a decimal number now
else
Lat2=Lat2D + (Lat2M/60) + (Lat2S/3600)
end if
'now get Lon1
lon2D=rsDest.Fields.Item("LonDeg").Value
lon2M=rsDest.Fields.Item("LonMin").Value
lon2S=rsDest.Fields.Item("LonSec").Value
'convert Lat2 to Decimal
If Lon2Dir > "" then 'if there is an operator, then insert it
Lon2= "-" & Lon2D + (Lon2M/60) + (Lon2S/3600) 'we should have a decimal number now
else
Lon2=Lon2D + (Lon2M/60) + (Lon2S/3600)
end if
?DEBUG
' response.write(lat1)& "<br>"
' response.write(lon1)& "<br>"
' response.write(lat2)& "<br>"
' response.write(lon2)& "<br>"
'distance=2* ASIN(sqr((sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
'distance=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(l at2)*cos(lon1-lon2))
%>
<%
Const pi = 3.14159265358979323846
function distance(lat1, lon1, lat2, lon2, unit)
Dim theta, dist
theta = lon1 - lon2
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * 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
End function
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
'::: This function get the arccos functi
' on from arctan function:::
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
function acos(rad)
if Abs(rad) <> 1 Then
acos = pi/2 - Atn(rad / Sqr(1 - rad * rad))
ElseIf rad = -1 Then
acos = pi
End if
End function
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
'::: This function converts decimal degr
' ees to radians :::
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
function deg2rad(Deg)
deg2rad = cdbl(Deg * pi / 180)
End function
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
'::: This function converts radians to d
' ecimal degrees :::
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
function rad2deg(Rad)
rad2deg = cdbl(Rad * 180 / pi)
End function
?DEBUG
' response.write distance(lat1, lon1, lat2, lon2, "M") & " Miles<BR>"
' response.write distance(lat1, lon1, lat2, lon2, "K") & " Kilometers<BR>"
' response.write distance(lat1, lon1, lat2, lon2, "N") & " Nautical Miles<BR>"
?d = result, using formatnumber call to ensure consitency of displayed variable.
d = FormatNumber(distance(lat1, lon1, lat2, lon2, "N"),2)
%>
I have the code for a page that calculates the distance as the bird flies between two different places but it is from an asp page. It uses latitude and longitude.
Is there anyone who would like to take the code and convert it to an addon page for use in vBulletin - if so it would be greatly appreciated.
The code is:
ASP Code, commented:
You?ll need to retrieve your start/finish locations however you wish (recordset from DB), pull your D:M:S data for each location, and feed into variables listed below.
If using another language such as PHP, most of the calculations will remain the same, though there will be differences in syntax of course.
<%
'Calculate the distance between the two selected airports
Dim lat1, lat2, lon1, lon2
Dim lat1D, lat1M, lat1S 'D:M:S values need to be converted to decimal degrees
Dim lat2D, lat2M, lat2S
Dim lon1D, lon1M, lon1S
Dim lon2D, lon2M, lon2S
Dim Lat1Dir, Lat2Dir, Lon1Dir, Lon2Dir
?rsStart= my recordset which retrieves the start location/airport
If rsStart.Fields.Item("LatDir").Value = "N" then
'do nothing
else
Lat1Dir="-" 'south is negative
End If
If rsStart.Fields.Item("LonDir").Value = "E" then
'do nothing
else
Lon1Dir="-" 'west is negative
End If
lat1D=rsStart.Fields.Item("LatDeg").Value
lat1M=rsStart.Fields.Item("LatMin").Value
lat1S=rsStart.Fields.Item("LatSec").Value
'convert Lat1 to Decimal
If Lat1Dir>"" then 'if there is an operator, then insert it
Lat1= "-" & Lat1D + (Lat1M/60) + (Lat1S/3600) 'we should have a decimal number now
else
Lat1=Lat1D + (Lat1M/60) + (Lat1S/3600)
end if
'now get Lon1
lon1D=rsStart.Fields.Item("LonDeg").Value
lon1M=rsStart.Fields.Item("LonMin").Value
lon1S=rsStart.Fields.Item("LonSec").Value
'convert Lat1 to Decimal
If Lon1Dir > "" then 'if there is an operator, then insert it
Lon1= "-" & Lon1D + (Lon1M/60) + (Lon1S/3600) 'we should have a decimal number now
else
Lon1=Lon1D + (Lon1M/60) + (Lon1S/3600)
end if
?rsDest is my recordset for the destination location/airport
'We have Lat1 and Lon1 now repeat for 2
If rsDest.Fields.Item("LatDir").Value = "N" then
'do nothing
else
Lat2Dir="-" 'south is negative
End If
If rsDest.Fields.Item("LonDir").Value = "E" then
'do nothing
else
Lon2Dir="-" 'west is negative
End If
lat2D=rsDest.Fields.Item("LatDeg").Value
lat2M=rsDest.Fields.Item("LatMin").Value
lat2S=rsDest.Fields.Item("LatSec").Value
'convert Lat2 to Decimal
If Lat2Dir>"" then 'if there is an operator, then insert it
Lat2= "-" & Lat2D + (Lat2M/60) + (Lat2S/3600) 'we should have a decimal number now
else
Lat2=Lat2D + (Lat2M/60) + (Lat2S/3600)
end if
'now get Lon1
lon2D=rsDest.Fields.Item("LonDeg").Value
lon2M=rsDest.Fields.Item("LonMin").Value
lon2S=rsDest.Fields.Item("LonSec").Value
'convert Lat2 to Decimal
If Lon2Dir > "" then 'if there is an operator, then insert it
Lon2= "-" & Lon2D + (Lon2M/60) + (Lon2S/3600) 'we should have a decimal number now
else
Lon2=Lon2D + (Lon2M/60) + (Lon2S/3600)
end if
?DEBUG
' response.write(lat1)& "<br>"
' response.write(lon1)& "<br>"
' response.write(lat2)& "<br>"
' response.write(lon2)& "<br>"
'distance=2* ASIN(sqr((sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
'distance=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(l at2)*cos(lon1-lon2))
%>
<%
Const pi = 3.14159265358979323846
function distance(lat1, lon1, lat2, lon2, unit)
Dim theta, dist
theta = lon1 - lon2
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * 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
End function
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
'::: This function get the arccos functi
' on from arctan function:::
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
function acos(rad)
if Abs(rad) <> 1 Then
acos = pi/2 - Atn(rad / Sqr(1 - rad * rad))
ElseIf rad = -1 Then
acos = pi
End if
End function
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
'::: This function converts decimal degr
' ees to radians :::
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
function deg2rad(Deg)
deg2rad = cdbl(Deg * pi / 180)
End function
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
'::: This function converts radians to d
' ecimal degrees :::
':::::::::::::::::::::::::::::::::::::::
' :::::::::::::::::::::::::::::::
function rad2deg(Rad)
rad2deg = cdbl(Rad * 180 / pi)
End function
?DEBUG
' response.write distance(lat1, lon1, lat2, lon2, "M") & " Miles<BR>"
' response.write distance(lat1, lon1, lat2, lon2, "K") & " Kilometers<BR>"
' response.write distance(lat1, lon1, lat2, lon2, "N") & " Nautical Miles<BR>"
?d = result, using formatnumber call to ensure consitency of displayed variable.
d = FormatNumber(distance(lat1, lon1, lat2, lon2, "N"),2)
%>