blib/lib/Geo/TCX/Trackpoint.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Geo/TCX/Track.pm ↑ Summary blib/lib/Geo/TCX/Lap.pm »
1 package Geo::TCX::Trackpoint;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = '1.04';
6  
7 =encoding utf-8
8  
9 =head1 NAME
10  
11 Geo::TCX::Trackpoint - Class to store and edit TCX trackpoints
12  
13 =head1 SYNOPSIS
14  
15 use Geo::TCX::Trackpoint;
16  
17 =head1 DESCRIPTION
18  
19 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.
20  
21 L<Geo::TCX::Trackpoint> provides a data structure for TCX trackpoints and provides accessor methods to read and edit trackpoint data.
22  
23 TCX trackpoints are different from GPX trackpoints in that they contain tags such as C<AltitudeMeters>, C<DistanceMeters>, C<HeartRateBpm>, C<Time>, and potentially C<Cadence>, C<SensorState>. Also the coordinates are tagged with longer-form fields as C<LatitudeDegrees>, C<LongitudeDegrees>.
24  
25 =cut
26  
27 8 ▶use Geo::Gpx::Point;
Subroutine BEGIN: called CC=1 SCAR=2.6
28 use Carp qw(confess croak cluck);
29 use vars qw($AUTOLOAD %possible_attr);
30  
31 # file-scoped lexicals
32 my @attr = qw/ LatitudeDegrees LongitudeDegrees /;
33 $possible_attr{$_} = 1 for @attr;
34  
35 =head2 Constructor Methods
36  
37 =over 4
38  
39 =item new( $xml_str )
40  
41 Takes an xml string argument containing coordinates contained within the C<Position> xml tag (optional) as recorded by Garmin Edge devices and returns a basic C<Geo::TCX::Trackpoint> object containing only coordinates.
42  
43 $str_basic = '<Position><LatitudeDegrees>45.304996</LatitudeDegrees><LongitudeDegrees>-72.637243</LongitudeDegrees></Position>';
44 $tp_basic = Geo::TCX::Trackpoint->new( $str_basic );
45  
46 =item Geo::TCX::Trackpoint::Full::new( $xml_str, $previous_pt )
47  
48 Takes an xml string argument in the form of a Garmin TCX trackpoint, as recorded by Garmin Edge devices, and returns a C<Geo::TCX::Trackpoint::Full> object containing fields that are supplementary to coordinates. See the list of fields in the AUTOLOAD section below.
49  
50 $str_full = '<Trackpoint><Time>2014-08-11T10:25:26Z</Time><Position><LatitudeDegrees>45.304996</LatitudeDegrees><LongitudeDegrees>-72.637243</LongitudeDegrees></Position><AltitudeMeters>211.082</AltitudeMeters><DistanceMeters>13.030</DistanceMeters><HeartRateBpm><Value>80</Value></HeartRateBpm></Trackpoint>';
51  
52 $tp_full = Geo::TCX::Trackpoint::Full->new( $str_full );
53  
54 I<$previous_pt> is optional and if specified will be interpreted as the previous trackpoint and be used to keep track of the distance and time that have elapsed since the latter. See the methods below to access these "elapsed" fields. If no previous trackpoint is provided, the elapsed time will remain undefined and the elapsed distance will set to the C<DistanceMeters> field of the trackpoint.
55  
56 =back
57  
58 =cut
59  
60 sub new {
61 my ($proto, $pt_str) = (shift, shift);
62 croak 'too many arguments specified' if @_;
63 my $class = ref($proto) || $proto;
64 my $pt = {};
65 bless($pt, $class);
66  
67 # Lat and Long are contained in that tag, not needed
68 $pt_str =~ s,\</*Position\>,,g;
69  
70 # initialize fields/attr
71 while ($pt_str =~ m,\<([^<>]*)\>(.*?)\</([^<>]*)\>,gs) {
72 # or could simply state =~ m,\<(.*?)\>(.*?)\</.*?\>,gs)
73 croak 'Could not match identical attr' unless $1 eq $3;
74 croak 'field not allowed' unless $possible_attr{$1};
75 $pt->{$1} = $2
76 }
77 return $pt
78 }
79  
80 =over 4
81  
82 =item clone()
83  
84 Returns a deep copy of a C<Geo::TCX::Trackpoint> instance.
85  
86 $clone = $trackpoint->clone;
87  
88 =back
89  
90 =cut
91  
92 sub clone {
93 my $clone;
94 eval(Data::Dumper->Dump([ shift ], ['$clone']));
95 confess $@ if $@;
96 return $clone
97 }
98  
99 =head2 AUTOLOAD Methods
100  
101 =cut
102  
103 =over 4
104  
105 =item I<field>( $value )
106  
107 Methods with respect to certain fields can be autoloaded and return the current or newly set value.
108  
109 For Basic trackpoints, LatitudeDegrees and LongitudeDegrees are the supported fields.
110  
111 For Full trackpoints, supported fields are: LatitudeDegrees, LongitudeDegrees, AltitudeMeters, HeartRateBpm, Cadence, and SensorState.
112  
113 Some fields may contain a value of 0. It is safer to check if a field is defined with C<< if (defined $trackpoint->Cadence) >> rather than C<< if ($trackpoint->Cadence) >>.
114  
115 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.
116  
117 =back
118  
119 =cut
120  
121 sub AUTOLOAD {
122 my $self = shift;
123 my $attr = $AUTOLOAD;
124 $attr =~ s/.*:://;
125 return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
126 croak "invalid attribute method: -> $attr()" unless $possible_attr{$attr};
127 $self->{$attr} = shift if @_;
128 return $self->{$attr}
129 }
130  
131 =head2 Object Methods
132  
133 =cut
134  
135 =over 4
136  
137 =item to_gpx()
138  
139 Returns a trackpoint as a L<Geo::Gpx::Point>.
140  
141 =back
142  
143 =cut
144  
145 sub to_gpx {
146 my ($pt, %attr) = @_; # call to new() will handle error check
147 my %fields = ( lat => $pt->LatitudeDegrees, lon => $pt->LongitudeDegrees );
148 $fields{ele} = $pt->AltitudeMeters if defined $pt->{AltitudeMeters};
149 $fields{time} = $pt->{_time_epoch} if defined $pt->{Time};
150 return Geo::Gpx::Point->new( %fields, %attr );
151 }
152  
153 =over 4
154  
155 =item to_geocalc()
156  
157 Returns a trackpoint as a L<Geo::Calc> object. (Requires that the L<Geo::Calc> module be installed.)
158  
159 =back
160  
161 =cut
162  
163 sub to_geocalc {
164 require Geo::Calc;
165 my $pt = shift;
166 croak "to_geocalc() takes no arguments" if @_;
167 return Geo::Calc->new( lat => $pt->LatitudeDegrees, lon => $pt->LongitudeDegrees );
168 }
169  
170 =over 4
171  
172 =item to_basic()
173  
174 Returns a trackpoint as a C<Geo::TCX::Trackpoint> object with only position information (i.e coordinates).
175  
176 =back
177  
178 =cut
179  
180 sub to_basic {
181 my $pt = shift;
182 croak "to_geocalc() takes no arguments" if @_;
183 my $newpt = {};
184 bless($newpt, 'Geo::TCX::Trackpoint');
185 $newpt->LatitudeDegrees( $pt->LatitudeDegrees );
186 $newpt->LongitudeDegrees( $pt->LongitudeDegrees );
187 return $newpt
188 }
189  
190 =over 4
191  
192 =item distance_to ( $trackpoint )
193  
194 Calculates and returns the distance to the specified I<$trackpoint> object.
195  
196 =back
197  
198 =cut
199  
200 sub distance_to {
201 my ($from, $to) = (shift, shift);
202 croak 'expects a single trackpoint as argument' if @_ or ! $to->isa('Geo::TCX::Trackpoint');
203 my $dist = $from->to_gpx->distance_to( lat => $to->LatitudeDegrees, lon => $to->LongitudeDegrees );
204 return $dist
205 }
206  
207 =over 4
208  
209 =item xml_string()
210  
211 returns a string containing the XML representation of the object, equivalent to the string argument expected by C<new()>.
212  
213 =back
214  
215 =cut
216  
217 sub xml_string {
218 my $pt = shift;
219 my %opts = @_;
220  
221 my $newline = $opts{indent} ? "\n" : '';
222 my $tab = $opts{indent} ? ' ' : '';
223 my $n_tabs = $opts{n_tabs} ? $opts{n_tabs} : 4;
224  
225 my $str;
226 $str .= $newline . $tab x ($n_tabs + 1) . '<Position>';
227 $str .= $newline . $tab x ($n_tabs + 2) . '<LatitudeDegrees>' . $pt->LatitudeDegrees . '</LatitudeDegrees>';
228 $str .= $newline . $tab x ($n_tabs + 2) . '<LongitudeDegrees>' . $pt->LongitudeDegrees . '</LongitudeDegrees>';
229 $str .= $newline . $tab x ($n_tabs + 1) . '</Position>';
230 return $str
231 }
232  
233 =over 4
234  
235 =item summ()
236  
237 For debugging purposes, summarizes the fields of the trackpoint by printing them to screen. Returns true.
238  
239 =back
240  
241 =cut
242  
243 sub summ {
244 my $pt = shift;
245 croak 'summ() expects no arguments' if @_;
246 my %fields;
247 foreach my $key (keys %{$pt}) {
248 print "$key: ", $pt->{$key}, "\n"
249 }
250 return 1
251 }
252  
253 package Geo::TCX::Trackpoint::Full;
254 use strict;
255 use warnings;
256  
257 use DateTime::Format::ISO8601;
258 use Carp qw(confess croak cluck);
259  
260 our $VERSION = '1.04';
261 our @ISA=qw(Geo::TCX::Trackpoint);
262  
263  
264 { # lexical scope for that package
265  
266 use vars qw($AUTOLOAD %possible_attr);
267  
268 our ($LocalTZ, $Formatter);
269 $LocalTZ = DateTime::TimeZone->new( name => 'local' );
270 $Formatter = DateTime::Format::Strptime->new( pattern => '%a %b %e %H:%M:%S %Y' );
271 my $formatter_xsd = DateTime::Format::Strptime->new( pattern => '%Y-%m-%dT%H:%M:%SZ' );
272 # ... to avoid looking up timezone each time Trackpoint->new is called
273  
274 # file-scoped lexicals
275 my @attr = qw/ LatitudeDegrees LongitudeDegrees AltitudeMeters DistanceMeters Time HeartRateBpm Cadence SensorState /;
276 $possible_attr{$_} = 1 for @attr;
277  
278 sub new {
279 my ($proto, $pt_str, $previous_pt) = (shift, shift, shift);
280 if (ref $previous_pt) {
281 croak 'second argument must be a Trackpoint object' unless $previous_pt->isa('Geo::TCX::Trackpoint')
282 }
283 croak 'too many arguments specified' if @_;
284 my $class = ref($proto) || $proto;
285  
286 # Ignoring Extensions tags, might support them at some point
287 $pt_str =~ s,\<Extensions\>.*?\</Extensions\>,,g;
288  
289 my $chomped_str = $pt_str;
290 if ( $chomped_str =~ m,\s*^\<Trackpoint\>(.*)\</Trackpoint\>\s*$,gs ) {
291 $chomped_str = $1
292 }
293 # contrary to Track, the <Trackpoint>...</Trackpoint> are optional
294  
295 # Extract the Position tag and create a basic positional trackpoint
296 my $pt;
297 if ( $chomped_str =~ s/(<Position>.*<\/Position>)//g ) {
298 $pt =$class->SUPER::new( $1 )
299 } else {
300 # $DB::single=1;
301 # I put a debug flag here because I want to see instances where
302 # a trackpoint does not have coordinates and see how I should address those
303 # croak 'no <Position>...</Position> xml tag in string'
304 # call it anyway for now until I figure out how to handle those
305 $pt = {};
306 bless($pt, $class);
307 }
308 $chomped_str =~ s,\</*Value\>,,g; # HeartRateBpm value contained in that tag, not needed
309  
310 # initialize fields/attr
311 while ($chomped_str=~ m,\<([^<>]*)\>(.*?)\</([^<>]*)\>,gs) {
312 # or could simply state =~ m,\<(.*?)\>(.*?)\</.*?\>,gs)
313 croak 'Could not match identical attr' unless $1 eq $3;
314 croak 'field not allowed' unless $possible_attr{$1};
315 $pt->{$1} = $2
316 }
317  
318 # for debugging -- allow trackpoints with only coordinates but inspect them in debugger
319 $pt->{_noTime} = 1 unless defined $pt->{Time};
320 $pt->{_noDist} = 1 unless defined $pt->{DistanceMeters};
321 if ($pt->{_noTime} or $pt->{_noDist}) {
322 # commented out as I am building my databases, way too many files to parse to inspect them now, will uncomment when I am done parsing my databases
323 # $DB::single=1
324 }
325  
326 $pt->_reset_distance( $pt->{DistanceMeters}, $previous_pt ) unless $pt->{_noDist};
327 unless ($pt->{_noTime}) {
328 my $orig_time_string = $pt->{Time};
329 $pt->_reset_time( $pt->{Time}, $previous_pt ) unless $pt->{_noTime};
330 print "strange ISO time not equal to time string from TCX file for this trackpoint\n"
331 if $orig_time_string ne $pt->{_time_iso8601};
332 }
333 return $pt
334 }
335  
336 sub AUTOLOAD {
337 my $self = shift;
338 my $attr = $AUTOLOAD;
339 $attr =~ s/.*:://;
340 return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
341 croak "invalid attribute method: -> $attr()" unless $possible_attr{$attr};
342 $self->{$attr} = shift if @_;
343 return $self->{$attr}
344 }
345  
346 =head2 Object Methods for class Geo::TXC::Trackpoint::Full
347  
348 =over 4
349  
350 =item DistanceMeters()
351  
352 Returns the C<DistanceMeters> field of a trackpoint.
353  
354 =back
355  
356 =cut
357  
358 sub DistanceMeters { return shift->{DistanceMeters} }
359  
360 =over 4
361  
362 =item distance_elapsed( $value, force => true/false )
363  
364 Returns the elapsed distance (in meters) of a point as initially computed when the trackpoint was created. The value is never reset unless C<< force => 1 >> is specified.
365  
366 C<force> is needed internally by L<Geo::TCX::Lap>'s C<split()> and L<Geo::TCX::Track>'s <merge()> methods. Use with caution.
367  
368 =back
369  
370 =cut
371  
372 sub distance_elapsed {
373 my ($pt, $value) = (shift, shift);
374 my %opts = @_;
375 if (defined $value) {
376 croak "need to specify option 'force => 1' to set a value" unless $opts{force};
377 $pt->{_distance_elapsed} = sprintf '%.3f', $value
378 }
379 return $pt->{_distance_elapsed}
380 }
381  
382 =over 4
383  
384 =item Time()
385  
386 Returns the C<Time> field of a trackpoint.
387  
388 =back
389  
390 =cut
391  
392 sub Time { return shift->{Time} }
393  
394 =over 4
395  
396 =item time_dt ()
397  
398 =item time_datetime ()
399  
400 Return a L<DateTime> object corresponding to the time of a trackpoint.
401  
402 =back
403  
404 =cut
405  
406 sub time_dt { return DateTime::Format::ISO8601->parse_datetime( shift->Time ) }
407 sub time_datetime { return DateTime::Format::ISO8601->parse_datetime( shift->Time ) }
408 # we never store a DateTime object but provide a method to create one
409  
410 =over 4
411  
412 =item time_local( $trackpoint )
413  
414 Returns the formatted local time of the trackpoint. The local time is always represented based on the locale of the system that calls this method, not that of where the trackpoint was recorded. It is not possible to know in which time zone a trackpoint was recorded at this stage.
415  
416 =back
417  
418 =cut
419  
420 sub time_local { return shift->{_time_local} }
421  
422 =over 4
423  
424 =item time_add( @duration )
425  
426 =item time_subtract( @duration )
427  
428 Perform L<DateTime> math on the timestamps of each lap's starttime and trackpoint by adding the specified time duration and return true.
429  
430 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, which expect a hash of keys such as
431 years => 3,
432 months => 5,
433 weeks => 1,
434 days => 1,
435 hours => 6,
436 minutes => 15,
437 seconds => 45,
438 nanoseconds => 12000,
439 end_of_month => 'limit'
440  
441 where only the relevant keys need to be specified i.e. C<< time_add( minutes > 30, seconds > 15) >>.
442  
443 =back
444  
445 =cut
446  
447 sub time_add {
448 my ($pt, $dur) = shift;
449 if (ref $_[0] and $_[0]->isa('DateTime::Duration') ) {
450 $dur = shift
451 } else { $dur = DateTime::Duration->new( @_ ) }
452 my $dt = $pt->time_datetime;
453 $dt->add( $dur );
454 $pt->_set_time_keys( $dt );
455 return 1
456 }
457  
458 sub time_subtract {
459 my ($pt, $dur) = shift;
460 if (ref $_[0] and $_[0]->isa('DateTime::Duration') ) {
461 $dur = shift
462 } else { $dur = DateTime::Duration->new( @_ ) }
463 my $dt = $pt->time_datetime;
464 $dt->subtract( $dur );
465 $pt->_set_time_keys( $dt );
466 return 1
467 }
468  
469 =over 4
470  
471 =item time_epoch()
472  
473 Returns the epoch time of a point.
474  
475 =back
476  
477 =cut
478  
479 sub time_epoch { return shift->{_time_epoch} }
480  
481 =over 4
482  
483 =item time_elapsed( $value, force => true/false )
484  
485 Returns the elapsed time of a point as initially computed when the trackpoint was created. The value is never reset unless C<< force => 1 >> is specified.
486  
487 C<force> is needed internally by L<Geo::TCX::Lap>'s constructor, C<split()>, and C<reverse()> methods as well as L<Geo::TCX::Track>'s <reverse()>. Use with caution.
488  
489 =back
490  
491 =cut
492  
493 sub time_elapsed {
494 my ($pt, $value) = (shift, shift);
495 my %opts = @_;
496 if (defined $value) {
497 croak "need to specify option 'force => 1' to set a value" unless $opts{force};
498 $pt->{_time_elapsed} = $value
499 }
500 return $pt->{_time_elapsed}
501 }
502  
503 =over 4
504  
505 =item time_duration( $datetime or $trackpoint or $string or $integer )
506  
507 Returns a L<DateTime::Duration> object containing the duration between the timestamps of two trackpoints. Consistent with the documentation for L<DateTime::Duration> the "duration is relative to the object from which I<$datetime> is subtracted". The duration will be positive if the timestamp of I<$datetime> occurs prior to the trackpoint, otherwise it will be negative.
508  
509 This method accepts four forms for the argument: a L<DateTime> object such as that returned by C<< $pt->time >>, an ISO8601 string such as that returned by C<< $pt->Time >>, a Trackpoint object, or an integer than can be interpreted as an epoch time.
510  
511 These duration objects are useful to pass to C<time_add()> or C<time_subtract>.
512  
513 =back
514  
515 =cut
516  
517 sub time_duration {
518 my $self = shift;
519 my ($dt, $datetime);
520 # first arg can time DateTime or trackpoint, and epoch time, or a time string
521 if (ref $_[0]) {
522 if ( $_[0]->isa('DateTime') ) {
523 $datetime = $_[0]
524 } else {
525 croak 'object as argument must be either a DateTime or a Trackpoint instance'
526 unless $_[0]->isa('Geo::TCX::Trackpoint');
527 $datetime = $_[0]->time_datetime
528 }
529 } elsif ($_[0] =~ /^(\d+)$/) {
530 $datetime = DateTime->from_epoch( epoch => $1 )
531 } else {
532 $datetime = DateTime::Format::ISO8601->parse_datetime( $_[0] )
533 }
534 $dt = $self->time_datetime;
535  
536 my $dur = $dt->subtract_datetime( $datetime );
537 return $dur
538 }
539  
540 sub xml_string {
541 my $pt = shift;
542 my %opts = @_;
543  
544 my $newline = $opts{indent} ? "\n" : '';
545 my $tab = $opts{indent} ? ' ' : '';
546 my $n_tabs = $opts{n_tabs} ? $opts{n_tabs} : 4;
547  
548 my $str;
549 $str .= $newline . $tab x $n_tabs . '<Trackpoint>';
550 $str .= $newline . $tab x ($n_tabs + 1) . '<Time>' . $pt->Time . '</Time>';
551 if (defined $pt->LatitudeDegrees) {
552 $str .= $newline . $tab x ($n_tabs + 1) . '<Position>';
553 $str .= $newline . $tab x ($n_tabs + 2) . '<LatitudeDegrees>' . $pt->LatitudeDegrees . '</LatitudeDegrees>';
554 $str .= $newline . $tab x ($n_tabs + 2) . '<LongitudeDegrees>' . $pt->LongitudeDegrees . '</LongitudeDegrees>';
555 $str .= $newline . $tab x ($n_tabs + 1) . '</Position>';
556 }
557 $str .= $newline . $tab x ($n_tabs + 1) . '<AltitudeMeters>'. $pt->AltitudeMeters . '</AltitudeMeters>';
558 $str .= $newline . $tab x ($n_tabs + 1) . '<DistanceMeters>'. $pt->DistanceMeters . '</DistanceMeters>';
559 if (defined $pt->HeartRateBpm) {
560 $str .= '<HeartRateBpm><Value>'. $pt->HeartRateBpm . '</Value></HeartRateBpm>'
561 }
562 if (defined $pt->Cadence) {
563 $str .= '<Cadence>'. $pt->Cadence . '</Cadence>'
564 }
565 if (defined $pt->SensorState) {
566 $str .= '<SensorState>'. $pt->SensorState . '</SensorState>'
567 }
568 $str .= $newline . $tab x $n_tabs . '</Trackpoint>';
569 return $str
570 }
571  
572 # Internal methods and functions
573  
574 sub _reset_time { # called by new() and by Track.pm
575 my ($pt, $time, $previous_pt) = @_;
576 $previous_pt = pop if ref $_[-1] and $_[-1]->isa('Geo::TCX::Trackpoint');
577 delete $pt->{_time_elapsed}; # by design, immutable in _set_*
578 $pt->_set_time_keys($time, $previous_pt);
579 return 1
580 }
581  
582 sub _reset_time_from_epoch { # called by Track.pm
583 my ($pt, $epoch, $previous_pt) = @_;
584 my $dt = DateTime->from_epoch( epoch => $epoch );
585 delete $pt->{_time_elapsed};
586 $pt->_set_time_keys( $dt, $previous_pt );
587 return 1
588 }
589  
590 sub _reset_distance { # called by new() and by Track.pm
591 my ($pt, $distance, $previous_pt) = @_;
592 if (ref $previous_pt) {
593 croak 'second argument must be a Trackpoint object' unless $previous_pt->isa('Geo::TCX::Trackpoint')
594 }
595 delete $pt->{_distance_elapsed};
596 $pt->_set_distance_keys($distance, $previous_pt);
597 return 1
598 }
599  
600 # Expects a I<$time_string> in a format parseable by L<DateTime::Format::ISO8601>'s C<parse_datetime> constructor
601 # . sets the time-related fields for the trackpoint. Returns true.
602 # . if the _time_elapsed key for the point is not already defined and another trackpoint object is also provided,
603 # e.g. the previous trackpoint, it will also set it (as number of seconds since the timestamp of that previous point)
604 # . allows a DateTime obj as argument instead of $time which is required by methods that need to modify time so
605 # that we can update the keys to be consistent with the new time e.g. time_add(), time_subtract(), _reset_time_from_epoch()
606  
607 sub _set_time_keys {
608 my ($pt, $time, $previous_pt) = (shift, shift);
609 $previous_pt = pop if ref $_[-1] and $_[-1]->isa('Geo::TCX::Trackpoint');
610  
611 my $dt;
612 if ( ref( $time ) and $time->isa('DateTime') ) {
613 $dt = $time
614 } else {
615 $pt->{Time} = $time;
616 $dt = $pt->time_datetime
617 }
618  
619 $pt->{Time} = _time_format($dt);
620 $pt->{_time_iso8601} = _time_format($dt);
621 $pt->{_time_local} = _time_format($dt, local => 1);
622 $pt->{_time_epoch} = $dt->epoch;
623  
624 if ( ! exists $pt->{_time_elapsed} ) { # i.e. immutable here
625 if ( $previous_pt ) {
626 $pt->{_time_elapsed} = $pt->{_time_epoch} - $previous_pt->{_time_epoch}
627 } else { $pt->{_time_elapsed} = undef }
628 }
629 return 1
630 }
631  
632 sub _time_format {
633 my $dt = shift;
634 # !! TODO: check that ref is not a Garmin Object (croack that function is not a class method)
635 my %opts = @_;
636 if ($opts{'local'}) {
637 $dt->set_formatter( $Formatter ); # see pattern in $Formatter
638 $dt->set_time_zone( $LocalTZ )
639 } else {
640 $dt->set_formatter( $formatter_xsd )
641 }
642 return $dt->stringify
643 }
644  
645 # Expects a decimal-number or integer and sets the C<DistanceMeters> field for the trackpoint and returns true
646 # . if the _distance_elapsed key for the point is not already defined and another trackpoint object is also provided,
647 # e.g. the previous trackpoint, it will also set it (number of meters from that previous point)
648  
649 sub _set_distance_keys {
650 my ($pt, $meters, $previous_pt) = shift;
651 $previous_pt = pop if ref $_[-1] and $_[-1]->isa('Geo::TCX::Trackpoint');
652 $meters = shift;
653  
654 my $meters_formatted;
655 $meters_formatted = sprintf("%.3f", $meters) if defined $meters;
656  
657 $pt->{DistanceMeters} = $meters_formatted;
658  
659 if ( ! exists $pt->{_distance_elapsed} ) { # i.e. immutable here
660 if ( $previous_pt ) {
661 my $dist_elapsed = $pt->DistanceMeters - $previous_pt->DistanceMeters;
662 $pt->{_distance_elapsed} = sprintf("%.3f", $dist_elapsed)
663 } else { $pt->{_distance_elapsed} = $meters_formatted }
664 }
665 return 1
666 }
667  
668 }
669  
670 =head1 EXAMPLES
671  
672 Coming soon.
673  
674 =head1 AUTHOR
675  
676 Patrick Joly
677  
678 =head1 VERSION
679  
680 1.04
681  
682 =head1 SEE ALSO
683  
684 perl(1).
685  
686 =cut
687  
688 1;
689  
690 __END__
691  
692 A trackpoint string looks like:
693  
694 <Time>2014-08-11T10:55:26Z</Time><Position><LatitudeDegrees>45.293131</LatitudeDegrees><LongitudeDegrees>-72.650505</LongitudeDegrees></Position><AltitudeMeters>368.591</AltitudeMeters><DistanceMeters>3844.748</DistanceMeters><HeartRateBpm><Value>128</Value></HeartRateBpm>
« blib/lib/Geo/TCX/Track.pm ↑ Summary blib/lib/Geo/TCX/Lap.pm »