-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeo.sh
284 lines (271 loc) · 6.96 KB
/
geo.sh
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- mode: sh -*-
#-
# Copyright © 2014, 2021
# mirabilos <m@mirbsd.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.
#-
# Geodesic helper functions
# lat_decimal=$(chklatlon lat "$userinput") # same for lon
# userinput: decimal, geocaching (deg/min) or deg/min/sec
function chklatlon {
local minus plus vmax mins
local -i10 val
local -u arg=${2//+([\'\" ]|’|′|°|$'\xB0')/ }
if [[ $arg = *.* ]]; then
arg=${arg//,/ }
else
arg=${arg//,/.}
fi
arg=${arg##+( )}
arg=${arg%%+( )}
case $1 {
(lat)
minus=S
plus=N
vmax=90
;;
(lon)
minus=W
plus=E
vmax=180
;;
(*)
print internal error
return 1
;;
}
if [[ $arg = ${minus}*( )+([0-9])?(.*([0-9])) ]]; then
arg=-${arg##$minus*( )}
elif [[ $arg = ${plus}*( )+([0-9])?(.*([0-9])) ]]; then
arg=${arg##$plus*( )}
elif [[ $arg = +([0-9])?(.*([0-9]))*( )$minus ]]; then
arg=-${arg%%*( )$minus}
elif [[ $arg = +([0-9])?(.*([0-9]))*( )$plus ]]; then
arg=${arg%%*( )$plus}
elif [[ $arg = ${minus}*( ).+([0-9]) ]]; then
arg=-0${arg##$minus*( )}
elif [[ $arg = ${plus}*( ).+([0-9]) ]]; then
arg=0${arg##$plus*( )}
elif [[ $arg = .+([0-9])*( )$minus ]]; then
arg=-0${arg%%*( )$minus}
elif [[ $arg = .+([0-9])*( )$plus ]]; then
arg=0${arg%%*( )$plus}
elif [[ $arg = ${minus}*( )+([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9])) ]]; then
arg=${arg##$minus*( )}
val=10#${arg%% *}
arg=${arg##*+( )}
arg=-$(dc -e "20k $arg 60/ ${val}+ps.")
elif [[ $arg = ${plus}*( )+([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9])) ]]; then
arg=${arg##$plus*( )}
val=10#${arg%% *}
arg=${arg##*+( )}
arg=$(dc -e "20k $arg 60/ ${val}+ps.")
elif [[ $arg = +([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9]))*( )$minus ]]; then
arg=${arg%%*( )$minus}
val=10#${arg%% *}
arg=${arg##*+( )}
arg=-$(dc -e "20k $arg 60/ ${val}+ps.")
elif [[ $arg = +([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9]))*( )$plus ]]; then
arg=${arg%%*( )$plus}
val=10#${arg%% *}
arg=${arg##*+( )}
arg=$(dc -e "20k $arg 60/ ${val}+ps.")
elif [[ $arg = ${minus}*( )+([0-9])+( )+([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9])) ]]; then
arg=${arg##$minus*( )}
mins=${arg#* }
val=10#${arg%% *}
arg=$mins
mins=10#${arg%% *}
arg=${arg##*+( )}
arg=-$(dc -e "20k $arg 60/ $((mins))+ 60/ ${val}+ps.")
elif [[ $arg = ${plus}*( )+([0-9])+( )+([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9])) ]]; then
arg=${arg##$plus*( )}
mins=${arg#* }
val=10#${arg%% *}
arg=$mins
mins=10#${arg%% *}
arg=${arg##*+( )}
arg=$(dc -e "20k $arg 60/ $((mins))+ 60/ ${val}+ps.")
elif [[ $arg = +([0-9])+( )+([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9]))*( )$minus ]]; then
arg=${arg%%*( )$minus}
mins=${arg#* }
val=10#${arg%% *}
arg=$mins
mins=10#${arg%% *}
arg=${arg##*+( )}
arg=-$(dc -e "20k $arg 60/ $((mins))+ 60/ ${val}+ps.")
elif [[ $arg = +([0-9])+( )+([0-9])+( )@(+([0-9])?(.*([0-9]))|.+([0-9]))*( )$plus ]]; then
arg=${arg%%*( )$plus}
mins=${arg#* }
val=10#${arg%% *}
arg=$mins
mins=10#${arg%% *}
arg=${arg##*+( )}
arg=$(dc -e "20k $arg 60/ $((mins))+ 60/ ${val}+ps.")
fi
arg=${arg#+}
[[ $arg = ?(-)+([0-9]) ]] && arg+=.
[[ $arg = ?(-).+([0-9]) ]] && arg=${arg/./0.}
case $arg {
(+([0-9]).*([0-9]))
#arg=${arg#+}
val=10#${arg%.*}
mins=${arg#*.}
mins=${mins%%*(0)}
if (( val < vmax )); then
:
elif (( val == vmax )) && [[ -z $mins ]]; then
:
else
print -r "value ${arg@Q} out of range"
return 1
fi
arg=$val.${mins:-0}
;;
(-+([0-9]).*([0-9]))
arg=${arg#-}
val=10#${arg%.*}
mins=${arg#*.}
mins=${mins%%*(0)}
if (( val < vmax )); then
:
elif (( val == vmax )) && [[ -z $mins ]]; then
:
else
print -r "value ${arg@Q} out of range"
return 1
fi
arg=-$val.${mins:-0}
;;
(*)
print -r "value ${arg@Q} unrecognised"
return 1
;;
}
print -r -- $arg
}
function decmin2min {
local x=${1#.}0000000
typeset -i10 -Z9 y
x=10#${x::7}
(( y = x * 60 ))
REPLY=${y::2}.${y:2}
}
function decmin2txt {
local graticule=$1 decimal=$2 plus=$3 minus=$4 places=$5 x
typeset -i10 -Z$places n
if [[ $graticule = -* ]]; then
REPLY=$minus
else
REPLY=$plus
fi
n=10#${graticule#-}
REPLY+=" ${n}° "
if ! x=${|decmin2min $decimal;}; then
REPLY="unable to convert ${decimal@Q}"
return 1
fi
typeset -i10 -Z2 n=${x%.*}
x=${x#*.}
typeset -i10 -Z4 m=${x::4}
if (( ${x:4:1} >= 5 )); then
if (( ++m > 9999 )); then
(( ++n ))
m=0
fi
fi
REPLY+=$n.$m
}
# echo "${|lldecmin2txt $lat_decimal $lon_decimal;}"
function lldecmin2txt {
local lat=$1 lon=$2
[[ $lat = *.* ]] || lat+=.
[[ $lon = *.* ]] || lon+=.
lat=${|decmin2txt "${lat%.*}" ".${lat#*.}" N S 2;}
lon=${|decmin2txt "${lon%.*}" ".${lon#*.}" E W 3;}
REPLY="$lat $lon"
}
# distance lat1 lon1 lat2 lon2 → metres (rounded to millimetres), error < ¼%
distance() (
set -e
# make GNU bc use POSIX mode and shut up
export BC_ENV_ARGS=-qs
# assignment of constants, variables and functions
# p: multiply with to convert from degrees to radians (π/180)
# r: earth radius in metres
# d: distance
# h: haversine intermediate
# i,j: (lat,lon) point 1
# x,y: (lat,lon) point 2
# k: delta lat/ΔΦ
# l: delta lon/Δλ
# m: sin(k/2) (square root of hav(k))
# n: sin(l/2) ( partial haversine )
# n(x): arcsin(x)
# r(x,n): round x to n decimal digits
# v(x): sign (Vorzeichen)
# w(x): min(1, sqrt(x)) (Wurzel)
exec bc -l <<-EOF
scale=64
define n(x) {
if (x == -1) return (-2 * a(1))
if (x == 1) return (2 * a(1))
return (a(x / sqrt(1 - x*x)))
}
define v(x) {
if (x < 0) return (-1)
if (x > 0) return (1)
return (0)
}
define r(x, n) {
auto o
o = scale
if (scale < (n + 1)) scale = (n + 1)
x += v(x) * 0.5 * A^-n
scale = n
x /= 1
scale = o
return (x)
}
define w(x) {
if (x >= 1) return (1)
return (sqrt(x))
}
/* WGS84 reference ellipsoid: große Halbachse (metres), Abplattung */
/* (6378137 in WGS84); this radius from Astronomical Almanac 2021 */
i = 6378136.600
x = 1/298.257223563
/* other axis */
j = i * (1 - x)
/* mean radius resulting */
r = (2 * i + j) / 3
/* coordinates */
p = (4 * a(1) / 180)
i = (p * $1)
j = (p * $2)
x = (p * $3)
y = (p * $4)
/* calculation */
k = (x - i)
l = (y - j)
m = s(k / 2)
n = s(l / 2)
h = ((m * m) + (c(i) * c(x) * n * n))
d = 2 * r * n(w(h))
r(d, 3)
EOF
)