Geographical distance between long and lat in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Geographical distance between long and lat in bash
# 1  
Old 08-21-2014
Geographical distance between long and lat in bash

Does anyone know of any script or packages that allow the calculation of the geographical distance between two points of lat/long from within a bash shell?

I have been searching the web for the past few days and none of the options seem compatible with bash variables... (eg. geodist)

Many thanks,
Lily
# 2  
Old 08-21-2014
Welcome lily-anne,

How would you expect to get the input?

It would seem to be a simple pair of subtractions, but the key is getting the two locations as data in the first place.



Robin
# 3  
Old 08-21-2014
The formula to compute the distance between a pair of latitude-longitude coordinates is actually rather complex. A simplified version is on this page.

**edit**
Actually, that doesn't look right. Earth's radius is more like 4000 miles. Your milage may vary. Smilie

** another edit **
I see what's happening.
Earth radius is:
3443 statute miles
3963 nautical miles
Make sure you don't confuse the two. Nautical miles would be the unit most likely used in a calculation like this.

Last edited by Perderabo; 08-21-2014 at 04:10 PM..
# 4  
Old 08-22-2014
I hope this will solve your problem, but not bash solution Smilie

Code:
[akshay@nio my_library]$ cat dis.awk
# Function to calculate the distance between two points

function atan(x) { return atan2(x,1) }
function acos(x) { return atan2(sqrt(1-x*x), x) }
function deg2rad(Deg){ return ( 4.0*atan(1.0)/180 ) * Deg }
function rad2deg(Rad){ return ( 45.0/atan(1.0) ) * Rad }

# Distance(lat1,lon1,lat2,lon2,unit)
     
#    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  
#    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  

#    If 4th argument is not found then it returns distance in Miles
     
# -------  Units ----------
#   "M" -> Miles (default) 
#   "K" -> Kilometers
#   "N" -> Nautical Miles

function distance(lat1, lon1, lat2, lon2, unit) {
  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)
  miles = dist * 60 * 1.1515
  unit = toupper(unit)
  return ( unit == "K" ) ? (miles * 1.609344) : (unit == "N") ? ( miles * 0.8684) : miles
}

Usage :

1. On Terminal

Code:
[akshay@nio my_library]$ awk -f dis.awk --source 'BEGIN{ UNIT["M"]="Miles"; 
    UNIT["K"]="Kilometers"; UNIT["N"]="Nautical Miles"; 
    for(i in UNIT)print distance(32.9697, -96.80322, 29.46786, -98.53506, i),UNIT[i]}'
228.109 Nautical Miles
422.739 Kilometers
262.678 Miles

2. If you have text file something like this
Code:
[akshay@nio my_library]$ cat test.dat
32.9697 -96.80322 29.46786 -98.53506
31.9697 -97.80322 27.46786 -94.53506
27.9697 -91.80322 29.46786 -92.53506
34.9697 -93.80322 29.46786 -91.53506

You can use function like this
Code:
[akshay@nio my_library]$ cat test.awk
BEGIN{ 
	UNIT["M"]="Miles"; 
	UNIT["K"]="Kilometers"; 
	UNIT["N"]="Nautical Miles"; 
}
NF{  
	print $0;
	for(i in UNIT)
	{
		print distance($1, $2, $3, $4, i),UNIT[i]
	}
	print ""
}

Code:
[akshay@nio my_library]$ awk -f dis.awk -f test.awk test.dat 
32.9697 -96.80322 29.46786 -98.53506
228.109 Nautical Miles
422.739 Kilometers
262.678 Miles

31.9697 -97.80322 27.46786 -94.53506
319.252 Nautical Miles
591.647 Kilometers
367.633 Miles

27.9697 -91.80322 29.46786 -92.53506
97.7865 Nautical Miles
181.221 Kilometers
112.605 Miles

34.9697 -93.80322 29.46786 -91.53506
349.567 Nautical Miles
647.827 Kilometers
402.541 Miles


Last edited by Akshay Hegde; 08-22-2014 at 02:02 AM..
These 3 Users Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash does not wrap long lines correctly

Ksh is my default shell, but I want use the bash shell since its convenient to me. When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed. $... (5 Replies)
Discussion started by: senthil.ak
5 Replies

2. Shell Programming and Scripting

Geographical location details of a server

Is there a way to find geographical location details of a server in shell scripting ? Say, which region server is located at etc. (5 Replies)
Discussion started by: blp_18
5 Replies

3. Shell Programming and Scripting

finding distance between numbers

Hi, I have a file as ABC 1634230,1634284,1634349,1634468 1634272,1634301,1634356,1634534 What I want is to find distance between the numbers.. column 1 is the gene name and column 2 are starts and column 3 are their respective stops for the starts. So what I want is column 3 which has +1... (2 Replies)
Discussion started by: Diya123
2 Replies

4. Shell Programming and Scripting

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 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

5. Shell Programming and Scripting

Calculate distance and azimuth

Hi all, I have a data file like this lat lon lat lon 12.000 25.125 14.235 25.012 14.200 81.000 25.584 25.014 45.023 25.365 25.152 35.222 I want to calculate distance and azimuth between this points eg:- 12.000,25.125 and 14.235,25.012 I want to use awk programming... (3 Replies)
Discussion started by: chamara
3 Replies

6. Programming

Converting distance list to distance matrix in R

Hi power user, I have this type of data (distance list): file1 A B 10 B C 20 C D 50I want output like this # A B C D A 0 10 30 80 B 10 0 20 70 C 30 20 0 50 D 80 70 50 0 Which is a distance matrix I have tried... (0 Replies)
Discussion started by: anjas
0 Replies

7. Shell Programming and Scripting

Find the geographical location within a shell script

Hi, I need a shell script that when run should be able to find the geographical location of the system. can anyone help me with this? Thanks, Sundeep (1 Reply)
Discussion started by: eamani_sun
1 Replies

8. UNIX for Dummies Questions & Answers

Long Distance UNIX (Solaris) Cloning ?

Need some advice and guidance for this UNIX beginner. Due to downsizing I have inherited the SysAdmin duties..(sigh). Please excuse and forgive me if I use the wrong terms below.... Situation: We have UNIX ( Solaris 7/8/9( it varies) on Sun Ultra 10's) servers located at several global... (1 Reply)
Discussion started by: HikerLT
1 Replies

9. Shell Programming and Scripting

Lat/Long Distance Calculation

I amtrying to write a script that would compute the distance between an "x" number of points. This is what I have come up with so far and it is not working. Can anyone modify it to make it work? A=34.16597 B=-84.33244 C=34.2344 D=-84.29189 test "$A" -eq "$C" -o "$B" -eq "$D" then echo... (3 Replies)
Discussion started by: Ernst
3 Replies
Login or Register to Ask a Question