Calculate distance using longitude and latitude
Forums
Hi,
I was hoping you can help me with the above. I have longitude and latitude for several different locations and would like to calculate the distance between them. I know we have functions like Cos, Sin and Tan to enable us to do this however I just can't seem to wrap my head around it. I have found similar scripts for VB but have been unsuccessful trying to incorporate it into IDEAScript.
Any help would be greatly appreciated and please let me know if you require any further information.
Thanks
Salman
Hi Brian,
Hi Brian,
Thank you for your prompt reply! I did find that VB code but was unsure on what amendments to make to make it work with IDEAscript. I have very limited knowledge of scripting but I am trying to learn as much as I can before my training course later this year.Please see attached image of the code which I have had to amend slightly. I only need the output in miles. It wouldn't let me proceed without defining the variables shown and now it gives me a constant value for every record. Are you able to use the coordinates in the image to help test the file? Can you please advise if I have missed something or defined the variables incorrectly?Thanks
Salman
Hi Salem, when using the
Hi Salem, when using the custom functions you have to define your parameters outside of the script, in your case you are defining your parameters within the script which is fine for IDEAScript code but not for a custom function. Here is a screen capture of the parameters dialog:
Here is the rest of the code. I will attach the updated custom function.
Option Explicit
Const pi = 3.14159265358979323846
Function distLatLong(Latitude1 As Double,Longitude1 As Double,Latitude2 As Double,Longitude2 As Double) As Double
Dim theta, dist
theta = Longitude1 - Longitude2
dist = Sin(deg2rad(Latitude1)) * Sin(deg2rad(Latitude2)) + Cos(deg2rad(Latitude1)) * Cos(deg2rad(Latitude2)) * Cos(deg2rad(theta))
dist = acos(dist)
dist = rad2deg(dist)
distLatLong = dist * 60 * 1.1515
End Function
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This function get the arccos function using 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 degrees to radians :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function deg2rad(Deg)
deg2rad = CDbl(Deg * pi / 180)
End Function
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This function converts radians to decimal degrees :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function rad2deg(Rad)
rad2deg = CDbl(Rad * 180 / pi)
End Function
Hi Salem, you are getting
Hi Salem, you are getting that error because you dont' have any parameters for the test equation. When you hit test it will take the parameters of the distlatlong() and use them in the function, as it is blank there are no parameters. Instead doing something like this: distlatlong(32.9697, -96.80322, 29.46786, -98.53) it will now have the 4 needed numeric paramters and it can calculate the distance.
Hi Brian, I knew it was
Hi Brian, I knew it was something silly I missed! That's it up and running now and I have tested the quality of data against web based calculators. Might be handy to put this in the scripts section for others to use as well. Thanks again for your help and patience, it's been a steep learning curve.
Regards
Salman
Hello Salman, I found a vb
Hello Salman, I found a vb script to calculate the distance at http://www.geodatasource.com/developers/vb and I have adapted it as a custom function. The function will accept the latitude and longitude of two points and return the distance in miles, kilometers or nautical miles. I have not tested this function so I can't guarentee the accuracy, if you can do that it would be appreciated.
Brian