Class: Broutes::Formats::Tcx

Inherits:
Object
  • Object
show all
Defined in:
lib/broutes/formats/tcx.rb

Instance Method Summary collapse

Instance Method Details

#load(file, route) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/broutes/formats/tcx.rb', line 5

def load(file, route)
  doc = Nokogiri::XML(file)
  Broutes.logger.info {"Loaded #{file} into #{doc.to_s.slice(0, 10)}"}

  i = 0
  doc.css('Trackpoint').each do |node|
    data = {
      elevation: point_elevation(node), 
      time: point_time(node), 
      distance: point_distance(node), 
      heart_rate: point_heart_rate(node),
      power: point_power(node),
      speed: point_speed(node),
      cadence: point_cadence(node)
      }
    if location = point_location(node)
      data[:lat] = location[0]
      data[:lon] = location[1]
    end
    
    p = route.add_point(data)
    i += 1
  end
  Broutes.logger.info {"Loaded #{i} data points"}

  # Load in summary values if time and distance nil, ie no points
  unless route.total_time
    route.total_time = doc.css('Activities > Activity > Lap > TotalTimeSeconds').reduce(0) { |sum, node| 
      sum + node.inner_text.to_i
    }
  end

  unless route.total_distance
    route.total_distance = doc.css('Activities > Activity > Lap > DistanceMeters').reduce(0) { |sum, node| 
      sum + node.inner_text.to_i
    }
  end

  unless route.started_at
    route.started_at = DateTime.parse(doc.css('Activities > Activity > Lap').first['StartTime']).to_time
  end
end

#point_cadence(node) ⇒ Object



78
79
80
81
82
# File 'lib/broutes/formats/tcx.rb', line 78

def point_cadence(node)
  if cadence_node = node.at_css('Cadence')
    cadence_node.inner_text.to_i
  end
end

#point_distance(node) ⇒ Object



54
55
56
57
58
# File 'lib/broutes/formats/tcx.rb', line 54

def point_distance(node)
  if distance_node = node.at_css('DistanceMeters')
    distance_node.inner_text.to_f
  end
end

#point_elevation(node) ⇒ Object



60
61
62
63
64
# File 'lib/broutes/formats/tcx.rb', line 60

def point_elevation(node)
  if elevation_node = node.at_css('AltitudeMeters')
    elevation_node.inner_text.to_f
  end
end

#point_heart_rate(node) ⇒ Object



72
73
74
75
76
# File 'lib/broutes/formats/tcx.rb', line 72

def point_heart_rate(node)
  if hr_node = node.at_css('HeartRateBpm')
    hr_node.inner_text.to_i
  end
end

#point_location(node) ⇒ Object



48
49
50
51
52
# File 'lib/broutes/formats/tcx.rb', line 48

def point_location(node)
  if position_node = node.at_css('Position')
    [ position_node.at_css('LatitudeDegrees').inner_text.to_f, position_node.at_css('LongitudeDegrees').inner_text.to_f ]
  end
end

#point_power(node) ⇒ Object



84
85
86
87
88
# File 'lib/broutes/formats/tcx.rb', line 84

def point_power(node)
  if power_node = node.at_xpath('.//tpx:Watts', 'tpx' => 'http://www.garmin.com/xmlschemas/ActivityExtension/v2')
    power_node.inner_text.to_i
  end
end

#point_speed(node) ⇒ Object



90
91
92
93
94
# File 'lib/broutes/formats/tcx.rb', line 90

def point_speed(node)
  if speed_node = node.at_xpath('.//tpx:Speed', 'tpx' => 'http://www.garmin.com/xmlschemas/ActivityExtension/v2')
    speed_node.inner_text.to_f
  end
end

#point_time(node) ⇒ Object



66
67
68
69
70
# File 'lib/broutes/formats/tcx.rb', line 66

def point_time(node)
  if time_node = node.at_css('Time')
    DateTime.parse(time_node.inner_text).to_time
  end
end