Parsing a NMEA GGA string

require 'time'

def decimalize(raw_x, raw_nesw)

  nesw = {n: :+, e: :+, s: :-, w: :-}
  x = 0.method(nesw[raw_nesw.downcase.to_sym]).call(raw_x.to_f)

  degrees = (x / 100).to_i
  minutes = x - (degrees * 100.0)
  (degrees + (minutes / 60)).round(8)
end

s = '$GPGGA,045612.00,5554.95457,N,00306.82246,W,1,08,1.22,96.7,M,49.8,M,,*76'
_, raw_time, raw_lat, ns, raw_lon, ew = s.split(',')
time, latitude, longitude = Time.parse(raw_time), 
                              decimalize(raw_lat, ns), decimalize(raw_lon, ew)

#=> [2014-07-05 04:56:12 +0100, 55.9159095, -3.11370767] 

Resources

Tags:
Source:
1030hrs.txt
Published:
05-07-2014 10:30