blib/script/locations2gpx.pl untested

filter: Statementstmt 0.0% 0 / 162 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 273 SCAR 64.0
CC
24
Cov
0%
_print_wpt
182.0
_message
56.0
_write_meta
30.0
CRAP 600.0
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4  
5 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 use Geo::FIT;
27 use Getopt::Long;
28 use HTML::Entities qw( encode_entities encode_entities_numeric );
29  
30 my ($force, $time_offset, $indent_n, $indent, $outfile, $help) = (0, 0, 2);
31 sub usage { "Usage: $0 [ --help --outfile=<fname> --force --indent=# --time_offset=# ] fit_locations_file\n" }
32  
33 GetOptions( "outfile=s" => \$outfile,
34 "indent=i" => \$indent_n,
35 "force" => \$force,
36 "time_offset=i" => \$time_offset,
37 "help" => \$help,
38 ) or die usage();
39  
40 die usage() if $help;
41 $indent = " " x $indent_n;
42  
43 my ($file, $gpx_file);
44 for (@ARGV) {
45 $file = $_ if /(?i:\.fit)$/;
46 die "No FIT input file provided" unless $file;
47 die "No file named $file found" unless -f $file
48 }
49  
50 if (defined $outfile) {
51 $gpx_file = $outfile
52 } else {
53 ($gpx_file = $file) =~ s/(?i:.fit)$/.gpx/i
54 }
55 die "File $gpx_file already exists\n" if -f $gpx_file and !$force;
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 my ($fit, @id, @locations);
82  
83 $fit = Geo::FIT->new();
84 $fit->file($file);
85  
86 $fit->use_gmtime(1);
87 $fit->numeric_date_time(1);
88 $fit->semicircles_to_degree(1);
89 $fit->mps_to_kph(1);
90 $fit->without_unit(1);
91 $fit->maybe_chained(0);
92 $fit->drop_developer_data(1);
93 $fit->data_message_callback_by_name('',
94 sub {
95 my ($msg, $name) = _message(@_);
96 if ($msg) {
97 push @id, $msg if $name eq "file_id";
98 push @locations, $msg if $name eq "location"
99 }
100 return 1
101 }
102 );
103 $fit->open() or die $fit->error();
104  
105 my ($f_size) = $fit->fetch_header();
106 unless (defined $f_size) {
107 $fit->error("can't read FIT header") unless defined $fit->error();
108 die $fit->error()
109 }
110 1 while $fit->fetch();
111  
112 my (%device, $oem, $prod, $desc);
113 %device = %{$id[0]};
114 $oem = defined $device{manufacturer} ? $device{manufacturer} : "Unknown OEM";
115 $prod = defined $device{garmin_product} ? $device{garmin_product} : "unknown";
116 $desc = ucfirst "recorded on a $oem $prod device";
117  
118 if (@locations) {
119 open (my $fh, ">", $gpx_file) or die "cannot open $gpx_file: $!";
120 select $fh;
121 printf "<?xml %s?>\n", 'version="1.0" encoding="UTF-8"';
122 _write_header();
123 _write_meta();
124 _write_waypoints();
125 printf "</gpx>\n";
126 select STDOUT
127 }
128  
129 sub _message {
130 my ($fit, $desc, $v) = @_;
131  
132 my $m_name = $desc->{message_name};
133 return undef unless $m_name;
134  
135 my $msg = {};
136  
137 for my $i_name (keys %$desc) {
138 next if $i_name !~ /^i_/;
139 my $name = $i_name;
140 $name =~ s/^i_//;
141  
142 my $attr = $desc->{'a_' . $name};
143 my $tname = $desc->{'t_' . $name};
144 my $pname = $name;
145  
146 if (ref $attr->{switch} eq 'HASH') {
147 my $t_attr = $fit->switched($desc, $v, $attr->{switch});
148  
149 if (ref $t_attr eq 'HASH') {
150 $attr = $t_attr;
151 $tname = $attr->{type_name};
152 $pname = $attr->{name}
153 }
154 }
155  
156 my $i = $desc->{$i_name};
157 my $c = $desc->{'c_' . $name};
158 my $type = $desc->{'T_' . $name};
159 my $invalid = $desc->{'I_' . $name};
160 my $j;
161  
162 my $len = @$v;
163 for ($j = 0 ; $j < $c ; $j++) {
164 my $ij = $i + $j;
165 $ij >= $len && next;
166 Geo::FIT->isnan($v->[$ij]) && next;
167 $v->[$ij] != $invalid && last
168 }
169 if ($j < $c) { # skip invalid
170 if ($type == FIT_STRING) {
171 $msg->{$pname} = Geo::FIT::_string_value($v, $i, $c)
172 }
173 else {
174 # return only the first value if array
175 $msg->{$pname} = $fit->value_cooked($tname, $attr, $invalid, $v->[$i])
176 }
177 }
178 }
179 return ($msg, $m_name)
180 }
181  
182 sub _enc {
183 return encode_entities_numeric( $_[0] );
184 }
185  
186 # https://www.topografix.com/GPX/1/1/#element_gpx
187 sub _write_header {
188 printf "<gpx %s", 'version="1.1" creator="locations2gpx.pl"';
189 my $loc = 'xsi:schemaLocation="';
190 $loc = $loc . "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd";
191 $loc = $loc . " http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd";
192 $loc = $loc . " http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www8.garmin.com/xmlschemas/TrackPointExtensionv1.xsd";
193 $loc = $loc . " http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd";
194 $loc = $loc . " http://www.cluetrust.com/XML/GPXDATA/1/0 http://www.cluetrust.com/Schemas/gpxdata10.xsd";
195 $loc = $loc . '"';
196 printf " %s", $loc;
197 printf " %s", 'xmlns="http://www.topografix.com/GPX/1/1"';
198 printf " %s", 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
199 # garmin_ext
200 printf " %s", 'xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"';
201 printf " %s", 'xmlns:gpxtrx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"';
202 printf " %s", 'xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1"';
203 printf " %s", 'xmlns:gpxwpx="http://www.garmin.com/xmlschemas/WaypointExtension/v1"';
204 printf ">\n";
205 }
206  
207 # https://www.topografix.com/GPX/1/1/#type_metadataType
208 sub _write_meta {
209 my $name; # none for now but declaring so it can be easily assigned
210 printf "%s<metadata>\n", $indent;
211 printf "%s<name>%s</name>\n", $indent x 2, _enc( $name ) if defined $name;
212 printf "%s<desc>%s</desc>\n", $indent x 2, _enc( $desc );
213  
214 my $author_name = 'Converted from Locations.fit by locations2gpx.pl';
215 printf "%s<author>\n", $indent x 2;
216 printf "%s<name>%s</name>\n", $indent x 3, _enc( $author_name );
217 printf "%s</author>\n", $indent x 2;
218  
219 my ($url, $text, $type); # none for now but declaring so they can be easily assigned
220 if (defined $url) {
221 printf "%s<link href=\"%s\">\n", $indent x 2, _enc( $url ) ;
222 printf "%s<text>%s</text>\n", $indent x 3, _enc( $text ) if defined $text;
223 printf "%s<type>%s</type>\n", $indent x 3, $type if defined $type;
224 printf "%s</link>\n", $indent x 2;
225 }
226 printf "%s</metadata>\n", $indent
227 }
228  
229 sub _write_waypoints {
230 if (@locations) {
231 my $ai = 0; # alt index
232 for (@locations) {
233 _print_wpt(\%$_, $ai);
234 $ai++
235 }
236 }
237 }
238  
239 sub _print_wpt {
240 my ($m, $alt_index) = @_; # alt_index is not used
241  
242 # https://www.topografix.com/GPX/1/1/#type_wptType
243  
244 my ($time, $lon, $lat, $ele, $name, $desc);
245 while (my ($key, $val) = each %{$m}) {
246 # print "\$key is: $key, \$val is $val \n";
247 if ($key eq "timestamp") { $time = $val + $time_offset } # + $timeoffs;
248 elsif ($key eq "position_long") { $lon = $val }
249 elsif ($key eq "position_lat") { $lat = $val }
250 elsif ($key eq "altitude" and !defined $ele) { $ele = $val }
251 elsif ($key eq "enhanced_altitude") {
252 if (defined $val) { $ele = $val } # only if valid
253 }
254 elsif ($key eq "name") { $name = $val }
255 elsif ($key eq "unknown6") { $desc = $val }
256 }
257 # TODO: dump other unknown\d keys to STDERR
258  
259 printf "%s<wpt lat=\"%s\" lon=\"%s\">\n", $indent, $lat, $lon;
260 printf "%s<ele>%s</ele>\n", $indent x 2, $ele if defined $ele;
261 if (defined $time) {
262 printf "%s<time>%s</time>\n", $indent x 2, $fit->date_string($time);
263 }
264 printf "%s<name>%s</name>\n", $indent x 2, _enc( $name ) if defined $name;
265 printf "%s<desc>%s</desc>\n", $indent x 2, _enc( $desc ) if defined $desc;
266 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;