Home Search Forums Register Forum Rules Man Pages Unix Commands Linux Commands FAQ Members Search Today's Posts Mark Forums Read

 Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

# Calculating distance between two LAT long coordinates

## Shell Programming and Scripting

#1
04-14-2010
 aemunathan Registered User Join Date: May 2008 Last Activity: 17 April 2012, 8:07 PM EDT Posts: 113 Thanks: 3 Thanked 0 Times in 0 Posts
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....

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

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

Function distance(lat1, lon1, lat2, lon2, unit)
Dim theta, dist
theta = lon1 - lon2
dist = acos(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

acos = pi
End If
End function

' This function converts decimal degrees to radians

deg2rad = cdbl(Deg * pi / 180)
End Function

' This function converts radians to decimal degrees

End Function

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

Pls help me !
#2
04-14-2010
 jgt   Registered User Join Date: Apr 2007 Last Activity: 22 August 2017, 6:36 PM EDT Location: 44.21.48N 80.50.15W Posts: 1,963 Thanks: 2 Thanked 260 Times in 243 Posts
use 'bc'. Read the man page, it has all the functions built in.
#3
04-15-2010
 aemunathan Registered User Join Date: May 2008 Last Activity: 17 April 2012, 8:07 PM EDT Posts: 113 Thanks: 3 Thanked 0 Times in 0 Posts
hi

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

Code:
```#!/bin/ksh
pi = 3.14159265358979323846

Function_distance()
{
theta =\$((\$2 - \$4))
\$theta)))))
dist = \$(acos \$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 = \$(\$((\$1 * \$pi)) / 180)
}

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

Function_distance```

error
Quote:
#4
04-15-2010
 pludi   Cat herder Join Date: Dec 2008 Last Activity: 28 March 2014, 8:35 AM EDT Location: Vienna, Austria, Earth Posts: 5,521 Thanks: 38 Thanked 335 Times in 308 Posts
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)?
#5
04-15-2010
 methyl   Advisor Join Date: Mar 2008 Last Activity: 8 June 2016, 9:58 PM EDT Posts: 6,402 Thanks: 288 Thanked 676 Times in 645 Posts
Quote:
 In vbscript i achieved in the following way
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-programmin...e-azimuth.html
#6
04-16-2010
 aemunathan Registered User Join Date: May 2008 Last Activity: 17 April 2012, 8:07 PM EDT Posts: 113 Thanks: 3 Thanked 0 Times in 0 Posts
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..
#7
04-16-2010
 jgt   Registered User Join Date: Apr 2007 Last Activity: 22 August 2017, 6:36 PM EDT Location: 44.21.48N 80.50.15W Posts: 1,963 Thanks: 2 Thanked 260 Times in 243 Posts
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?