Sha256: b6395286c55e55990ffb09a945258404013c2b50b8508249efb23a8306347e74

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

module Importer
	class GPX
		def initialize(opts = {:data => nil, :time_zone => "UTC"})
			@data = opts[:data]
		end

		def restore
			workout = HRMParser::Workout.new(:duration => 0)
			@xml = Hpricot::XML(@data)

			# Set the time based on first trackpoint.  Seen an instance where the gpx begining time is wrong
			ttime = (@xml/:trk/:trkpt/:time).first.innerHTML
			workout.time = Time.parse(ttime)

			trackpoints = []
			distance = 0
			(@xml/:trk).each do |trk|
				(trk/:trkpt).each do |trkpt|
					trackpoint = HRMParser::TrackPoint.new
					trackpoint.altitude = (trkpt/:ele).innerHTML.to_f
					trackpoint.time = Time.parse((trkpt/:time).innerHTML)

					trackpoint.lat = (trkpt.attributes)["lat"].to_f
					trackpoint.lng = (trkpt.attributes)["lon"].to_f

					distance += trackpoint.calc_distance(trackpoints.last, trackpoint)
					trackpoint.distance = distance

					trackpoint.speed = trackpoint.calc_speed(trackpoints.last, trackpoint)

					trackpoints << trackpoint
				end
			end

			workout.duration = trackpoints.last.time - trackpoints.first.time
			workout.trackpoints = trackpoints
			workout.calc_average_speed! 
			workout.calc_altitude_gain!
			workout.distance = trackpoints.last.distance
			return workout
		end
	end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
teich-hrmparser-0.4.5 lib/hrmparser/importer/gpx.rb
teich-hrmparser-0.4.6 lib/hrmparser/importer/gpx.rb
teich-hrmparser-0.4.7 lib/hrmparser/importer/gpx.rb
teich-hrmparser-0.4.8 lib/hrmparser/importer/gpx.rb
teich-hrmparser-0.4.9 lib/hrmparser/importer/gpx.rb