forked from cyclingzealot/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoHashNote.bash
executable file
·102 lines (75 loc) · 2.59 KB
/
geoHashNote.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
# notify-send, bc, sed, xml_grep are needed to run this tool
#exit when command fails (use || true when a command can fail)
set -o errexit
#exit when your script tries to use undeclared variables
set -o nounset
#(a.k.a set -x) to trace what gets executed
#set -o xtrace
# in scripts to catch mysqldump fails
set -o pipefail
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
#Capture everything to log
log=~/log/$__base.log
exec > >(tee -a $log)
exec 2> >(tee -a $log >&2)
export DISPLAY=:0
deg2rad () {
bc -l <<< "$1 * 0.0174532925"
}
rad2deg () {
bc -l <<< "$1 * 57.2957795"
}
acos () {
pi="3.141592653589793"
bc -l <<<"$pi / 2 - a($1 / sqrt(1 - $1 * $1))"
}
distance () {
earth_radius="3960.00"
lat_1="$1"
lon_1="$2"
lat_2="$3"
lon_2="$4"
delta_lat=`bc <<<"$lat_2 - $lat_1"`
delta_lon=`bc <<<"$lon_2 - $lon_1"`
lat_1="`deg2rad $lat_1`"
lon_1="`deg2rad $lon_1`"
lat_2="`deg2rad $lat_2`"
lon_2="`deg2rad $lon_2`"
delta_lat="`deg2rad $delta_lat`"
delta_lon="`deg2rad $delta_lon`"
distance=`bc -l <<< "s($lat_1) * s($lat_2) + c($lat_1) * c($lat_2) * c($delta_lon)"`
distance=`acos $distance`
distance="`rad2deg $distance`"
distance=`bc -l <<< "$distance * 60 * 1.1515"`
distance=`bc <<<"scale=4; $distance / 1"`
echo $distance
}
if [[ ! -f ~/.homeCoords ]] ; then
echo "I need a file named ~/.homeCoords with your lat long in degree decimal"
exit 1
fi
homeCoords=`cat ~/.homeCoords`
homeLat=`echo $homeCoords | cut -d ',' -f 1 | tr -d ' ' `
homeLong=`echo $homeCoords | cut -d ',' -f 2 | tr -d ' ' `
#echo Your lat long is $homeLat,$homeLong
coords=`curl "http://www.geohash.info/srv/feed.php?lat=${homeLat}&lon=${homeLong}" | xml_grep --text_only --cond 'item/description' | cut -c 33-54 | sed 's/°//g' `
destLat=`echo $coords | cut -d ' ' -f 1 | tr -d ' ' `
destLong=`echo $coords | cut -d ' ' -f 2 | tr -d ' ' `
#echo The destination lat long is $destLat,$destLong
dist=`distance $homeLat $homeLong $destLat $destLong | sed 's/\./,/g'`
dist=`printf "%0.0f" "$dist"`
message=''
maxDist=7
compare=`echo "$dist<$maxDist" | bc`
if [[ "$compare" -eq "1" ]] ; then
message="Time for geohashing! The geohash is $dist km away!"
else
message="Sorry, geohash is $dist km away"
fi
echo $message
notify-send "$message"