script/locations2gpx.pl

filter: Statementstmt 92.9% 146 / 157 Branchbran 61.9% 57 / 92 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod n/a totaltotal 80.1% 218 / 272 SCAR 37.8
CC
35
Cov
81%
_print_wpt
21.5
_message
12.1
_write_meta
7.1
CRAP 43.7
« script/fitdump.pl ↑ Summary Build »
1 #!/usr/bin/env perl
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '1.09';
6  
7 =encoding utf-8
8  
9 =head1 NAME
10  
11 locations2gpx.pl - script to convert a FIT locations file to gpx format
12  
13 =head1 SYNOPSIS
14  
15 locations2gpx.pl --help
16 locations2gpx.pl [ --outfile=$filename --force --indent=# --time_offset=# ] $fit_locations_file
17  
18 =head1 DESCRIPTION
19  
20 C<locations2gpx.pl> reads the contents of a FIT file containing the locations saved on a GPS device and converts it to a GPX file as waypoints.
21  
22 The converted file will have the same basename as the I<$fit_locations_file> but with a I<*.gpx> extension. The name of the converted file may be overriden with with option C<--outfile>.
23  
24 =cut
25  
26 2 ▶use Geo::FIT;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use HTML::Entities qw( encode_entities encode_entities_numeric );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 my ($force, $time_offset, $indent_n, $indent, $outfile, $help) = (0, 0, 2);
31 0 ▶sub usage { "Usage: $0 [ --help --outfile=<fname> --force --indent=# --time_offset=# ] fit_locations_file\n" }
Subroutine usage: not called CC=1 SCAR=6.9
32  
33 2 ▶GetOptions( "outfile=s" => \$outfile,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("outfile=s", \$outfile, "indent=i", \$indent_n, "force", \$force, "time_offset=i", \$time_offset, "help", \$help) or die usage() 0 2 2
34 "indent=i" => \$indent_n,
35 "force" => \$force,
36 "time_offset=i" => \$time_offset,
37 "help" => \$help,
38 ) or die usage();
39  
40 2 ▶die usage() if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 2 2
41 2 $indent = " " x $indent_n;
42  
43 2 my ($file, $gpx_file);
44 2 for (@ARGV) {
45 2 ▶ $file = $_ if /(?i:\.fit)$/;
Branch: 1 branch
BranchTrueFalseTotal
if /(?i:\.fit)$/ 2 0 2
46 2 ▶ die "No FIT input file provided" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 2 2
47 2 ▶ die "No file named $file found" unless -f $file
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 2 2
48 }
49  
50 2 ▶if (defined $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outfile) { } 1 1 2
51 1 $gpx_file = $outfile
52 } else {
53 1 ($gpx_file = $file) =~ s/(?i:.fit)$/.gpx/i
54 }
55 2 ▶die "File $gpx_file already exists\n" if -f $gpx_file and !$force;
Branch: 1 branch
BranchTrueFalseTotal
if -f $gpx_file and not $force 0 2 2
Condition66%2/3
-f $gpx_file and not $force
!ll&&!rl&&r
110
MC/DC0%0/2
-f $gpx_file and not $force
-f $gpx_file not $force
Truth table66%2/3
A and not(B)
A= -f $gpx_fileB= not $force
ABresult
0X0
100
111
56  
57 =head2 Options
58  
59 =over 4
60  
61 =item C<< --outfile=I<$filename> >>
62  
63 specifies the name of the converted file instead of the default to simply change the extension to I<*.gpx>.
64  
65 =item C<< --force >>
66  
67 overwrites an existing file if true.
68  
69 =item C<< --indent=# >>
70  
71 specifies the number of spaces to use for indentation of the XML in the GPX file. The default is 2.
72  
73 =item C<< --time_offset=I<#> >>
74  
75 adds a time offset to the timestamp of waypoints, where I<#> refers to the number of seconds by which to offset by.
76  
77 =back
78  
79 =cut
80  
81 2 my ($fit, @id, @locations);
82  
83 2 $fit = Geo::FIT->new();
84 2 $fit->file($file);
85  
86 2 $fit->use_gmtime(1);
87 2 $fit->numeric_date_time(1);
88 2 $fit->semicircles_to_degree(1);
89 2 $fit->mps_to_kph(1);
90 2 $fit->without_unit(1);
91 2 $fit->maybe_chained(0);
92 2 $fit->drop_developer_data(1);
93 $fit->data_message_callback_by_name('',
94 sub {
95 10 ▶ my ($msg, $name) = _message(@_);
Subroutine __ANON__: called CC=4 SCAR=13.9
96 10 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 10 0 10
97 10 ▶ push @id, $msg if $name eq "file_id";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "file_id" 2 8 10
98 10 ▶ push @locations, $msg if $name eq "location"
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "location" 6 4 10
99 }
100 10 return 1
101 }
102 2 );
103 2 ▶$fit->open() or die $fit->error();
Branch: 1 branch
BranchTrueFalseTotal
$fit->open or die $fit->error 0 2 2
104  
105 2 my ($f_size) = $fit->fetch_header();
106 2 ▶unless (defined $f_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $f_size) 0 2 2
107 0 ▶ $fit->error("can't read FIT header") unless defined $fit->error();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fit->error 0 0 0
108 0 die $fit->error()
109 }
110 2 ▶1 while $fit->fetch();
Branch: 1 branch
BranchTrueFalseTotal
$fit->fetch and do { '???' } 18 2 20
111  
112 2 my (%device, $oem, $prod, $desc);
113 2 %device = %{$id[0]};
114 2 ▶$oem = defined $device{manufacturer} ? $device{manufacturer} : "Unknown OEM";
Branch: 1 branch
BranchTrueFalseTotal
defined $device{'manufacturer'} ? : 2 0 2
115 2 ▶$prod = defined $device{garmin_product} ? $device{garmin_product} : "unknown";
Branch: 1 branch
BranchTrueFalseTotal
defined $device{'garmin_product'} ? : 2 0 2
116 2 $desc = ucfirst "recorded on a $oem $prod device";
117  
118 2 ▶if (@locations) {
Branch: 1 branch
BranchTrueFalseTotal
if (@locations) 2 0 2
119 2 ▶ open (my $fh, ">", $gpx_file) or die "cannot open $gpx_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $gpx_file or die "cannot open ${gpx_file}: $!" 0 2 2
120 2 select $fh;
121 2 printf "<?xml %s?>\n", 'version="1.0" encoding="UTF-8"';
122 2 _write_header();
123 2 _write_meta();
124 2 _write_waypoints();
125 2 printf "</gpx>\n";
126 2 select STDOUT
127 }
128  
129 sub _message {
130 10 ▶ my ($fit, $desc, $v) = @_;
Subroutine _message: called CC=12 SCAR=24.9
131  
132 10 my $m_name = $desc->{message_name};
133 10 ▶ return undef unless $m_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $m_name 0 10 10
134  
135 10 my $msg = {};
136  
137 10 for my $i_name (keys %$desc) {
138 792 ▶ next if $i_name !~ /^i_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $i_name =~ /^i_/ 718 74 792
139 74 my $name = $i_name;
140 74 $name =~ s/^i_//;
141  
142 74 my $attr = $desc->{'a_' . $name};
143 74 my $tname = $desc->{'t_' . $name};
144 74 my $pname = $name;
145  
146 74 ▶ if (ref $attr->{switch} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr->{"switch"} eq "HASH") 2 72 74
147 2 my $t_attr = $fit->switched($desc, $v, $attr->{switch});
148  
149 2 ▶ if (ref $t_attr eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t_attr eq "HASH") 2 0 2
150 2 $attr = $t_attr;
151 2 $tname = $attr->{type_name};
152 $pname = $attr->{name}
153 2 }
154 }
155  
156 74 my $i = $desc->{$i_name};
157 74 my $c = $desc->{'c_' . $name};
158 74 my $type = $desc->{'T_' . $name};
159 74 my $invalid = $desc->{'I_' . $name};
160 74 my $j;
161  
162 74 my $len = @$v;
163 74 ▶ for ($j = 0 ; $j < $c ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $c) 406 22 428
164 406 my $ij = $i + $j;
165 406 ▶ $ij >= $len && next;
Branch: 1 branch
BranchTrueFalseTotal
$ij >= $len and next 0 406 406
166 406 ▶ Geo::FIT->isnan($v->[$ij]) && next;
Branch: 1 branch
BranchTrueFalseTotal
"Geo::FIT"->isnan($v->[$ij]) and next 0 406 406
167 406 ▶ $v->[$ij] != $invalid && last
Branch: 1 branch
BranchTrueFalseTotal
$v->[$ij] != $invalid and last 52 354 406
168 }
169 74 ▶ if ($j < $c) { # skip invalid
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $c) 52 22 74
170 52 ▶ if ($type == FIT_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 7) { } 6 46 52
171 6 $msg->{$pname} = Geo::FIT::_string_value($v, $i, $c)
172 }
173 else {
174 # return only the first value if array
175 46 $msg->{$pname} = $fit->value_cooked($tname, $attr, $invalid, $v->[$i])
176 }
177 }
178 }
179 10 return ($msg, $m_name)
180 }
181  
182 sub _enc {
183 10 ▶ return encode_entities_numeric( $_[0] );
Subroutine _enc: called CC=1 SCAR=0.0
184 }
185  
186 # https://www.topografix.com/GPX/1/1/#element_gpx
187 sub _write_header {
188 2 ▶ printf "<gpx %s", 'version="1.1" creator="locations2gpx.pl"';
Subroutine _write_header: called CC=1 SCAR=0.0
189 2 my $loc = 'xsi:schemaLocation="';
190 2 $loc = $loc . "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd";
191 2 $loc = $loc . " http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd";
192 2 $loc = $loc . " http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www8.garmin.com/xmlschemas/TrackPointExtensionv1.xsd";
193 2 $loc = $loc . " http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd";
194 2 $loc = $loc . " http://www.cluetrust.com/XML/GPXDATA/1/0 http://www.cluetrust.com/Schemas/gpxdata10.xsd";
195 2 $loc = $loc . '"';
196 2 printf " %s", $loc;
197 2 printf " %s", 'xmlns="http://www.topografix.com/GPX/1/1"';
198 2 printf " %s", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
199 # garmin_ext
200 2 printf " %s", 'xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"';
201 2 printf " %s", 'xmlns:gpxtrx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"';
202 2 printf " %s", 'xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"';
203 2 printf " %s", 'xmlns:gpxwpx="http://www.garmin.com/xmlschemas/WaypointExtension/v1"';
204 2 printf ">\n";
205 }
206  
207 # https://www.topografix.com/GPX/1/1/#type_metadataType
208 sub _write_meta {
209 2 ▶ my $name; # none for now but declaring so it can be easily assigned
Subroutine _write_meta: called CC=5 SCAR=19.5
210 2 printf "%s<metadata>\n", $indent;
211 2 ▶ printf "%s<name>%s</name>\n", $indent x 2, _enc( $name ) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 2 2
212 2 printf "%s<desc>%s</desc>\n", $indent x 2, _enc( $desc );
213  
214 2 my $author_name = 'Converted from Locations.fit by locations2gpx.pl';
215 2 printf "%s<author>\n", $indent x 2;
216 2 printf "%s<name>%s</name>\n", $indent x 3, _enc( $author_name );
217 2 printf "%s</author>\n", $indent x 2;
218  
219 2 my ($url, $text, $type); # none for now but declaring so they can be easily assigned
220 2 ▶ if (defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $url) 0 2 2
221 0 printf "%s<link href=\"%s\">\n", $indent x 2, _enc( $url ) ;
222 0 ▶ printf "%s<text>%s</text>\n", $indent x 3, _enc( $text ) if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 0 0 0
223 0 ▶ printf "%s<type>%s</type>\n", $indent x 3, $type if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 0 0 0
224 0 printf "%s</link>\n", $indent x 2;
225 }
226 2 printf "%s</metadata>\n", $indent
227 }
228  
229 sub _write_waypoints {
230 2 ▶ if (@locations) {
Subroutine _write_waypoints: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if (@locations) 2 0 2
231 2 my $ai = 0; # alt index
232 2 for (@locations) {
233 6 _print_wpt(\%$_, $ai);
234 6 $ai++
235 }
236 }
237 }
238  
239 sub _print_wpt {
240 6 ▶ my ($m, $alt_index) = @_; # alt_index is not used
Subroutine _print_wpt: called CC=15 SCAR=30.7
241  
242 # https://www.topografix.com/GPX/1/1/#type_wptType
243  
244 6 my ($time, $lon, $lat, $ele, $name, $desc);
245 6 ▶ while (my ($key, $val) = each %{$m}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$m;}) 42 6 48
246 # print "\$key is: $key, \$val is $val \n";
247 42 ▶ if ($key eq "timestamp") { $time = $val + $time_offset } # + $timeoffs;
Branch: 7 branches
BranchTrueFalseTotal
if ($key eq 'timestamp') { } 6 36 42
elsif ($key eq 'position_long') { } 6 30 36
elsif ($key eq 'position_lat') { } 6 24 30
elsif ($key eq 'altitude' and not defined $ele) { } 0 24 24
elsif ($key eq 'enhanced_altitude') { } 0 24 24
elsif ($key eq 'name') { } 6 18 24
elsif ($key eq 'unknown6') { } 0 18 18
248 6 elsif ($key eq "position_long") { $lon = $val }
249 6 elsif ($key eq "position_lat") { $lat = $val }
250 0 ▶ elsif ($key eq "altitude" and !defined $ele) { $ele = $val }
Condition33%1/3
$key eq "altitude" and not defined $ele
!ll&&!rl&&r
2400
MC/DC0%0/2
$key eq "altitude" and not defined $ele
$key eq "altitude" not defined $ele
Truth table33%1/3
A and not(B)
A= $key eq "altitude"B= not defined $ele
ABresult
0X0
100
111
251 elsif ($key eq "enhanced_altitude") {
252 0 ▶ if (defined $val) { $ele = $val } # only if valid
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
253 }
254 6 elsif ($key eq "name") { $name = $val }
255 0 elsif ($key eq "unknown6") { $desc = $val }
256 }
257 # TODO: dump other unknown\d keys to STDERR
258  
259 6 printf "%s<wpt lat=\"%s\" lon=\"%s\">\n", $indent, $lat, $lon;
260 6 ▶ printf "%s<ele>%s</ele>\n", $indent x 2, $ele if defined $ele;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ele 0 6 6
261 6 ▶ if (defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) 6 0 6
262 6 printf "%s<time>%s</time>\n", $indent x 2, $fit->date_string($time);
263 }
264 6 ▶ printf "%s<name>%s</name>\n", $indent x 2, _enc( $name ) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 6 0 6
265 6 ▶ printf "%s<desc>%s</desc>\n", $indent x 2, _enc( $desc ) if defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc 0 6 6
266 6 printf "%s</wpt>\n", $indent
267 }
268  
269 =head1 DEPENDENCIES
270  
271 L<Geo::FIT>
272  
273 =head1 SEE ALSO
274  
275 L<Geo::Gpx>
276  
277 =head1 BUGS AND LIMITATIONS
278  
279 No bugs have been reported.
280  
281 Please report any bugs or feature requests to C<bug-geo-gpx@rt.cpan.org>, or through the web interface at L<http://rt.cpan.org>.
282  
283 =head1 AUTHOR
284  
285 Patrick Joly
286  
287 =head1 VERSION
288  
289 1.09
290  
291 =head1 LICENSE AND COPYRIGHT
292  
293 Copyright 2022, Patrick Joly C<< patjol@cpan.org >>. All rights reserved.
294  
295 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>.
296  
297 =head1 DISCLAIMER OF WARRANTY
298  
299 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.
300  
301 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.
302  
303 =cut
304  
305 1;
« script/fitdump.pl ↑ Summary Build »