geo3x3 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/geo3x3/version.rb +1 -1
- data/lib/geo3x3.rb +29 -15
- data/test/test_geo3x3.rb +49 -0
- metadata +5 -2
data/lib/geo3x3/version.rb
CHANGED
data/lib/geo3x3.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "geo3x3/version"
|
1
|
+
require File.expand_path("../geo3x3/version", __FILE__)
|
2
2
|
|
3
3
|
class Numeric
|
4
4
|
def east_longitude?
|
@@ -21,34 +21,35 @@ end
|
|
21
21
|
|
22
22
|
class String
|
23
23
|
def east_longitude?
|
24
|
-
self[0] == "E"
|
24
|
+
self[0] == "E" or self[0] == "+"
|
25
25
|
end
|
26
26
|
|
27
27
|
def west_longitude?
|
28
|
-
self[0] == "W"
|
28
|
+
self[0] == "W" or self[0] == "-"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
module Geo3x3
|
33
33
|
|
34
34
|
def self.encode(params)
|
35
|
-
lat = params[:lat]
|
36
|
-
lng = params[:lng]
|
35
|
+
lat = params[:lat].to_f
|
36
|
+
lng = params[:lng].to_f
|
37
37
|
level = params[:level]
|
38
|
+
raise ArgumentError if level < 1
|
38
39
|
|
39
40
|
code =
|
40
41
|
if lng.east_longitude?
|
41
42
|
"E"
|
42
43
|
else
|
44
|
+
lng += 180
|
43
45
|
"W"
|
44
|
-
lng = (180-lng)
|
45
46
|
end
|
46
47
|
|
47
48
|
lat = 90 - lat
|
48
49
|
|
49
|
-
unit = 180
|
50
|
-
(level-1).times do
|
51
|
-
unit /= 3
|
50
|
+
unit = 180.0
|
51
|
+
(level-1).times do
|
52
|
+
unit /= 3
|
52
53
|
x = (lng/unit).floor
|
53
54
|
y = (lat/unit).floor
|
54
55
|
code += ((x + y*3) + 1).to_s
|
@@ -61,24 +62,37 @@ module Geo3x3
|
|
61
62
|
end
|
62
63
|
|
63
64
|
def self.decode(code)
|
64
|
-
|
65
|
-
|
66
|
-
|
65
|
+
code =
|
66
|
+
case code
|
67
|
+
when Integer
|
68
|
+
code.west_longitude? ? "W#{code}" : "E#{code}"
|
69
|
+
when String
|
70
|
+
raise ArgumentError if code.empty?
|
71
|
+
code
|
72
|
+
else
|
73
|
+
raise ArgumentError
|
74
|
+
end
|
75
|
+
|
76
|
+
unit = 180.0
|
77
|
+
lat = 0.0
|
78
|
+
lng = 0.0
|
67
79
|
level = 1
|
68
80
|
|
69
81
|
code[1..-1].each_char do |c|
|
70
82
|
break if c == "0"
|
71
83
|
n = c.to_i
|
72
84
|
|
73
|
-
unit /= 3
|
85
|
+
unit /= 3
|
74
86
|
n -= 1
|
75
87
|
lng += (n % 3) * unit
|
76
|
-
lat += (n / 3)
|
88
|
+
lat += (n / 3) * unit
|
77
89
|
level += 1
|
78
90
|
end
|
79
91
|
|
80
|
-
|
92
|
+
lat += unit / 2
|
93
|
+
lng += unit / 2
|
81
94
|
lat = 90 - lat
|
95
|
+
lng -= 180 if code.west_longitude?
|
82
96
|
|
83
97
|
{
|
84
98
|
lat: lat,
|
data/test/test_geo3x3.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "minitest/unit"
|
2
|
+
require "minitest/autorun"
|
3
|
+
require File.expand_path("../../lib/geo3x3", __FILE__)
|
4
|
+
|
5
|
+
class TestGeo3x3 < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
def test_decode
|
8
|
+
assert_equal({
|
9
|
+
lat: 40.0,
|
10
|
+
lng: -86.2962962962963,
|
11
|
+
level: 6,
|
12
|
+
unit: 0.7407407407407408}, Geo3x3.decode("W28644"))
|
13
|
+
assert_equal({
|
14
|
+
lat: 11.111111111111114,
|
15
|
+
lng: 105.55555555555556,
|
16
|
+
level: 5.0,
|
17
|
+
unit: 2.2222222222222223}, Geo3x3.decode("E5379"))
|
18
|
+
assert_equal({
|
19
|
+
lat: 40.0,
|
20
|
+
lng: -86.2962962962963,
|
21
|
+
level: 6,
|
22
|
+
unit: 0.7407407407407408}, Geo3x3.decode("-28644"))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_encode
|
26
|
+
accepts = [
|
27
|
+
%w(W
|
28
|
+
W2
|
29
|
+
W28
|
30
|
+
W286
|
31
|
+
W2864
|
32
|
+
W28644
|
33
|
+
W286445
|
34
|
+
W2864455
|
35
|
+
W28644555)
|
36
|
+
]
|
37
|
+
|
38
|
+
[
|
39
|
+
[40.0, -86.2962962962963]
|
40
|
+
].each_with_index do |params, i|
|
41
|
+
assert_raises(ArgumentError) do
|
42
|
+
Geo3x3.encode(lat: params[0], lng: params[1], level: 0)
|
43
|
+
end
|
44
|
+
(1..9).each do |l|
|
45
|
+
assert_equal accepts[i][l - 1], Geo3x3.encode(lat: params[0], lng: params[1], level: l)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo3x3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- lib/geo3x3/version.rb
|
45
45
|
- spec/geo3x3_spec.rb
|
46
46
|
- spec/spec_helper.rb
|
47
|
+
- test/test_geo3x3.rb
|
47
48
|
homepage: https://github.com/kitak/geo3x3
|
48
49
|
licenses: []
|
49
50
|
post_install_message:
|
@@ -71,3 +72,5 @@ summary: geo zone encoding
|
|
71
72
|
test_files:
|
72
73
|
- spec/geo3x3_spec.rb
|
73
74
|
- spec/spec_helper.rb
|
75
|
+
- test/test_geo3x3.rb
|
76
|
+
has_rdoc:
|