-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgeo3x3.sh
73 lines (71 loc) · 1.58 KB
/
geo3x3.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
geo3x3_encode() {
local lat=$1
local lng=$2
local level=$3
local res=
if [ $level -gt 0 ]; then
if [ `echo "$lng>0" | bc` -eq 1 ]; then
res=E
else
res=W
lng=`echo "scale=16; $lng+180" | bc`
fi
lat=`echo "scale=16; $lat+90" | bc`
unit=180
local i
for ((i=1; i<$level; i++)) do
unit=`echo "scale=16; $unit/3" | bc`
local x=`echo "scale=0; $lng/$unit" | bc`
local y=`echo "scale=0; $lat/$unit" | bc`
res=$res$(($x+$y*3+1))
lng=`echo "scale=16; $lng-$x*$unit" | bc`
lat=`echo "scale=16; $lat-$y*$unit" | bc`
done
fi
echo $res
}
geo3x3_decode() {
local code=$1
local clen=${#code}
if [ $clen -eq 0 ]; then
echo 0 0 0 180
else
local begin=0
local flg=0
local c=${code:0:1}
if [ c = "W" ]; then
flg=1
begin=1
elif [ c = "E" ]; then
begin=1
fi
local lat=0
local lng=0
local level=1
local unit=180
local i=0
for ((i=begin; i<$(($clen-1)); i++)) do
n=$((${code:$i+1:1}-1))
if [ $n -lt 0 ]; then
break
fi
unit=`echo "scale=16; $unit/3" | bc`
local x=$((n%3))
local y=$((n/3))
lng=`echo "scale=16; $lng+$x*$unit" | bc`
lat=`echo "scale=16; $lat+$y*$unit" | bc`
level=$(($level+1))
done
lat=`echo "scale=16; $lat+$unit/2-90" | bc`
lng=`echo "scale=16; $lng+$unit/2" | bc`
if [ $flg -eq 1 ]; then
lng=`echo "scale=16; $lng-180" | bc`
fi
echo $lat $lng $level $unit
fi
}
if [ $1 = "encode" ]; then
echo `geo3x3_encode $2 $3 $4`
else
echo `geo3x3_decode $2`
fi