X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/043d29fd7eb72048cf5d07edfbc20ec5c25af708..9143fd61e8ae268ec40837efdb2b7cae49783b4d:/lib/short_link.rb?ds=sidebyside diff --git a/lib/short_link.rb b/lib/short_link.rb index ec83ce33f..eb73d6029 100644 --- a/lib/short_link.rb +++ b/lib/short_link.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + ## # Encodes and decodes locations from Morton-coded "quad tile" strings. Each # variable-length string encodes to a precision of one pixel per tile (roughly, @@ -23,7 +25,7 @@ module ShortLink # keep support for old shortlinks which use the @ character, now # replaced by the ~ character because twitter is horribly broken # and we can't have that. - str.tr!("@", "~") + str = str.tr("@", "~") str.each_char do |c| t = ARRAY.index c @@ -32,11 +34,11 @@ module ShortLink else 3.times do x <<= 1 - x |= 1 unless (t & 32).zero? + x |= 1 unless t.nobits?(32) t <<= 1 y <<= 1 - y |= 1 unless (t & 32).zero? + y |= 1 unless t.nobits?(32) t <<= 1 end z += 3 @@ -47,21 +49,21 @@ module ShortLink y <<= (32 - z) # project the parameters back to their coordinate ranges. - [(x * 360.0 / 2**32) - 180.0, - (y * 180.0 / 2**32) - 90.0, + [(x * 360.0 / (2**32)) - 180.0, + (y * 180.0 / (2**32)) - 90.0, z - 8 - (z_offset % 3)] end ## # given a location and zoom, return a short string representing it. def encode(lon, lat, z) - code = interleave_bits(((lon + 180.0) * 2**32 / 360.0).to_i, - ((lat + 90.0) * 2**32 / 180.0).to_i) - str = "" + code = interleave_bits(((lon + 180.0) * (2**32) / 360.0).to_i, + ((lat + 90.0) * (2**32) / 180.0).to_i) + str = +"" # add eight to the zoom level, which approximates an accuracy of # one pixel in a tile. ((z + 8) / 3.0).ceil.times do |i| - digit = (code >> (58 - 6 * i)) & 0x3f + digit = (code >> (58 - (6 * i))) & 0x3f str << ARRAY[digit] end # append characters onto the end of the string to represent