Class: GoobyGpxParser
- Inherits:
-
GoobyBaseSaxParser
- Object
- GoobyBaseSaxParser
- GoobyGpxParser
- Defined in:
- lib/gooby_gpx_parser.rb
Overview
Gooby = Google APIs + Ruby. Copyright 2009 by Chris Joakim. Gooby is available under GNU General Public License (GPL) license.
This class is used to parse a gpx xml file, such as from MotionX GPS software on an iPhone.
This implementation, based on one sample file, handles a gpx file with one ‘trk’. A future implementation may handle multiple ‘trk’ elements per document, as well as ‘trkseg’ aggregates. The observed structure of a gpx document is:
gpx
gpx|trk
gpx|trk|name
gpx|trk|desc
gpx|trk|trkseg
gpx|trk|trkseg|trkpt
gpx|trk|trkseg|trkpt|ele
gpx|trk|trkseg|trkpt|time
Instance Attribute Summary
Attributes inherited from GoobyBaseSaxParser
#current_tagname, #end_reached, #error, #exception, #list, #mappings, #xml
Instance Method Summary collapse
- #current_path ⇒ Object
- #on_end_element(name, prefix = nil, uri = nil) ⇒ Object
- #on_start_element(name, attributes, prefix = nil, uri = nil, namespaces = nil) ⇒ Object
-
#post_process ⇒ Object
Coumpute the elapsed time and distance of each Trackpoint within the Activity.
- #subclass_initialize ⇒ Object
- #trackpoints_to_csv ⇒ Object
Methods inherited from GoobyBaseSaxParser
#end_reached?, #get_current_text, #has_error?, #has_exception?, #initialize, #on_cdata_block, #on_characters, #on_end_document, #on_error, #process_file, #process_xml, #set_error
Constructor Details
This class inherits a constructor from GoobyBaseSaxParser
Instance Method Details
#current_path ⇒ Object
60 61 62 |
# File 'lib/gooby_gpx_parser.rb', line 60 def current_path @tags.join('|') end |
#on_end_element(name, prefix = nil, uri = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gooby_gpx_parser.rb', line 45 def on_end_element(name, prefix=nil, uri=nil) path = current_path if path == 'gpx|trk|name' @name = get_current_text elsif path == 'gpx|trk|desc' @desc = get_current_text elsif path == 'gpx|trk|trkseg|trkpt|ele' @current_trackpoint.set('AltitudeMeters', get_current_text) elsif path == 'gpx|trk|trkseg|trkpt|time' @current_trackpoint.set('Time', get_current_text) end @current_text = '' @tags.pop end |
#on_start_element(name, attributes, prefix = nil, uri = nil, namespaces = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gooby_gpx_parser.rb', line 32 def on_start_element(name, attributes, prefix=nil, uri=nil, namespaces=nil) @tags.push(name) @current_text = '' path = current_path if path == 'gpx|trk|trkseg|trkpt' @current_trackpoint = GoobyTrackpoint.new @current_trackpoint.set('LatitudeDegrees', attributes['lat']) @current_trackpoint.set('LongitudeDegrees', attributes['lon']) @current_trackpoint.set('Time', get_current_text) @trackpoints << @current_trackpoint end end |
#post_process ⇒ Object
Coumpute the elapsed time and distance of each Trackpoint within the Activity.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gooby_gpx_parser.rb', line 65 def post_process cumulative_distance, first_tkpt_dttm, prev_tkpt, activ_id = 0.0, nil, nil, @name @trackpoints.each_with_index { | tkpt, idx | if prev_tkpt incremental_distance = tkpt.proximity(prev_tkpt) cumulative_distance = cumulative_distance + incremental_distance tkpt.set('Distance', cumulative_distance) else first_tkpt_dttm = tkpt.dttm tkpt.set('Distance', 0.0) if activ_id.size < 1 activ_id = first_tkpt_dttm end end tkpt.set('Seq', idx + 1) tkpt.set('LapSeq', 1) tkpt.set('ActivityId', activ_id) tkpt.set('LapStartTime', first_tkpt_dttm) tkpt.compute_elapsed_time(first_tkpt_dttm) tkpt.compute_cumulative_pace_and_mph(first_tkpt_dttm) prev_tkpt = tkpt } end |
#subclass_initialize ⇒ Object
27 28 29 30 |
# File 'lib/gooby_gpx_parser.rb', line 27 def subclass_initialize @tags, @trackpoints, @mappings, = [], [], {}, @name, @desc = '', '' end |
#trackpoints_to_csv ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/gooby_gpx_parser.rb', line 89 def trackpoints_to_csv lines = [] @trackpoints.each { | tkpt | lines << tkpt.to_csv } lines end |