blib/lib/Geo/TCX.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/script/tcx_interactive.pl ↑ Summary blib/lib/Geo/TCX/Track.pm »
1 package Geo::TCX;
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  
7 =encoding utf-8
8  
9 =head1 NAME
10  
11 Geo::TCX - Parse and edit and TCX activity and course files from GPS training devices
12  
13 =head1 SYNOPSIS
14  
15 use Geo::TCX;
16  
17 =head1 DESCRIPTION
18  
19 C<Geo::TCX> enables the parsing and editing of TCX activity and course files, including those from FIT files. TCX files follow an XML schema developed by Garmin and common to its GPS sports devices. Among other methods, the module enables laps from an activity to be saved as individual *.tcx files, split into separate laps based on a given point, merged, or converted to courses to plan a future activity.
20  
21 FIT activity and course files are supported provided that L<Geo::FIT> is installed and that the C<fit2tcx.pl> script it provides appears on the user's path.
22  
23 The module supports files containing a single Activity or Course. Database files consisting of multiple activities or courses are not supported.
24  
25 The documentation regarding TCX files in general uses the terms history and activity quite interchangeably, including in the user guides such as the one for the Garmin Edge device the author of this module is using. In C<Geo::TCX>, the terms Activity/Activities are used to refer to tracks recorded by a device (consistently with the XML mark-up) and Course/Courses refer to planned tracks meant to be followed during an activity (i.e. the term history is seldomly used).
26  
27 =cut
28  
29 7 ▶use Geo::TCX::Lap;
Subroutine BEGIN: called CC=1 SCAR=2.6
30 use File::Basename;
31 use File::Temp qw/ tempfile /;
32 use IPC::System::Simple qw(system);
33 use Cwd qw(cwd abs_path);
34 use Carp qw(confess croak cluck);
35  
36 =head2 Constructor Methods (class)
37  
38 =over 4
39  
40 =item new( $filename or $str_ref, work_dir => $working_directory )
41  
42 loads and returns a new Geo::TCX instance using the I<$filename> supplied as first argument or a string reference equivalent to the xml tags of a *.tcx file.
43  
44 $o = Geo::TCX->new('2022-08-11-10-27-15.tcx');
45 or
46 $o = Geo::TCX->new( \'...');
47  
48 The optional C<work_dir> (or C<wd> for short) specifies where to save any working files, such as with the save_laps() method. It can be supplied as a relative path or as an absolute path. If C<work_dir> is omitted, it is set based on the path of the I<$filename> supplied or the current working directory if the constructor is called with an XML string reference (see C<< set_wd() >> for more info).
49  
50 =back
51  
52 =cut
53  
54 sub new {
55 my ($proto, $first_arg) = (shift, shift);
56 my %opts = @_;
57 my $o = {};
58 my $class = ref($proto) || $proto;
59 bless($o, $class);
60  
61 my $txt;
62 if (ref( $first_arg ) eq 'SCALAR') {
63 $txt = $$first_arg
64 } else {
65 my $fname = $first_arg;
66 my $file_to_read = $first_arg;
67 croak 'first argument must be a filename' unless -f $fname;
68  
69 if ($fname =~ /(?i:\.fit$)/) {
70 my ($fh, $tmp_fname) = tempfile();
71 _convert_fit_to_tcx( $fname, $tmp_fname );
72 $file_to_read = $tmp_fname;
73 $fname =~ s/(?i:.fit)$/.tcx/
74 }
75 $txt = do { local(@ARGV, $/) = $file_to_read; <> };
76 $o->set_filename($fname)
77 }
78  
79 $txt =~ s,\r,,g; # if it's a windows file
80 $txt =~ s,>\s+<,><,g;
81 $o->{tag_creator} = $1 if $txt =~ s/(<Creator.*<\/Creator>)//;
82  
83 # Activities/Activity - are as recorded by an EDGE 705 device
84 # Courses/Course - are as converted by an EDGE 705 device from an Activity
85  
86 $o->{tag_xml_version} = $1 if $txt =~ /(<.xml version[^>]*>)/;
87 $o->{tag_trainingcenterdatabase} = $1 if $txt =~ /(<TrainingCenterDatabase.*<\/TrainingCenterDatabase>)/;
88 $o->{tag_activities} = $1 if $txt =~ /(<Activities.*<\/Activities>)/;
89 $o->{tag_activity} = $1 if $txt =~ /(<Activity.*<\/Activity>)/;
90 $o->{tag_courses} = $1 if $txt =~ /(<Courses.*<\/Courses>)/;
91 $o->{tag_course} = $1 if $txt =~ /(<Course(?!s).*<\/Course>)/;
92  
93 # Id seems only for Activities/Activity...
94 if ($o->{tag_activity}) {
95 $o->{tag_id} = $1 if $o->{tag_activity} =~ /<Activity.*<Id>(.*)<\/Id>/;
96 $o->{tag_activity_type} = $1 if $o->{tag_activity} =~ /<Activity Sport="([^"]+)"/;
97 }
98  
99 # ... and Name only for Courses/Course
100 if ($o->{tag_course}) {
101 # will pick up device name under Creator if we are not specific about the Course tag
102 $o->{tag_name} = $1 if $o->{tag_course} =~ /<Course.*<Name>(.*)<\/Name>/
103 }
104  
105 $o->{tag_author} = $1 if $txt =~ /(<Author.*<\/Author>)/;
106 $o->_parse_author_tag if $o->{tag_author};
107  
108 my @Lap;
109 if ( $o->{tag_activity} ) {
110 my $i = 0;
111 my $lap;
112 while ( $o->{tag_activity} =~ /(\<Lap StartTime=.*?\>.*?\<\/Lap\>)/g ) {
113 my ($lapstring, $last_point_previous_lap);
114 $lapstring = $1;
115 $last_point_previous_lap = $lap->trackpoint(-1) if $i > 0;
116 $lap = Geo::TCX::Lap->new($lapstring, ++$i, $last_point_previous_lap);
117 push @{ $o->{Laps} }, $lap
118 }
119 }
120 if ( $o->{tag_course} ) {
121 # in Courses, data is structured as <Lap>...</Lap><Lap>...</Lap><Track>...</Track><Track>...</Track>
122 # actually, not sure just seem like it's one long ... track, not multiple ones, which complicates things
123 my $xml_str = $o->{tag_course};
124  
125 my (@lap_tags, @lap_endpoints, @track_tags);
126  
127 if ( $xml_str =~ m,(<Lap>.*</Lap>),s ) {
128 my $str = $1;
129 @lap_tags = split(/(?s)<\/Lap>\s*<Lap>/, $str );
130 if (@lap_tags == 0) { push @lap_tags, $str }
131 }
132  
133 for my $i (0 .. $#lap_tags) {
134 my ($end_pos, $end_pt);
135 if ( $lap_tags[$i] =~ m,<EndPosition>(.*)</EndPosition>,s ) {
136 $end_pt = Geo::TCX::Trackpoint->new( $1 );
137 push @lap_endpoints, $end_pt
138 }
139 # since split removed tags sometimes at ^ of string for other at $
140 # let's remove them all and add back
141 $lap_tags[$i] =~ s,</?Lap>,,g;
142 $lap_tags[$i] =~ s,^,<Lap>,g;
143 $lap_tags[$i] =~ s,$,</Lap>,g
144 }
145 my $track_str;
146 if ( $xml_str =~ m,(<Track>.*</Track>),s ) {
147 $track_str = $1;
148 }
149  
150 my $t = Geo::TCX::Track->new( $track_str );
151 if (@lap_tags ==1) { $track_tags[0] = $track_str }
152 else {
153 my ($t1, $t2);
154 for my $i (0 .. $#lap_tags ) {
155 if ($i < $#lap_tags) {
156 ($t1, $t2) = $t->split_at_point_closest_to( $lap_endpoints[$i] );
157 push @track_tags, $t1->xml_string;
158 $t = $t2
159 } else { push @track_tags, $t->xml_string } # ie don't split the last track portion
160 }
161 }
162  
163 my $lap;
164 for my $i (0 .. $#lap_tags) {
165 my ($lapstring, $last_point_previous_lap);
166 $lapstring = $lap_tags[$i] . $track_tags[$i];
167 $last_point_previous_lap = $lap->trackpoint(-1) if $i > 0;
168 $lap = Geo::TCX::Lap->new($lapstring, ++$i, $last_point_previous_lap);
169 push @{ $o->{Laps} }, $lap
170 }
171 }
172  
173 my $n = $o->laps;
174 die "cannot find any laps, must not be a *.tcx file or str" unless $n;
175 print "\nFound " . $n, ($n > 1 ? " Laps": " Lap"), "\n\n";
176 $o->{_txt} = $txt; # only for debugging
177 $o->set_wd( $opts{work_dir} || $opts{wd} );
178 return $o
179 }
180  
181 =head2 Constructor Methods (object)
182  
183 =over 4
184  
185 =item activity_to_course( key/values )
186  
187 returns a new <Geo::TCX> instance as a course, based on the current activity.
188  
189 All I<key/values> are optional:
190  
191 Z<> C<< lap => I<#> >>: converts lap number I<#> to a course, dropping all other laps. All laps are converted if C<lap> is omitted.
192 Z<> C<< course_name => I<$string> >>: the name for the course. The name will be the lap's C<StartTime> if a value is not specified.
193 Z<> C<< filename => I<$filename> >>: will call C<set_filename()> with this value.
194 Z<> C<< work_dir => I<$work_dir> >>: if omitted, it will be set to the same as that of the current object.
195  
196 =back
197  
198 =cut
199  
200 sub activity_to_course {
201 my $clone = shift->clone;
202 my %opts = @_;
203 croak 'this instance is already a course' if $clone->is_course;
204 my $wd = $opts{work_dir} || $opts{wd} || $clone->set_wd();
205  
206 my (@laps, $course);
207 @laps = $opts{lap} ? ($opts{lap}) : (1 .. $clone->laps);
208  
209 for my $lap_i (@laps) {
210 my $str = $clone->save_laps( [ $lap_i ], nosave => 1, course => 1, course_name => $opts{course_name} );
211 my $course_i = Geo::TCX->new( \$str, work_dir => $wd );
212 if ( defined $course ) {
213 push @{ $course->{Laps} }, $course_i->lap(1)
214 } else { $course = $course_i }
215 }
216 $course->set_filename( $opts{filename} );
217 return $course
218 }
219  
220 =over 4
221  
222 =item clone()
223  
224 Returns a deep copy of a C<Geo::TCX> instance.
225  
226 $clone = $o->clone;
227  
228 =back
229  
230 =cut
231  
232 sub clone {
233 my $clone;
234 eval(Data::Dumper->Dump([ shift ], ['$clone']));
235 confess $@ if $@;
236 return $clone
237 }
238  
239 =head2 Object Methods
240  
241 =over 4
242  
243 =item lap( # )
244  
245 Returns the lap object corresponding to the lap number I<#> specified. I<#> is one-indexed but negative numbers can be used to count from the end, e.g C<-1> to get the last lap.
246  
247 =back
248  
249 =cut
250  
251 sub lap {
252 my ($o, $lap_i, %exists) = (shift, shift);
253 croak 'requires a single integer as argument' if ! $lap_i or @_;
254 $lap_i = $o->_lap_number($lap_i);
255 return $o->{Laps}[$lap_i-1]
256 }
257  
258 =over 4
259  
260 =item laps( qw/ # # ... / )
261  
262 Returns a list of L<Geo::TCX::Lap> objects corresponding to the lap number(s) specified, or all laps if called without arguments. This method is useful as an access for the number of laps (i.e. without arguments in scalar context).
263  
264 =back
265  
266 =cut
267  
268 sub laps {
269 my $o = shift;
270 return @{$o->{Laps}} unless @_;
271 my @numbers = @_;
272 my @laps;
273 for my $lap_i (@numbers) {
274 $lap_i = $o->_lap_number($lap_i);
275 push @laps, $o->{Laps}[$lap_i-1]
276 }
277 return @laps
278 }
279  
280 =over 4
281  
282 =item merge_laps( #1, #2 )
283  
284 Merges lap I<#1> with lap I<#2> and returns true. Both laps must be consecutive laps and the number of laps in the object decreases by one.
285  
286 The C<TotalTimeSeconds> and C<DistanceMeters> aggregates of the merged lap are adjusted. For Activity laps, performance metrics are also adjusted. For Course laps, C<EndPosition> is also adjusted. See L<Geo::TCX::Lap>.
287  
288 =back
289  
290 =cut
291  
292 sub merge_laps {
293 my ($o, $i1, $i2, %exists) = (shift, shift, shift);
294 croak 'merge_laps() requires two integers as argument' if ! $i2 or @_;
295 croak 'can only merge consecutive laps' unless ($i2 - $i1)==1;
296 my $l1 = $o->lap($i1);
297 my $l2 = $o->lap($i2);
298  
299 my $lap = $l1->merge($l2, as_is => 1);
300  
301 splice @{ $o->{Laps}}, $i1 - 1, 2, $lap;
302 return 1
303 }
304  
305 =over 4
306  
307 =item split_lap( #, $trackpoint_no )
308  
309 Splits lap number I<#> at the specified I<$trackpoint_no> into two laps and returns true. The number of laps in the object increases by one.
310  
311 =back
312  
313 =cut
314  
315 sub split_lap {
316 my ($o, $lap_i, $pt_no, %exists) = (shift, shift, shift);
317 croak 'split_lap() requires two integers as argument' if ! $pt_no or @_;
318 $lap_i = $o->_lap_number($lap_i);
319 my ($lap_a, $lap_b) = $o->lap($lap_i)->split($pt_no);
320 splice @{ $o->{Laps}}, $lap_i -1, 1, ( $lap_a, $lap_b );
321 return 1
322 }
323  
324 =over 4
325  
326 =item split_lap_at_point_closest_to(#, $point or $trackpoint or $coord_str )
327  
328 Equivalent to C<split_lap()> but splits the specified lap I<#> at the trackpoint that lies closest to a given L<Geo::Gpx::Point>, L<Geo::TCX::Trackpoint>, or a string that can be interpreted as coordinates by C<< Geo::Gpx::Point->flex_coordinates >>. Returns true.
329  
330 =back
331  
332 =cut
333  
334 sub split_lap_at_point_closest_to {
335 my ($o, $lap_i, $to_pt) = (shift, shift, shift);
336 croak 'split_lap_at_point_closest_to() expects two arguments' if @_;
337 $lap_i = $o->_lap_number($lap_i);
338 $to_pt = Geo::Gpx::Point->flex_coordinates( \$to_pt ) unless ref $to_pt;
339 my ($closest_pt, $min_dist, $pt_no) = $o->lap($lap_i)->point_closest_to( $to_pt );
340 # here we can print some info about the original track and where it will be split
341 $o->split_lap( $lap_i, $pt_no );
342 return 1
343 }
344  
345 =over 4
346  
347 =item time_add( @duration )
348  
349 =item time_subtract( @duration )
350  
351 Perform L<DateTime> math on the timestamps of each trackpoint in the track by adding the specified time as per the syntax of L<DateTime>'s C<add()> and C<subtract()> methods. Returns true.
352  
353 Perform L<Date::Time> math on the timestamps of each lap's starttime and trackpoint by adding the specified time as per the syntax of L<Date::Time>'s C<add()> method. Returns true.
354  
355 =back
356  
357 =cut
358  
359 sub time_add {
360 my $o = shift;
361 my @duration = @_;
362 my @laps = @{$o->{Laps}};
363 for my $l (@laps) {
364 $l->time_add( @duration )
365 }
366 return 1
367 }
368  
369 sub time_subtract {
370 my $o = shift;
371 my @duration = @_;
372 my @laps = @{$o->{Laps}};
373 for my $l (@laps) {
374 $l->time_subtract( @duration )
375 }
376 return 1
377 }
378  
379 =over 4
380  
381 =item delete_lap( # )
382  
383 =item keep_lap( # )
384  
385 delete or keep the specified lap I<#> form the object. Returns the list of laps removed in both cases.
386  
387 =back
388  
389 =cut
390  
391 sub delete_lap {
392 my ($o, $lap_i) = (shift, shift);
393 croak 'requires a single integer as argument' unless $lap_i;
394 $lap_i = $o->_lap_number( $lap_i );
395 my @removed = splice @{ $o->{Laps}}, $lap_i - 1, 1;
396 return @removed
397 }
398  
399 sub keep_lap {
400 my ($o, $lap_i) = (shift, shift);
401 my @keep = $o->delete_lap($lap_i);
402 my @removed = @{ $o->{Laps}};
403 @{ $o->{Laps}} = @keep;
404 return @removed
405 }
406  
407 =over 4
408  
409 =item save_laps( \@laplist , key/values )
410  
411 saves each lap as a separate *.tcx file in the working directory as per <set_wd()>. The filenames will consist of the original source file's name, suffixed by the respective lap number.
412  
413 An array reference can be provided to save only a a subset of lap numbers.
414  
415 I<key/values> are:
416  
417 Z<> C<course>: converts activity lap(s) as course files if true.
418 Z<> C<< course_name => $string >>: is only relevant with C<course> and will set the name of the course to I<$string>.
419 Z<> C<force>: overwrites existing files if true, otherwise it won't.
420 Z<> C<indent>: adds white space and indents the xml mark-up in the saved file if true.
421 Z<> C<nosave>: no files are actually saved if true. Useful if only interested in the xml string of the last lap processed.
422  
423 C<course_name> will be ignored if there is more than one lap and the lap's C<StartTime> will be used instead. This is to avoid having multiple files with the same name given that devices use this tag when listing available courses. Acttvity files have an C<Id> tag instead of C<Name> and the laps's C<StartTime> is used at all times. It is easy to edit any of these tags manually in a text editor; just look for the C<< <Name>...</Name> >> tag or C<< <Id>...</Id> >> tags near the top of the files.
424  
425 Returns a string containing the xml of the last lap processed which can subsequently be passed directly to C<< Geo::TCX->new() >> to construct a new instsance.
426  
427 =back
428  
429 =cut
430  
431 sub save_laps {
432 my $o = shift;
433 my @laps_to_save;
434 if (ref ($_[0]) eq 'ARRAY') {
435 my $aref = shift;
436 for my $lap_i (@$aref) {
437 push @laps_to_save, $o->lap($lap_i)
438 }
439 } else { @laps_to_save = @{$o->{Laps}} }
440 my %opts = @_;
441  
442 my ($as_course, $fname);
443 $as_course = 1 if $o->is_course or $opts{course};
444 $fname = $o->set_filename;
445 croak 'no filename found, set_filename(<name>) before saving' unless $fname;
446  
447 # as mentioned in the pod, files will be saved in work_dir as they are new files
448 # use has expectation that that's where working files go
449 my ($name, $path, $ext) = fileparse( $fname, '\..*' );
450 my $wd = $o->set_wd();
451  
452 my ($tags_before_lap, $tags_after_lap) = $o->_prep_tags( %opts );
453  
454 # Id (for Activity) or Name (for Course) tag
455 my $tag_id_or_name = '';
456 if ($as_course) {
457 # a bit tricky to determine Name when saving as course, bear with us here
458 if (@laps_to_save == 1 ) {
459 my $name;
460 if ( defined $opts{course_name} ) { $name = $opts{course_name} }
461 else {
462 if ($o->is_course) { $name = $o->{tag_name} }
463 else { $name = 'StartTimePlaceHolder' }
464 }
465 $tag_id_or_name .= '<Name>' . $name . '</Name>'
466 } else {
467 $tag_id_or_name .= '<Name>' . 'StartTimePlaceHolder' . '</Name>';
468 # i.e. it's StartTime regardless if more than one lap
469 }
470 } else { $tag_id_or_name .= '<Id>' . 'StartTimePlaceHolder' . '</Id>' }
471  
472 # Now from what is left below, we can create a save() method to save a file with multilaps. Simply move the $tags_before_lap and $tags_after_lap outside of the loop, continue to add to the $str (i.e. it gets appended to at all times) and we put the saving block outside of the loop at the very end.
473 # Yah ! And don't distance_net
474  
475 my $str;
476 for my $i (0 .. $#laps_to_save) {
477 my $l = $laps_to_save[$i]->clone;
478 $l->distance_net;
479  
480 $str = $tags_before_lap;
481 $str .= $tag_id_or_name;
482 $str =~ s/StartTimePlaceHolder/$l->StartTime/e;
483  
484 my $xml_lap = $l->xml_string( course => $as_course, indent => $opts{indent} );
485 $str .= $xml_lap;
486 $str .= $tags_after_lap;
487  
488 unless ($opts{nosave}) {
489 my $fname_lap = $wd . $name . '-Lap-' . ($i+1) . $ext;
490 croak "$fname_lap already exists" if -f $fname_lap and !$opts{force};
491 open(my $fh, '>', $fname_lap) or die "can't open $fname_lap $!";
492 print $fh $str
493 }
494 }
495 return $str
496 }
497  
498 =over 4
499  
500 =item save( key/values )
501  
502 saves the current instance.
503  
504 I<key/values> are:
505  
506 Z<> C<filename>: the name of the file to be saved. Has the effect calling C<set_filename()> and changes the name of the file in the current instance (e.g. akin to "save as" in many applications).
507 Z<> C<force>: overwrites existing files if true, otherwise it won't.
508 Z<> C<indent>: adds white space and indents the xml mark-up in the saved file if true.
509  
510 Returns a string containing the xml representation of the file.
511  
512 =back
513  
514 =cut
515  
516 sub save {
517 my ($o, %opts) = @_;
518  
519 my $fname;
520 if ( $opts{filename} ) { $fname = $o->set_filename( $opts{filename} ) }
521 else { $fname = $o->set_filename() }
522 croak 'no filename found, provide one with set_filename() or use key \'filename\'' unless $fname;
523 croak "$fname already exists" if -f $fname and !$opts{force};
524  
525 my ($tags_before_lap, $tags_after_lap) = $o->_prep_tags( indent => $opts{indent} );
526 my $str = $tags_before_lap;
527 if ($o->is_course) { $str .= '<Name>' . $o->{tag_name} . '</Name>' }
528 else { $str .= '<Id>' . $o->{tag_id} . '</Id>' }
529  
530 my ($str_activity_laps, $str_course_laps, $str_course_tracks);
531 for my $lap ($o->laps) {
532 my $str_lap = $lap->xml_string( indent => $opts{indent} );
533  
534 if ($lap->is_course) {
535 # for courses, the xml track tags are not nested within the lap
536 # tags but follow them instead. Yah, weird. So need to collect
537 # the strings seperately then assemble after the loop
538 if ( $str_lap =~ s,\s*(<Lap>.*</Lap>)\s*(<Track>.*</Track>)\s*,,s ) {
539 $str_course_laps .= $1;
540 $str_course_tracks .= $2
541 } else { croak "cannot find lap or track tags in Laps object" }
542 } else {
543 $str_activity_laps .= $str_lap
544 }
545 }
546  
547 # Flatten the course tracks into a a single track
548 $str_course_tracks =~ s,</Track>\s*<Track>,,gs if $str_course_tracks;
549  
550 if ($o->is_course) { $str .= $str_course_laps . $str_course_tracks }
551 else { $str .= $str_activity_laps }
552  
553 $str .= $tags_after_lap;
554  
555 open(my $fh, '>', $fname) or die "can't open $fname $!";
556 print $fh $str;
557 return $str
558 }
559  
560 sub _prep_tags {
561 my ($o, %opts) = @_;
562  
563 # identical to save_laps()
564 my ($newline, $tab, $as_course);
565 $newline = $opts{indent} ? "\n" : '';
566 $tab = $opts{indent} ? ' ' : '';
567 $as_course = 1 if $o->is_course or $opts{course};
568  
569 #
570 # Prepare the mark-up that appears *outside* the laps (therefore will be common to all saved laps)
571  
572 # These tag collection blocks could be shortened but it might not become more legible.
573 # The many variables help for debugging as the resulting string can be an extremely
574 # long flat string
575  
576 # we first collect the tags we need, we assemble them later
577 # we need these 3 pairs of tags so declare in a block
578 my ($tag_open_trainctrdb, $tag_close_trainctrdb);
579 my ($tag_open_activity_or_course_plural, $tag_close_activity_or_course_plural );
580 my ($tag_open_activity_or_course_singular, $tag_close_activity_or_course_singular);
581  
582 if ($o->{tag_trainingcenterdatabase} =~ /(<TrainingCenterDatabase[^>]*>)/) {
583 $tag_open_trainctrdb = $1;
584 $tag_close_trainctrdb = '</TrainingCenterDatabase>'
585 } else { croak 'can\'t find the expected <TrainingCenterDatabase ...> tag' }
586  
587 # in history files (in mine at least), these tags ever appear only once, nesting all of the data within them
588 # <Activities><Activity Sport="Biking">
589 # <Courses><Course>
590 # Activity is nested within Activities and similarly Course is nested within Courses
591  
592 if ($o->{tag_activities}) {
593 if ( $o->{tag_activities} =~ /(<Activities[^>]*>)/) {
594 $tag_open_activity_or_course_plural = $1
595 } else { croak 'can\'t find the expected <Activities> tag' }
596 if ( $o->{tag_activity} =~ /(<Activity[^>]*>)/ ) {
597 $tag_open_activity_or_course_singular = $1
598 } else { croak 'can\'t find the expected <Activity Sport="..."> tag' }
599 ($tag_close_activity_or_course_singular, $tag_close_activity_or_course_plural) = ('</Activity>', '</Activities>')
600 }
601 if ($o->{tag_courses}) {
602 if ( $o->{tag_courses} =~ /(<Courses[^>]*>)/) {
603 $tag_open_activity_or_course_plural = $1
604 } else { croak 'can\'t find the expected <Courses> tag' }
605 if ( $o->{tag_course} =~ /(<Course(?!s)[^>]*>)/ ) {
606 $tag_open_activity_or_course_singular = $1
607 } else { croak 'can\'t find the expected <Course> tag' }
608 ($tag_close_activity_or_course_singular, $tag_close_activity_or_course_plural) = ('</Course>', '</Courses>')
609 }
610 if ($as_course and !$o->{tag_courses}) { # i.e. when saving an activity as a course
611 $tag_open_activity_or_course_plural = '<Courses>';
612 $tag_open_activity_or_course_singular = '<Course>';
613 ($tag_close_activity_or_course_singular, $tag_close_activity_or_course_plural) = ('</Course>', '</Courses>')
614 }
615  
616 # assembling the tags to get the mark-up that appears *before* the laps
617 my $tags_before_lap = '';
618 $tags_before_lap = $o->{tag_xml_version} . "\n";
619 $tags_before_lap .= $tag_open_trainctrdb;
620 $tags_before_lap .= $newline . $tab . $tag_open_activity_or_course_plural;
621 $tags_before_lap .= $newline . ($tab x 2) . $tag_open_activity_or_course_singular;
622 $tags_before_lap .= $newline . ($tab x 3);
623  
624 # assembling the tags to get the mark-up that appears *after* the laps
625 my ($tags_after_lap) = '';
626 $tags_after_lap = $newline . ($tab x 3) . $o->{tag_creator} if $o->{tag_creator};
627 $tags_after_lap .= $newline . ($tab x 2) . $tag_close_activity_or_course_singular;
628 $tags_after_lap .= $newline . $tab . $tag_close_activity_or_course_plural;
629 $tags_after_lap .= $newline . $tab . $o->{tag_author} if $o->{tag_author};
630 $tags_after_lap .= $newline . $tag_close_trainctrdb;
631  
632 return $tags_before_lap, $tags_after_lap
633 }
634  
635 =over 4
636  
637 =item set_filename( $filename )
638  
639 Sets/gets the filename. Returns the name of the file with the complete path.
640  
641 If the instance was created from a FIT file, the filename is set to the same name but with a C<.tcx> extension by default.
642  
643 =back
644  
645 =cut
646  
647 sub set_filename {
648 my ($o, $fname) = (shift, shift);
649 return $o->{_fileABSOLUTENAME} unless $fname;
650 croak 'set_filename() takes only a single name as argument' if @_;
651 my $wd;
652 if ($o->_is_wd_defined) { $wd = $o->set_wd }
653 # set_filename gets called before set_wd by new() so can't access work_dir until initialized
654  
655 my ($name, $path, $ext);
656 ($name, $path, $ext) = fileparse( $fname, '\..*' );
657 if ($wd) {
658 if ( ! ($fname =~ /^\// ) ) {
659 # ie if fname is not an abolsute path, adjust $path to be relative to work_dir
660 ($name, $path, $ext) = fileparse( $wd . $fname, '\..*' )
661 }
662 }
663 $o->{_fileABSOLUTEPATH} = abs_path( $path ) . '/';
664 $o->{_fileABSOLUTENAME} = $o->{_fileABSOLUTEPATH} . $name . $ext;
665 croak 'directory ' . $o->{_fileABSOLUTEPATH} . ' doesn\'t exist' unless -d $o->{_fileABSOLUTEPATH};
666 $o->{_fileNAME} = $name;
667 $o->{_filePATH} = $path;
668 $o->{_fileEXT} = $ext;
669 $o->{_filePARSEDNAME} = $fname;
670 # _file* keys only for debugging, should not be used anywhere else
671 return $o->{_fileABSOLUTENAME}
672 }
673  
674 =over 4
675  
676 =item set_wd( $folder )
677  
678 Sets/gets the working directory for any eventual saving of the *.tcx file and checks the validity of that path. It can be set as a relative path (i.e. relative to the actual L<Cwd>) or as an absolute path, but is always returned as a full path.
679  
680 This working directory is always defined. The previous one is also stored in memory, such that C<set_wd('-')> switches back and forth between two directories. The module never actually C<chdir>'s, it just keeps track of where the user wishes to save files.
681  
682 =back
683  
684 =cut
685  
686 sub set_wd {
687 my ($o, $dir) = (shift, shift);
688 croak 'set_wd() takes only a single folder as argument' if @_;
689 my $first_call = ! $o->_is_wd_defined; # ie if called for 1st time -- at construction by new()
690  
691 if (! $dir) {
692 return $o->{work_dir} unless $first_call;
693 my $fname = $o->set_filename;
694 if ($fname) {
695 my ($name, $path, $ext) = fileparse( $fname );
696 $o->set_wd( $path )
697 } else { $o->set_wd( cwd ) }
698 } else {
699 $dir =~ s/^\s+|\s+$//g; # some clean-up
700 $dir =~ s/~/$ENV{'HOME'}/ if $dir =~ /^~/;
701 $dir = $o->_set_wd_old if $dir eq '-';
702  
703 if ($dir =~ m,^[^/], ) { # convert rel path to full
704 $dir = $first_call ? cwd . '/' . $dir : $o->{work_dir} . $dir
705 }
706 $dir =~ s,/*$,/,; # some more cleaning
707 1 while ( $dir =~ s,/\./,/, ); # support '.'
708 1 while ( $dir =~ s,[^/]+/\.\./,, ); # and '..'
709 croak "$dir not a valid directory" unless -d $dir;
710  
711 if ($first_call) { $o->_set_wd_old( $dir ) }
712 else { $o->_set_wd_old( $o->{work_dir} ) }
713 $o->{work_dir} = $dir
714 }
715 return $o->{work_dir}
716 }
717  
718 # if ($o->set_filename) { $o->set_wd() } # if we have a filename
719 # else { $o->set_wd( cwd ) } # if we don't
720  
721 sub _set_wd_old {
722 my ($o, $dir) = @_;
723 $o->{work_dir_old} = $dir if $dir;
724 return $o->{work_dir_old}
725 }
726  
727 sub _is_wd_defined { return defined shift->{work_dir} }
728  
729 =over 4
730  
731 =item is_activity()
732  
733 =item is_course()
734  
735 True if the C<Geo::TCX> instance is a of the type indicated by the method, false otherwise.
736  
737 =back
738  
739 =cut
740  
741 sub is_activity { return defined shift->{tag_activity} }
742 sub is_course { return defined shift->{tag_course} }
743  
744 =over 4
745  
746 =item activity( $string )
747  
748 Gets/sets the Activity type as detected from C<\<Activity Sport="*"\>>, sets it to I<$string> if provided. Garmin devices (at least the Edge) record activities as being of types 'Running', 'Biking', 'MultiSport', etc.
749  
750 =back
751  
752 =cut
753  
754 sub activity {
755 my ($o, $activity) = @_;
756 # should I check what activity types are allowed? Must they be single words?
757 if ($activity) {
758 $o->{tag_activity} =~ s,(\<Activity Sport=)"[^"]*",$1"$activity",;
759 return $o->activity
760 }
761 $activity = $1 if $o->{tag_activity} =~ /<Activity Sport="([^"]*)"/;
762 return $activity
763 }
764  
765 =over 4
766  
767 =item author( key/value )
768  
769 Gets/sets the fields of the Author tag. Supported keys are C<Name>, C<LangID>, C<PartNumber> and all excpect a string as value.
770  
771 The C<Build> field can also be accesses but the intent is to set it, the string supplied should be in the form of an xml string in the way this tag appears in a *.tcx file (e.g. Version, VersionMajor, VersionMinor, Type, …). Simply access that key of the returned hash ref to see what is should look like.
772  
773 Returns a hash reference of key/value pairs.
774  
775 This method is under development and behaviour could change in the future.
776  
777 =back
778  
779 =cut
780  
781 # the only purpose I have for this at this stage is to set the name mostly, we'll see if I have a need for other, but will take the Build key as is and not set up sub-keys yet.
782 # I mostly want to use this method so that I can set it if I want for any *.tcx I generate (Courses, save laps), with the version number of my module as well
783 # the Build entries contain integers but I am not supporting this at this point
784 #
785 # <Author xsi:type="Application_t"
786 # <Name>string</Name>
787 # <Build containis
788 # <Version>
789 # VersionMajor
790 # VersionMinor
791 # BuildMajor
792 # BuildMinor
793 # </Version>
794 # <Type>Release<Type>
795 # </Build>
796 # <LangID>EN</LangID>
797 # <PartNumber>digit and dash string</PartNumber>
798 # </Author>
799  
800 my %possible_author_keys;
801 my @author_keys = qw/ Name Build LangID PartNumber /;
802 $possible_author_keys{$_} = 1 for @author_keys;
803  
804 # NB: similar to the _file* keys, the _Author href can not exist at any time
805 sub _parse_author_tag {
806 my $o = shift;
807 $o->{_Author} = {};
808 my $href = $o->{_Author};
809 my $author_xml;
810 if ( $o->{tag_author} =~ m,<Author\s+([^=]+="[^"]+")>(.*)<\/Author>, ) {
811 $href->{string_inside_author_tag} = $1;
812 $author_xml = $2
813 }
814 for my $key (@author_keys) {
815 $href->{$key} = $1 if $author_xml =~ m,<$key>(.+)</$key>,
816 }
817 return $o->{tag_author}
818 }
819  
820 sub _update_author_tag {
821 my $o = shift;
822 my $href = $o->{_Author};
823  
824 my $str = '<Author ' . $href->{string_inside_author_tag} . '>';
825 for my $key (@author_keys) {
826 $str .= "<$key>" . $href->{$key} . "</$key>" if defined $href->{key}
827 }
828 $str .= '</Author>';
829 $o->{tag_author} = $str;
830 return $o->{tag_author}
831 }
832  
833 sub author {
834 my ($o, %keys_values) = @_;
835 my $href = $o->{_Author};
836 croak 'no author tag found in object' if (!%keys_values and ! $href);
837 if (%keys_values) {
838 for my $key (keys %keys_values) {
839 croak 'unsupported Author field' unless $possible_author_keys{$key};
840 $href->{$key} = $keys_values{$key}
841 }
842 $o->_update_author_tag
843 }
844 return $href
845 }
846  
847 # returns the actual lap number if a negative index is passed to count from the end
848 sub _lap_number {
849 my ($o, $lap_i, $n, %exists) = (shift, shift);
850 $n = $o->laps;
851 $lap_i += $n + 1 if $lap_i < 0;
852 $exists{$_} = 1 for (1 .. $n);
853 croak "Lap $lap_i does not exist" unless $exists{$lap_i};
854 return $lap_i
855 }
856  
857 our $FitConvertPl;
858 sub _convert_fit_to_tcx {
859 require Geo::FIT;
860 my ( $fname, $tmp_fname ) = @_;
861 if (!defined $FitConvertPl) {
862 for (split /:/, $ENV{PATH} ) {
863 $FitConvertPl = $_ . '/fit2tcx.pl';
864 last if -f $FitConvertPl
865 }
866 }
867 my @args = ($fname, $tmp_fname);
868 system($^X, $FitConvertPl, @args);
869 return 1
870 }
871  
872 =head1 EXAMPLES
873  
874 Coming soon.
875  
876 =head1 BUGS
877  
878 Nothing to report yet.
879  
880 =head1 AUTHOR
881  
882 Patrick Joly
883  
884 =head1 VERSION
885  
886 1.04
887  
888 =head1 LICENSE AND COPYRIGHT
889  
890 Copyright (c) 2022, Patrick Joly C<< <patjol@cpan.org> >>. All rights reserved.
891  
892 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>.
893  
894 =head1 SEE ALSO
895  
896 L<Geo::Gpx>, L<Geo::FIT>.
897  
898 =head1 DISCLAIMER OF WARRANTY
899  
900 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
901  
902 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
903  
904 =cut
905  
906 1;
907  
« blib/script/tcx_interactive.pl ↑ Summary blib/lib/Geo/TCX/Track.pm »