blib/lib/Geo/Location/Point.pm

filter: Statementstmt 89.8% 159 / 177 Branchbran 80.4% 74 / 92 Conditioncond 80.7% 46 / 57 MC/DCmcdc 43.7% 14 / 32 Subroutinesub 91.8% 34 / 37 Podpod 100% 20 / 20 totaltotal 83.6% 347 / 415 CC 67 SCAR 25.3
CC
67
Cov
86%
as_string
31.8
tz
30.0
new
11.0
CRAP 12.6
↑ Summary
1 package Geo::Location::Point;
2  
3 11 ▶use 5.10.0; # For the //= operator
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use GIS::Distance;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Params::Get 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
13 '==' => \&equal,
14 '!=' => \&not_equal,
15 '""' => \&as_string,
16 1 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 11 fallback => 1 # So that boolean tests don't cause as_string to be called
18 11 );
19  
20 =head1 NAME
21  
22 Geo::Location::Point - Location information
23  
24 =head1 VERSION
25  
26 Version 0.17
27  
28 =cut
29  
30 11 our $VERSION = '0.17';
31  
32 =head1 SYNOPSIS
33  
34 Geo::Location::Point encapsulates geographical point data with latitude and longitude.
35 It supports distance calculations,
36 comparison between points,
37 and provides various convenience methods for attributes like latitude, longitude, and related string representations.
38  
39 use Geo::Location::Point;
40  
41 my $location = Geo::Location::Point->new(latitude => 0.01, longitude => -71);
42  
43 =head1 SUBROUTINES/METHODS
44  
45 =head2 new
46  
47 Initialise a new object, accepting latitude and longitude via a hash or hash reference.
48 Takes one optional argument 'key' which is an API key for L<https://timezonedb.com> for looking up timezone data.
49  
50 $location = Geo::Location::Point->new({ latitude => 0.01, longitude => -71 });
51  
52 =cut
53  
54 41 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
55 41 my $class = shift;
56 41 my $params = Params::Get::get_params(undef, \@_);
57  
58 40 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 39 40
elsif (builtin::blessed($class)) { } 1 38 39
59 1 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
60 1 return;
61 } elsif(Scalar::Util::blessed($class)) {
62 # If $class is an object, clone it with new arguments
63 1 return bless { %{$class}, %{$params} }, ref($class);
64 }
65  
66 38 ▶ $params->{'lat'} //= $params->{'latitude'} // $params->{'Latitude'};
Condition100%6/6
$params->{"lat"} //= $params->{"latitude"} // $params->{"Latitude"}
l!l&&r!l&&!r
19172
$params->{"latitude"} // $params->{"Latitude"}
l!l&&r!l&&!r
1342
MC/DC0%0/3
$params->{"lat"} //= $params->{"latitude"} // $params->{"Latitude"}
$params->{"lat"} $params->{"latitude"} $params->{"Latitude"}
Truth table0%0/4
A //= B // C
A= $params->{"lat"}B= $params->{"latitude"}C= $params->{"Latitude"}
ABCresult
1XX1
01X1
0011
0000
67 38 ▶ if(!defined($params->{'lat'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"lat"}) 2 36 38
68 2 Carp::carp(__PACKAGE__, ': latitude not given');
69 2 return;
70 }
71 36 ▶ if(abs($params->{'lat'}) > 90) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $params->{"lat"} > 90) 3 33 36
72 3 Carp::carp(__PACKAGE__, ': ', $params->{'lat'}, ': invalid latitude');
73 3 return;
74 }
75  
76 33 ▶ $params->{'long'} //= $params->{'longitude'} // $params->{'Longitude'};
Condition100%6/6
$params->{"long"} //= $params->{"longitude"} // $params->{"Longitude"}
l!l&&r!l&&!r
16152
$params->{"longitude"} // $params->{"Longitude"}
l!l&&r!l&&!r
1232
MC/DC0%0/3
$params->{"long"} //= $params->{"longitude"} // $params->{"Longitude"}
$params->{"long"} $params->{"longitude"} $params->{"Longitude"}
Truth table0%0/4
A //= B // C
A= $params->{"long"}B= $params->{"longitude"}C= $params->{"Longitude"}
ABCresult
1XX1
01X1
0011
0000
77 33 ▶ if(!defined($params->{'long'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"long"}) 2 31 33
78 2 Carp::carp(__PACKAGE__, ': longitude not given');
79 2 return;
80 }
81 31 ▶ if(abs($params->{'long'}) > 180) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $params->{"long"} > 180) 3 28 31
82 3 Carp::carp(__PACKAGE__, ': ', $params->{'long'}, ': invalid longitude');
83 3 return;
84 }
85 28 $params->{'lng'} = $params->{'long'};
86  
87 # Return the blessed object
88 28 return bless $params, $class;
89 }
90  
91 =head2 lat
92  
93 print 'Latitude: ', $location->lat(), "\n";
94 print 'Longitude: ', $location->long(), "\n";
95  
96 =cut
97  
98 22 ▶sub lat {
Subroutine lat: called CC=1 SCAR=0.0
Pod documented
99 22 my $self = shift;
100  
101 22 return $self->{'lat'};
102 }
103  
104 =head2 latitude
105  
106 Synonym for lat().
107  
108 =cut
109  
110 1 ▶sub latitude {
Subroutine latitude: called CC=1 SCAR=0.0
Pod documented
111 1 my $self = shift;
112  
113 1 return $self->{'lat'};
114 }
115  
116 =head2 long
117  
118 print 'Latitude: ', $location->lat(), "\n";
119 print 'Longitude: ', $location->long(), "\n";
120  
121 =cut
122  
123 14 ▶sub long {
Subroutine long: called CC=1 SCAR=0.0
Pod documented
124 14 my $self = shift;
125  
126 14 return $self->{'long'};
127 }
128  
129 =head2 lng
130  
131 Synonym for long().
132  
133 =cut
134  
135 0 ▶sub lng {
Subroutine lng: not called CC=1 SCAR=6.9
Pod documented
136 0 my $self = shift;
137  
138 0 return $self->{'long'};
139 }
140  
141 =head2 longitude
142  
143 Synonym for long().
144  
145 =cut
146  
147 1 ▶sub longitude {
Subroutine longitude: called CC=1 SCAR=0.0
Pod documented
148 1 my $self = shift;
149  
150 1 return $self->{'long'};
151 }
152  
153 =head2 distance
154  
155 Determine the distance between two geographical points,
156 returns a L<Class::Measure::Length> object.
157  
158 =cut
159  
160 5 ▶sub distance {
Subroutine distance: called CC=3 SCAR=11.0
Pod documented
161 5 my ($self, $location) = @_;
162  
163 5 ▶ if(!defined($location)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $location) 1 4 5
164 1 Carp::carp('Usage: ', __PACKAGE__, '->distance($location)');
165 1 return;
166 }
167  
168 4 ▶ $self->{'gis'} //= GIS::Distance->new();
Condition66%2/3
$self->{"gis"} //= "GIS::Distance"->new
l!l&&r!l&&!r
130
MC/DC0%0/2
$self->{"gis"} //= "GIS::Distance"->new
$self->{"gis"} "GIS::Distance"->new
Truth table66%2/3
A //= B
A= $self->{"gis"}B= "GIS::Distance"->new
ABresult
1X1
011
000
169  
170 4 return $self->{'gis'}->distance($self->{'lat'}, $self->{'long'}, $location->lat(), $location->long());
171 }
172  
173 =head2 equal
174  
175 Check if two points are identical within a small tolerance.
176  
177 my $loc1 = Geo::Location::Point->new(lat => 2, long => 2);
178 my $loc2 = Geo::Location::Point->new(lat => 2, long => 2);
179 print ($loc1 == $loc2), "\n"; # Prints 1
180  
181 =cut
182  
183 8 ▶sub equal {
Subroutine equal: called CC=2 SCAR=7.0
Pod documented
184 8 my $self = shift;
185 8 my $other = shift;
186  
187 # return ($self->distance($other) <= 1e-5);
188 # 1e-5 is about 1m, that's tolerant enough
189 8 ▶ return((abs($self->lat() - $other->lat()) <= 1e-5) && (abs(($self->long() - $other->long())) <= 1e-5));
Condition66%2/3
abs $self->lat - $other->lat <= 1e-05 && abs $self->long - $other->long <= 1e-05
!ll&&!rl&&r
404
MC/DC50%1/2
abs $self->lat - $other->lat <= 1e-05 && abs $self->long - $other->long <= 1e-05
abs $self->lat - $other->lat <= 1e-05 abs $self->long - $other->long <= 1e-05
Truth table66%2/3
A && B
A= abs $self->lat - $other->lat <= 1e-05B= abs $self->long - $other->long <= 1e-05
ABresult
0X0
100
111
190 }
191  
192 =head2 not_equal
193  
194 Are two points different?
195  
196 my $loc1 = Geo::Location::Point->new(lat => 2, long => 2);
197 my $loc2 = Geo::Location::Point->new(lat => 2, long => 2);
198 print ($loc1 != $loc2), "\n"; # Prints 0
199  
200 =cut
201  
202 4 ▶sub not_equal {
Subroutine not_equal: called CC=1 SCAR=0.0
Pod documented
203 4 my $self = shift;
204  
205 4 return(!$self->equal(shift));
206 }
207  
208 =head2 tz
209  
210 Returns the timezone of the location.
211 Needs L<TimeZone::TimeZoneDB>.
212  
213 =cut
214  
215 0 ▶sub tz {
Subroutine tz: not called CC=5 SCAR=34.0
Pod documented
216 0 my $self = shift;
217  
218 0 ▶ if(defined($self->{'key'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"key"}) 0 0 0
219 0 ▶ return $self->{'tz'} if(defined($self->{'tz'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tz"} 0 0 0
220  
221 0 ▶ if(!defined($self->{'timezonedb'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"timezonedb"}) 0 0 0
222 0 ▶ unless(TimeZone::TimeZoneDB->can('get_time_zone')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("TimeZone::TimeZoneDB"->can("get_time_zone")) 0 0 0
223 0 require TimeZone::TimeZoneDB;
224 0 TimeZone::TimeZoneDB->import();
225 }
226  
227 0 $self->{'timezonedb'} = TimeZone::TimeZoneDB->new(key => $self->{'key'});
228 }
229 0 $self->{'tz'} = $self->{'timezonedb'}->get_time_zone($self)->{'zoneName'};
230  
231 0 return $self->{'tz'};
232 }
233 }
234  
235 =head2 timezone
236  
237 Synonym for tz().
238  
239 =cut
240  
241 0 ▶sub timezone {
Subroutine timezone: not called CC=1 SCAR=6.9
Pod documented
242 0 my $self = shift;
243  
244 0 return $self->tz();
245 }
246  
247 =head2 as_string
248  
249 Generate a human-readable string describing the point,
250 incorporating additional attributes like city or country if available.
251  
252 =cut
253  
254 8 ▶sub as_string {
Subroutine as_string: called CC=27 SCAR=34.6
Pod documented
255 8 my $self = shift;
256  
257 8 ▶ if($self->{'location'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"location"}) 1 7 8
258 1 return $self->{'location'};
259 }
260  
261 7 my $rc = $self->{'name'};
262 7 ▶ if($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 7 7
263 0 $rc = ucfirst(lc($rc));
264 }
265  
266 # TODO: make this order configurable
267 # foreach my $field('house_number', 'number', 'road', 'street', 'AccentCity', 'city', 'county', 'region', 'state_district', 'state', 'country') {
268 7 foreach my $field('house_number', 'number', 'road', 'street', 'city', 'county', 'region', 'state_district', 'state', 'country') {
269 70 ▶ if(my $value = ($self->{$field} || $self->{ucfirst($field)})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $self->{$field} || $$self{ucfirst $field}) 18 52 70
Condition100%3/3
$self->{$field} || $$self{ucfirst $field}
l!l&&r!l&&!r
12652
MC/DC100%2/2
$self->{$field} || $$self{ucfirst $field}
$self->{$field} $$self{ucfirst $field}
Truth table100%3/3
A || B
A= $self->{$field}B= $$self{ucfirst $field}
ABresult
1X1
011
000
270 18 ▶ if($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 12 6 18
271 12 ▶ if(($field eq 'street') || ($field eq 'road')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'street' or $field eq 'road') { } 2 10 12
Condition66%2/3
$field eq "street" or $field eq "road"
l!l&&r!l&&!r
2010
MC/DC50%1/2
$field eq "street" or $field eq "road"
$field eq "street" $field eq "road"
Truth table66%2/3
A or B
A= $field eq "street"B= $field eq "road"
ABresult
1X1
011
000
272 2 ▶ if($self->{'number'} || $self->{'house_number'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'number'} or $self->{'house_number'}) { } 2 0 2
Condition33%1/3
$self->{"number"} or $self->{"house_number"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->{"number"} or $self->{"house_number"}
$self->{"number"} $self->{"house_number"}
Truth table33%1/3
A or B
A= $self->{"number"}B= $self->{"house_number"}
ABresult
1X1
011
000
273 2 $rc .= ' ';
274 } else {
275 0 $rc .= ', '
276 }
277 } else {
278 10 $rc .= ', ';
279 }
280 }
281 18 my $leave_case = 0;
282 18 ▶ if(my $country = $self->{'country'} // $self->{'Country'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $country = $self->{"country"} // $self->{"Country"}) 17 1 18
Condition100%3/3
$self->{"country"} // $self->{"Country"}
l!l&&r!l&&!r
1161
MC/DC100%2/2
$self->{"country"} // $self->{"Country"}
$self->{"country"} $self->{"Country"}
Truth table100%3/3
A // B
A= $self->{"country"}B= $self->{"Country"}
ABresult
1X1
011
000
283 17 ▶ if(uc($country) eq 'US') {
Branch: 3 branches
BranchTrueFalseTotal
if (uc $country eq 'US') { } 14 3 17
elsif ($country eq 'Canada' or $country eq 'Australia') { } 0 3 3
elsif (uc $country eq 'GB') { } 3 0 3
284 14 ▶ if(($field eq 'state') || ($field eq 'region') || ($field eq 'country')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "state" or $field eq "region" or $field eq "country") 6 8 14
Condition100%6/6
$field eq "state" or $field eq "region" or $field eq "country"
l!l&&r!l&&!r
248
$field eq "state" or $field eq "region"
l!l&&r!l&&!r
1112
MC/DC100%3/3
$field eq "state" or $field eq "region" or $field eq "country"
$field eq "state" $field eq "region" $field eq "country"
Truth table100%4/4
A or B or C
A= $field eq "state"B= $field eq "region"C= $field eq "country"
ABCresult
1XX1
01X1
0011
0000
285 6 $leave_case = 1;
286 6 ▶ if(lc($field) eq 'country') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $field eq "country") 4 2 6
287 4 $value = 'US';
288 }
289 }
290 ▶ } elsif(($country eq 'Canada') || ($country eq 'Australia')) {
Condition33%1/3
$country eq "Canada" or $country eq "Australia"
l!l&&r!l&&!r
003
MC/DC0%0/2
$country eq "Canada" or $country eq "Australia"
$country eq "Canada" $country eq "Australia"
Truth table33%1/3
A or B
A= $country eq "Canada"B= $country eq "Australia"
ABresult
1X1
011
000
291 0 ▶ if($field eq 'state') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "state") 0 0 0
292 0 $leave_case = 1;
293 }
294 } elsif(uc($country) eq 'GB') {
295 3 ▶ if($field eq 'country') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "country") 1 2 3
296 1 $leave_case = 1;
297 1 $value = 'GB';
298 }
299 }
300 }
301 18 ▶ if($leave_case) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leave_case) { } 7 11 18
302 7 $rc .= $value;
303 } else {
304 11 $rc .= $self->_sortoutcase($value);
305 11 ▶ if((($field eq 'street') || ($field eq 'road')) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "street" || $field eq "road" and $rc =~ /(.+)\s([NS][ew])$/) 2 9 11
Condition66%4/6
$field eq "street" || $field eq "road" and $rc =~ /(.+)\s([NS][ew])$/
!ll&&!rl&&r
902
$field eq "street" or $field eq "road"
l!l&&r!l&&!r
209
MC/DC33%1/3
$field eq "street" || $field eq "road" and $rc =~ /(.+)\s([NS][ew])$/
$field eq "street" $field eq "road" $rc =~ /(.+)\s([NS][ew])$/
Truth table40%2/5
A or B and C
A= $field eq "street"B= $field eq "road"C= $rc =~ /(.+)\s([NS][ew])$/
ABCresult
00X0
1X00
0100
1X11
0111
306 ($rc =~ /(.+)\s([NS][ew])$/)) {
307 # e.g South Street NW
308 2 $rc = "$1 " . uc($2);
309 }
310 }
311 }
312 }
313  
314 7 return $self->{'location'} = $rc;
315 }
316  
317 sub _sortoutcase
318 11 ▶{
Subroutine _sortoutcase: called CC=1 SCAR=0.0
319 # Use lc to ensure the input string is in lowercase before capitalisation,
320 # split to break the string into words,
321 # map to capitalise each word and
322 # join to concatenate the capitalised words back into a single string with spaces
323 11 return join ' ', map { ucfirst } split ' ', lc($_[1]);
324 }
325  
326 =head2 as_uri
327  
328 Convert the point to a Geo URI scheme string (geo:latitude,longitude).
329 See L<https://en.wikipedia.org/wiki/Geo_URI_scheme>.
330 Arguably it should return a L<URI> object instead.
331  
332 =cut
333  
334 sub as_uri
335 2 ▶{
Subroutine as_uri: called CC=1 SCAR=0.0
Pod documented
336 2 my $self = shift;
337  
338 2 return 'geo:' . $self->{'lat'} . ',' . $self->{'long'};
339 }
340  
341 =head2 attr
342  
343 Get or set arbitrary attributes, such as city or country.
344 A call with an argument sets the attribute and returns the value that was set;
345 a call without one reads it back and returns C<undef> if it was never set.
346  
347 $location->city('London');
348 $location->country('UK');
349 print $location->as_string(), "\n";
350 print "$location\n"; # Calls as_string
351  
352 It is the argument list, not the value, that decides whether a call is a read or a write,
353 so false values are attributes like any other and are stored and returned unchanged:
354  
355 $location->confidence(0);
356 print 'matched: ', defined($location->confidence()) && $location->confidence(), "\n";
357  
358 The upper case variant of the key is consulted
359 (but not overwritten) when the key as given has never been set,
360 which is how the MaxMind field names are read. An attribute that has been set,
361 however false, takes precedence over it.
362  
363 =cut
364  
365 36 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
366 36 our $AUTOLOAD;
367 36 my $key = $AUTOLOAD;
368  
369 36 $key =~ s/.*:://;
370  
371 36 ▶ return if($key eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "DESTROY" 29 7 36
372  
373 7 my $self = shift;
374  
375 # The argument list, not the value, decides read or write. Testing the value here
376 # (my $value = shift) meant a false value could neither be stored nor read back,
377 # so e.g. a confidence of 0 looked like no data at all.
378 7 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 6 7
379 1 my $value = shift;
380 1 delete $self->{'location'}; # Invalidate the cache
381 1 delete $self->{'tz'}; # Invalidate the cache
382 1 return $self->{$key} = $value;
383 }
384  
385 # Defined, not true: a stored 0 or empty string is the caller's data, so return it
386 6 ▶ return $self->{$key} if(defined($self->{$key}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$key} 5 1 6
387  
388 1 return $self->{ucfirst($key)};
389 }
390  
391 # The accuracy levels a point may claim, most precise first. This is the one
392 # place the vocabulary is written down: Geo::Coder::Free::Provenance reads it
393 # from here instead of keeping a copy of its own, so a level added to one of
394 # them cannot go missing from the other. It is defined here because
395 # Geo::Coder::Free depends on this module, and not the other way round.
396 11 ▶use constant ACCURACY_LEVELS => [qw(address street postcode locality city region country)];
Subroutine BEGIN: called CC=1 SCAR=0.0
397  
398 11 my %IS_ACCURACY_LEVEL = map { $_ => 1 } @{ACCURACY_LEVELS()};
399  
400 # Read a slot the way AUTOLOAD reads it: a lower case key that was never set
401 # falls back to its upper case spelling, so a field the caller stored under the
402 # upper case name is still found.
403 20 ▶sub _attribute {
Subroutine _attribute: called CC=2 SCAR=6.9
404 20 my ($self, $key) = @_;
405  
406 20 ▶ return $self->{$key} if(defined($self->{$key}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$key} 15 5 20
407  
408 5 return $self->{ucfirst($key)};
409 }
410  
411 # Write a slot the way AUTOLOAD writes it, invalidating the caches derived from
412 # the attributes: a validated accessor has to keep that promise too, or setting
413 # a city after reading as_string() would be invisible.
414 28 ▶sub _set_attribute {
Subroutine _set_attribute: called CC=1 SCAR=0.0
415 28 my ($self, $key, $value) = @_;
416  
417 28 delete $self->{'location'};
418 28 delete $self->{'tz'};
419  
420 28 return $self->{$key} = $value;
421 }
422  
423 # Passing undef clears an attribute, so every validated accessor accepts it;
424 # anything else has to be the type the field promises.
425 12 ▶sub _check_string {
Subroutine _check_string: called CC=3 SCAR=11.0
426 12 my ($field, $value) = @_;
427  
428 12 ▶ return if(!defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 12 12
429 12 ▶ Carp::croak(__PACKAGE__, ": $field must be a string, not a ", ref($value)) if(ref($value));
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 11 12
430  
431 11 return $value;
432 }
433  
434 9 ▶sub _check_confidence {
Subroutine _check_confidence: called CC=7 SCAR=19.5
435 9 my ($value) = @_;
436  
437 9 ▶ return if(!defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
438 8 ▶ Carp::croak(__PACKAGE__, ': confidence must be a number from 0 to 1, not ',
Branch: 2 branches
BranchTrueFalseTotal
if ref $value or not &Scalar::Util::looks_like_number($value) or $value < 0 or $value > 1 3 5 8
ref $value ? : 0 3 3
Condition88%8/9
ref $value or not &Scalar::Util::looks_like_number($value) or $value < 0 or $value > 1
l!l&&r!l&&!r
215
ref $value or not &Scalar::Util::looks_like_number($value) or $value < 0
l!l&&r!l&&!r
116
ref $value or not &Scalar::Util::looks_like_number($value)
l!l&&r!l&&!r
017
MC/DC75%3/4
ref $value or not &Scalar::Util::looks_like_number($value) or $value < 0 or $value > 1
ref $value not &Scalar::Util::looks_like_number($value) $value < 0 $value > 1
Truth table80%4/5
A or not(B) or C or D
A= ref $valueB= not &Scalar::Util::looks_like_number($value)C= $value < 0D= $value > 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
439 ref($value) ? ref($value) : $value)
440 if(ref($value) || !Scalar::Util::looks_like_number($value) || ($value < 0) || ($value > 1));
441  
442 5 return $value;
443 }
444  
445 13 ▶sub _check_accuracy {
Subroutine _check_accuracy: called CC=5 SCAR=16.3
446 13 my ($value) = @_;
447  
448 13 ▶ return if(!defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 12 13
449 Carp::croak(__PACKAGE__, ': unknown accuracy: ', ref($value) ? ref($value) : $value)
450 12 ▶ if(ref($value) || !$IS_ACCURACY_LEVEL{$value});
Branch: 2 branches
BranchTrueFalseTotal
if ref $value or not $IS_ACCURACY_LEVEL{$value} 2 10 12
ref $value ? : 0 2 2
Condition66%2/3
ref $value or not $IS_ACCURACY_LEVEL{$value}
l!l&&r!l&&!r
0210
MC/DC50%1/2
ref $value or not $IS_ACCURACY_LEVEL{$value}
ref $value not $IS_ACCURACY_LEVEL{$value}
Truth table66%2/3
A or not(B)
A= ref $valueB= not $IS_ACCURACY_LEVEL{$value}
ABresult
1X1
011
000
451  
452 10 return $value;
453 }
454  
455 =head2 geocoder
456  
457 The distribution that produced the point, such as C<Geo::Coder::Free>.
458  
459 $location->geocoder('Geo::Coder::Free');
460  
461 =cut
462  
463 2 ▶sub geocoder {
Subroutine geocoder: called CC=2 SCAR=6.9
Pod documented
464 2 my $self = shift;
465  
466 2 ▶ return $self->_attribute('geocoder') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 1 2
467  
468 1 return $self->_set_attribute('geocoder', _check_string('geocoder', shift));
469 }
470  
471 =head2 database
472  
473 The database the distribution answered from, such as C<OpenAddresses>, C<Local>
474 or C<MaxMind>.
475  
476 $location->database('OpenAddresses');
477  
478 =cut
479  
480 1 ▶sub database {
Subroutine database: called CC=2 SCAR=7.1
Pod documented
481 1 my $self = shift;
482  
483 1 ▶ return $self->_attribute('database') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
484  
485 1 return $self->_set_attribute('database', _check_string('database', shift));
486 }
487  
488 =head2 confidence
489  
490 How good the match is, from C<0> to C<1> inclusive, where C<1> is an exact
491 match. A confidence that is not a number, or that falls outside that range, is
492 rejected rather than stored.
493  
494 $location->confidence(0); # Tried and rejected
495 $location->confidence(0.8); # A good match
496  
497 =cut
498  
499 18 ▶sub confidence {
Subroutine confidence: called CC=2 SCAR=6.9
Pod documented
500 18 my $self = shift;
501  
502 18 ▶ return $self->_attribute('confidence') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 9 9 18
503  
504 9 return $self->_set_attribute('confidence', _check_confidence(shift));
505 }
506  
507 =head2 ACCURACY_LEVELS
508  
509 The accuracy levels a point may claim, most precise first: C<address> (a house
510 number), C<street>, C<postcode>, C<locality>, C<city>, C<region> and
511 C<country>.
512  
513 This is the vocabulary, written down once here and read from here by
514 L<Geo::Coder::Free::Provenance> rather than copied, so the levels a point
515 accepts and the levels a backend reports cannot drift apart. C<accuracy()>
516 accepts these and refuses anything else.
517  
518 my $levels = Geo::Location::Point::ACCURACY_LEVELS();
519  
520 =cut
521  
522 =head2 accuracy
523  
524 How precise the coordinates behind a match can claim to be. The levels are
525 those in L</ACCURACY_LEVELS>; anything else is rejected, because a reader
526 deciding what a point may claim about itself has to be able to trust the
527 value.
528  
529 $location->accuracy('city');
530  
531 =cut
532  
533 17 ▶sub accuracy {
Subroutine accuracy: called CC=2 SCAR=6.9
Pod documented
534 17 my $self = shift;
535  
536 17 ▶ return $self->_attribute('accuracy') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 13 17
537  
538 13 return $self->_set_attribute('accuracy', _check_accuracy(shift));
539 }
540  
541 =head2 country
542  
543 The country of the point, as the data spells it: C<GB>, C<US>, C<Canada>. It
544 is stored as given rather than normalised, so it is whatever the backend
545 matched.
546  
547 $location->country('GB');
548  
549 =cut
550  
551 6 ▶sub country {
Subroutine country: called CC=2 SCAR=6.9
Pod documented
552 6 my $self = shift;
553  
554 6 ▶ return $self->_attribute('country') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 2 6
555  
556 2 return $self->_set_attribute('country', _check_string('country', shift));
557 }
558  
559 =head2 city
560  
561 The city, town or similar locality of the point.
562  
563 $location->city('London');
564  
565 =cut
566  
567 5 ▶sub city {
Subroutine city: called CC=2 SCAR=6.9
Pod documented
568 5 my $self = shift;
569  
570 5 ▶ return $self->_attribute('city') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 4 5
571  
572 4 return $self->_set_attribute('city', _check_string('city', shift));
573 }
574  
575 =head2 state
576  
577 The state, province or region of the point.
578  
579 $location->state('KY');
580  
581 =cut
582  
583 5 ▶sub state {
Subroutine state: called CC=2 SCAR=6.9
Pod documented
584 5 my $self = shift;
585  
586 5 ▶ return $self->_attribute('state') unless(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 4 5
587  
588 4 return $self->_set_attribute('state', _check_string('state', shift));
589 }
590  
591 =head1 SUPPORT
592  
593 This module is provided as-is without any warranty.
594  
595 =head1 AUTHOR
596  
597 Nigel Horne, C<< <njh at nigelhorne.com> >>
598  
599 =head1 BUGS
600  
601 There is no validation on the attribute in the AUTOLOAD method,
602 so typos such as "citty" will not be caught.
603 C<geocoder>, C<database>, C<confidence>, C<accuracy>, C<country>, C<city> and
604 C<state> have documented accessors of their own that do check what they are
605 given,
606 but they are the only attributes that do,
607 and C<new()> stores whatever it is handed without checking it,
608 so a provenance field set through the constructor can still be any value.
609  
610 =head1 SEE ALSO
611  
612 =over 4
613  
614 =item * L<Test Dashboard|https://nigelhorne.github.io/Geo-Location-Point/coverage/>
615  
616 =back
617  
618 L<GIS::Distance>,
619 L<Geo::Point>,
620 L<TimeZone::TimeZoneDB>.
621  
622 =head1 LICENSE AND COPYRIGHT
623  
624 Copyright 2019-2026 Nigel Horne.
625  
626 Usage is subject to the GPL2 licence terms.
627 If you use it,
628 please let me know.
629  
630 =cut
631  
632 11 1;
↑ Summary