blib/lib/Geo/WebService/Elevation/USGS.pm

filter: Statementstmt 77.2% 160 / 207 Branchbran 49.0% 49 / 100 Conditioncond 43.9% 18 / 41 MC/DCmcdc 20.0% 5 / 25 Subroutinesub 70.7% 29 / 41 Podpod 100% 6 / 6 totaltotal 63.5% 267 / 420 CC 64 SCAR 25.9
CC
64
Cov
65%
_set_throttle
56.0
_deprecate
28.1
_request
26.7
CRAP 13.3
↑ Summary Build »
1 =head1 NAME
2  
3 Geo::WebService::Elevation::USGS - Elevation queries against USGS web services.
4  
5 =head1 SYNOPSIS
6  
7 use Geo::WebService::Elevation::USGS;
8
9 my $eq = Geo::WebService::Elevation::USGS->new();
10 print "The elevation of the White House is ",
11 $eq->elevation( 38.898748, -77.037684 )->{Elevation},
12 " feet above sea level.\n";
13  
14 =head1 NOTICE
15  
16 Some time while I was not looking the USGS changed the web address and
17 the API for the point elevation service yet again.
18  
19 I have dealt with this by resurrecting the C<compatible> attribute and
20 defaulting it to a true value. This attribute affects the hash returned
21 by the C<elevation()> method. See that method's documentation
22 for the gory details.
23  
24 You are encouraged to set this attribute to a false value as soon as you
25 can, since my long-term plan is to discourage a true value, and then
26 ultimately deprecate and remove it.
27  
28 =head1 DESCRIPTION
29  
30 This module executes elevation queries against the United States
31 Geological Survey's Elevation Data Point Service. You provide the
32 latitude and longitude in degrees, with south latitude and west
33 longitude being negative. The return is typically a hash containing the
34 data you want. Query errors are exceptions by default, though the object
35 can be configured to signal an error by an undef response, with the
36 error retrievable from the 'error' attribute.
37  
38 For documentation on the underlying web service, see
39 L<https://www.usgs.gov/programs/national-geospatial-program/national-map>,
40 particularly L<https://epqs.nationalmap.gov/v1/docs>.
41  
42 For all methods, the input latitude and longitude are documented at the
43 above web site as being WGS84, which for practical purposes I understand
44 to be equivalent to NAD83. The vertical reference is not documented
45 under the above link, but correspondence with the USGS says that it is
46 derived from the National Elevation Dataset (NED; see
47 L<https://www.usgs.gov/programs/national-geospatial-program/national-map>).
48 This is referred to NAD83 (horizontal) and NAVD88 (vertical). NAVD88 is
49 based on geodetic leveling surveys, B<not the WGS84/NAD83 ellipsoid,>
50 and takes as its zero datum sea level at Father Point/Rimouski, in
51 Quebec, Canada. Alaska is an exception, and is based on NAD27
52 (horizontal) and NAVD29 (vertical).
53  
54 Anyone interested in the gory details may find the paper I<Converting
55 GPS Height into NAVD88 Elevation with the GEOID96 Geoid Height Model> by
56 Dennis G. Milbert, Ph.D. and Dru A. Smith, Ph.D helpful. This is
57 available at L<https://www.ngs.noaa.gov/PUBS_LIB/gislis96.html>. This
58 paper states that the difference between ellipsoid and geoid heights
59 ranges between -75 and +100 meters globally, and between -53 and -8
60 meters in "the conterminous United States."
61  
62 =head2 Methods
63  
64 The following public methods are provided:
65  
66 =cut
67  
68 package Geo::WebService::Elevation::USGS;
69  
70 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use Scalar::Util 1.10 qw{ blessed looks_like_number };
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 2 our $VERSION = '0.201_01';
82  
83 # use constant USGS_URL => 'https://ned.usgs.gov/epqs/pqs.php';
84 # use constant USGS_URL => 'https://nationalmap.gov/epqs/pqs.php';
85 2 ▶use constant USGS_URL => 'https://epqs.nationalmap.gov/v1/json';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 2 ▶use constant ARRAY_REF => ref [];
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use constant CODE_REF => ref sub {};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
89 2 ▶use constant HASH_REF => ref {};
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use constant REGEXP_REF => ref qr{};
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 2 my $using_time_hires;
93 {
94 2 my $mark;
95 2 ▶ if ( eval {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Time::HiRes; 'Time::HiRes'->can('time') and 'Time::HiRes'->can('sleep') } }) { } 2 0 2
96 2 require Time::HiRes;
97 2 ▶ Time::HiRes->can( 'time' ) && Time::HiRes->can( 'sleep' );
Branch: 1 branch
BranchTrueFalseTotal
"Time::HiRes"->can("time") and "Time::HiRes"->can("sleep") 2 0 2
98 } ) {
99 2 *_time = \&Time::HiRes::time;
100 2 *_sleep = \&Time::HiRes::sleep;
101 2 $using_time_hires = 1;
102 } else {
103 0 ▶ *_time = sub { return time };
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 ▶ *_sleep = sub { return sleep $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 }
106  
107 2 $mark = _time();
108 sub _pause {
109 ## my ( $self ) = @_; # Invocant unused
110 4 ▶ my $now = _time();
Subroutine _pause: called CC=2 SCAR=7.9
111 4 ▶ while ( $now < $mark ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now < $mark) 0 4 4
112 0 _sleep( $mark - $now );
113 0 $now = _time();
114 }
115 # We use __PACKAGE__ rather than $self because the attribute is
116 # static, and it needs to be static because it needs to apply to
117 # everything coming from this user, not just everything coming
118 # from the invoking object.
119 4 $mark = $now + __PACKAGE__->get( 'throttle' );
120 4 return;
121 }
122 }
123  
124 =head3 $eq = Geo::WebService::Elevation::USGS->new();
125  
126 This method instantiates a query object. If any arguments are given,
127 they are passed to the set() method. The instantiated object is
128 returned.
129  
130 =cut
131  
132 2 sub new {
133 3 ▶ my ($class, @args) = @_;
Subroutine new: called CC=5 SCAR=16.2
Pod documented
134 3 ▶ ref $class and $class = ref $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 3 3
135 3 ▶ $class or croak "No class name specified";
Branch: 1 branch
BranchTrueFalseTotal
$class or croak("No class name specified") 1 2 3
136 2 shift;
137 my $self = {
138 carp => 0,
139 compatible => 1,
140 croak => 1,
141 error => undef,
142 places => undef,
143 retry => 0,
144 ▶ retry_hook => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
145 timeout => 30,
146 trace => undef,
147 units => 'FEET',
148 2 ▶ usgs_url => $ENV{GEO_WEBSERVICE_ELEVATION_USGS_URL} || USGS_URL,
Condition50%1/2
$ENV{'GEO_WEBSERVICE_ELEVATION_USGS_URL'} || 'https://epqs.nationalmap.gov/v1/json'
l!l
02
MC/DC0%0/1
$ENV{'GEO_WEBSERVICE_ELEVATION_USGS_URL'} || 'https://epqs.nationalmap.gov/v1/json'
$ENV{'GEO_WEBSERVICE_ELEVATION_USGS_URL'}
Truth table50%1/2
A
A= $ENV{'GEO_WEBSERVICE_ELEVATION_USGS_URL'}
Aresult
11
00
149 };
150 2 bless $self, $class;
151 2 ▶ @args and $self->set(@args);
Branch: 1 branch
BranchTrueFalseTotal
@args and $self->set(@args) 1 1 2
152 2 return $self;
153 }
154  
155 2 my %mutator = (
156 croak => \&_set_literal,
157 carp => \&_set_literal,
158 compatible => \&_set_literal,
159 error => \&_set_literal,
160 places => \&_set_integer_or_undef,
161 retry => \&_set_unsigned_integer,
162 retry_hook => \&_set_hook,
163 throttle => \&_set_throttle,
164 timeout => \&_set_integer_or_undef,
165 trace => \&_set_literal,
166 units => \&_set_literal,
167 usgs_url => \&_set_literal,
168 );
169  
170 2 my %access_type = (
171 throttle => \&_only_static_attr,
172 );
173  
174 2 foreach my $name ( keys %mutator ) {
175 exists $access_type{$name}
176 24 ▶ or $access_type{$name} = \&_no_static_attr;
Branch: 1 branch
BranchTrueFalseTotal
exists $access_type{$name} or $access_type{$name} = \&Geo::WebService::Elevation::USGS::_no_static_attr 22 2 24
177 }
178  
179 =head3 %values = $eq->attributes();
180  
181 This method returns a list of the names and values of all attributes of
182 the object. If called in scalar context it returns a hash reference.
183  
184 =cut
185  
186 sub attributes {
187 3 ▶ my $self = shift;
Subroutine attributes: called CC=3 SCAR=11.0
Pod documented
188 3 my %attr;
189 3 foreach (keys %mutator) {
190 36 $attr{$_} = $self->{$_};
191 }
192 3 ▶ return wantarray ? %attr : \%attr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
193 }
194  
195 =head3 $rslt = $usgs->elevation($lat, $lon, $valid);
196  
197 This method queries the data base for the elevation at the given
198 latitude and longitude, returning the results as a hash reference.
199  
200 If the C<compatible> attribute is true, this hash will contain the
201 following keys:
202  
203 =over
204  
205 =item {Data_Source} => A text description of the data source (always 'USGS Elevation Point Query Service');
206  
207 =item {Elevation} => The elevation in the given units;
208  
209 =item {Units} => The units of the elevation (C<'Feet'> or C<'Meters'>);
210  
211 =item {x} => The C<$lon> argument;
212  
213 =item {y} => The C<$lat> argument.
214  
215 =back
216  
217 If the C<compatible> attribute is false, the hash will contain the
218 values documented at L<https://epqs.nationalmap.gov/v1/docs>. B<Note>
219 that the elevation comes back in key C<{value}>. For my own sanity key
220 C<{Elevation}> is added to this hash; it contains the value of
221 C<{value}>, rounded to C<places> if that attribute is set.
222  
223 You can also pass a C<Geo::Point>, C<GPS::Point>, or C<Net::GPSD::Point>
224 object in lieu of the C<$lat> and C<$lon> arguments. If you do this,
225 C<$valid> becomes the second argument, rather than the third.
226  
227 If the optional C<$valid> argument is specified as a true value B<and>
228 the returned data are invalid, nothing is returned. The source does not
229 seem to produce data recognizable as invalid, so you will probably not
230 see this.
231  
232 =cut
233  
234 sub elevation {
235 4 ▶ my ( $self, $lat, $lon, $valid ) = _latlon( @_ );
Subroutine elevation: called CC=8 SCAR=27.3
Pod documented
236 4 my $retry_limit = $self->get( 'retry' );
237 4 my $retry = 0;
238  
239 4 ▶ while ( $retry++ <= $retry_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry++ <= $retry_limit) 4 0 4
240  
241 4 $self->{error} = undef;
242  
243 4 $self->_pause();
244  
245 4 my $rslt;
246 eval {
247 $rslt = $self->_request(
248 x => $lon,
249 y => $lat,
250 units => $self->{units},
251 4 );
252 4 1;
253 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $rslt = $self->_request("x", $lon, "y", $lat, "units", $self->{"units"}); 1 } } or do { $self->_error($@); next } 0 4 4
254 0 $self->_error( $@ );
255 0 next;
256 };
257  
258 4 ▶ $rslt
Branch: 1 branch
BranchTrueFalseTotal
$rslt or next 0 4 4
259 or next;
260  
261 4 ▶ not $valid
Branch: 1 branch
BranchTrueFalseTotal
not $valid or is_valid($rslt) or next 0 4 4
Condition33%1/3
not $valid or is_valid($rslt)
l!l&&r!l&&!r
400
MC/DC0%0/2
not $valid or is_valid($rslt)
not $valid is_valid($rslt)
Truth table33%1/3
not(A) or B
A= not $validB= is_valid($rslt)
ABresult
1X1
011
000
262 or is_valid( $rslt )
263 or next;
264  
265 4 return $rslt;
266  
267 } continue {
268  
269 0 ▶ if ( $retry <= $retry_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry <= $retry_limit) 0 0 0
270 0 ( my $sub = ( caller( 0 ) )[3] ) =~ s/ .* :: //smx;
271 0 $self->get( 'retry_hook' )->( $self, $retry, $sub, $lat,
272 $lon );
273 }
274  
275 }
276  
277 0 ▶ $self->{croak} and croak $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"croak"} and croak($self->{"error"}) 0 0 0
278 0 return;
279  
280 }
281  
282 =head3 $value = $eq->get($attribute);
283  
284 This method returns the value of the given attribute. It will croak if
285 the attribute does not exist.
286  
287 =cut
288  
289 sub get {
290 37 ▶ my ($self, $name) = @_;
Subroutine get: called CC=2 SCAR=6.9
Pod documented
291 37 ▶ $access_type{$name}
Branch: 1 branch
BranchTrueFalseTotal
$access_type{$name} or croak("No such attribute as '${name}'") 2 35 37
292 or croak "No such attribute as '$name'";
293 35 my $holder = $access_type{$name}->( $self, $name );
294 35 return $holder->{$name};
295 }
296  
297 =head3 $rslt = $eq->getAllElevations($lat, $lon, $valid);
298  
299 This method was removed in version 0.116_01. Please use the
300 C<elevation()> method instead. See the L<NOTICE|/NOTICE> above for
301 details.
302  
303 =head3 $rslt = $eq->getElevation($lat, $lon, $source, $elevation_only);
304  
305 This method was removed in version 0.116_01. Please use the
306 C<elevation()> method instead. See the L<NOTICE|/NOTICE> above for
307 details.
308  
309 =cut
310  
311 =head3 $boolean = $eq->is_valid($elevation);
312  
313 This method (which can also be called as a static method or as a
314 subroutine) returns true if the given datum represents a valid
315 elevation, and false otherwise. A valid elevation is a number having a
316 value greater than -1e+300. The input can be either an elevation value
317 or a hash whose {Elevation} key supplies the elevation value.
318  
319 B<Note> that as of June 11 2024 I am unable to find any documentation to
320 support this method. Therefore use of this method is discouraged, and it
321 will deprecated and removed when I drop support for the C<compatible>
322 attribute.
323  
324 =cut
325  
326 sub is_valid {
327 6 ▶ my $ele = pop;
Subroutine is_valid: called CC=5 SCAR=16.1
Pod documented
328 6 my $ref = ref $ele;
329 6 ▶ if ( HASH_REF eq $ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq $ref) { } 1 5 6
elsif ($ref) { } 1 4 5
330 1 $ele = $ele->{Elevation};
331 } elsif ($ref) {
332 1 croak "$ref reference not understood";
333 }
334 5 ▶ return defined( $ele ) && looks_like_number($ele) && $ele > -1e+300;
Condition100%6/6
defined $ele && &looks_like_number($ele) && $ele > -1e+300
!ll&&!rl&&r
212
defined $ele and &looks_like_number($ele)
!ll&&!rl&&r
113
MC/DC100%3/3
defined $ele && &looks_like_number($ele) && $ele > -1e+300
defined $ele &looks_like_number($ele) $ele > -1e+300
Truth table100%4/4
A and B && C
A= defined $eleB= &looks_like_number($ele)C= $ele > -1e+300
ABCresult
0XX0
10X0
1100
1111
335 }
336  
337 =head3 $eq = $eq->set($attribute => $value ...);
338  
339 This method sets the value of the given attribute. Multiple
340 attribute/value pairs may be specified. The object itself is returned,
341 to allow call chaining. An attempt to set a non-existent attribute will
342 result in an exception being thrown.
343  
344 =cut
345  
346 {
347  
348 # Changes in these values require re-instantiating the transport
349 # object. Or at least, they may do, under the following assumptions:
350 # HTTP_Post: timeout.
351 2 my %clean_transport_object = map { $_ => 1 } qw{ timeout };
352  
353 sub set { ## no critic (ProhibitAmbiguousNames)
354 11 ▶ my ($self, @args) = @_;
Subroutine set: called CC=6 SCAR=18.2
Pod documented
355 11 my $clean;
356 11 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 12 8 20
357 12 my ( $name, $val ) = splice @args, 0, 2;
358 12 ▶ $access_type{$name}
Branch: 1 branch
BranchTrueFalseTotal
$access_type{$name} or croak("No such attribute as '${name}'") 2 10 12
359 or croak "No such attribute as '$name'";
360 10 ▶ exists $mutator{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $mutator{$name} or croak("Attribute '${name}' is read-only") 0 10 10
361 or croak "Attribute '$name' is read-only";
362 10 _deprecate( attribute => $name );
363 10 my $holder = $access_type{$name}->( $self, $name );
364 10 $mutator{$name}->( $holder, $name, $val );
365 9 ▶ $clean ||= $clean_transport_object{$name};
Condition33%1/3
$clean ||= $clean_transport_object{$name}
l!l&&r!l&&!r
009
MC/DC0%0/2
$clean ||= $clean_transport_object{$name}
$clean $clean_transport_object{$name}
Truth table33%1/3
A ||= B
A= $cleanB= $clean_transport_object{$name}
ABresult
1X1
011
000
366 }
367 8 ▶ $clean and delete $self->{_transport_object};
Branch: 1 branch
BranchTrueFalseTotal
$clean and delete $self->{"_transport_object"} 0 8 8
368 8 return $self;
369 }
370  
371 }
372  
373 2 sub _set_hook {
374 0 ▶ my ( $self, $name, $val ) = @_;
Subroutine _set_hook: not called CC=2 SCAR=17.9
375 0 ▶ CODE_REF eq ref $val
Branch: 1 branch
BranchTrueFalseTotal
"CODE" eq ref $val or croak("Attribute $name must be a code reference") 0 0 0
376 or croak "Attribute $name must be a code reference";
377 0 return( $self->{$name} = $val );
378 }
379  
380 sub _set_integer_or_undef {
381 5 ▶ my ($self, $name, $val) = @_;
Subroutine _set_integer_or_undef: called CC=3 SCAR=11.0
382 5 ▶ (defined $val && $val !~ m/ \A \d+ \z /smx)
Branch: 1 branch
BranchTrueFalseTotal
defined $val and not $val =~ / \A \d+ \z /msx and croak("Attribute $name must be an unsigned integer or undef") 1 4 5
Condition100%3/3
defined $val and not $val =~ / \A \d+ \z /msx
!ll&&!rl&&r
131
MC/DC100%2/2
defined $val and not $val =~ / \A \d+ \z /msx
defined $val not $val =~ / \A \d+ \z /msx
Truth table100%3/3
A and not(B)
A= defined $valB= not $val =~ / \A \d+ \z /msx
ABresult
0X0
100
111
383 and croak "Attribute $name must be an unsigned integer or undef";
384 4 return ($self->{$name} = $val);
385 }
386  
387 sub _set_literal {
388 5 ▶ return $_[0]{$_[1]} = $_[2];
Subroutine _set_literal: called CC=1 SCAR=0.0
389 }
390  
391 sub _set_throttle {
392 0 ▶ my ( $self, $name, $val ) = @_;
Subroutine _set_throttle: not called CC=7 SCAR=40.3
393 0 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
394 0 ▶ looks_like_number( $val )
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($val) and $val >= 0 or croak("The $name attribute must be undef or a ", "non-negative number") 0 0 0
Condition0%0/3
&looks_like_number($val) and $val >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
&looks_like_number($val) and $val >= 0
&looks_like_number($val) $val >= 0
Truth table0%0/3
A and B
A= &looks_like_number($val)B= $val >= 0
ABresult
0X0
100
111
395 and $val >= 0
396 or croak "The $name attribute must be undef or a ",
397 'non-negative number';
398 0 ▶ $using_time_hires
Branch: 1 branch
BranchTrueFalseTotal
$using_time_hires or $val >= 1 or $val == 0 or $val = 1 0 0 0
Condition0%0/6
$using_time_hires or $val >= 1 or $val == 0
l!l&&r!l&&!r
000
$using_time_hires or $val >= 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$using_time_hires or $val >= 1 or $val == 0
$using_time_hires $val >= 1 $val == 0
Truth table0%0/4
A or B or C
A= $using_time_hiresB= $val >= 1C= $val == 0
ABCresult
1XX1
01X1
0011
0000
399 or $val >= 1
400 or $val == 0
401 or $val = 1;
402 } else {
403 0 $val = 0;
404 }
405 0 return( $self->{$name} = $val );
406 }
407  
408 sub _set_unsigned_integer {
409 0 ▶ my ($self, $name, $val) = @_;
Subroutine _set_unsigned_integer: not called CC=3 SCAR=24.8
410 0 ▶ ( !defined $val || $val !~ m/ \A \d+ \z /smx )
Branch: 1 branch
BranchTrueFalseTotal
defined $val and $val =~ / \A \d+ \z /msx or croak("Attribute $name must be an unsigned integer") 0 0 0
Condition0%0/3
defined $val and $val =~ / \A \d+ \z /msx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and $val =~ / \A \d+ \z /msx
defined $val $val =~ / \A \d+ \z /msx
Truth table0%0/3
A and B
A= defined $valB= $val =~ / \A \d+ \z /msx
ABresult
0X0
100
111
411 and croak "Attribute $name must be an unsigned integer";
412 0 return ($self->{$name} = $val + 0);
413 }
414  
415 ########################################################################
416 #
417 # Private methods
418 #
419 # The author reserves the right to change these without notice.
420  
421 {
422 # NOTE to me: The deprecation of everything but 'compatible' is on
423 # hold until 'compatible' gets to 2. Then everything goes to 3
424 # together.
425 my %dep = (
426 attribute => {
427 0 ▶ dflt => sub { return },
Subroutine __ANON__: not called CC=1 SCAR=6.9
428 item => {
429 compatible => 0,
430 default_ns => 3,
431 proxy => 3,
432 source => 3,
433 use_all_limit => 3,
434 },
435 },
436 subroutine => {
437 dflt => sub {
438 0 ▶ ( my $name = ( caller( 2 ) )[3] ) =~ s/ .* :: //smx;
Subroutine __ANON__: not called CC=1 SCAR=6.9
439 0 return $name;
440 },
441 2 item => {
442 getElevation => 3,
443 getAllElevations => 3,
444 },
445 },
446 );
447  
448 sub _deprecate {
449 10 ▶ my ( $group, $item ) = @_;
Subroutine _deprecate: called CC=8 SCAR=33.4
450 10 ▶ my $info = $dep{$group}
Branch: 1 branch
BranchTrueFalseTotal
my $info = $dep{$group} or confess("Programming error - Deprecation group '${group}' unknown") 0 10 10
451 or confess "Programming error - Deprecation group '$group' unknown";
452 defined $item
453 10 ▶ or defined( $item = $info->{dflt}->() )
Branch: 1 branch
BranchTrueFalseTotal
defined $item or defined($item = $info->{"dflt"}->()) or croak("Programming error - No item default for group '${group}'") 0 10 10
Condition33%1/3
defined $item or defined($item = $info->{"dflt"}->())
l!l&&r!l&&!r
1000
MC/DC0%0/2
defined $item or defined($item = $info->{"dflt"}->())
defined $item defined($item = $info->{"dflt"}->())
Truth table33%1/3
A or B
A= defined $itemB= defined($item = $info->{"dflt"}->())
ABresult
1X1
011
000
454 or croak "Programming error - No item default for group '$group'";
455 10 ▶ $info->{item}{$item}
Branch: 1 branch
BranchTrueFalseTotal
$info->{"item"}{$item} or return 10 0 10
456 or return;
457 0 my $msg = ucfirst "$group $item is deprecated";
458 0 ▶ $info->{item}{$item} > 2
Branch: 1 branch
BranchTrueFalseTotal
$info->{"item"}{$item} > 2 and croak("Fatal - $msg") 0 0 0
459 and croak "Fatal - $msg";
460 0 ▶ warnings::enabled( 'deprecated' )
Branch: 1 branch
BranchTrueFalseTotal
warnings::enabled("deprecated") or return 0 0 0
461 or return;
462 0 carp "Warning - $msg";
463 $info->{item}{$item} == 1
464 0 ▶ and $info->{item}{$item} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$info->{"item"}{$item} == 1 and $info->{"item"}{$item} = 0 0 0 0
465 0 return;
466 }
467 }
468  
469 # $ele->_error($text);
470 #
471 # Set the error attribute, and croak if the croak attribute is
472 # true. If croak is false, just return, carping if the carp
473 # attribute is true.
474  
475 2 sub _error {
476 0 ▶ my ($self, @args) = @_;
Subroutine _error: not called CC=3 SCAR=24.8
477 0 $self->{error} = join '', @args;
478 ## $self->{croak} and croak $self->{error};
479 0 ▶ $self->{croak} and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"croak"} and return 0 0 0
480 0 ▶ $self->{carp} and carp $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"carp"} and carp($self->{"error"}) 0 0 0
481 0 return;
482 }
483  
484 # _instance( $object, $class )
485 # and print "\$object isa $class\n";
486 #
487 # Return true if $object is an instance of class $class, and false
488 # otherwise. Unlike UNIVERSAL::isa, this is false if the first
489 # object is not a reference.
490  
491 sub _instance {
492 12 ▶ my ( $object, $class ) = @_;
Subroutine _instance: called CC=2 SCAR=6.9
493 12 ▶ blessed( $object ) or return;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($object) or return 9 3 12
494 3 return $object->isa( $class );
495 }
496  
497 # my ($self, $lat, $lon, @_) = _latlon(@_);
498 #
499 # Strip the object reference, latitude, and longitude off the
500 # argument list. If the first argument is a Geo::Point,
501 # GPS::Point, or Net::GPSD::Point object the latitude and
502 # longitude come from it. Otherwise the first argument is assumed
503 # to be latitude, and the second to be longitude.
504  
505 {
506  
507 my %known = (
508 0 ▶ 'Geo::Point' => sub {$_[0]->latlong('wgs84')},
Subroutine __ANON__: not called CC=1 SCAR=6.9
509 1 ▶ 'GPS::Point' => sub {$_[0]->latlon()},
Subroutine __ANON__: called CC=1 SCAR=0.0
510 0 ▶ 'Net::GPSD::Point' => sub {$_[0]->latlon()},
Subroutine __ANON__: not called CC=1 SCAR=6.9
511 2 );
512  
513 sub _latlon {
514 4 ▶ my ($self, $obj, @args) = @_;
Subroutine _latlon: called CC=3 SCAR=11.0
515 4 foreach my $class (keys %known) {
516 12 ▶ if (_instance( $obj, $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_instance($obj, $class)) 1 11 12
517 1 return ($self, $known{$class}->($obj), @args);
518 }
519 }
520 3 return ($self, $obj, @args);
521 }
522 }
523  
524 {
525 2 my %static = ( # Static attribute values.
526 throttle => 0,
527 );
528  
529 # $self->_no_static_attr( $name );
530 #
531 # Croaks if the invocant is not a reference. The message assumes
532 # the method was called trying to access an attribute, whose name
533 # is $name.
534  
535 sub _no_static_attr {
536 41 ▶ my ( $self, $name ) = @_;
Subroutine _no_static_attr: called CC=2 SCAR=7.1
537 41 ▶ ref $self
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Attribute $name may not be accessed statically") 0 41 41
538 or croak "Attribute $name may not be accessed statically";
539 41 return $self;
540 }
541  
542 # $self->_only_static_attr( $name );
543 #
544 # Croaks if the invocant is a reference. The message assumes the
545 # method was called trying to access an attribute, whose name is
546 # $name.
547  
548 sub _only_static_attr {
549 4 ▶ my ( $self, $name ) = @_;
Subroutine _only_static_attr: called CC=2 SCAR=7.1
550 4 ▶ ref $self
Branch: 1 branch
BranchTrueFalseTotal
ref $self and croak("Attribute $name may only be accessed statically") 0 4 4
551 and croak "Attribute $name may only be accessed statically";
552 4 return \%static;
553 }
554  
555 }
556  
557 # $rslt = $self->_request( %args );
558 #
559 # This private method requests data from the USGS' web service.
560 # The %args are the arguments for the request:
561 # {x} => longitude (West is negative)
562 # {y} => latitude (South is negative)
563 # {units} => desired units ('Meters' or 'Feet')
564 # The return is a reference to a hash containing the parsed JSON
565 # returned from the NAD server.
566  
567 2 sub _request {
568 4 ▶ my ( $self, %arg ) = @_;
Subroutine _request: called CC=15 SCAR=32.8
569  
570 # The allow_nonref() is for the benefit of {_hack_result}.
571 4 ▶ my $json = $self->{_json} ||= JSON->new()->allow_nonref();
Condition66%2/3
$self->{"_json"} ||= "JSON"->new->allow_nonref
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{"_json"} ||= "JSON"->new->allow_nonref
$self->{"_json"} "JSON"->new->allow_nonref
Truth table66%2/3
A ||= B
A= $self->{"_json"}B= "JSON"->new->allow_nonref
ABresult
1X1
011
000
572  
573 my $ua = $self->{_transport_object} ||=
574 4 ▶ LWP::UserAgent->new( timeout => $self->{timeout} );
Condition66%2/3
$self->{"_transport_object"} ||= "LWP::UserAgent"->new("timeout", $self->{"timeout"})
l!l&&r!l&&!r
310
MC/DC0%0/2
$self->{"_transport_object"} ||= "LWP::UserAgent"->new("timeout", $self->{"timeout"})
$self->{"_transport_object"} "LWP::UserAgent"->new("timeout", $self->{"timeout"})
Truth table66%2/3
A ||= B
A= $self->{"_transport_object"}B= "LWP::UserAgent"->new("timeout", $self->{"timeout"})
ABresult
1X1
011
000
575  
576 defined $arg{units}
577 4 ▶ or $arg{units} = 'Feet';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{"units"} or $arg{"units"} = "Feet" 0 4 4
578 4 ▶ $arg{units} = $arg{units} =~ m/ \A meters \z /smxi
Branch: 1 branch
BranchTrueFalseTotal
$arg{'units'} =~ / \A meters \z /imsx ? : 3 1 4
579 ? 'Meters'
580 : 'Feet';
581  
582 4 my $uri = URI->new( $self->get( 'usgs_url' ) );
583 4 $uri->query_form( \%arg );
584 4 my $rqst = HTTP::Request::Common::GET( $uri );
585  
586 $self->{trace}
587 4 ▶ and print STDERR $rqst->as_string();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"trace"} and print STDERR $rqst->as_string 0 4 4
588  
589 4 ▶ my $rslt = exists $self->{_hack_result} ? do {
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_hack_result'} ? : 0 4 4
590 0 my $data = delete $self->{_hack_result};
591 0 ▶ CODE_REF eq ref $data ? $data->( $self, %arg ) : $data;
Branch: 1 branch
BranchTrueFalseTotal
'CODE' eq ref $data ? : 0 0 0
592 } : $ua->request( $rqst );
593  
594 4 ▶ if ( $self->{trace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trace"}) 0 4 4
595 0 ▶ if ( my $redir = $rslt->request() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redir = $rslt->request) 0 0 0
596 0 print STDERR $redir->as_string();
597 }
598 0 print STDERR $rslt->as_string();
599 }
600  
601 $rslt->is_success()
602 4 ▶ or croak $rslt->status_line();
Branch: 1 branch
BranchTrueFalseTotal
$rslt->is_success or croak($rslt->status_line) 0 4 4
603  
604 {
605 4 local $@ = undef;
606 4 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $rslt = $json->decode($rslt->decoded_content); ref $rslt } } or return $self->_error($rslt->decoded_content) 0 4 4
607 4 $rslt = $json->decode( $rslt->decoded_content() );
608 4 ref $rslt;
609 } or return $self->_error( $rslt->decoded_content() );
610 }
611  
612 4 ▶ if ( $self->get( 'compatible' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get('compatible')) { } 4 0 4
613 $rslt = {
614 x => $rslt->{location}{x},
615 y => $rslt->{location}{y},
616 Data_Source => 'USGS Elevation Point Query Service',
617 Elevation => $rslt->{value},
618 Units => $arg{units},
619 4 };
620 } else {
621 0 $rslt->{Elevation} = $rslt->{value};
622 }
623  
624 =begin comment
625  
626 foreach my $key (
627 qw{ USGS_Elevation_Point_Query_Service Elevation_Query }
628 ) {
629 HASH_REF eq ref $rslt
630 and exists $rslt->{$key}
631 or return $self->_error(
632 "Elevation result is missing element {$key}" );
633 $rslt = $rslt->{$key};
634 }
635  
636 unless ( ref $rslt ) {
637 $rslt =~ s/ (?<! [.?!] ) \z /./smx;
638 return $self->_error( $rslt );
639 }
640  
641 =end comment
642  
643 =cut
644  
645 4 my $places;
646 defined $rslt->{Elevation}
647 and defined( $places = $self->get( 'places' ) )
648 4 ▶ and $rslt->{Elevation} = sprintf '%.*f', $places, $rslt->{Elevation};
Branch: 1 branch
BranchTrueFalseTotal
defined $rslt->{"Elevation"} and defined($places = $self->get("places")) and $rslt->{"Elevation"} = sprintf("%.*f", $places, $rslt->{"Elevation"}) 4 0 4
Condition33%1/3
defined $rslt->{"Elevation"} and defined($places = $self->get("places"))
!ll&&!rl&&r
004
MC/DC0%0/2
defined $rslt->{"Elevation"} and defined($places = $self->get("places"))
defined $rslt->{"Elevation"} defined($places = $self->get("places"))
Truth table33%1/3
A and B
A= defined $rslt->{"Elevation"}B= defined($places = $self->get("places"))
ABresult
0X0
100
111
649  
650 4 return $rslt;
651 }
652  
653 2 1;
654  
655 __END__
↑ Summary Build »