Class: GPX::MagellanTrackLog

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx/magellan_track_log.rb

Overview

This class will parse the lat/lon and time data from a Magellan track log, which is a NMEA formatted CSV list of points.

Constant Summary collapse

LAT =
1
LAT_HEMI =
2
LON =
3
LON_HEMI =
4
ELE =
5
ELE_UNITS =
6
TIME =
7
INVALID_FLAG =
8
DATE =
10
FEET_TO_METERS =
0.3048

Class Method Summary collapse

Class Method Details

.convert_to_gpx(magellan_filename, gpx_filename) ⇒ Object

Takes the name of a magellan file, converts the contents to GPX, and writes the result to gpx_filename.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gpx/magellan_track_log.rb', line 38

def convert_to_gpx(magellan_filename, gpx_filename)
  segment = Segment.new

  CSV.open(magellan_filename, 'r').each do |row|
    next if (row.size < 10) || (row[INVALID_FLAG] == 'V')

    lat_deg = row[LAT][0..1]
    lat_min = row[LAT][2...-1]
    lat_hemi = row[LAT_HEMI]

    lat = lat_deg.to_f + (lat_min.to_f / 60.0)
    lat = -lat if lat_hemi == 'S'

    lon_deg = row[LON][0..2]
    lon_min = row[LON][3..-1]
    lon_hemi = row[LON_HEMI]

    lon = lon_deg.to_f + (lon_min.to_f / 60.0)
    lon = -lon if lon_hemi == 'W'

    ele = row[ELE]
    ele_units = row[ELE_UNITS]
    ele = ele.to_f
    ele *= FEET_TO_METERS if ele_units == 'F'

    hrs = row[TIME][0..1].to_i
    mins = row[TIME][2..3].to_i
    secs = row[TIME][4..5].to_i
    day = row[DATE][0..1].to_i
    mon = row[DATE][2..3].to_i
    yr = 2000 + row[DATE][4..5].to_i

    time = Time.gm(yr, mon, day, hrs, mins, secs)

    # must create point
    pt = TrackPoint.new(lat: lat, lon: lon, time: time, elevation: ele)
    segment.append_point(pt)
  end

  trk = Track.new
  trk.append_segment(segment)
  gpx_file = GPXFile.new(tracks: [trk])
  gpx_file.write(gpx_filename)
end

.magellan_file?(filename) ⇒ Boolean

Tests to see if the given file is a magellan NMEA track log.

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gpx/magellan_track_log.rb', line 84

def magellan_file?(filename)
  i = 0
  File.open(filename, 'r') do |f|
    f.each do |line|
      i += 1
      return true if line =~ /^\$PMGNTRK/
      return false if line =~ /<\?xml/
      return false if i > 10
    end
  end
  false
end