-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from taisukef/DNCL3
add DNCL3
- Loading branch information
Showing
3 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
function encode(lat, lng, level) { | ||
res = "E" | ||
if lng < 0 { | ||
res = "W" | ||
lng = lng + 180 | ||
} | ||
lat = lat + 90 # 180:the North Pole, 0:the South Pole | ||
unit = 180 | ||
for i = 1 to level - 1 { | ||
unit = unit / 3 | ||
x = lng // unit | ||
y = lat // unit | ||
res = res + (x + y * 3 + 1) | ||
lng = lng - x * unit | ||
lat = lat - y * unit | ||
} | ||
return res | ||
} | ||
|
||
function char2n(c) { | ||
if c == "0" { | ||
return 0 | ||
} else if c == "1" { | ||
return 1 | ||
} else if c == "2" { | ||
return 2 | ||
} else if c == "3" { | ||
return 3 | ||
} else if c == "4" { | ||
return 4 | ||
} else if c == "5" { | ||
return 5 | ||
} else if c == "6" { | ||
return 6 | ||
} else if c == "7" { | ||
return 7 | ||
} else if c == "8" { | ||
return 8 | ||
} else if c == "9" { | ||
return 9 | ||
} | ||
return -1 | ||
} | ||
|
||
function decode(code) { | ||
flg = 0 | ||
if code[0] == "W" { | ||
flg = 1 | ||
} | ||
unit = 180 | ||
lat = 0 | ||
lng = 0 | ||
level = 1 | ||
while 1 { | ||
n = char2n(code[level]) | ||
if n <= 0 { | ||
break | ||
} | ||
unit = unit / 3 | ||
n = n - 1 | ||
lng = lng + n % 3 * unit | ||
lat = lat + n // 3 * unit | ||
level = level + 1 | ||
} | ||
lat = lat + unit / 2 | ||
lng = lng + unit / 2 | ||
lat = lat - 90 | ||
if flg { | ||
lng = lng - 180 | ||
} | ||
return [lat, lng, level, unit] | ||
} | ||
|
||
print encode(35.65858, 139.745433, 14) | ||
print decode("E9139659937288") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters