
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. 

Thread Tools  Search this Thread  Display Modes 
#1




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 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 ! 
Sponsored Links  

#2




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

Sponsored Links  

#3




hi this is the way i converted ....not succeeded.. Code:
#!/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 Quote:

#4




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)? 
Sponsored Links  

#5




Quote:
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 highlevel 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/shellprogrammin...eazimuth.html 
Sponsored Links  

#6




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.. 
Sponsored Links  

#7




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? 
Sponsored Links  

Thread Tools  Search this Thread 
Display Modes  
More UNIX and Linux Forum Topics You Might Find Helpful  
Thread  Thread Starter  Forum  Replies  Last Post 
opening mulitple different videos with mplayer and placing them in x,y coordinates?  danpaluska  UNIX for Dummies Questions & Answers  1  12032009 01:17 AM 
Converting distance list to distance matrix in R  anjas  Programming  0  07242009 06:32 AM 
Long Distance UNIX (Solaris) Cloning ?  HikerLT  UNIX for Dummies Questions & Answers  1  01102008 04:29 PM 
Defining X and Y Coordinates Inside A Window  lesnaubr  Shell Programming and Scripting  1  08162007 08:19 AM 
Lat/Long Distance Calculation  Ernst  Shell Programming and Scripting  3  04112007 11:15 PM 
