open-location-code 0.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 378b33bb5f5bdf0ec82c286db7e7b54b762f2723
4
- data.tar.gz: b2f0ded83b4ed9c914a0220c177ae6bb3e43adf5
3
+ metadata.gz: a0d234e42bc6ce5440921cf810d7dc09afb7db98
4
+ data.tar.gz: 9cc1ebd02ade1948031790c45dafaec52e71c21c
5
5
  SHA512:
6
- metadata.gz: 052d9fb0aeeab2fd7969ae25b00824813e9f63b4877d7e73d5a5aa75d5d51c17fc1eeb51482c1f08f8cfa3d50ca3793ca2e44263c225c9244b9e5fa541f94c51
7
- data.tar.gz: 34f7c50ad9dd924e7afaa441ab3cdefe80b32de655b97cdb17f5c06d3c698738855e5c80af8d3c08f1cdce82bb494e23b22580933893d8a77cfb686fe4fa5f47
6
+ metadata.gz: 596fcb762c45bc6eee4c3ec289e4310f4b12e61dd9b8c7589eed88e3f4f8f302580f92902df835796ae92678abd9c006f810b396769d6c94e504cbf011f25dd1
7
+ data.tar.gz: 0bbc96634049251bb8fb133baeed3c93622805d23c8740fb0a526748a08b33a1f911cb9d4cc98d5fe72a3dcd1970ab007a6e0b0303ee4d2b2ba796bf1e198293
@@ -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 = 10)
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 < 10
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
- area_range = precision_by_length(prefix_len)
125
- area_edge = area_range / 2
124
+ resolution = precision_by_length(prefix_len)
125
+ half_res = resolution / 2
126
126
 
127
127
  latitude = code_area.latitude_center
128
- latitude_diff = latitude - ref_lat
129
- if (latitude_diff > area_edge)
130
- latitude -= area_range
131
- elsif (latitude_diff < -area_edge)
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
- longitude_diff = longitude - ref_lng
137
- if (longitude_diff > area_edge)
138
- longitude -= area_range
139
- elsif (longitude_diff < -area_edge)
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 + 2) / 2
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 < 10
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 < 10
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 < SEPARATOR_POSITION && code_length.odd?)
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 <= 10
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 - 10))
242
+ precision = (20 ** -3) / (5 ** (code_length - PAIR_CODE_LENGTH))
245
243
  end
246
244
  precision.to_r
247
245
  end
@@ -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
- assert_true(result)
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
- assert_true((code_area.south_latitude - cols[3].to_f).abs < 0.001)
37
- assert_true((code_area.west_longitude - cols[4].to_f).abs < 0.001)
38
- assert_true((code_area.north_latitude - cols[5].to_f).abs < 0.001)
39
- assert_true((code_area.east_longitude - cols[6].to_f).abs < 0.001)
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
- short = @olc.shorten(code, lat, lng)
51
- assert_equal(short_code, short)
52
- expanded = @olc.recover_nearest(short, lat, lng)
53
- assert_equal(code, expanded)
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
- assert_false(@olc.valid?('3W00CJJJ+'))
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: 0.0.1
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: 2015-10-11 00:00:00.000000000 Z
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.4.5
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)