Class: Natour::FITFile

Inherits:
GPSTrack show all
Defined in:
lib/natour/fit_file.rb

Instance Attribute Summary

Attributes inherited from GPSTrack

#ascent, #date, #descent, #distance, #duration, #end_point, #path, #start_point

Instance Method Summary collapse

Methods inherited from GPSTrack

load_file, #round_effective_km!, #save_gpx

Constructor Details

#initialize(filename) ⇒ FITFile

Returns a new instance of FITFile.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/natour/fit_file.rb', line 9

def initialize(filename)
  Fit4Ruby::Log.level = Logger::ERROR
  @activity = Fit4Ruby.read(filename)

  date = Date.parse(@activity.local_timestamp.to_s)
  session = @activity.sessions.first
  ascent = session.total_ascent
  descent = session.total_descent
  distance = session.total_distance.to_i
  duration = Duration.new(session.total_elapsed_time)

  records = @activity.records.reject { |record| [record.position_lat, record.position_lat].any?(&:nil?) }
  start_point = to_track_point(records[0])
  end_point = to_track_point(records[-1])

  super(filename, date, ascent, descent, distance, duration, start_point, end_point)
end

Instance Method Details

#to_gpxObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/natour/fit_file.rb', line 27

def to_gpx
  decl = Nokogiri.XML('<?xml version="1.0" encoding="UTF-8" standalone="no"?>')
  builder = Nokogiri::XML::Builder.with(decl) do |doc|
    doc.gpx(
      'creator' => 'natour',
      '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'
    ) do
      doc.trk do
        doc.trkseg do
          records = @activity.records.reject { |record| [record.position_lat, record.position_lat].any?(&:nil?) }
          records.each do |record|
            doc.trkpt('lat' => record.position_lat, 'lon' => record.position_long) do
              doc.ele record.enhanced_elevation
              doc.time record.timestamp.utc.xmlschema
            end
          end
        end
      end
    end
  end

  builder.to_xml
end