Sha256: b06c56aacf14447a746ca1fa152f5e978bf38e8b97d79097b854c5ba5cd46c95

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 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
			workout.duration = 0
			last_trackpoint = false
			(@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)

					if last_trackpoint && trackpoints.last && trackpoint.speed
            workout.duration += trackpoint.time - last_trackpoint.time if trackpoint.speed > 0.04
          end
					trackpoints << trackpoint
					last_trackpoint = 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

4 entries across 4 versions & 2 rubygems

Version Path
teich-hrmparser-0.5.0 lib/hrmparser/importer/gpx.rb
teich-hrmparser-0.6.0 lib/hrmparser/importer/gpx.rb
hrmparser-0.7.0 lib/hrmparser/importer/gpx.rb
hrmparser-0.6.0 lib/hrmparser/importer/gpx.rb