Josef
Josef

Reputation: 232

Bit operations in Lua

I try to parse gpx files and to output encoded polylines (Google algorithm)

test.gpx

    <trkseg>
                <trkpt lon="-120.2" lat="38.5"/>
                <trkpt lon="-120.95" lat="40.7"/>
                <trkpt lon="-126.453" lat="43.252"/>
    </trkseg>

I managed most of it, but have trouble with encoding the numbers

gpx2epl:

file = io.open(arg[1], "r")
io.input(file)
--
function round(number, precision)
   return math.floor(number*math.pow(10,precision)+0.5) / math.pow(10,precision)
end

function encodeNumber(number)
  return number
end
--
local Olatitude = 0
local Olongitude = 0
--
while true do
  local line = io.read()
  if line == nil 
  then 
    break 
  end
  if string.match(line, "trkpt") then
    local latitude
    local longitude
    local encnum
    latitude = string.match(line, 'lat="(.-)"')
    longitude = string.match(line, 'lon="(.-)"')
    latitude = round(latitude,5)*100000
    longitude = round(longitude,5)*100000
    encnum = encodeNumber(latitude-Olatitude)
    print(encnum)
    encnum = encodeNumber(longitude-Olongitude)
    print(encnum)
    Olatitude = latitude
    Olongitude = longitude
  end
end

This script produces the expected output (see: Google Link), with the exception of encoded latitude and longitude.

3850000
-12020000
220000
-75000
255200
-550300

Mapquest provides an implementation in Javascript:

function encodeNumber(num) {
   var num = num << 1;
   if (num < 0) {
      num = ~(num);
   }
   var encoded = '';
   while (num >= 0x20) {
      encoded += String.fromCharCode((0x20 | (num & 0x1f)) + 63);
      num >>= 5;
   }
   encoded += String.fromCharCode(num + 63);
   return encoded;   
}

Can this be done in Lua? Can somebody please help me out. I have no idea how to implement this in Lua.

Edit:

Based on Doug's advice, I did:

function encodeNumber(number)
  local num = number
  num = num * 2
  if num < 0
  then
    num = (num * -1) - 1
  end
  while num >= 32
  do
    local num2 = 32 + (num % 32) + 63
    print(string.char(num2))
    num = num / 32
  end
  print(string.char(num + 63) .. "\n-----")
end

encodeNumber(3850000)   -- _p~iF
encodeNumber(-12020000) -- ~ps|U
encodeNumber(220000)    -- _ulL
encodeNumber(-75000)    -- nnqC
encodeNumber(255200)    -- _mqN
encodeNumber(-550300)   -- vxq`@

It's near expected output, but only near ... Any hint?

Upvotes: 1

Views: 1760

Answers (1)

Doug Currie
Doug Currie

Reputation: 41180

Taking encodeNumber piecemeal...

   var num = num << 1;

This is just num = num * 2

   num = ~(num);

This is num = (- num) - 1

   0x20 | (num & 0x1f)

Is equivalent to 32 + (num % 32)

   num >>= 5

Is equivalent to num = math.floor(num / 32)

ADDENDUM

To concatenate the characters, use a table to collect them:

function encodeNumber(number)
  local num = number
  num = num * 2
  if num < 0
  then
    num = (num * -1) - 1
  end
  local t = {}
  while num >= 32
  do
    local num2 = 32 + (num % 32) + 63
    table.insert(t,string.char(num2))
    num = math.floor(num / 32) -- use floor to keep integer portion only
  end
  table.insert(t,string.char(num + 63))
  return table.concat(t)
end

Upvotes: 3

Related Questions