-
Notifications
You must be signed in to change notification settings - Fork 16
/
geo3x3.psm1
69 lines (68 loc) · 1.44 KB
/
geo3x3.psm1
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
function ConvertTo-Geo3x3 {
param (
[double]$lat,
[double]$lng,
[int]$level
)
if ($level -lt 1) {
return $null
}
[string]$res = ""
if ($lng -ge 0) {
$res = "E"
}else {
$res = "W"
$lng += 180
}
$lat += 90
[double]$unit = 180
for ($i = 1; $i -lt $level; $i++) {
$unit /= 3
$x = [Math]::Floor($lng / $unit)
$y = [Math]::Floor($lat / $unit)
$res += $x + $y * 3 + 1
$lng -= [double]$x * $unit
$lat -= [double]$y * $unit
}
return $res
}
function ConvertFrom-Geo3x3 {
param (
[string]$code
)
[int]$clen = $code.Length
if ($clen -eq 0) {
return $null
}
[int]$begin = 0
[bool]$flg = $false
[char]$c = $code[0]
if (($c -eq '-') -or ($c -eq 'W')) {
$flg = $true
$begin = 1
}elseif (($c -eq '+') -or ($c -eq 'E')) {
$begin = 1
}
[double]$unit = 180
[double]$lat = 0
[double]$lng = 0
[int]$level = 1
for ($i = $begin; $i -lt $clen; $i++) {
$n = "0123456789".IndexOf($code[$i])
if ($n -le 0) {
return $null
}
$unit /= 3
$n -= 1
$lng += [double]$n % 3 * $unit
$lat += [double]$n / 3 * $unit
$level += 1
}
$lat += $unit / 2
$lng += $unit / 2
$lat -= 90
if ($flg) {
$lng -= 180
}
return ($lat, $lng, $level,$unit)
}