blib/lib/Geo/Location/Point.pm

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