-
Notifications
You must be signed in to change notification settings - Fork 16
/
geo3x3.f90
75 lines (68 loc) · 1.44 KB
/
geo3x3.f90
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
module geo3x3
implicit none
contains
character(50) function geo3x3_encode(lat, lng, level)
real lat
real lng
integer level
real flat
real flng
real unit
integer i, x, y
flat = lat
flng = lng
if (lng >= 0.0) then
geo3x3_encode = "E"
else
geo3x3_encode = "W"
flng = flng + 180.0
end if
flat = flat + 90.0
unit = 180.0
do i = 1, level - 1
unit = unit / 3
x = floor(flng / unit)
y = floor(flat / unit)
geo3x3_encode(i + 1: i + 1) = char(x + y * 3 + 1 + 48)
flng = flng - x * unit
flat = flat - y * unit
end do
!geo3x3_encode = "W"
end
function geo3x3_decode(code)
character(len=*) code
real geo3x3_decode(4)
real lat
real lng
real level
real unit
integer flg, i, n
lat = 0.0
lng = 0.0
level = 0
unit = 180.0
if (len(code) > 0) then
flg = 0
if (code(1:1) == "W") then
flg = 1
end if
level = level + 1
do i = 2, len(code)
n = index("123456789", code(i:i)) - 1
if (n >= 0) then
unit = unit / 3
lng = lng + mod(n, 3) * unit
lat = lat + n / 3 * unit
level = level + 1
end if
end do
lat = lat + unit / 2
lng = lng + unit / 2
lat = lat - 90
if (flg == 1) then
lng = lng - 180.0
end if
end if
geo3x3_decode = [lat, lng, level, unit]
end
end