Class: ComtecDR::GpxGenerator
- Inherits:
-
Object
- Object
- ComtecDR::GpxGenerator
- Defined in:
- lib/comtec-dr/gpx_generator.rb
Instance Method Summary collapse
- #add_track(track = nil) ⇒ Object
- #add_trkseg(track) ⇒ Object
-
#initialize(filename) ⇒ GpxGenerator
constructor
A new instance of GpxGenerator.
- #write ⇒ Object
Constructor Details
#initialize(filename) ⇒ GpxGenerator
Returns a new instance of GpxGenerator.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/comtec-dr/gpx_generator.rb', line 3 def initialize filename @filename = filename @doc = REXML::Document.new @gpx = REXML::Element.new('gpx') @gpx.add_attributes({ 'creator' => "comtec-dr-ruby/#{ComtecDR::VERSION}", 'version' => "1.1", 'xmlns' => 'http://www.topografix.com/GPX/1/1', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation' => 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd' }) @doc.add_element(@gpx) end |
Instance Method Details
#add_track(track = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/comtec-dr/gpx_generator.rb', line 18 def add_track track = nil @trk = REXML::Element.new('trk') @gpx.add_element(@trk) add_trkseg track if !track.nil? end |
#add_trkseg(track) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/comtec-dr/gpx_generator.rb', line 25 def add_trkseg track trkseg = REXML::Element.new('trkseg') @trk.add_element(trkseg) track.track.each do |t| trkpt = REXML::Element.new('trkpt') trkpt.add_attributes({'lat' => t.lat, 'lon' => t.lon}) time = REXML::Element.new('time') time.add_text(t.jst.utc.iso8601) trkpt.add_element(time) speed = REXML::Element.new('speed') speed.add_text((t.speed.to_f * 1000 / 3600).to_s) trkpt.add_element(speed) trkseg.add_element(trkpt) end end |
#write ⇒ Object
45 46 47 48 49 |
# File 'lib/comtec-dr/gpx_generator.rb', line 45 def write File.open(@filename, 'w') do |file| @doc.write(file, indent=2) end end |