Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ruby/lib/plus_codes/open_location_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ def full?(code)
# @return [Array<Integer, Integer>] with the latitude and longitude integer
# values.
def location_to_integers(latitude, longitude)
lat_val = (latitude * PAIR_CODE_PRECISION * LAT_GRID_PRECISION).round
lat_val = (latitude * PAIR_CODE_PRECISION * LAT_GRID_PRECISION).floor
lat_val += 90 * PAIR_CODE_PRECISION * LAT_GRID_PRECISION
if lat_val.negative?
lat_val = 0
elsif lat_val >= 2 * 90 * PAIR_CODE_PRECISION * LAT_GRID_PRECISION
lat_val = 2 * 90 * PAIR_CODE_PRECISION * LAT_GRID_PRECISION - 1
end
lng_val = (longitude * PAIR_CODE_PRECISION * LNG_GRID_PRECISION).round
lng_val = (longitude * PAIR_CODE_PRECISION * LNG_GRID_PRECISION).floor
lng_val += 180 * PAIR_CODE_PRECISION * LNG_GRID_PRECISION
if lng_val.negative?
# Ruby's % operator differs from other languages in that it returns
Expand All @@ -72,11 +72,6 @@ def location_to_integers(latitude, longitude)
# excludes the separator
# @return [String] a plus+codes
def encode(latitude, longitude, code_length = PAIR_CODE_LENGTH)
if invalid_length?(code_length)
raise ArgumentError, 'Invalid Open Location Code(Plus+Codes) length'
end

code_length = MAX_CODE_LENGTH if code_length > MAX_CODE_LENGTH
lat_val, lng_val = location_to_integers(latitude, longitude)
encode_integers(lat_val, lng_val, code_length)
end
Expand All @@ -91,6 +86,11 @@ def encode(latitude, longitude, code_length = PAIR_CODE_LENGTH)
# excludes the separator
# @return [String] a plus+codes
def encode_integers(lat_val, lng_val, code_length = PAIR_CODE_LENGTH)
if invalid_length?(code_length)
raise ArgumentError, 'Invalid Open Location Code(Plus+Codes) length'
end

code_length = MAX_CODE_LENGTH if code_length > MAX_CODE_LENGTH
# Initialise the code using an Array. Array.join is more efficient that
# string addition.
code = Array.new(MAX_CODE_LENGTH + 1, '')
Expand Down
52 changes: 50 additions & 2 deletions ruby/test/plus_codes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,60 @@ def test_decode
end

def test_encode
# Allow a 5% error rate encoding from degree coordinates (because of
# floating point precision).
allowed_error_rate = 0.05
errors = 0
tests = 0
read_csv_lines('encoding.csv').each do |line|
next if line.empty?

tests += 1
cols = line.split(',')
code = @olc.encode(cols[0].to_f, cols[1].to_f, cols[2].to_i)
assert_equal(cols[3], code)
lat_degrees = cols[0].to_f
lng_degrees = cols[1].to_f
code_length = cols[4].to_i
want = cols[5]

code = @olc.encode(lat_degrees, lng_degrees, code_length)
if want != code
errors += 1
puts "ENCODING DIFFERENCE: want #{want}, got #{code}"
end
end
assert_compare(errors.to_f / tests, '<=', allowed_error_rate)
end

def test_location_to_integers
read_csv_lines('encoding.csv').each do |line|
next if line.empty?

cols = line.split(',')
lat_degrees = cols[0].to_f
lng_degrees = cols[1].to_f
lat_integer = cols[2].to_i
lng_integer = cols[3].to_i

got_lat, got_lng = @olc.location_to_integers(lat_degrees, lng_degrees)
# Due to floating point precision limitations, we may get values 1 less
# than expected.
assert_include([lat_integer - 1, lat_integer], got_lat)
assert_include([lng_integer - 1, lng_integer], got_lng)
end
end

def test_encode_integers
read_csv_lines('encoding.csv').each do |line|
next if line.empty?

cols = line.split(',')
lat_integer = cols[2].to_i
lng_integer = cols[3].to_i
code_length = cols[4].to_i
want = cols[5]

code = @olc.encode_integers(lat_integer, lng_integer, code_length)
assert_equal(want, code)
end
end

Expand Down
Loading