Geo/Hashing.pm

filter: Statementstmt 89.0% 122 / 137 Branchbran 67.1% 47 / 70 Conditioncond 24.2% 8 / 33 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 71.3% 202 / 283 CC 48 SCAR 28.0
CC
48
Cov
74%
use_30w_rule
48.7
lat
21.3
lon
13.4
CRAP 16.4
↑ Summary Geo/Hashing/Source/Peeron.pm »
1 #!/usr/bin/perl -w
2 #
3 # $Id: Hashing.pm 257 2008-06-25 02:02:19Z dan $
4 #
5  
6 package Geo::Hashing;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.06';
14  
15 =head1 NAME
16  
17 Geo::Hashing - Perl library to calculate Geohashing points
18  
19 =head1 SYNOPSIS
20  
21 use Geo::Hashing;
22 my $g = new Geo::Hashing(lat => 37, lon => -122, date => "2008-05-24");
23 printf "Today's location is at %.6f, %.6f.\n", $g->lat, $g->lon;
24  
25 =head1 DESCRIPTION
26  
27 This module allows calculating the locaiton of Geohashes as described
28 in http://wiki.xkcd.com/geohashing/Main_Page.
29  
30 =head1 METHODS
31  
32 =cut
33  
34 =head2 new
35  
36 Create a new Geo::Hashing object.
37  
38 =cut
39  
40 3 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
41 3 my $class = shift;
42 3 my %p = @_;
43  
44 3 my $self = {_lat => 0, _lon => 0, _dlat => 0, _dlon => 0, _debug => 0};
45 3 bless $self, $class;
46  
47 3 $self->{_date} = sprintf("%04d-%02d-%02d", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3]);
48  
49 {
50 1 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 foreach (qw/debug source lat lon date use_30w_rule djia/) {
52 21 ▶ if (exists $p{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{$_}) 7 14 21
53 7 $self->_log("Setting $_ to $p{$_}");
54 7 $self->$_($p{$_});
55 }
56 }
57 }
58  
59 3 ▶ unless ($p{source}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"source"}) 2 1 3
60 2 $self->source('peeron');
61 }
62  
63 3 $self->_log("Using", $self->source, "as the DJIA source");
64  
65 3 return $self;
66 }
67  
68 =head2 lat
69  
70 Set or get the points latitude. When settings, only the integer portion is
71 considered. Set to undef to just get the offset.
72  
73 =cut
74  
75 7 ▶sub lat {
Subroutine lat: called CC=10 SCAR=30.6
Pod documented
76 7 my $self = shift;
77 7 my $lat = shift;
78  
79 7 ▶ if (defined $lat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lat) 2 5 7
80 2 ▶ if ($lat =~ /^-?\d+(?:\.\d+)?$/ and $lat > -180 and $lat < 180) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lat =~ /^-?\d+(?:\.\d+)?$/ and $lat > -180 and $lat < 180) { } 2 0 2
Condition33%2/6
$lat =~ /^-?\d+(?:\.\d+)?$/ and $lat > -180 and $lat < 180
!ll&&!rl&&r
002
$lat =~ /^-?\d+(?:\.\d+)?$/ and $lat > -180
!ll&&!rl&&r
002
MC/DC0%0/3
$lat =~ /^-?\d+(?:\.\d+)?$/ and $lat > -180 and $lat < 180
$lat =~ /^-?\d+(?:\.\d+)?$/ $lat > -180 $lat < 180
Truth table25%1/4
A and B and C
A= $lat =~ /^-?\d+(?:\.\d+)?$/B= $lat > -180C= $lat < 180
ABCresult
0XX0
10X0
1100
1111
81 2 ▶ $self->{_lat} = $lat ne "-0" ? int($lat) : "-0";
Branch: 1 branch
BranchTrueFalseTotal
$lat ne '-0' ? : 1 1 2
82 2 $self->_update();
83 } else {
84 0 croak "Invalid latitude ($lat)!";
85 }
86 }
87  
88 7 ▶ return undef unless defined $self->{_dlat} and defined $self->{_dlon};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_dlat"} and defined $self->{"_dlon"} 7 0 7
Condition33%1/3
defined $self->{"_dlat"} and defined $self->{"_dlon"}
!ll&&!rl&&r
700
MC/DC0%0/2
defined $self->{"_dlat"} and defined $self->{"_dlon"}
defined $self->{"_dlat"} defined $self->{"_dlon"}
Truth table33%1/3
A and B
A= defined $self->{"_dlat"}B= defined $self->{"_dlon"}
ABresult
0X0
100
111
89  
90 return $self->{_lat} eq "-0" || $self->{_lat} < 0 ?
91 $self->{_lat} - $self->{_dlat} :
92 0 ▶ $self->{_lat} + $self->{_dlat};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_lat'} eq '-0' || $self->{'_lat'} < 0 ? : 0 0 0
Condition0%0/3
$self->{"_lat"} eq "-0" or $self->{"_lat"} < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_lat"} eq "-0" or $self->{"_lat"} < 0
$self->{"_lat"} eq "-0" $self->{"_lat"} < 0
Truth table0%0/3
A or B
A= $self->{"_lat"} eq "-0"B= $self->{"_lat"} < 0
ABresult
1X1
011
000
93 }
94  
95 =head2 lon
96  
97 Set or get the points longitude. When settings, only the integer portion is
98 considered. Set to undef to just get the offset.
99 =cut
100  
101 31 ▶sub lon {
Subroutine lon: called CC=10 SCAR=25.9
Pod documented
102 31 my $self = shift;
103 31 my $lon = shift;
104  
105 31 ▶ if (defined $lon) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lon) 3 28 31
106 3 ▶ if ($lon =~ /^-?\d+(?:\.\d+)?$/ and $lon > -180 and $lon < 180) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lon =~ /^-?\d+(?:\.\d+)?$/ and $lon > -180 and $lon < 180) { } 3 0 3
Condition33%2/6
$lon =~ /^-?\d+(?:\.\d+)?$/ and $lon > -180 and $lon < 180
!ll&&!rl&&r
003
$lon =~ /^-?\d+(?:\.\d+)?$/ and $lon > -180
!ll&&!rl&&r
003
MC/DC0%0/3
$lon =~ /^-?\d+(?:\.\d+)?$/ and $lon > -180 and $lon < 180
$lon =~ /^-?\d+(?:\.\d+)?$/ $lon > -180 $lon < 180
Truth table25%1/4
A and B and C
A= $lon =~ /^-?\d+(?:\.\d+)?$/B= $lon > -180C= $lon < 180
ABCresult
0XX0
10X0
1100
1111
107 3 ▶ $self->{_lon} = $lon ne "-0" ? int($lon) : "-0";
Branch: 1 branch
BranchTrueFalseTotal
$lon ne '-0' ? : 2 1 3
108 3 $self->_update();
109 } else {
110 0 croak "Invalid longitude ($lon)!";
111 }
112 }
113  
114 31 ▶ return undef unless defined $self->{_dlat} and defined $self->{_dlon};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_dlat"} and defined $self->{"_dlon"} 23 8 31
Condition66%2/3
defined $self->{"_dlat"} and defined $self->{"_dlon"}
!ll&&!rl&&r
2308
MC/DC50%1/2
defined $self->{"_dlat"} and defined $self->{"_dlon"}
defined $self->{"_dlat"} defined $self->{"_dlon"}
Truth table66%2/3
A and B
A= defined $self->{"_dlat"}B= defined $self->{"_dlon"}
ABresult
0X0
100
111
115  
116 return $self->{_lon} eq "-0" || $self->{_lon} < 0 ?
117 $self->{_lon} - $self->{_dlon} :
118 8 ▶ $self->{_lon} + $self->{_dlon};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_lon'} eq '-0' || $self->{'_lon'} < 0 ? : 0 8 8
Condition33%1/3
$self->{"_lon"} eq "-0" or $self->{"_lon"} < 0
l!l&&r!l&&!r
008
MC/DC0%0/2
$self->{"_lon"} eq "-0" or $self->{"_lon"} < 0
$self->{"_lon"} eq "-0" $self->{"_lon"} < 0
Truth table33%1/3
A or B
A= $self->{"_lon"} eq "-0"B= $self->{"_lon"} < 0
ABresult
1X1
011
000
119 }
120  
121 =head2 date
122  
123 Set or get the date used for the calculation. Note that this is the actual
124 date of the meetup in question, even when the 30w rule is in effect.
125 =cut
126  
127 42 ▶sub date {
Subroutine date: called CC=3 SCAR=11.1
Pod documented
128 42 my $self = shift;
129 42 my $date = shift;
130  
131 42 ▶ if (defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date) 5 37 42
132 5 ▶ if ($date =~ /^\d\d\d\d-\d\d-\d\d$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^\d\d\d\d-\d\d-\d\d$/) { } 5 0 5
133 5 $self->{_date} = $date;
134 5 $self->djia(undef);
135 5 $self->_update();
136 } else {
137 0 croak "Invalid date ($date)!";
138 }
139 }
140  
141 42 return $self->{_date};
142 }
143  
144 =head2 djia
145  
146 Set or get the Dow Jones Industrial Average used for the calculation. If not
147 set, it will be automatically retrieved depending on the value of
148 $self->source. If the data cannot be retrieved, undef will be returned.
149 =cut
150  
151 21 ▶sub djia {
Subroutine djia: called CC=5 SCAR=16.6
Pod documented
152 21 my $self = shift;
153 21 my $djia = shift;
154  
155 21 ▶ if ($djia) {
Branch: 2 branches
BranchTrueFalseTotal
if ($djia) { } 0 21 21
elsif ($self->source) { } 17 4 21
156 0 ▶ if ($djia =~ /^\d+(?:\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($djia =~ /^\d+(?:\.\d+)?$/) { } 0 0 0
157 0 $self->{_djia} = $djia;
158 } else {
159 0 croak "Invalid DJIA ($djia)!";
160 }
161 } elsif ($self->source) {
162 17 my $date = $self->date;
163 17 ▶ if ($self->use_30w_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_30w_rule) 6 11 17
164 6 require Time::Local;
165 6 my ($y, $m, $d) = split /-/, $self->date;
166 6 my $time = Time::Local::timelocal(0, 0, 0, $d, $m-1, $y);
167 6 ($d, $m, $y) = (localtime($time - 24*60*60))[3,4,5];
168 6 $m++; $y += 1900;
169 6 $date = sprintf("%04d-%02d-%02d", $y, $m, $d);
170 }
171 17 $self->_log("Requesting", $self->source, "->DJIA($date)");
172 17 $self->{_djia} = $self->_get_djia($date);
173 } else {
174 4 $self->_log("No source set, can't automatically get DJIA");
175 4 return undef;
176 }
177  
178 17 return $self->{_djia};
179 }
180  
181 =head2 source
182  
183 Set the source of the DJIA opening data. Will load Geo::Hashing::Source::Name
184 and call get_djia($date). See Geo::Hashing::Source::Random for a sample.
185 =cut
186  
187 80 ▶sub source {
Subroutine source: called CC=4 SCAR=13.9
Pod documented
188 80 my $self = shift;
189 80 my $source = shift;
190  
191 80 ▶ if (defined $source) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source) 3 77 80
192 3 $self->_log("Loading source Geo::Hashing::Source::\u$source");
193 3 eval " require Geo::Hashing::Source::\u$source";
194  
195 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
196 0 croak "Failed to load Geo::Hashing::Source::\u$source: $@";
197 }
198  
199 3 $self->{_source} = $source;
200 3 $self->_update();
201 }
202  
203 80 ▶ if ($self->{_source}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_source'}) { } 76 4 80
204 76 return "Geo::Hashing::Source::" . ucfirst $self->{_source};
205 } else {
206 4 return undef;
207 }
208 }
209  
210 =head2 use_30w_rule
211  
212 Set or get the 30w flag. Will be set automatically if lon is set and is
213 greater than -30.
214 =cut
215  
216 20 ▶sub use_30w_rule {
Subroutine use_30w_rule: called CC=12 SCAR=38.9
Pod documented
217 20 my $self = shift;
218 20 my $w30 = shift;
219  
220 20 ▶ if (defined $w30) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $w30) { } 0 20 20
elsif (defined $self->lon) { } 4 16 20
221 0 ▶ $self->{_30w} = $w30 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$w30 ? : 0 0 0
222 0 $self->_update();
223 } elsif (defined $self->lon) {
224 4 ▶ if ($self->lon > -30) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lon > -30) { } 4 0 4
225 4 ▶ if (not $self->date) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->date) { } 0 4 4
226 0 $self->{_30w} = 1;
227 } else {
228 4 my ($y, $m, $d) = split /-/, $self->date;
229 4 ▶ if ($y > 2008) {
Branch: 3 branches
BranchTrueFalseTotal
if ($y > 2008) { } 4 0 4
elsif ($y == 2008 and $m > 5) { } 0 0 0
elsif ($y == 2008 and $m == 5 and $d >= 27) { } 0 0 0
230 4 $self->{_30w} = 1;
231 ▶ } elsif ($y == 2008 and $m > 5) {
Condition0%0/3
$y == 2008 and $m > 5
!ll&&!rl&&r
000
MC/DC0%0/2
$y == 2008 and $m > 5
$y == 2008 $m > 5
Truth table0%0/3
A and B
A= $y == 2008B= $m > 5
ABresult
0X0
100
111
232 0 $self->{_30w} = 1;
233 ▶ } elsif ($y == 2008 and $m == 5 and $d >= 27) {
Condition0%0/6
$y == 2008 and $m == 5 and $d >= 27
!ll&&!rl&&r
000
$y == 2008 and $m == 5
!ll&&!rl&&r
000
MC/DC0%0/3
$y == 2008 and $m == 5 and $d >= 27
$y == 2008 $m == 5 $d >= 27
Truth table0%0/4
A and B and C
A= $y == 2008B= $m == 5C= $d >= 27
ABCresult
0XX0
10X0
1100
1111
234 0 $self->{_30w} = 1;
235 } else {
236 0 $self->{_30w} = 0;
237 }
238 }
239 } else {
240 0 $self->{_30w} = 0;
241 }
242 }
243  
244 20 return $self->{_30w};
245 }
246  
247 =head2 debug
248  
249 Enable or disable diagnostic logging
250 =cut
251  
252 74 ▶sub debug {
Subroutine debug: called CC=4 SCAR=14.0
Pod documented
253 74 my $self = shift;
254 74 my $debug = shift;
255  
256 74 ▶ if (defined $debug) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug) 3 71 74
257 3 ▶ $self->{_debug} = $debug ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$debug ? : 3 0 3
258 3 ▶ $self->_log("Debug", $self->{_debug} ? "enabled" : "disabled");
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_debug'} ? : 3 0 3
259 }
260  
261 74 return $self->{_debug};
262 }
263  
264 # private methods
265 # _update - given all the information in the object, calculate the day's
266 # offsets
267 13 ▶sub _update {
Subroutine _update: called CC=3 SCAR=11.0
268 13 my $self = shift;
269  
270 13 my $djia = $self->djia;
271 13 ▶ unless (defined $djia) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $djia) 12 1 13
272 12 $self->_log("Failed to get DJIA");
273 12 $self->{_dlat} = $self->{_dlon} = undef;
274 12 return undef;
275 }
276  
277 1 $self->_log("DJIA for", $self->date, "is $djia");
278  
279 1 my $md5 = md5_hex($self->date . "-" . $djia);
280 1 $self->_log(" - md5(". $self->date ."-$djia)");
281 1 $self->_log(" - md5 = $md5");
282  
283 1 my ($md5lat, $md5lon) = (substr($md5, 0, 16), substr($md5, 16, 16));
284 1 $self->_log(" - = $md5lat, $md5lon");
285 1 ($self->{_dlat}, $self->{_dlon}) = (0, 0);
286  
287 1 ▶ while (length $md5lat) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $md5lat) 16 1 17
288 16 $self->{_dlat} += hex substr($md5lat, -1, 1, "");
289 16 $self->{_dlon} += hex substr($md5lon, -1, 1, "");
290 16 $self->{_dlat} /= 16;
291 16 $self->{_dlon} /= 16;
292 }
293  
294 1 $self->_log(" dlat, dlon => $self->{_dlat}, $self->{_dlon}");
295 }
296  
297 # _log - print out a timestampped log entry
298 71 ▶sub _log {
Subroutine _log: called CC=2 SCAR=6.9
299 71 my $self = shift;
300  
301 71 ▶ return unless $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 3 68 71
302  
303 68 print scalar localtime, " - @_\n";
304 }
305  
306 # _get_djia - call get_djia on from the current source
307 17 ▶sub _get_djia {
Subroutine _get_djia: called CC=1 SCAR=0.0
308 17 my $self = shift;
309  
310 17 $self->_log("getting DJIA from", $self->source);
311 17 return $self->source->get_djia(@_);
312 }
313  
314 =head1 SEE ALSO
315  
316 Original comic: http://www.xkcd.com/426/
317  
318 Wiki: http://wiki.xkcd.com/geohashing/Main_Page
319  
320 IRC: irc://irc.xkcd.com/geohashing
321  
322 =head1 AUTHOR
323  
324 Dan Boger, E<lt>zigdon@gmail.comE<gt>
325  
326 =head1 COPYRIGHT AND LICENSE
327  
328 Copyright (C) 2008 by Dan Boger
329  
330 This library is free software; you can redistribute it and/or modify
331 it under the same terms as Perl itself, either Perl version 5.10.0 or,
332 at your option, any later version of Perl 5 you may have available.
333  
334  
335 =cut
336  
337 1 1;
338  
↑ Summary Geo/Hashing/Source/Peeron.pm »