blib/lib/Geo/Gpx/Point.pm

filter: Statementstmt 75.2% 79 / 105 Branchbran 50.0% 14 / 28 Conditioncond 50.0% 6 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 76.4% 13 / 17 Podpod 100% 8 / 8 totaltotal 67.9% 121 / 178 CC 22 SCAR 16.8
CC
22
Cov
68%
time_datetime
12.0
summ
12.0
new
7.5
CRAP 5.4
↑ Summary Build »
1 package Geo::Gpx::Point;
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.07';
6  
7 =encoding utf8
8  
9 =head1 NAME
10  
11 Geo::Gpx::Point - Class to store and edit GPX Waypoints
12  
13 =head1 SYNOPSIS
14  
15 use Geo::Gpx::Point;
16  
17 =head1 DESCRIPTION
18  
19 L<Geo::Gpx::Point> provides a data structure for GPX points and provides accessor methods to read and edit point data.
20  
21 =cut
22  
23 2 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Geo::Calc;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Geo::Coordinates::Transform;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Carp qw(confess croak cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use vars qw($AUTOLOAD %possible_attr);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use overload ('""' => 'as_string');
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # file-scoped lexicals
31 2 my @attr = qw/ lat lon ele time magvar geoidheight name cmt desc src link sym type fix sat hdop vdop pdop ageofdgpsdata dgpsid extensions /;
32 2 $possible_attr{$_} = 1 for @attr;
33  
34 =head2 Constructor Method
35  
36 =over 4
37  
38 =item new( lat => $lat, lon => $lon [, ele => $ele, desc => $desc, … ] )
39  
40 Create and return a new point as per the fields provided, which can be any of C<lat lon ele time magvar geoidheight name cmt desc src link sym type fix sat hdop vdop pdop ageofdgpsdata dgpsid>. Most expect numberial values except: C<name>, C<cmt>, C<desc>, C<src>, C<sym>, C<type>, C<fix> that can contain strings.
41  
42 C<lat> and C<lon> are required, all others keys are optional.
43  
44 %fields = ( lat => 47.0871, lon => 70.9318, ele => 808.000, name => 'MSA', desc => 'A nice view of the River at the top');
45 $pt = Geo::Gpx::Point->new( %fields );
46  
47 The C<link> field is expected to be structured as:
48  
49 link => { href => 'http://hexten.net/', text => 'Hexten', type => 'Blah' },
50  
51 =back
52  
53 =cut
54  
55 sub new {
56 5 ▶ my $proto = shift;
Subroutine new: called CC=6 SCAR=20.2
Pod documented
57 5 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
140
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
58 5 my %fields = @_;
59 5 my $wpt = {};
60 5 bless( $wpt, $class);
61  
62 5 foreach my $key ( keys %fields ) {
63 18 ▶ if ( $possible_attr{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Geo::Gpx::Point::possible_attr{$key}) { } 18 0 18
64 18 $wpt->{ $key } = $fields { $key }
65 0 } else { croak "field '$key' not supported" }
66 }
67  
68 5 ▶ if (defined $wpt->{time} and $wpt->{time} =~ /-|:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wpt->{"time"} and $wpt->{"time"} =~ /-|:/) 0 5 5
Condition33%1/3
defined $wpt->{"time"} and $wpt->{"time"} =~ /-|:/
!ll&&!rl&&r
500
MC/DC0%0/2
defined $wpt->{"time"} and $wpt->{"time"} =~ /-|:/
defined $wpt->{"time"} $wpt->{"time"} =~ /-|:/
Truth table33%1/3
A and B
A= defined $wpt->{"time"}B= $wpt->{"time"} =~ /-|:/
ABresult
0X0
100
111
69 0 my $dt = DateTime::Format::ISO8601->parse_datetime( $wpt->{time} );
70 0 $wpt->{time} = $dt->epoch
71 }
72 5 return $wpt
73 }
74  
75 =over 4
76  
77 =item flex_coordinates( $lat, $lon, %fields )
78  
79 Takes latitude and longitude decimal values or strings and returns a C<Geo::Gpx::Point> object. The latitude should always appear before the longitude and both can be in formatted form (i.e Degrees, Minutes, Seconds or "dms") and the constructor will attempt to convert them to decimals. Any other I<%fields> are optional.
80  
81 $pt = Geo::Gpx::Point->flex_coordinates( '47.0871', '-70.9318', desc => 'Mont Ste-Anne' );
82  
83 If a string reference is passed as the first argument (instead of I<$lat> and I<$lon>), the constructor will attempt to parse it as coordinates (decimal-form only). For instance you can simply call C<< flex_coordinates( '47.0871 -70.9318' ) >> with or without a comma along with optional fields.
84  
85 $str_ref = \'47.0871 -70.9318';
86 $pt = Geo::Gpx::Point->flex_coordinates($str_ref, desc => 'Mont Ste-Anne' );
87  
88 =back
89  
90 =cut
91  
92 sub flex_coordinates {
93 3 ▶ my ($proto, $lat, $lon, %fields) = shift;
Subroutine flex_coordinates: called CC=6 SCAR=18.0
Pod documented
94 # objective of this constructor is to be super flexible, for additionl safety do not use
95 # if string ref provided, expect coord in single string (decimal form only I think)
96 3 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 1 2 3
97 1 $lat = shift;
98 1 $lat = $$lat;
99 1 $lat =~ s/(,| )+/ /g;
100 1 ($lat, $lon) = split ' ', $lat;
101 1 %fields = @_
102 2 } else { ($lat, $lon, %fields) = @_ }
103 3 ▶ croak 'flex_coordinates takes at least 2 arguments: latitude and longitude'
Branch: 1 branch
BranchTrueFalseTotal
unless $lon 0 3 3
104 unless ($lon);
105 3 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
106  
107 3 $lat =~ s/N//; $lon =~ s/N//;
108 3 $lat =~ s/E//; $lon =~ s/E//;
109 3 $lat =~ s/S/-/; $lon =~ s/S/-/;
110 3 $lat =~ s/W/-/; $lon =~ s/W/-/;
111 # convert coord if in DM or DMS format? (they are if whitesp or >2 dots)
112 3 ▶ if ( ( $lat =~ /\d[ \.]+\d+[ \.]/) or
Branch: 1 branch
BranchTrueFalseTotal
if ($lat =~ /\d[ \.]+\d+[ \.]/ or $lon =~ /\d[ \.]+\d+[ \.]/) 1 2 3
Condition66%2/3
$lat =~ /\d[ \.]+\d+[ \.]/ or $lon =~ /\d[ \.]+\d+[ \.]/
l!l&&r!l&&!r
102
MC/DC50%1/2
$lat =~ /\d[ \.]+\d+[ \.]/ or $lon =~ /\d[ \.]+\d+[ \.]/
$lat =~ /\d[ \.]+\d+[ \.]/ $lon =~ /\d[ \.]+\d+[ \.]/
Truth table66%2/3
A or B
A= $lat =~ /\d[ \.]+\d+[ \.]/B= $lon =~ /\d[ \.]+\d+[ \.]/
ABresult
1X1
011
000
113 ( $lon =~ /\d[ \.]+\d+[ \.]/) ) {
114 1 my $cnv = new Geo::Coordinates::Transform();
115 1 my $ref = $cnv->cnv_to_dd( [ $lat, $lon ] );
116 1 $lat = $ref->[0];
117 1 $lon = $ref->[1]
118 }
119 3 return $class->new( lat => $lat, lon => $lon, %fields)
120 }
121  
122 =over 4
123  
124 =item clone()
125  
126 Returns a deep copy of the C<Geo::Gpx::Point>.
127  
128 $clone = $ggp->clone;
129  
130 =back
131  
132 =cut
133  
134 sub clone {
135 1 ▶ my $pt = shift;
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
136 1 ▶ croak 'clone() expects no arguments' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
137 1 my %fields;
138 1 foreach my $key (keys %{$pt}) {
139 5 $fields{$key} = $pt->{$key}
140 }
141 1 return $pt->new( %fields )
142 }
143  
144 =head2 AUTOLOAD Methods
145  
146 =over 4
147  
148 =item I<field>( $value )
149  
150 Methods with respect to fields of the object can be autoloaded.
151  
152 Possible fields consist of those listed and accepted by C<new()>, specifically:
153 lat, lon, ele, time, magvar, geoidheight, name, cmt, desc, src, link, sym, type, fix, sat, hdop, vdop, pdop, ageofdgpsdata, and dgpsid.
154  
155 Some fields may contain a value of 0. It is safer to check if a field is defined with C<< if (defined $point->ele) >> rather than C<< if ($point->ele) >>.
156  
157 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.
158  
159 =back
160  
161 =cut
162  
163 sub AUTOLOAD {
164 12 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
165 12 my $attr = $AUTOLOAD;
166 12 $attr =~ s/.*:://;
167 12 ▶ return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /[^A-Z]/ 5 7 12
168 7 ▶ croak "invalid attribute method: -> $attr()" unless $possible_attr{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $Geo::Gpx::Point::possible_attr{$attr} 0 7 7
169 7 ▶ $self->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 6 7
170 7 return $self->{$attr}
171 }
172  
173 =head2 Object Methods
174  
175 =over 4
176  
177 =item to_geocalc()
178  
179 Returns a point as a L<Geo::Calc> object.
180  
181 =back
182  
183 =cut
184  
185 sub to_geocalc {
186 1 ▶ my $pt = shift;
Subroutine to_geocalc: called CC=2 SCAR=7.1
Pod documented
187 1 ▶ croak "to_geocalc() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
188 1 return Geo::Calc->new( lat => $pt->lat, lon => $pt->lon );
189 }
190  
191 =over 4
192  
193 =item to_tcx()
194  
195 Returns a point as a basic L<Geo::TCX::Trackpoint> object, i.e. a point with only Position information. (Requires the L<Geo::TCX> module.)
196  
197 =back
198  
199 =cut
200  
201 sub to_tcx {
202 0 ▶ require 'Geo::TCX';
Subroutine to_tcx: not called CC=2 SCAR=17.9
Pod documented
203 0 my $pt = shift;
204 0 ▶ croak "to_tcx() takes no arguments" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
205 0 my $xml = '<Position><LatitudeDegrees>' . $pt->lat . '</LatitudeDegrees>' .
206 '<LongitudeDegrees>' . $pt->lon . '</LongitudeDegrees></Position';
207 0 return Geo::TCX::Trackpoint->new( $xml )
208 }
209  
210 =over 4
211  
212 =item time_datetime ()
213  
214 Return a L<DateTime> object corresponding to the time of the point. The C<time_zone> of the object will be C<< 'UTC' >>. Specify C<< time_zone => $tz >> to set a different one.
215  
216 =back
217  
218 =cut
219  
220 # we never store a DateTime object but provide a method to return one
221 sub time_datetime {
222 0 ▶ my $pt = shift;
Subroutine time_datetime: not called CC=3 SCAR=24.8
Pod documented
223 0 my %opts = @_;
224 0 ▶ croak 'Geo::Gpx::Point has no time field' unless $pt->time;
Branch: 1 branch
BranchTrueFalseTotal
unless $pt->time 0 0 0
225 0 my $dt = DateTime->from_epoch( $pt->time );
226 0 ▶ $dt->set_time_zone( $opts{time_zone} ) if $opts{time_zone};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"time_zone"} 0 0 0
227 0 return $dt
228 }
229  
230 =over 4
231  
232 =item summ()
233  
234 For debugging purposes mostly. Summarizes the fields of point by printing to screen. Returns nothing.
235  
236 =back
237  
238 =cut
239  
240 sub summ {
241 0 ▶ my $pt = shift;
Subroutine summ: not called CC=3 SCAR=24.8
Pod documented
242 0 ▶ croak 'summ() expects no arguments' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
243 0 my %fields;
244 0 foreach my $key (keys %{$pt}) {
245 0 print "$key: ", $pt->{$key}, "\n"
246 }
247 return undef
248 0 }
249  
250 =over 4
251  
252 =item as_string()
253  
254 Returns a string with the coordinates e.g. C<lat="47.0871" lon="-70.9318">.
255  
256 =back
257  
258 =cut
259  
260 sub as_string {
261 0 ▶ my $pt = shift;
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
262 0 my %fields;
263 my $str;
264 0 $str = 'lat="' . $pt->{lat} . '" ';
265 0 $str .= 'lon="' . $pt->{lon} . '"';
266 0 return $str
267 }
268  
269 =head2 Overloaded Methods
270  
271 C<as_string()> is called when using a C<Geo::Gpx::Point> instance as a string.
272  
273 =head1 EXAMPLES
274  
275 Coming soon.
276  
277 =head1 AUTHOR
278  
279 Patrick Joly C<< <patjol@cpan.org> >>.
280  
281 =head1 VERSION
282  
283 1.07
284  
285 =head1 SEE ALSO
286  
287 perl(1).
288  
289 =cut
290  
291 2 1;
292  
↑ Summary Build »