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