blib/lib/Geo/TCX/Lap.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Geo/TCX/Trackpoint.pm ↑ Summary
1 package Geo::TCX::Lap;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = '1.04';
6 our @ISA=qw(Geo::TCX::Track);
7  
8 =encoding utf-8
9  
10 =head1 NAME
11  
12 Geo::TCX::Lap - Extract and edit info from Lap data
13  
14 =head1 SYNOPSIS
15  
16 use Geo::TCX::Lap;
17  
18 =head1 DESCRIPTION
19  
20 This package is mainly used by the L<Geo::TCX> module and serves little purpose on its own. The interface is documented mostly for the purpose of code maintainance.
21  
22 A sub-class of L<Geo::TCX::Track>, it enables extracting and editing lap information associated with tracks contained in Garmin TCX files. Laps are a more specific form of a Track in that may contain additional information such as lap aggregates (e.g. TotalTimeSeconds, DistanceMeters, …), performance metrics (e.g. MaximumSpeed, AverageHeartRateBpm, …), and other useful fields.
23  
24 The are two types of C<Geo::TCX::Lap>: Activity and Courses.
25  
26 =over 4
27  
28 =item Activity
29  
30 Activity laps are tracks recorded by the Garmin from one of the activity types ('Biking', 'Running', 'MultiSport', 'Other') and saved in what is often refered to ashistory files.
31  
32 =item Course
33  
34 Course laps typically originate from history files that are converted to a course either by a Garmin device or some other software for the purpose of navigation or training. They contain course-specific fields such as C<BeginPosition> and C<EndPosition> and some lap aggregagates but do not contain the performance-metrics or other fields that acivity laps contain.
35  
36 =back
37  
38 See the AUTOLOAD section for a list of all supported fields for each type of lap.
39  
40 Some methods and accessors are applicable only to one type. This is specified in the documentation for each.
41  
42 =cut
43  
44 7 ▶use Carp qw(confess croak cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use Geo::TCX::Track;
Subroutine BEGIN: called CC=1 SCAR=2.6
46 use overload '+' => \&merge;
47 use vars qw($AUTOLOAD %possible_attr);
48  
49 # file-scoped lexicals
50 my @attr = qw/ AverageHeartRateBpm Cadence Calories DistanceMeters Intensity MaximumHeartRateBpm MaximumSpeed TotalTimeSeconds TriggerMethod StartTime BeginPosition EndPosition/;
51 $possible_attr{$_} = 1 for @attr;
52 # last 2 are specific to courses only
53 # no Track tag, wouldn't make sense to AUTOLOAD it
54  
55 =head2 Constructor Methods (class)
56  
57 =over 4
58  
59 =item new( $xml_string, $lapno )
60  
61 parses and xml string in the form of the lap portion from a Garmin Activity or Course and returns a C<Geo::TCX::Lap> object.
62  
63 No examples are provided as this constructor is typically called by instances of L<Geo::TCX>. The latter then provides various methods to access lap data and info. The I<$lapno> (lap number) is optional.
64  
65 =back
66  
67 =cut
68  
69 sub new {
70 my $proto = shift;
71 my $class = ref($proto) || $proto;
72 my ($str, $lapnumber, $last_point_previous_lap) = (shift, shift, shift);
73 if (ref $last_point_previous_lap) {
74 croak 'second argument must be a Trackpoint object'
75 unless $last_point_previous_lap->isa('Geo::TCX::Trackpoint')
76 }
77 my %opts = @_; # none for now, but setting it up
78  
79 my ($type, $starttime, $metrics, $metrics_and_track, $track_str);
80 if ( $str =~ /\<Lap StartTime="(.*?)"\>(.*?)\<\/Lap\>/s ) {
81 $type = 'Activity';
82 $starttime = $1;
83 $metrics_and_track = $2;
84 if ( $metrics_and_track =~ /(.*?)(\<Track\>.*\<\/Track\>)/s ) {
85 $metrics = $1;
86 $track_str = $2
87 }
88 } elsif ( $str =~ /\<Lap\>(.*?)\<\/Lap\>(.*)/s ) {
89 $type = 'Course';
90 $metrics = $1;
91 $track_str = $2
92 } else { croak 'string argument not in a format supported' }
93 croak 'No track data found in lap' unless $track_str;
94  
95 # First, create the track object from the super-class
96  
97 my $l = $class->SUPER::new( $track_str, $last_point_previous_lap );
98 bless($l, $class);
99  
100 if ($type eq 'Activity') {
101 $l->{_type} = 'Activity';
102 $l->{StartTime} = $starttime;
103  
104 $l->_process_remaining_lap_metrics( \$metrics );
105  
106 # Lap is smarter than Track:
107 # it knows that its StartTime may be ahead of the time of the first trackpoint
108 # so force a replace of the elapsed time with that time difference
109  
110 # StartTime is not a trackpoint, but we can create a fake one so we can
111 # get an trackpoint object that allows us to get the epoch time from it
112 my $fake = _fake_starttime_point( $l->{StartTime} );
113 my $time_elapsed = $l->trackpoint(1)->time_epoch - $fake->time_epoch;
114 $l->trackpoint(1)->time_elapsed( $time_elapsed, force => 1)
115 }
116 if ($type eq 'Course') {
117 $l->{_type} = 'Course';
118  
119 # Lap is again smarter than Track:
120 # but instead of knowing *when* it started (as for activities), it knows *where*
121 # nb: courses converted by save_laps() and Ride with GPS always have the BeginPosition
122 # equal to the first trackpoint.
123  
124 if ( $metrics =~ s,\<BeginPosition\>(.*)\</BeginPosition\>,,g) {
125 $l->{BeginPosition} = Geo::TCX::Trackpoint->new( $1 )
126 }
127 if ( $metrics =~ s,\<EndPosition\>(.*)\</EndPosition\>,,g) {
128 $l->{EndPosition} = Geo::TCX::Trackpoint->new( $1 )
129 }
130  
131 $l->_process_remaining_lap_metrics( \$metrics );
132  
133 my ($meters, $time_elapsed) = (undef, 0);
134 # can compare if $meters is almost identical to $l->trackpoint(1)->DistanceMeters;
135 # we could simply have used the later to estimate the time elapsed but it is nice
136 # to check from the BeginPosition
137 $meters = $l->{BeginPosition}->distance_to( $l->trackpoint(1) );
138 if ($meters > 0) {
139 my $avg_speed = $l->_avg_speed_meters_per_second;
140 $time_elapsed = sprintf( '%.0f', $meters / $avg_speed );
141 }
142 $l->trackpoint(1)->time_elapsed( $time_elapsed, force => 1 )
143 }
144  
145 $l->{_lapmetrics} = $metrics; # delete this ine once I am sure that I capture all metrics and track info properly
146  
147 # estimate auto-pause time for use by split()
148 $l->{_time_auto_paused} = sprintf( '%.2f', $l->totaltimeseconds - $l->TotalTimeSeconds);
149 return $l
150 }
151  
152 =head2 Constructor Methods (object)
153  
154 =over 4
155  
156 =item merge( $lap, as_is => boolean )
157  
158 Returns a new C<Geo::TCX::Lap> merged with the lap specified in I<$lap>.
159  
160 $merged = $lap1->merge( $lap2 );
161  
162 Adjustments for the C<DistanceMeters> and C<Time> fields of each trackpoint in the lap are made unless C<as_is> is set to true.
163  
164 Lap aggregates C<TotalTimeSeconds> and C<DistanceMeters> are adjusted. For Activity laps, performance metrics such as C<MaximumSpeed>, C<AverageHeartRateBpm>, …, are also adjusted. For Course laps, C<EndPosition> is also adjusted.
165  
166 Unlike the C<merge_laps()> method in L<Geo::TCX>, the laps do not need to originate from the same *.tcx file, hence there is also no requirement that they be consecutive laps as is the case in the former.
167  
168 =back
169  
170 =cut
171  
172 sub merge {
173 my ($x, $y) = (shift, shift);
174 croak 'both operands must be Lap objects' unless $y->isa('Geo::TCX::Lap');
175 my %opts = @_;
176  
177 my $m = $x->SUPER::merge($y, speed => $y->_avg_speed_meters_per_second, as_is => $opts{'as_is'});
178  
179 $m->{DistanceMeters} = $m->DistanceMeters + $y->DistanceMeters;
180 $m->{_time_auto_paused} = sprintf('%.2f', $m->{_time_auto_paused} + $y->{_time_auto_paused});
181  
182 if ($opts{as_is}) { # then do not adjust TTS, just summ them up
183 $m->{TotalTimeSeconds} = sprintf('%.2f', $m->TotalTimeSeconds + $y->TotalTimeSeconds)
184 } else {
185 # i.e. if the 2nd lap did not come from the same ride, we will have estimated the elapsed time bewteen the two tracks
186 $m->{TotalTimeSeconds} = sprintf('%.2f', $m->totaltimeseconds - $m->{_time_auto_paused})
187 }
188  
189 if ($m->is_activity) { # aggregates specific to activities
190 my $pcent = $y->TotalTimeSeconds / $m->TotalTimeSeconds;
191  
192 # max values
193 if (defined $m->MaximumSpeed) {
194 if (defined $y->MaximumSpeed) {
195 $m->{MaximumSpeed} = ($m->MaximumSpeed > $y->MaximumSpeed) ? $m->MaximumSpeed : $y->MaximumSpeed
196 } else { $m->{MaximumSpeed} = undef }
197 }
198 if (defined $m->MaximumHeartRateBpm) {
199 if (defined $y->MaximumHeartRateBpm) {
200 $m->{MaximumHeartRateBpm} = ($m->MaximumHeartRateBpm > $y->MaximumHeartRateBpm) ? $m->MaximumHeartRateBpm : $y->MaximumHeartRateBpm
201 } else { $m->{MaximumHeartRateBpm} = undef }
202 }
203  
204 # average values
205 if (defined $m->AverageHeartRateBpm) {
206 if (defined $y->AverageHeartRateBpm) {
207 $m->{AverageHeartRateBpm} = sprintf '%.0f', ( (1 - $pcent) * $m->AverageHeartRateBpm + $pcent * $y->AverageHeartRateBpm )
208 } else { $m->{AverageHeartRateBpm} = undef }
209 }
210 if (defined $m->Cadence) {
211 if (defined $y->Cadence) {
212 $m->{Cadence} = sprintf '%.0f', ( (1 - $pcent) * $m->Cadence + $pcent * $y->Cadence )
213 } else { $m->{Cadence} = undef }
214 }
215  
216 # summed values
217 if (defined $m->Calories) {
218 if (defined $y->Calories) {
219 $m->{Calories} = $m->Calories + $y->Calories
220 } else { $m->{Calories} = undef }
221 }
222  
223 # keep values of first lap for other attr: Intensity, TriggerMethod, and StartTime
224 # Intensity: I have never seen another setting than Active
225 # TriggerMethod: I consider that one barely relevant
226  
227 } else { # aggregates specific to courses
228 $m->{EndPosition} = $y->trackpoint(-1)->to_basic
229 }
230 return $m
231 }
232  
233 =over 4
234  
235 =item split( # )
236  
237 Returns a 2-element array of C<Geo::TCX::Lap> objects with the first consisting of the lap up to and including point number I<#> and the second consisting of the all trackpoints after that point.
238  
239 ($lap1, $lap2) = $merged->split( 45 );
240  
241 Lap aggregates C<TotalTimeSeconds> and C<DistanceMeters> are recalculated, some small measurement error is to be expected due to the amount of time the device was an auto-pause.
242  
243 For Activity laps, the performance metrics C<MaximumSpeed>, C<MaximumHeartRateBpm>, C<AverageHeartRateBpm>, C<Cadence>, and C<Calories> are also recalculated for each lap (if they were defined). C<StartTime> is also adjusted for the second lap.
244  
245 For Course laps, C<BeginPosition> and C<EndPosition> are also adjusted.
246  
247 Will raise exception unless called in list context.
248  
249 =back
250  
251 =cut
252  
253 sub split {
254 my $lap = shift;
255 croak 'split() expects to be called in list context' unless wantarray;
256 my ($l1, $l2) = $lap->SUPER::split( shift );
257  
258 if ($lap->is_activity) {
259 $l2->{StartTime} = $l1->trackpoint(-1)->Time;
260 for my $l ($l1, $l2 ) {
261 $l->{MaximumSpeed} = $l->maximumspeed if defined $l->MaximumSpeed;
262 $l->{MaximumHeartRateBpm} = $l->maximumheartratebpm if defined $l->MaximumHeartRateBpm;
263 $l->{AverageHeartRateBpm} = $l->averageheartratebpm if defined $l->AverageHeartRateBpm;
264 $l->{Cadence} = $l->cadence if defined $l->Cadence;
265  
266 my $pcent = $l->trackpoints / $lap->trackpoints;
267 $l->{_time_auto_paused} = sprintf( '%.2f', $lap->{_time_auto_paused} * $pcent );
268 $l->{TotalTimeSeconds} = sprintf( '%.2f', $l->totaltimeseconds - $l->{_time_auto_paused});
269 $l->{DistanceMeters} = $l->distancemeters;
270  
271 $l->{Calories} = sprintf('%.0f', $lap->Calories * $pcent) if defined $l->Calories
272 }
273 } else {
274 $l1->{EndPosition} = $l1->trackpoint(-1)->to_basic;
275 $l2->{BeginPosition} = $l2->trackpoint( 1)->to_basic;
276 $l2->trackpoint(1)->distance_elapsed(0, force => 1 );
277 $l2->trackpoint(1)->time_elapsed( 0, force => 1 );
278 for my $l ($l1, $l2 ) {
279 my $pcent = $l->trackpoints / $lap->trackpoints;
280 $l->{_time_auto_paused} = sprintf( '%.2f', $lap->{_time_auto_paused} * $pcent );
281 $l->{TotalTimeSeconds} = sprintf( '%.2f', $l->totaltimeseconds - $l->{_time_auto_paused});
282 $l->{DistanceMeters} = $l->distancemeters
283 }
284 }
285 return $l1, $l2
286 }
287  
288 =over 4
289  
290 =item reverse( # )
291  
292 This method is allowed only for Courses and returns a clone of the lap object with the order of the trackpoints reversed.
293  
294 $reversed = $lap->reverse;
295  
296 When reversing a course, the time and distance information is set at 0 at the first trackpoint. Therefore, the lap aggregates (C<DistanceMeters>, C<TotalTimeSeconds>) may be smaller by a few seconds and meters compared to the original lap due to loss of elapsed time and distance information from the original lap's first point.
297  
298 =back
299  
300 =cut
301  
302 sub reverse {
303 my $l = shift->clone;
304 croak 'reverse() can only be used on Course laps' unless $l->is_course;
305  
306 $l = $l->SUPER::reverse;
307 $l->trackpoint(1)->time_elapsed( 0, force => 1);
308 # will always be 0 for a reversed lap because I never estimate time b/w
309 # the last point of a track and the EndPosition (would not make sense)
310 $l->{BeginPosition} = $l->trackpoint( 1)->to_basic;
311 $l->{EndPosition} = $l->trackpoint(-1)->to_basic;
312 # if we assign an existing trackpoint to Begin/EndPos, should we strip the non-positional info?
313 # we could get the xml_string from the trakcpoints and create a new point with just the <Position>...</Position> stuff.
314 # I think we should, think about it
315 $l->{DistanceMeters} = $l->distancemeters;
316 $l->{TotalTimeSeconds} = $l->totaltimeseconds;
317 return $l
318 }
319  
320 =head2 AUTOLOAD Methods
321  
322 =over 4
323  
324 =item I<field>( $value )
325  
326 Methods with respect to certain fields can be autoloaded and return the current or newly set value.
327  
328 Possible fields for Activity laps consist of: C<AverageHeartRateBpm>, C<Cadence>, C<Calories>, C<DistanceMeters>, C<Intensity>, C<MaximumHeartRateBpm>, C<MaximumSpeed>, C<TotalTimeSeconds>, C<TriggerMethod>, C<StartTime>.
329  
330 Course laps contain aggregates such as C<DistanceMeters>, C<TotalTimeSeconds> but not much else. They also contain C<BeginPosition> and C<EndPosition> which are exclusive to courses. They also contain C<Intensity> which almost always equal to 'Active'.
331  
332 Some fields may contain a value of 0, C<Calories> being one example. It is safer to check if a field is defined with C<< if (defined $lap->Calories) >> rather than C<< if ($lap->Calories) >>.
333  
334 Caution should be used if setting a I<$value> as no checks are performed to ensure the value is appropriate or in the proper format.
335  
336 =back
337  
338 =cut
339  
340 sub AUTOLOAD {
341 my $self = shift;
342 my $attr = $AUTOLOAD;
343 $attr =~ s/.*:://;
344 return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
345 croak "invalid attribute method: -> $attr()" unless $possible_attr{$attr};
346 $self->{$attr} = shift if @_;
347 return $self->{$attr};
348 }
349  
350 =head2 Object Methods
351  
352 =over 4
353  
354 =item is_activity()
355  
356 =item is_course()
357  
358 True if the given lap is of the type indicated by the method, false otherwise.
359  
360 =back
361  
362 =cut
363  
364 sub is_activity { return (shift->StartTime) ? 1 : 0 }
365 sub is_course { return (shift->StartTime) ? 0 : 1 }
366  
367 =over 4
368  
369 =item time_add( @duration )
370  
371 =item time_subtract( @duration )
372  
373 Perform L<DateTime> math on the timestamps of each trackpoint in the lap by adding or subtracting the specified duration. Return true.
374  
375 The duration can be provided as an actual L<DateTime::Duration> object or an array of arguments as per the syntax of L<DateTime>'s C<add()> or C<subtract()> methods. See the pod for C<< Geo::TCX::Trackpoint->time_add() >>.
376  
377 =back
378  
379 =cut
380  
381 sub time_add {
382 my $l = shift;
383 my @duration = @_;
384 $l->SUPER::time_add( @duration);
385  
386 if ($l->is_activity) {
387 # need to increment StartTime as well since not <=> Time of 1st point
388 my $fake = _fake_starttime_point( $l->{StartTime} );
389 $fake->time_add(@duration);
390 $l->{StartTime} = $fake->Time
391 }
392 return 1
393 }
394  
395 sub time_subtract {
396 my $l = shift;
397 my @duration = @_;
398 $l->SUPER::time_subtract( @duration);
399  
400 if ($l->is_activity) {
401 # need to increment StartTime as well since not <=> Time of 1st point
402 my $fake = _fake_starttime_point( $l->{StartTime} );
403 $fake->time_subtract(@duration);
404 $l->{StartTime} = $fake->Time
405 }
406 return 1
407 }
408  
409 sub _fake_starttime_point {
410 my $starttime = shift;
411 my $fake_pt = Geo::TCX::Trackpoint::Full->new("<Trackpoint><Time>$starttime</Time><Position><LatitudeDegrees>45.5</LatitudeDegrees><LongitudeDegrees>-72.5</LongitudeDegrees></Position><DistanceMeters>0</DistanceMeters></Trackpoint>");
412 return $fake_pt
413 }
414  
415 =over 4
416  
417 =item distancemeters()
418  
419 =item totaltimeseconds()
420  
421 =item maximumspeed()
422  
423 =item maximumheartratebpm()
424  
425 =item averageheartratebpm()
426  
427 =item cadence()
428  
429 Calculate and return the distance meters, totaltimeseconds, maximum speed (notionally corresponding to a lap's C<DistanceMeters> and C<TotalTimeSeconds> fields) from the elapsed data contained in each point of the lap's track. The heartrate information is calculated based on the C<HeartRateBpm> field of the trackpoints. The cadence is computed from the average cadence of all the trackpoints' C<Cadence> fields.
430  
431 The methods do not (yet) reset the fields of the lap yet. The two values may differ due to rounding, the fact that the Garmin recorded the aggregate field with miliseconds and some additional distance the garmin may have recorded between laps, etc. Any difference should be insignificant in relation to the measurement error introduced by the device itself.
432  
433 =back
434  
435 =cut
436  
437 sub distancemeters {
438 my $l = shift;
439 croak 'distancemeters() expects no arguments' if @_;
440 my $distancemeters = 0;
441 for my $i (1 .. $l->trackpoints) {
442 $distancemeters += $l->trackpoint($i)->distance_elapsed
443 }
444 return $distancemeters
445 }
446  
447 sub totaltimeseconds {
448 my $l = shift;
449 croak 'totaltimeseconds() expects no arguments' if @_;
450 my $totaltimeseconds = 0;
451 for my $i (1 .. $l->trackpoints) {
452 $totaltimeseconds += $l->trackpoint($i)->time_elapsed
453 }
454 return $totaltimeseconds
455 }
456  
457 sub maximumspeed {
458 my $l = shift;
459 croak 'maximumspeed() expects no arguments' if @_;
460 my ($max_speed, $speed) = (0);
461 for (1 .. $l->trackpoints) {
462 $speed = $l->trackpoint($_)->distance_elapsed / $l->trackpoint($_)->time_elapsed;
463 $max_speed = $speed if $speed > $max_speed
464 }
465 return sprintf("%.3f", $max_speed )
466 }
467  
468 sub maximumheartratebpm {
469 my $l = shift;
470 croak 'maximumheartratebpm() expects no arguments' if @_;
471 croak 'lap has no heart rate information' unless $l->MaximumHeartRateBpm;
472 my ($max_hr, $hr) = (0);
473 for (1 .. $l->trackpoints) {
474 $hr = $l->trackpoint($_)->HeartRateBpm;
475 $max_hr = $hr if $hr > $max_hr
476 }
477 return sprintf("%.0f", $max_hr)
478 }
479  
480 sub averageheartratebpm {
481 my $l = shift;
482 croak 'averageheartratebpm() expects no arguments' if @_;
483 croak 'lap has no heart rate information' unless $l->AverageHeartRateBpm;
484 my $n_points = $l->trackpoints;
485 my $sum_hr;
486 for (1 .. $n_points) {
487 $sum_hr += $l->trackpoint($_)->HeartRateBpm
488 }
489 return sprintf("%.0f", $sum_hr / $n_points)
490 }
491  
492 sub cadence {
493 my $l = shift;
494 croak 'cadence() expects no arguments' if @_;
495 croak 'lap has no cadence information' unless $l->Cadence;
496 my $n_points = $l->trackpoints;
497 my $sum_cadence;
498 for (1 .. $n_points) {
499 $sum_cadence += $l->trackpoint($_)->Cadence
500 }
501 return sprintf("%.0f", $sum_cadence / $n_points)
502 }
503  
504 =over 4
505  
506 =item xml_string()
507  
508 returns a string containing the XML representation of object, useful for subsequent saving into an *.tcx file. The string is equivalent to the string argument expected by C<new()>.
509  
510 =back
511  
512 =cut
513  
514 sub xml_string {
515 my ($l, $as_course, $str, %opts);
516 $l = shift;
517 %opts = @_;
518 $as_course = 1 if $opts{course} or $l->is_course;
519  
520 my $newline = $opts{indent} ? "\n" : '';
521 my $tab = $opts{indent} ? ' ' : '';
522  
523 if ( $as_course ) {
524 $str .= $newline . $tab x 3 . "<Lap>"
525 } else {
526 $str .= $newline . $tab x 3 . "<Lap StartTime=\"" . $l->{StartTime} . "\">"
527 }
528  
529 # the lap meta data
530 $str .= $newline . $tab x 4 . "<TotalTimeSeconds>" . $l->{TotalTimeSeconds} . "</TotalTimeSeconds>" if $l->{TotalTimeSeconds};
531 $str .= $newline . $tab x 4 . "<DistanceMeters>" . $l->{DistanceMeters} . "</DistanceMeters>" if $l->{DistanceMeters};
532  
533 if ( $as_course ) {
534 my ($beg, $end, $beg_lat, $beg_lon, $end_lat, $end_lon);
535 if ($l->is_course) {
536 $beg_lat = $l->BeginPosition->LatitudeDegrees;
537 $beg_lon = $l->BeginPosition->LongitudeDegrees;
538 $end_lat = $l->EndPosition->LatitudeDegrees;
539 $end_lon = $l->EndPosition->LongitudeDegrees;
540 } else {
541 $beg_lat = $l->trackpoint( 1)->LatitudeDegrees;
542 $beg_lon = $l->trackpoint( 1)->LongitudeDegrees;
543 $end_lat = $l->trackpoint(-1)->LatitudeDegrees;
544 $end_lon = $l->trackpoint(-1)->LongitudeDegrees;
545 }
546 $str .= $newline . $tab x 4 . "<BeginPosition>";
547 $str .= $newline . $tab x 5 . "<LatitudeDegrees>$beg_lat</LatitudeDegrees>";
548 $str .= $newline . $tab x 5 . "<LongitudeDegrees>$beg_lon</LongitudeDegrees>";
549 $str .= $newline . $tab x 4 . "</BeginPosition>";
550 $str .= $newline . $tab x 4 . "<EndPosition>";
551 $str .= $newline . $tab x 5 . "<LatitudeDegrees>$end_lat</LatitudeDegrees>";
552 $str .= $newline . $tab x 5 . "<LongitudeDegrees>$end_lon</LongitudeDegrees>";
553 $str .= $newline . $tab x 4 . "</EndPosition>";
554 $str .= $newline . $tab x 4 . "<Intensity>" . $l->{Intensity} . "</Intensity>" if $l->{Intensity};
555 $str .= $newline . $tab x 3 . "</Lap>"
556 } else {
557 $str .= $newline . $tab x 4 . "<MaximumSpeed>" . $l->{MaximumSpeed} . "</MaximumSpeed>" if $l->{MaximumSpeed};
558 $str .= $newline . $tab x 4 . "<Calories>" . $l->{Calories} . "</Calories>" if $l->{Calories};
559 $str .= $newline . $tab x 4 . "<AverageHeartRateBpm><Value>" . $l->{AverageHeartRateBpm} . "</Value></AverageHeartRateBpm>" if $l->{AverageHeartRateBpm};
560 $str .= $newline . $tab x 4 . "<MaximumHeartRateBpm><Value>" . $l->{MaximumHeartRateBpm} . "</Value></MaximumHeartRateBpm>" if $l->{MaximumHeartRateBpm};
561 $str .= $newline . $tab x 4 . "<Intensity>" . $l->{Intensity} . "</Intensity>" if $l->{Intensity};
562 $str .= $newline . $tab x 4 . "<Cadence>" . $l->{Cadence} . "</Cadence>" if $l->{Cadence};
563 $str .= $newline . $tab x 4 . "<TriggerMethod>" . $l->{TriggerMethod} . "</TriggerMethod>" if $l->{TriggerMethod};
564 }
565  
566 my $n_tabs = ($as_course) ? 3 : 4; # <Track> for Activities have one more level of indentation compared to Courses
567  
568 $str .= $l->SUPER::xml_string( indent => $opts{indent}, n_tabs => $n_tabs );
569  
570 unless ($as_course) {
571 $str .= $newline . $tab x 3 . "</Lap>"
572 }
573 return $str
574 }
575  
576 =head2 Overloaded Methods
577  
578 =over 4
579  
580 =item +
581  
582 can concatenate two laps by issuing C<$lap = $lap1 + $lap2> on two Lap objects.
583  
584 =back
585  
586 =cut
587  
588 #
589 # internal methods
590  
591 sub _process_remaining_lap_metrics {
592 my ($self, $lap_metrics) = @_;
593 # Some fields are contained within <Value>#</Value> attr, don't need this
594 # will add those back before saving any files
595 $$lap_metrics =~ s,\<Value\>(.*?)\<\/Value\>,$1,g;
596 while ( $$lap_metrics =~ /\<(.*?)\>(.*?)\<.*?\>/sg ) {
597 $self->{$1} = $2
598 }
599 }
600  
601 sub _avg_speed_meters_per_second {
602 my $self = shift;
603 return $self->DistanceMeters / $self->TotalTimeSeconds
604 }
605  
606 sub _avg_speed_km_per_hour {
607 my $self = shift;
608 return $self->_avg_speed_meters_per_second * 3600 / 1000
609 }
610  
611 =head1 EXAMPLES
612  
613 Coming soon.
614  
615 =head1 AUTHOR
616  
617 Patrick Joly
618  
619 =head1 VERSION
620  
621 1.04
622  
623 =head1 SEE ALSO
624  
625 perl(1).
626  
627 =cut
628  
629 1;
630  
« blib/lib/Geo/TCX/Trackpoint.pm ↑ Summary