Unix/Linux Go Back    


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


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-14-2010
aemunathan aemunathan is offline
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

			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  
Old Unix and Linux 04-14-2010
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
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.
Sponsored Links
    #3  
Old Unix and Linux 04-15-2010
aemunathan aemunathan is offline
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
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:
./calcDist[2]: pi: not found
./calcDist[3]: Earth_Radius: not found
./calcDist[16]: syntax error at line 18 : `(' unexpected
    #4  
Old Unix and Linux 04-15-2010
pludi's Unix or Linux Image
pludi pludi is offline Forum Advisor  
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)?
Sponsored Links
    #5  
Old Unix and Linux 04-15-2010
methyl methyl is offline Forum Advisor  
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
Sponsored Links
    #6  
Old Unix and Linux 04-16-2010
aemunathan aemunathan is offline
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..
Sponsored Links
    #7  
Old Unix and Linux 04-16-2010
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
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?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12-03-2009 01:17 AM
Converting distance list to distance matrix in R anjas Programming 0 07-24-2009 06:32 AM
Long Distance UNIX (Solaris) Cloning ? HikerLT UNIX for Dummies Questions & Answers 1 01-10-2008 04:29 PM
Defining X and Y Coordinates Inside A Window lesnaubr Shell Programming and Scripting 1 08-16-2007 08:19 AM
Lat/Long Distance Calculation Ernst Shell Programming and Scripting 3 04-11-2007 11:15 PM



All times are GMT -4. The time now is 01:33 PM.