Calculating distance between two LAT long coordinates

hi,
i have a pair of latitude and longitude and i want to calculate the distance between these two points. In vbscript i achieved in the following way...Now i want to implement this in unix shell scripting....

		<%
			Dim lat1, lon1, lat2, lon2
			const pi = 3.14159265358979323846

			lon1=80.25239109992
			lat1=12.99665987491
			lon2=80.24728417396545
			lat2=13.06229889392853

			Earth_Radius = 6378


			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 function 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 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


			'Display on screen
			response.write distance(lat1, lon1, lat2, lon2, "K") & " Km<br>"
			%>





Pls help me !

use 'bc'. Read the man page, it has all the functions built in.

hi

this is the way i converted ....not succeeded..

#!/bin/ksh
pi = 3.14159265358979323846
Earth_Radius = 6378

Function_distance()
{
theta =$(($2 - $4))
dist = $($($(sin($(deg2rad $1))) * $(sin($(deg2rad $3)))) + $($(cos($(deg2rad $1))) * $(cos($(deg2rad $3))) * $(cos($(deg2rad
 $theta)))))
dist = $(acos $dist)
dist = $(rad2deg $dist)
distance = $(($dist * 60 * 1.1515))
distance = $(($distance * 1.609344))
echo $distance
}

acos()
{
If ( $(abs($1)) != 1 ) Then
acos = $($($pi/2) - $(atan2($1 / $(sqrt(1 - $($1 * $1))))))
Else acos = $pi
if
}

deg2rad()
{
deg2rad = $($(($1 * $pi)) / 180)
}

rad2deg()
{
rad2deg = $($(($1 * 180)) / pi)
}

Function_distance

error

You've got too many whitespaces, you're using uppercase statements when only lowercase are allowed, and you won't get the desired result, as the shell can't do floating point arithmetics.

2 questions:

  • Why do you want to convert it to a shell script?
  • Why a shell script, and not a general purpose scripting language (which would make this far easier)?

I don't know where you found this program but one of the many places this VB program is published is:
http://www.zipcodeworld.com/docs/distance.pdf

I don't know whether you are a programmer or shell scripter or what computer or software you have. To embark on writing this sort of program you would need to know what tools are available and know how to use them.
What might be trivial in a high-level language beomes a nightmare if you start in the wrong language (like trying to use ksh when you do not know the capabilities or syntax).

Of the tools supplied with most unixes "bs" might be more suitable than "bc".
Both "bs" and "bc" are hard to get right. However you are more likely to find examples in "awk", "C" or "perl" online.

This "awk" idea posted recently on unix.com might get you started (not checked, not tested).
http://www.unix.com/shell-programming-scripting/129989-calculate-distance-azimuth.html

Hi,

its right ..am neither a programmer nor a shell scripter...am simply learning new things through this kind of forums...since i had the intention to learn, i came here....am not confident enough to read any book continuously for some time so am searching about my small small official requirements and carrying out day to day works...

by the way i tried the link u posted before posting my query here...that gave some 200 metre difference in value with that of vbscript code i gave here. thats y i thought of converting the same to shell script..

And the 200 metre difference is what percentage of the actual distance?
Would this difference be lost in order of calculation, limits of precision, rounding?

no its not exactly 200 m ...it varies randomly for different lat long...34m, around 100m...etc...am not rounding off

My point was not that it is exactly 200m but that you may be using 4 place tables in one environment and 5 place tables in another, or that 25.25 * 3.14159 does not necessarily give the same result as 25.2500000 * 3.14159 because the calculation result is limited to the variable with the least precision.