hillpace 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 865ed429b462ed2c5715b1ca6b582a47cde2fba3
4
+ data.tar.gz: 9f0b932215cd03f7d53a8672740b1e49ddc85cb8
5
+ SHA512:
6
+ metadata.gz: b88e9025cf018b4bdf8dee78e4fe90335e879367496dee5817901bd6f73efaf38765a78ba1ab1270551e06fa5a6ff239f5e4b573f8a577e9fa2ba5462f0fdb00
7
+ data.tar.gz: 6496edc92a564a6abae32ba801f906c09b429ffb447daff16d1cb0641d98e88a4c0b343bfea492582f1f26ee6764bdaf500236982b66e03192dcf397a8ac4325
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Juan Ramírez Ruiz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,77 @@
1
+ # HillPace
2
+
3
+ HillPace is a library for planning running races. It can take the GPX or TCX of a route and a reference pace (the pace you would go on ideal conditions) and generate planned paces by segments, based on configurable external factors like the climb grade of each segment.
4
+
5
+ ### Status
6
+
7
+ This is a work in progress, but the core of the code is functional.
8
+
9
+ [![Code Climate](https://codeclimate.com/github/juanramirez/hillpace/badges/gpa.svg)](https://codeclimate.com/github/juanramirez/hillpace)
10
+
11
+ #### Installation
12
+
13
+ Go to the root directory of the package and run:
14
+ ```
15
+ bundle install
16
+ ```
17
+
18
+ #### Main dependencies
19
+
20
+ * [Nokogiri](http://www.nokogiri.org/)
21
+ * [Geokit](https://github.com/geokit/geokit)
22
+ * [geoelevation](https://github.com/tkrajina/geoelevations)
23
+
24
+ **Important**: Notice that [geoelevation](https://github.com/tkrajina/geoelevations) needs the whole [SRTM database](https://en.wikipedia.org/wiki/Shuttle_Radar_Topography_Mission) to work and it will download it automatically on `~/.elevations.rb` on the first run (this can be hundreds of MB, but it is only necessary once).
25
+
26
+ #### Code organization
27
+
28
+ * [lib/hillpace](lib/hillpace): contains the core classes of the package, which manage paces, routes, segments and track points
29
+ * [lib/hillpace/input](lib/hillpace/input) contains classes for importing routes (at the moment, just GPX)
30
+ * [lib/hillpace/pace_adjuster](lib/hillpace/pace_adjuster) contains the pace adjuster and its strategies
31
+ * [lib/hillpace/race_planner](lib/hillpace/race_planner) contains a convenient race planner to, based on a strategy, just obtain the data for a combination of route, split distances and reference pace.
32
+ * [specs](specs) contains the specs :)
33
+ * [example.rb](example.rb) contains an example of use (see the [Example](README.md####example) section)
34
+
35
+ #### Tests
36
+
37
+ All classes in the codebase are tested, and you can run those tests by typing:
38
+ ```
39
+ bundle exec rake tests
40
+ ```
41
+
42
+ #### Example
43
+
44
+ An example can be found in [examples/race_planning.rb](race_planning.rb), and it can be run this way:
45
+ ```
46
+ bundle exec rake example
47
+ ```
48
+
49
+ We use a GPX exported from [this Garmin Connect activity](https://connect.garmin.com/modern/activity/770166012) which maps the [2015 Granada Half Marathon](http://www.granada.es/inet/MediaMaraton.nsf/xnotweb/3F5884FDDFD1A9EDC1257E43004048B9?open).
50
+
51
+ The example is assumed to get that GPX and an input pace of 4:08 minutes per km, which is my theoretical pace for a flat course, and gives the following output:
52
+
53
+ ```
54
+ Example: Granada Half Marathon (from flat surface pace: 4:08 min/km)
55
+ 0.0Km - 1.0Km (incline 1.85%): 04:30 min/km
56
+ 1.0Km - 2.0Km (incline 0.65%): 04:15 min/km
57
+ 2.0Km - 3.0Km (incline -2.7%): 03:41 min/km
58
+ 3.0Km - 4.0Km (incline 0.0%): 04:08 min/km
59
+ 4.0Km - 5.0Km (incline -1.6%): 03:51 min/km
60
+ 5.0Km - 6.0Km (incline 0.53%): 04:14 min/km
61
+ 6.0Km - 7.0Km (incline -1.03%): 03:57 min/km
62
+ 7.0Km - 8.0Km (incline 0.2%): 04:10 min/km
63
+ 8.0Km - 9.0Km (incline -0.4%): 04:03 min/km
64
+ 9.0Km - 10.0Km (incline 2.3%): 04:36 min/km
65
+ 10.0Km - 11.0Km (incline -0.47%): 04:02 min/km
66
+ 11.0Km - 12.0Km (incline -0.31%): 04:04 min/km
67
+ 12.0Km - 13.0Km (incline 1.99%): 04:32 min/km
68
+ 13.0Km - 14.0Km (incline -0.5%): 04:02 min/km
69
+ 14.0Km - 15.0Km (incline 1.47%): 04:25 min/km
70
+ 15.0Km - 16.0Km (incline 0.68%): 04:15 min/km
71
+ 16.0Km - 17.0Km (incline -0.95%): 03:57 min/km
72
+ 17.0Km - 18.0Km (incline 0.5%): 04:13 min/km
73
+ 18.0Km - 19.0Km (incline -1.6%): 03:51 min/km
74
+ 19.0Km - 20.0Km (incline -0.49%): 04:02 min/km
75
+ 20.0Km - 21.0Km (incline 0.19%): 04:10 min/km
76
+ 21.0Km - 21.23Km (incline -0.43%): 04:03 min/km
77
+ ```
@@ -0,0 +1,16 @@
1
+ require 'nokogiri'
2
+ require 'geoelevation'
3
+
4
+ require 'hillpace/version'
5
+ require 'hillpace/pace'
6
+ require 'hillpace/track_point'
7
+ require 'hillpace/segment'
8
+ require 'hillpace/route'
9
+ require 'hillpace/import/kalman_filter'
10
+ require 'hillpace/import/gpx/gpx_parser'
11
+ require 'hillpace/import/tcx/tcx_parser'
12
+ require 'hillpace/import/xml/xml_validator'
13
+ require 'hillpace/pace_adjuster/pace_adjuster'
14
+ require 'hillpace/pace_adjuster/strategies/mervs_running'
15
+ require 'hillpace/pace_adjuster/strategies/lets_run_forum'
16
+ require 'hillpace/race_planner/race_planner'
@@ -0,0 +1,100 @@
1
+ module Hillpace
2
+ module Import
3
+ module Gpx
4
+ # Parser for GPX files (see http://www.topografix.com/gpx.asp).
5
+ class GpxParser
6
+ attr_reader :document
7
+
8
+ # Initializes a GpxParser object.
9
+ # @param gpx_content [string] The content of a GPX file.
10
+ def initialize(gpx_content)
11
+ @document = Nokogiri::XML gpx_content
12
+ @kalman_filter = KalmanFilter.new
13
+ @srtm = GeoElevation::Srtm.new
14
+ end
15
+
16
+ # Creates a new GpxParser object from a GPX file path.
17
+ # @param gpx_path [string] The path of the file to be parsed.
18
+ # @return [GpxParser]
19
+ def self.from_file(gpx_path)
20
+ new File.open gpx_path
21
+ end
22
+
23
+ # Creates a new GpxParser object from a GPX file URL.
24
+ # @param gpx_url [string] The URL of the file to be parsed.
25
+ # @return [GpxParser]
26
+ def self.from_url(gpx_url)
27
+ new open gpx_url
28
+ end
29
+
30
+ # Parses a GPX document.
31
+ # Looks for route nodes inside the document and parses them.
32
+ # @return [Route]
33
+ def parse
34
+ routes = Array.new
35
+ document.search('trk').each do |route_node|
36
+ routes << parse_route(route_node)
37
+ end
38
+ routes
39
+ end
40
+
41
+ private
42
+
43
+ # Parses a route node.
44
+ # Looks for segment nodes inside the route node and parses them.
45
+ # @param route_node [Nokogiri::XML::Node] The XML node corresponding to the route.
46
+ # @return [Route]
47
+ def parse_route(route_node)
48
+ segments = Array.new
49
+ route_node.search('trkseg').each do |segment_node|
50
+ segments << parse_segment(segment_node)
51
+ end
52
+ Route.new(segments)
53
+ end
54
+
55
+ # Parses a segment node.
56
+ # Looks for track point nodes inside the segment node and parses them.
57
+ # @param segment_node [Nokogiri::XML::Node] The XML node corresponding to the segment.
58
+ # @return [Segment]
59
+ def parse_segment(segment_node)
60
+ @kalman_filter.reset
61
+ track_points = Array.new
62
+
63
+ segment_node.search('trkpt').each do |track_point_node|
64
+ track_points << parse_track_point(track_point_node)
65
+ end
66
+
67
+ Segment.new track_points
68
+ end
69
+
70
+ # Parses a track point node.
71
+ # Looks for the attributes of a track node (longitude, latitude, elevation and time) and creates a TrackPoint object
72
+ # accordingly.
73
+ #
74
+ # Note: These attributes don't need to be exactly the ones the GPX provides.
75
+ # For longitude and latitude, a Kalman filter is applied if the time is also provided.
76
+ # Also, the elevation is not got from the GPX, but deduced from longitude and latitude.
77
+ #
78
+ # @param track_point_node [Nokogiri::XML::Node] The XML node corresponding to the track point.
79
+ # @return [TrackPoint]
80
+ def parse_track_point(track_point_node)
81
+ longitude = (track_point_node ['lon']).to_f
82
+ latitude = (track_point_node ['lat']).to_f
83
+
84
+ # Yes, we could do
85
+ # elevation = (track_point_node.at('ele').content).to_f
86
+ # but elevation from GPX not reliable in some cases.
87
+ # Google Maps API is expensive, so we get data from SRTM database
88
+ # (see https://en.wikipedia.org/wiki/Shuttle_Radar_Topography_Mission)
89
+ elevation = @srtm.get_elevation(latitude, longitude)
90
+
91
+ track_point = TrackPoint.new longitude, latitude, elevation
92
+ track_point.time = Time.parse track_point_node.at('time').content unless track_point_node.search('time').empty?
93
+ @kalman_filter.apply track_point
94
+ end
95
+
96
+ private_class_method :new
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,58 @@
1
+ module Hillpace
2
+ module Import
3
+ # See https://en.wikipedia.org/wiki/Kalman_filter
4
+ # Based on this implementation by Stochastically: http://stackoverflow.com/a/15657798/1147175
5
+ class KalmanFilter
6
+ ACCURACY = 3.0
7
+
8
+ # Initializes a KalmanFilter object.
9
+ def initialize
10
+ reset
11
+ end
12
+
13
+ # Resets a KalmanFilter object.
14
+ def reset
15
+ @variance = -1.0
16
+ end
17
+
18
+ # Applies the filter to a track point
19
+ # @param track_point [TrackPoint] The track point which will be filtered.
20
+ # @return [TrackPoint]
21
+ def apply(track_point)
22
+ if track_point.time.nil?
23
+ return track_point
24
+ elsif @variance < 0
25
+ @track_point = track_point
26
+ @variance = ACCURACY ** 2
27
+ else
28
+ time_delta = track_point.time - @track_point.time
29
+ if time_delta > 0
30
+ @variance += time_delta * (get_speed(@track_point, track_point, time_delta) ** 2)
31
+ @track_point.time = track_point.time
32
+ end
33
+
34
+ k = @variance / (@variance + ACCURACY ** 2)
35
+ @track_point.longitude += k * (track_point.longitude - @track_point.longitude)
36
+ @track_point.latitude += k * (track_point.latitude - @track_point.latitude)
37
+ @track_point.elevation += k * (track_point.elevation - @track_point.elevation)
38
+
39
+ @variance = (1.0 - k) * @variance
40
+ end
41
+
42
+ @track_point.clone
43
+ end
44
+
45
+ private
46
+
47
+ # Gets the speed from two trackpoints with their time difference.
48
+ # @param previous_track_point [TrackPoint] The previous track point.
49
+ # @param track_point [TrackPoint] The current track point
50
+ # @param time_delta [Number] The time difference between the track points.
51
+ # @return [Number]
52
+ def get_speed(previous_track_point, track_point, time_delta)
53
+ distance_delta = previous_track_point.distance_meters_to track_point
54
+ distance_delta / time_delta
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,103 @@
1
+ module Hillpace
2
+ module Import
3
+ module Tcx
4
+ # Parser for TCX files (see https://en.wikipedia.org/wiki/Training_Center_XML).
5
+ class TcxParser
6
+ attr_reader :document
7
+
8
+ # Initializes a TcxParser object.
9
+ # @param tcx_content [string] The content of a TCX file.
10
+ def initialize(tcx_content)
11
+ @document = Nokogiri::XML tcx_content
12
+ @kalman_filter = KalmanFilter.new
13
+ @srtm = GeoElevation::Srtm.new
14
+ end
15
+
16
+ # Creates a new TcxParser object from a TCX file path.
17
+ # @param tcx_path [string] The path of the file to be parsed.
18
+ # @return [TcxParser]
19
+ def self.from_file(tcx_path)
20
+ new File.open tcx_path
21
+ end
22
+
23
+ # Creates a new TcxParser object from a TCX file URL.
24
+ # @param tcx_url [string] The URL of the file to be parsed.
25
+ # @return [TcxParser]
26
+ def self.from_url(tcx_url)
27
+ new open tcx_url
28
+ end
29
+
30
+ # Parses a TCX document.
31
+ # Looks for route nodes inside the document and parses them.
32
+ # @return [Route]
33
+ def parse
34
+ routes = Array.new
35
+ document.search('Activity').each do |route_node|
36
+ routes << parse_route(route_node)
37
+ end
38
+ routes
39
+ end
40
+
41
+ private
42
+
43
+ # Parses a route node.
44
+ # Looks for segment nodes inside the route node and parses them.
45
+ # @param route_node [Nokogiri::XML::Node] The XML node corresponding to the route.
46
+ # @return [Route]
47
+ def parse_route(route_node)
48
+ segments = Array.new
49
+ route_node.search('Lap').each_with_index do |segment_node, index|
50
+ segment = parse_segment(segment_node)
51
+ # Start new segment with last known point, as segments are not consecutive in TCX.
52
+ segment.track_points.unshift(segments.last.track_points.last.clone) unless index == 0
53
+ segments << segment
54
+ end
55
+ Route.new(segments)
56
+ end
57
+
58
+ # Parses a segment node.
59
+ # Looks for track point nodes inside the segment node and parses them.
60
+ # @param segment_node [Nokogiri::XML::Node] The XML node corresponding to the segment.
61
+ # @return [Segment]
62
+ def parse_segment(segment_node)
63
+ @kalman_filter.reset
64
+ track_points = Array.new
65
+
66
+ segment_node.search('Trackpoint').each do |track_point_node|
67
+ track_points << parse_track_point(track_point_node)
68
+ end
69
+
70
+ Segment.new track_points
71
+ end
72
+
73
+ # Parses a track point node.
74
+ # Looks for the attributes of a track node (longitude, latitude and elevation) and creates a TrackPoint object
75
+ # accordingly.
76
+ #
77
+ # Note: These attributes don't need to be exactly the ones the TCX provides.
78
+ # For longitude and latitude, a Kalman filter is applied if the time is also provided.
79
+ # Also, the elevation is not got from the TCX, but deduced from longitude and latitude.
80
+ #
81
+ # @param track_point_node [Nokogiri::XML::Node] The XML node corresponding to the track point.
82
+ # @return [TrackPoint]
83
+ def parse_track_point(track_point_node)
84
+ longitude = track_point_node.at('Position').at('LongitudeDegrees').content.to_f
85
+ latitude = track_point_node.at('Position').at('LatitudeDegrees').content.to_f
86
+
87
+ # Yes, we could do
88
+ # elevation = (track_point_node.at('ele').content).to_f
89
+ # but elevation from GPX not reliable in some cases.
90
+ # Google Maps API is expensive, so we get data from SRTM database
91
+ # (see https://en.wikipedia.org/wiki/Shuttle_Radar_Topography_Mission)
92
+ elevation = @srtm.get_elevation(latitude, longitude)
93
+
94
+ track_point = TrackPoint.new longitude, latitude, elevation
95
+ track_point.time = Time.parse track_point_node.at('Time').content
96
+ @kalman_filter.apply track_point
97
+ end
98
+
99
+ private_class_method :new
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,59 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ module Hillpace
5
+ module Import
6
+ module Xml
7
+ # Validator for XML files
8
+ class XmlValidator
9
+ DEFAULT_GPX_SCHEMA_URL = 'http://www.topografix.com/gpx/1/1/gpx.xsd'
10
+ DEFAULT_TCX_SCHEMA_URL = 'http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd'
11
+
12
+ attr_reader :schema
13
+
14
+ # Initializes a XmlValidator object.
15
+ # @param schema_content [string] The content of an XML schema file.
16
+ def initialize(schema_content)
17
+ @schema = Nokogiri::XML::Schema schema_content
18
+ end
19
+
20
+ # Creates a new XmlValidator object from an XML schema file path.
21
+ # @param schema_path [String] The path of the XML schema file to use.
22
+ # @return [XmlValidator]
23
+ def self.from_file(schema_path)
24
+ new File.open(schema_path)
25
+ end
26
+
27
+ # Creates a new XmlValidator object from an XML schema file URL.
28
+ # @param schema_url [String] The URL of the XML schema file to use.
29
+ # @return [XmlValidator]
30
+ def self.from_url(schema_url)
31
+ new open(schema_url)
32
+ end
33
+
34
+ # Creates a new XmlValidator object using the default GPX schema.
35
+ # (see http://www.topografix.com/gpx.asp)
36
+ # @return [XmlValidator]
37
+ def self.from_gpx_schema
38
+ new open(DEFAULT_GPX_SCHEMA_URL)
39
+ end
40
+
41
+ # Creates a new XmlValidator object using the default TCX schema.
42
+ # (see https://en.wikipedia.org/wiki/Training_Center_XML)
43
+ # @return [XmlValidator]
44
+ def self.from_tcx_schema
45
+ new open(DEFAULT_TCX_SCHEMA_URL)
46
+ end
47
+
48
+ # Validates an XML file content and return errors found.
49
+ # @return [Array<Nokogiri::XML::SyntaxError>]
50
+ def validate(xml_content)
51
+ document = Nokogiri::XML xml_content
52
+ schema.validate document
53
+ end
54
+
55
+ private_class_method :new
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,47 @@
1
+ module Hillpace
2
+ # Represents a pace.
3
+ class Pace
4
+ METERS_PER_KILOMETER = 1000
5
+ SECONDS_IN_AN_HOUR = 3600
6
+
7
+ attr_reader :meters_per_second
8
+
9
+ # Inicializes a Pace object.
10
+ # @param meters_per_second [Number] The pace expressed in meters per second.
11
+ def initialize(meters_per_second)
12
+ @meters_per_second = meters_per_second
13
+ end
14
+
15
+ # Creates a new Pace object from a pace expressed in seconds per kilometer.
16
+ # @param seconds_per_km [Number] The pace expressed in seconds per kilometer.
17
+ def self.from_seconds_per_km(seconds_per_km)
18
+ meters_per_second = METERS_PER_KILOMETER / seconds_per_km.to_f
19
+ new meters_per_second
20
+ end
21
+
22
+ # Creates a new Pace object from a pace expressed in meters per second.
23
+ # @param meters_per_second [Number] The pace expressed in meters per second.
24
+ def self.from_meters_per_second(meters_per_second)
25
+ new meters_per_second
26
+ end
27
+
28
+ # Returns the pace in seconds per kilometer.
29
+ # @return [Number]
30
+ def seconds_per_km
31
+ METERS_PER_KILOMETER / @meters_per_second.to_f
32
+ end
33
+
34
+ # Returns the pace in minutes per kilometer.
35
+ # @return [String]
36
+ # @raise [RuntimeError] if the pace is slower than an hour per kilometer.
37
+ def minutes_per_km
38
+ if seconds_per_km >= SECONDS_IN_AN_HOUR
39
+ raise 'Too slow pace: more than an hour per kilometer.'
40
+ end
41
+
42
+ Time.at(seconds_per_km).strftime '%M:%S'
43
+ end
44
+
45
+ private_class_method :new
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ module Hillpace
2
+ module PaceAdjuster
3
+
4
+ class PaceAdjuster
5
+ # Initializes a PaceAdjuster object.
6
+ # @param strategy [Proc] The adjust strategy that the pace adjuster will use.
7
+ def initialize(strategy)
8
+ @strategy = strategy
9
+ end
10
+
11
+ # Adjusts a reference pace depending on the incline.
12
+ # @param pace [Pace] A reference pace,
13
+ # @param incline [Number] An incline value.
14
+ # @return [Pace]
15
+ def adjust_pace(pace, incline)
16
+ @strategy.call pace, incline
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ module Hillpace
2
+ module PaceAdjuster
3
+ module Strategies
4
+ # source: http://www.letsrun.com/forum/flat_read.php?thread=197366&page=0
5
+ LETS_RUN_FORUM = lambda do |pace, incline|
6
+ division_factor = 1.0 + (incline * 9)
7
+ Hillpace::Pace.from_meters_per_second(pace.meters_per_second / division_factor)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,11 @@
1
+ module Hillpace
2
+ module PaceAdjuster
3
+ module Strategies
4
+ # source: http://merv.stanford.edu/runcalc?Request=explanation
5
+ MERVS_RUNNING = lambda do |pace, incline|
6
+ division_factor = 1.0 - (incline * 4.5)
7
+ Pace.from_seconds_per_km(pace.seconds_per_km / division_factor)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module Hillpace
2
+ module RacePlanner
3
+ class RacePlanner
4
+
5
+ def initialize(strategy)
6
+ @pace_adjuster = Hillpace::PaceAdjuster::PaceAdjuster.new strategy
7
+ end
8
+
9
+ def plan_race(route, split_distances, reference_pace)
10
+ splitted_route = route.split split_distances
11
+ segments = splitted_route.segments.clone.lazy
12
+ race_plan = []
13
+
14
+ accumulated_distance = 0
15
+ segments.each do |segment|
16
+ segment_plan = {
17
+ :segment_start => accumulated_distance,
18
+ :segment_end => accumulated_distance + segment.distance_meters,
19
+ :incline => segment.incline,
20
+ :adjusted_pace => @pace_adjuster.adjust_pace(reference_pace, segment.incline)
21
+ }
22
+ race_plan << segment_plan
23
+ accumulated_distance += segment.distance_meters
24
+ end
25
+
26
+ race_plan
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,94 @@
1
+ module Hillpace
2
+ # Represents a geographic route in the Earth, made out of consecutive segments.
3
+ class Route
4
+ attr_reader :segments
5
+
6
+ # Initializes a Route object.
7
+ # @param segments [Array<Segment>] The segments of the route.
8
+ # @raise [RuntimeError] if _segments_ is not a collection of [Segment] objects.
9
+ # @raise [RuntimeError] if any of the _segments_ start is not the same point of the previous segment
10
+ # end.
11
+ def initialize(segments)
12
+ raise 'Invalid segment array to initialize Route' unless segments.respond_to?('each') &&
13
+ segments.all? {|segment| segment.is_a? Segment}
14
+
15
+ unless segments.empty?
16
+ last_track_point = segments.first.track_points.first
17
+ segments.each do |segment|
18
+ raise 'Segments must be consecutive' unless last_track_point == segment.track_points.first
19
+ last_track_point = segment.track_points.last
20
+ end
21
+ end
22
+
23
+ @segments = segments
24
+ end
25
+
26
+ # Measures the distance of the route, in meters.
27
+ # @return [Number]
28
+ def distance_meters
29
+ segments.inject(0) {|result, segment| result + segment.distance_meters}
30
+ end
31
+
32
+ # Measures the elevation difference from the start to the end of the route, in meters.
33
+ # @return [Number]
34
+ def climb
35
+ segments.inject(0) {|result, segment| result + segment.climb}
36
+ end
37
+
38
+ # Measures the climb of the route relative to its distance.
39
+ # @return [Number]
40
+ def incline
41
+ self.climb / self.distance_meters
42
+ end
43
+
44
+ # Measures the sum of uphills between segments of the route.
45
+ # @return [Number]
46
+ def total_uphills
47
+ segments.inject(0) {|result, segment| result + segment.total_uphills}
48
+ end
49
+
50
+ # Measures the sum of downhills between segments of the route.
51
+ # @return [Number]
52
+ def total_downhills
53
+ segments.inject(0) {|result, segment| result + segment.total_downhills}
54
+ end
55
+
56
+ # Measures the duration of the route, in seconds.
57
+ # @return [Number]
58
+ def duration
59
+ segments.inject(0) {|result, segment| result + segment.duration}
60
+ end
61
+
62
+ # Measures the pace of the route.
63
+ # @return [Pace]
64
+ def pace
65
+ Pace.from_meters_per_second(distance_meters / duration)
66
+ end
67
+
68
+ # Splits the segments in the route in the distances indicated.
69
+ # @param split_distances [Array<Number>] Distances where the segments of the route should be splitted.
70
+ # @return [Route] A copy of this route, with splitted segments
71
+ def split(split_distances)
72
+ split_distances_enumerator = split_distances.sort.each
73
+ next_split_distance = split_distances_enumerator.next
74
+ accumulated_distance = 0
75
+ result = []
76
+
77
+ segments.lazy.each do |segment|
78
+ while next_split_distance < accumulated_distance + segment.distance_meters
79
+ subsegments = segment.split (next_split_distance - accumulated_distance)
80
+ result << subsegments.first
81
+ accumulated_distance += subsegments.first.distance_meters
82
+ if subsegments.length > 1
83
+ segment = subsegments.last
84
+ next_split_distance = split_distances_enumerator.next rescue Float::INFINITY
85
+ end
86
+ end
87
+ result << segment.clone
88
+ accumulated_distance += segment.distance_meters
89
+ end
90
+
91
+ Route.new result
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,132 @@
1
+ require 'geokit'
2
+
3
+ module Hillpace
4
+ # Represents a geographic segment in the Earth, made out of sorted track points.
5
+ class Segment
6
+ attr_reader :track_points
7
+
8
+ # Initializes a Segment object.
9
+ # @param track_points [Array<TrackPoint>] The track points of the segment.
10
+ # @raise [RuntimeError] if _track_points_ is not a collection of [TrackPoint]
11
+ # objects.
12
+ def initialize(track_points)
13
+ raise 'Invalid track point array to initialize Segment' unless track_points.respond_to?('each') &&
14
+ track_points.all? {|track_point| track_point.is_a? TrackPoint}
15
+ @track_points = track_points
16
+ end
17
+
18
+ # Overwrites the #== operator to be able to use custom getters.
19
+ # @param other [Segment] The other segment to be compared.
20
+ # @return [boolean]
21
+ def ==(other)
22
+ self.class == other.class &&
23
+ track_points == other.track_points
24
+ end
25
+
26
+ # Measures the distance of the segment, in meters.
27
+ # @return [Number]
28
+ def distance_meters
29
+ return 0 if track_points.length <= 1
30
+
31
+ result = 0
32
+ latest_track_point = track_points.first
33
+ track_points.each do |track_point|
34
+ next if track_point.equal? latest_track_point
35
+ result += latest_track_point.distance_meters_to track_point
36
+ latest_track_point = track_point
37
+ end
38
+ result
39
+ end
40
+
41
+ # Measures the elevation difference from the start to the end of the segment, in meters.
42
+ # @return [Number]
43
+ def climb
44
+ return 0 if track_points.length <= 1
45
+ track_points.first.climb_to track_points.last
46
+ end
47
+
48
+ # Measures the climb of the segment relative to its distance.
49
+ # @return [Number]
50
+ def incline
51
+ return 0 if track_points.length <= 1
52
+ self.climb / self.distance_meters
53
+ end
54
+
55
+ # Measures the sum of uphills between track points of the segment.
56
+ # @return [Number]
57
+ def total_uphills
58
+ return 0 if track_points.length <= 1
59
+
60
+ result = 0
61
+ latest_track_point = track_points.first
62
+ track_points.each do |track_point|
63
+ if track_point.elevation > latest_track_point.elevation
64
+ result += latest_track_point.climb_to(track_point)
65
+ end
66
+ latest_track_point = track_point
67
+ end
68
+ result
69
+ end
70
+
71
+ # Measures the sum of downhills between track points of the segment.
72
+ # @return [Number]
73
+ def total_downhills
74
+ return 0 if track_points.length <= 1
75
+
76
+ result = 0
77
+ latest_track_point = track_points.first
78
+ track_points.each do |track_point|
79
+ if track_point.elevation < latest_track_point.elevation
80
+ result -= latest_track_point.climb_to(track_point)
81
+ end
82
+ latest_track_point = track_point
83
+ end
84
+ result
85
+ end
86
+
87
+ # Measures the duration of the segment, in seconds.
88
+ # @return [Number]
89
+ def duration
90
+ track_points.last.time.to_f - track_points.first.time.to_f
91
+ end
92
+
93
+ # Measures the pace of the segment.
94
+ # @return [Pace]
95
+ def pace
96
+ Pace.from_meters_per_second(distance_meters / duration)
97
+ end
98
+
99
+ # Returns an array of segments, result of splitting _self_ in the distance indicated.
100
+ # @param distance_meters [Number] The distance in the segment where it should be splitted.
101
+ # @return [Array<Segment>]
102
+ def split(distance_meters)
103
+ result = []
104
+ accumulated_distance = 0
105
+ latest_track_point = track_points.first
106
+ subsegment_track_points = [latest_track_point]
107
+
108
+ track_points.lazy.each_with_index do |track_point, index|
109
+ next if index == 0
110
+ distance_delta = latest_track_point.distance_meters_to track_point
111
+ accumulated_distance += distance_delta
112
+
113
+ if result.empty?
114
+ # in case the distance exceeds the reference distance, we add an interpolated track point
115
+ # both to the end of the actual subsegment and to the start of the next one
116
+ if accumulated_distance > distance_meters
117
+ bias = (distance_meters - (accumulated_distance - distance_delta)) / distance_delta
118
+ interpolated_track_point = latest_track_point.get_linear_interpolation_with track_point, bias
119
+ subsegment_track_points << interpolated_track_point
120
+ result << (Segment.new subsegment_track_points)
121
+ subsegment_track_points = [interpolated_track_point]
122
+ end
123
+ end
124
+
125
+ subsegment_track_points << track_point.clone
126
+ latest_track_point = track_point
127
+ end
128
+
129
+ result << (Segment.new subsegment_track_points)
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,123 @@
1
+ require 'geokit'
2
+
3
+ module Hillpace
4
+ # Represents a geographic track point in the Earth.
5
+ class TrackPoint
6
+ attr_reader :longitude, :latitude, :elevation, :time
7
+
8
+ MINIMUM_LONGITUDE = -180
9
+ MAXIMUM_LONGITUDE = 180
10
+ MINIMUM_LATITUDE = -90
11
+ MAXIMUM_LATITUDE = 90
12
+
13
+ METERS_PER_KILOMETER = 1000
14
+
15
+ # Initializes a TrackPoint object.
16
+ # @param longitude [Number] The geographic longitude of the track point.
17
+ # @param latitude [Number] The geographic latitude of the track point.
18
+ # @param elevation [Number] The elevation of the track point relative to sea level.
19
+ # @param time [Time] The time when the track point was registered.
20
+ def initialize(longitude, latitude, elevation, time = nil)
21
+ self.longitude = longitude
22
+ self.latitude = latitude
23
+ self.elevation = elevation
24
+ self.time = time
25
+ end
26
+
27
+ # Overwrites the #== operator to be able to use custom getters.
28
+ # @param other [TrackPoint] The other track point to be compared.
29
+ # @return [boolean]
30
+ def ==(other)
31
+ self.class == other.class &&
32
+ longitude == other.longitude &&
33
+ latitude == other.latitude &&
34
+ elevation == other.elevation &&
35
+ time == other.time
36
+ end
37
+
38
+ # Setter for the longitude class member.
39
+ # @param longitude [Number] The longitude value to be set.
40
+ # @raise [RuntimeError] if _longitude_ is invalid
41
+ def longitude=(longitude)
42
+ raise 'Invalid longitude' unless
43
+ longitude.is_a?(Numeric) &&
44
+ longitude >= MINIMUM_LONGITUDE &&
45
+ longitude <= MAXIMUM_LONGITUDE
46
+ @longitude = longitude
47
+ end
48
+
49
+ # Setter for the latitude class member.
50
+ # @param latitude [Number] The latitude value to be set.
51
+ # @raise [RuntimeError] if _latitude_ is invalid
52
+ def latitude=(latitude)
53
+ raise 'Invalid latitude' unless
54
+ latitude.is_a?(Numeric) &&
55
+ latitude >= MINIMUM_LATITUDE &&
56
+ latitude <= MAXIMUM_LATITUDE
57
+ @latitude = latitude
58
+ end
59
+
60
+ # Setter for the elevation class member.
61
+ # @param elevation [Number] The elevation value to be set.
62
+ # @raise [RuntimeError] if _elevation_ is not [Numeric]
63
+ def elevation=(elevation)
64
+ raise 'Invalid elevation' unless elevation.is_a? Numeric
65
+ @elevation = elevation
66
+ end
67
+
68
+ # Setter for the time class member.
69
+ # @param time [Time] The time to be set.
70
+ # @raise [RuntimeError] if _time_ is not a [Time]
71
+ def time=(time)
72
+ raise 'Invalid time' unless time.nil? || time.is_a?(Time)
73
+ @time = time
74
+ end
75
+
76
+ # Measures the distance to other track point, in meters.
77
+ # @param other [TrackPoint] The other track point to compare.
78
+ # @raise [RuntimeError] if _other_ is not a TrackPoint object
79
+ # @return [Number]
80
+ def distance_meters_to(other)
81
+ raise 'Invalid track point' unless other.is_a? TrackPoint
82
+ a = Geokit::GeoLoc.new({:lat => latitude, :lng => longitude})
83
+ b = Geokit::GeoLoc.new({:lat => other.latitude, :lng => other.longitude})
84
+ METERS_PER_KILOMETER * (a.distance_to b, {:units => :kms})
85
+ end
86
+
87
+ # Measures the elevation difference to other track point, in meters.
88
+ # @param other [TrackPoint] The other track point to compare.
89
+ # @raise [RuntimeError] if _other_ is not a TrackPoint object
90
+ # @return [Number]
91
+ def climb_to(other)
92
+ raise 'Invalid track point' unless other.is_a? TrackPoint
93
+ other.elevation - elevation
94
+ end
95
+
96
+ # Measures the elevation difference to other track point relative to the distance to it.
97
+ # @param other [TrackPoint] The other track point to compare.
98
+ # @raise [RuntimeError] if _other_ is not a TrackPoint object
99
+ # @return [Number]
100
+ def incline_to(other)
101
+ raise 'Invalid track point' unless other.is_a? TrackPoint
102
+ distance_meters = self.distance_meters_to other
103
+ return 0 if distance_meters == 0
104
+
105
+ (self.climb_to other) / distance_meters
106
+ end
107
+
108
+ # Returns a linear interpolation between _self_ and the one provided.
109
+ # @note For small distances between track points, a linear interpolation should be accurate enough.
110
+ # @param other [TrackPoint] The other track point to compare.
111
+ # @param bias [Number] Value which will determine the position in that line. *0* would be the position of _self_,
112
+ # *1* would be _other_ position.
113
+ # @raise [Runtimeerror] if _other_ is not a TrackPoint object
114
+ # @return [Number]
115
+ def get_linear_interpolation_with(other, bias = 0.5)
116
+ raise 'Invalid track point' unless other.is_a? TrackPoint
117
+ TrackPoint.new longitude * (1.0 - bias) + other.longitude * bias,
118
+ latitude * (1.0 - bias) + other.latitude * bias,
119
+ elevation * (1.0 - bias) + other.elevation * bias,
120
+ time.nil? || other.time.nil? ? nil : Time.at(time.to_f * (1.0 - bias) + other.time.to_f * bias)
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,3 @@
1
+ module Hillpace
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hillpace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Juan Ramírez Ruiz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: zip
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: geokit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.10'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.10'
111
+ - !ruby/object:Gem::Dependency
112
+ name: geoelevation
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.0'
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 0.0.1
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '0.0'
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 0.0.1
131
+ description: |2
132
+ Hillpace is a gem for running races planning. It can take a route an a reference pace (the pace you would go on a
133
+ flat course of the same distance) and generate planned paces by segments, based on the incline of each segment.
134
+ email:
135
+ - juan.ramirez.ruiz@gmail.com
136
+ executables: []
137
+ extensions: []
138
+ extra_rdoc_files: []
139
+ files:
140
+ - LICENSE
141
+ - README.md
142
+ - lib/hillpace.rb
143
+ - lib/hillpace/import/gpx/gpx_parser.rb
144
+ - lib/hillpace/import/kalman_filter.rb
145
+ - lib/hillpace/import/tcx/tcx_parser.rb
146
+ - lib/hillpace/import/xml/xml_validator.rb
147
+ - lib/hillpace/pace.rb
148
+ - lib/hillpace/pace_adjuster/pace_adjuster.rb
149
+ - lib/hillpace/pace_adjuster/strategies/lets_run_forum.rb
150
+ - lib/hillpace/pace_adjuster/strategies/mervs_running.rb
151
+ - lib/hillpace/race_planner/race_planner.rb
152
+ - lib/hillpace/route.rb
153
+ - lib/hillpace/segment.rb
154
+ - lib/hillpace/track_point.rb
155
+ - lib/hillpace/version.rb
156
+ homepage: https://github.com/juanramirez/hillpace
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.2.2
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Estimate running paces depending on climb grade
180
+ test_files: []
181
+ has_rdoc: