open-location-code 0.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/lib/plus_codes.rb +5 -0
- data/lib/plus_codes/open_location_code.rb +18 -20
- data/test/plus_codes_test.rb +18 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0d234e42bc6ce5440921cf810d7dc09afb7db98
|
4
|
+
data.tar.gz: 9cc1ebd02ade1948031790c45dafaec52e71c21c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 596fcb762c45bc6eee4c3ec289e4310f4b12e61dd9b8c7589eed88e3f4f8f302580f92902df835796ae92678abd9c006f810b396769d6c94e504cbf011f25dd1
|
7
|
+
data.tar.gz: 0bbc96634049251bb8fb133baeed3c93622805d23c8740fb0a526748a08b33a1f911cb9d4cc98d5fe72a3dcd1970ab007a6e0b0303ee4d2b2ba796bf1e198293
|
data/lib/plus_codes.rb
CHANGED
@@ -9,6 +9,11 @@ module PlusCodes
|
|
9
9
|
# The max number of characters can be placed before the separator.
|
10
10
|
SEPARATOR_POSITION = 8
|
11
11
|
|
12
|
+
# Maxiumum code length using lat/lng pair encoding. The area of such a
|
13
|
+
# code is approximately 13x13 meters (at the equator), and should be suitable
|
14
|
+
# for identifying buildings. This excludes prefix and separator characters.
|
15
|
+
PAIR_CODE_LENGTH = 10
|
16
|
+
|
12
17
|
# The character used to pad a code
|
13
18
|
PADDING = '0'.freeze
|
14
19
|
|
@@ -41,7 +41,7 @@ module PlusCodes
|
|
41
41
|
# @param longitude [Numeric] a longitude in degrees
|
42
42
|
# @param code_length [Integer] the number of characters in the code, this excludes the separator
|
43
43
|
# @return [String] a plus+codes
|
44
|
-
def encode(latitude, longitude, code_length =
|
44
|
+
def encode(latitude, longitude, code_length = PAIR_CODE_LENGTH)
|
45
45
|
raise ArgumentError,
|
46
46
|
"Invalid Open Location Code(Plus+Codes) length: #{code_length}" if invalid_length?(code_length)
|
47
47
|
|
@@ -83,7 +83,7 @@ module PlusCodes
|
|
83
83
|
|
84
84
|
digit = 0
|
85
85
|
while digit < code.length
|
86
|
-
if digit <
|
86
|
+
if digit < PAIR_CODE_LENGTH
|
87
87
|
lat_resolution /= 20
|
88
88
|
lng_resolution /= 20
|
89
89
|
south_latitude += lat_resolution * DECODE[code[digit].ord]
|
@@ -121,23 +121,21 @@ module PlusCodes
|
|
121
121
|
code = prefix_by_reference(ref_lat, ref_lng, prefix_len) << short_code
|
122
122
|
code_area = decode(code)
|
123
123
|
|
124
|
-
|
125
|
-
|
124
|
+
resolution = precision_by_length(prefix_len)
|
125
|
+
half_res = resolution / 2
|
126
126
|
|
127
127
|
latitude = code_area.latitude_center
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
latitude += area_range
|
128
|
+
if (ref_lat + half_res < latitude && latitude - resolution >= -90)
|
129
|
+
latitude -= resolution
|
130
|
+
elsif (ref_lat - half_res > latitude && latitude + resolution <= 90)
|
131
|
+
latitude += resolution
|
133
132
|
end
|
134
133
|
|
135
134
|
longitude = code_area.longitude_center
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
longitude += area_range
|
135
|
+
if (ref_lng + half_res < longitude)
|
136
|
+
longitude -= resolution
|
137
|
+
elsif (ref_lng - half_res > longitude)
|
138
|
+
longitude += resolution
|
141
139
|
end
|
142
140
|
|
143
141
|
encode(latitude, longitude, code.length - SEPARATOR.length)
|
@@ -160,7 +158,7 @@ module PlusCodes
|
|
160
158
|
lng_diff = (longitude - code_area.longitude_center).abs
|
161
159
|
max_diff = [lat_diff, lng_diff].max
|
162
160
|
[8, 6, 4].each do |removal_len|
|
163
|
-
area_edge = precision_by_length(removal_len
|
161
|
+
area_edge = precision_by_length(removal_len) * 0.3
|
164
162
|
return code[removal_len..-1] if max_diff < area_edge
|
165
163
|
end
|
166
164
|
|
@@ -180,7 +178,7 @@ module PlusCodes
|
|
180
178
|
if digit == 0
|
181
179
|
latitude /= 20
|
182
180
|
longitude /= 20
|
183
|
-
elsif digit <
|
181
|
+
elsif digit < PAIR_CODE_LENGTH
|
184
182
|
latitude *= 20
|
185
183
|
longitude *= 20
|
186
184
|
else
|
@@ -193,7 +191,7 @@ module PlusCodes
|
|
193
191
|
def build_code(digit_count, code, latitude, longitude)
|
194
192
|
lat_digit = latitude.to_i
|
195
193
|
lng_digit = longitude.to_i
|
196
|
-
if digit_count <
|
194
|
+
if digit_count < PAIR_CODE_LENGTH
|
197
195
|
code << CODE_ALPHABET[lat_digit]
|
198
196
|
code << CODE_ALPHABET[lng_digit]
|
199
197
|
[digit_count + 2, latitude - lat_digit, longitude - lng_digit]
|
@@ -230,7 +228,7 @@ module PlusCodes
|
|
230
228
|
end
|
231
229
|
|
232
230
|
def invalid_length?(code_length)
|
233
|
-
code_length < 2 || (code_length <
|
231
|
+
code_length < 2 || (code_length < PAIR_CODE_LENGTH && code_length.odd?)
|
234
232
|
end
|
235
233
|
|
236
234
|
def padded(code)
|
@@ -238,10 +236,10 @@ module PlusCodes
|
|
238
236
|
end
|
239
237
|
|
240
238
|
def precision_by_length(code_length)
|
241
|
-
if code_length <=
|
239
|
+
if code_length <= PAIR_CODE_LENGTH
|
242
240
|
precision = 20 ** ((code_length / -2).to_i + 2)
|
243
241
|
else
|
244
|
-
precision = (20 ** -3) / (5 ** (code_length -
|
242
|
+
precision = (20 ** -3) / (5 ** (code_length - PAIR_CODE_LENGTH))
|
245
243
|
end
|
246
244
|
precision.to_r
|
247
245
|
end
|
data/test/plus_codes_test.rb
CHANGED
@@ -19,7 +19,7 @@ class PlusCodesTest < Test::Unit::TestCase
|
|
19
19
|
is_short_olc = @olc.short?(code)
|
20
20
|
is_full_olc = @olc.full?(code)
|
21
21
|
result = is_valid_olc == is_valid && is_short_olc == is_short && is_full_olc == is_full
|
22
|
-
|
22
|
+
assert(result)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -33,10 +33,10 @@ class PlusCodesTest < Test::Unit::TestCase
|
|
33
33
|
code = @olc.encode(cols[1].to_f, cols[2].to_f, cols[0].length - 1)
|
34
34
|
end
|
35
35
|
assert_equal(cols[0], code)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
assert((code_area.south_latitude - cols[3].to_f).abs < 0.001)
|
37
|
+
assert((code_area.west_longitude - cols[4].to_f).abs < 0.001)
|
38
|
+
assert((code_area.north_latitude - cols[5].to_f).abs < 0.001)
|
39
|
+
assert((code_area.east_longitude - cols[6].to_f).abs < 0.001)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -47,10 +47,15 @@ class PlusCodesTest < Test::Unit::TestCase
|
|
47
47
|
lat = cols[1].to_f
|
48
48
|
lng = cols[2].to_f
|
49
49
|
short_code = cols[3]
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
test_type = cols[4]
|
51
|
+
if test_type == 'B' || test_type == 'S'
|
52
|
+
short = @olc.shorten(code, lat, lng)
|
53
|
+
assert_equal(short_code, short)
|
54
|
+
end
|
55
|
+
if test_type == 'B' || test_type == 'R'
|
56
|
+
expanded = @olc.recover_nearest(short_code, lat, lng)
|
57
|
+
assert_equal(code, expanded)
|
58
|
+
end
|
54
59
|
end
|
55
60
|
@olc.shorten('9C3W9QCJ+2VX', 60.3701125, 10.202665625)
|
56
61
|
end
|
@@ -63,6 +68,9 @@ class PlusCodesTest < Test::Unit::TestCase
|
|
63
68
|
assert_raise ArgumentError do
|
64
69
|
@olc.encode(20, 30, 1)
|
65
70
|
end
|
71
|
+
assert_raise ArgumentError do
|
72
|
+
@olc.encode(20, 30, 9)
|
73
|
+
end
|
66
74
|
assert_raise ArgumentError do
|
67
75
|
@olc.recover_nearest('9C3W9QCJ-2VX', 51.3708675, -1.217765625)
|
68
76
|
end
|
@@ -79,7 +87,7 @@ class PlusCodesTest < Test::Unit::TestCase
|
|
79
87
|
end
|
80
88
|
|
81
89
|
def test_valid_with_special_case
|
82
|
-
|
90
|
+
assert(!@olc.valid?('3W00CJJJ+'))
|
83
91
|
end
|
84
92
|
|
85
93
|
def read_csv_lines(csv_file)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open-location-code
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wei-Ming Wu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
113
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.
|
114
|
+
rubygems_version: 2.6.13
|
115
115
|
signing_key:
|
116
116
|
specification_version: 4
|
117
117
|
summary: Ruby implementation of Google Open Location Code(Plus+Codes)
|