blib/lib/Geo/Hash/Grid.pm

filter: Statementstmt 100% 81 / 81 Branchbran 66.6% 12 / 18 Conditioncond 42.8% 9 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 78.8% 119 / 151 CC 19 SCAR 24.6
CC
19
Cov
85%
BUILD
21.3
origins
3.0
bboxes
2.0
CRAP 11.7
↑ Summary
1 package Geo::Hash::Grid;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 extends 'Geo::Hash::XS';
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  
11 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Geo::Hash::XS qw( ADJ_RIGHT ADJ_TOP );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 =head1 NAME
18  
19 Geo::Hash::Grid - Make a grid based off of GeoHashes
20  
21 =head1 VERSION
22  
23 Version 0.08
24  
25 =cut
26  
27 1 our $VERSION = '0.08';
28  
29  
30 =head1 SYNOPSIS
31  
32 Sometimes you need a simple grid to cover a geographic area. This is a pretty
33 easy way to get one that covers a geographic box and with a little spillover.
34  
35 This module inherits from Geo::Hash::XS and subclasses all it's methods.
36  
37 use Geo::Hash::Grid;
38
39 my $grid = Geo::Hash::Grid->new(
40 sw_lat => $south_west_latitude,
41 sw_lon => $south_west_longidude,
42 ne_lat => $north_east_latitude,
43 ne_lon => $north_east_longitude,
44 precision => 8,
45 );
46
47 my $hash_count = $grid->count;
48
49 my $geohash_list = $grid->hashes;
50
51 my $origin_list = $grid->origins;
52
53  
54 =head1 METHODS
55  
56 =head2 new
57  
58 Create the geohash grid that fits in a bounding box and specify the grid size.
59  
60 =over 4
61  
62 =item * sw_lat => $decimal_degrees
63  
64 Latitude of the southwest corner of bounding box
65  
66 =item * sw_lon => $decimal_degrees
67  
68 Longitude of the southwest corner of bounding box
69  
70 =item * ne_lat => $decimal_degrees
71  
72 Latitude of the northeast corner of bounding box
73  
74 =item * ne_lon => $decimal_degrees
75  
76 Longitude of the northeast corner of bounding box
77  
78 =item * precision => $integer
79  
80 Geohash precision
81  
82 =back
83  
84 =cut
85  
86 1 has 'sw_lat' => ( is => 'ro', required => 1 );
87 1 has 'sw_lon' => ( is => 'ro', required => 1 );
88 1 has 'ne_lat' => ( is => 'ro', required => 1 );
89 1 has 'ne_lon' => ( is => 'ro', required => 1 );
90 1 has 'precision' => ( is => 'ro', required => 1 );
91  
92 1 ▶sub BUILD {
Subroutine BUILD: called CC=16 SCAR=30.6
Pod undocumented
93  
94 1 my $self = shift;
95
96
97 1 ▶ croak "sw_lat attribute missing or malformed" if not defined $self->sw_lat or not looks_like_number $self->sw_lat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sw_lat and &looks_like_number(scalar $self->sw_lat) 0 1 1
Condition33%1/3
defined $self->sw_lat and &looks_like_number(scalar $self->sw_lat)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->sw_lat and &looks_like_number(scalar $self->sw_lat)
defined $self->sw_lat &looks_like_number(scalar $self->sw_lat)
Truth table33%1/3
A and B
A= defined $self->sw_latB= &looks_like_number(scalar $self->sw_lat)
ABresult
0X0
100
111
98 1 ▶ croak "sw_lon attribute missing or malformed" if not defined $self->sw_lon or not looks_like_number $self->sw_lon;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->sw_lon and &looks_like_number(scalar $self->sw_lon) 0 1 1
Condition33%1/3
defined $self->sw_lon and &looks_like_number(scalar $self->sw_lon)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->sw_lon and &looks_like_number(scalar $self->sw_lon)
defined $self->sw_lon &looks_like_number(scalar $self->sw_lon)
Truth table33%1/3
A and B
A= defined $self->sw_lonB= &looks_like_number(scalar $self->sw_lon)
ABresult
0X0
100
111
99 1 ▶ croak "ne_lat attribute missing or malformed" if not defined $self->ne_lat or not looks_like_number $self->ne_lat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ne_lat and &looks_like_number(scalar $self->ne_lat) 0 1 1
Condition33%1/3
defined $self->ne_lat and &looks_like_number(scalar $self->ne_lat)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->ne_lat and &looks_like_number(scalar $self->ne_lat)
defined $self->ne_lat &looks_like_number(scalar $self->ne_lat)
Truth table33%1/3
A and B
A= defined $self->ne_latB= &looks_like_number(scalar $self->ne_lat)
ABresult
0X0
100
111
100 1 ▶ croak "ne_lon attribute missing or malformed" if not defined $self->ne_lon or not looks_like_number $self->ne_lon;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ne_lon and &looks_like_number(scalar $self->ne_lon) 0 1 1
Condition33%1/3
defined $self->ne_lon and &looks_like_number(scalar $self->ne_lon)
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->ne_lon and &looks_like_number(scalar $self->ne_lon)
defined $self->ne_lon &looks_like_number(scalar $self->ne_lon)
Truth table33%1/3
A and B
A= defined $self->ne_lonB= &looks_like_number(scalar $self->ne_lon)
ABresult
0X0
100
111
101 1 ▶ croak "precision attribute missing or malformed" if not defined $self->precision or not $self->precision =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->precision and $self->precision =~ /^\d+$/ 0 1 1
Condition33%1/3
defined $self->precision and $self->precision =~ /^\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->precision and $self->precision =~ /^\d+$/
defined $self->precision $self->precision =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $self->precisionB= $self->precision =~ /^\d+$/
ABresult
0X0
100
111
102
103 1 my $gh = Geo::Hash::XS->new();
104
105
106 # place to store the hashes
107 1 my @coverage;
108
109
110 # convert the southwest corner into the start hash
111 1 my $current_hash = $gh->encode( $self->sw_lat, $self->sw_lon, $self->precision );
112
113
114 # let's find our start lat/lon
115 1 my ( $current_lat, $current_lon ) = $gh->decode( $current_hash );
116
117 1 my $row_start_hash = $current_hash;
118
119 # while both our current lat/longs are still within the bounding box
120 1 my $over_bounds_detected = 0;
121 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$current_lat <= $self->ne_lat || $current_lon <= $self->ne_lon and not $over_bounds_detected and do { do { my $next_hash; if ($current_lon <= $self->ne_lon) { $next_hash = $gh->adjacent($current_hash, ADJ_RIGHT()); push @coverage, $current_hash; } else { $current_hash = $row_start_hash; $next_hash = $gh->adjacent($current_hash, ADJ_TOP()); $row_start_hash = $next_hash; my($lat) = $gh->decode($row_start_hash); $over_bounds_detected = 1 if $lat > $self->ne_lat; } ; $current_hash = $next_hash; () = $gh->decode($current_hash) } } 35 1 36
Condition66%4/6
$current_lat <= $self->ne_lat || $current_lon <= $self->ne_lon and not $over_bounds_detected
!ll&&!rl&&r
0135
$current_lat <= $self->ne_lat or $current_lon <= $self->ne_lon
l!l&&r!l&&!r
3510
MC/DC0%0/3
$current_lat <= $self->ne_lat || $current_lon <= $self->ne_lon and not $over_bounds_detected
$current_lat <= $self->ne_lat $current_lon <= $self->ne_lon not $over_bounds_detected
Truth table40%2/5
A or B and not(C)
A= $current_lat <= $self->ne_latB= $current_lon <= $self->ne_lonC= not $over_bounds_detected
ABCresult
00X0
1X00
0100
1X11
0111
122
123 # if our longitude hasn't over run our bounding box, then we need the next to the right
124 # if our longitude has run over the bounding box, then we need the next one on the top
125 36 my $next_hash;
126 36 ▶ if ( $current_lon <= $self->ne_lon ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_lon <= $self->ne_lon) { } 32 4 36
127
128 32 $next_hash = $gh->adjacent( $current_hash, ADJ_RIGHT );
129
130 32 push @coverage, $current_hash;
131
132 } else {
133
134 4 $current_hash = $row_start_hash;
135 4 $next_hash = $gh->adjacent( $current_hash, ADJ_TOP );
136 4 $row_start_hash = $next_hash;
137
138 # check to see if the next row is out of bounds
139 4 my ( $lat ) = $gh->decode( $row_start_hash );
140
141 4 ▶ $over_bounds_detected = 1 if $lat > $self->ne_lat;
Branch: 1 branch
BranchTrueFalseTotal
if $lat > $self->ne_lat 1 3 4
142
143 }
144
145 # get ready to evaluate the next hash
146 36 $current_hash = $next_hash;
147 36 ( $current_lat, $current_lon ) = $gh->decode( $current_hash );
148
149 } while ( ( $current_lat <= $self->ne_lat or $current_lon <= $self->ne_lon ) and not $over_bounds_detected );
150
151 # store data
152 1 $self->{'coverage'} = [ @coverage ];
153
154 }
155  
156 =head2 count
157  
158 Get count of GeoHash's in bounding box
159  
160 =cut
161  
162 1 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
163 1 my $self = shift;
164 1 return scalar @{$self->{'coverage'}};
165 }
166  
167 =head2 hashes
168  
169 Get array reference of GeoHash's in bounding box
170  
171 =cut
172  
173 1 ▶sub hashes {
Subroutine hashes: called CC=1 SCAR=0.0
Pod documented
174 1 my $self = shift;
175 1 return $self->{'coverage'};
176 }
177  
178 =head2 origins
179  
180 Get list of hash references of GeoHash lat/long origins in bounding box
181  
182 =cut
183  
184 1 ▶sub origins {
Subroutine origins: called CC=3 SCAR=11.0
Pod documented
185 1 my $self = shift;
186
187 1 my $gh = Geo::Hash::XS->new();
188
189 1 ▶ if ( not defined $self->{'origins'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"origins"}) 1 0 1
190 1 my $origins;
191 1 foreach my $hash ( @{$self->{'coverage'}} ) {
192 32 my ( $lat, $lon ) = $gh->decode( $hash );
193 32 push @$origins, {
194 lat => $lat,
195 lon => $lon,
196 };
197 }
198 1 $self->{'origins'} = $origins;
199 }
200
201 1 return $self->{'origins'};
202
203 }
204  
205 =head2 bboxes
206  
207 Get a list of bounding boxes for each hash in the grid
208  
209 =cut
210  
211 1 ▶sub bboxes {
Subroutine bboxes: called CC=2 SCAR=6.9
Pod documented
212 1 my $self = shift;
213
214 1 my @bboxes;
215 1 foreach my $hash ( @{$self->{'coverage'}} ) {
216
217 32 my $bbox = $self->_get_bbox( $hash );
218 32 push @bboxes, $bbox;
219
220 }
221
222 1 return [ @bboxes ];
223  
224 }
225  
226  
227 32 ▶sub _get_bbox {
Subroutine _get_bbox: called CC=1 SCAR=0.0
228 32 my $self = shift;
229 32 my $hash = shift;
230
231 32 my $gh = Geo::Hash::XS->new();
232  
233 32 my ( $lat_range, $lon_range ) = $gh->decode_to_interval( $hash );
234
235 return {
236 32 sw => {
237 lat => $lat_range->[1],
238 lon => $lon_range->[1],
239 },
240 ne => {
241 lat => $lat_range->[0],
242 lon => $lon_range->[0],
243 },
244 };
245
246 }
247  
248 =head1 AUTHOR
249  
250 Adam Wohld, <adam at spatialsystems.org>
251  
252 =head1 BUGS
253  
254 Please report any bugs or feature requests to C<bug-geo-hash-grid at rt.cpan.org>, or through
255 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-Hash-Grid>. I will be notified, and then you'll
256 automatically be notified of progress on your bug as I make changes.
257  
258  
259  
260  
261 =head1 SUPPORT
262  
263 You can find documentation for this module with the perldoc command.
264  
265 perldoc Geo::Hash::Grid
266  
267  
268 You can also look for information at:
269  
270 =over 4
271  
272 =item * RT: CPAN's request tracker (report bugs here)
273  
274 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-Hash-Grid>
275  
276 =item * AnnoCPAN: Annotated CPAN documentation
277  
278 L<http://annocpan.org/dist/Geo-Hash-Grid>
279  
280 =item * CPAN Ratings
281  
282 L<http://cpanratings.perl.org/d/Geo-Hash-Grid>
283  
284 =item * Search CPAN
285  
286 L<http://search.cpan.org/dist/Geo-Hash-Grid/>
287  
288 =back
289  
290  
291 =head1 ACKNOWLEDGEMENTS
292  
293  
294 =head1 LICENSE AND COPYRIGHT
295  
296 Copyright 2015 Adam Wohld.
297  
298 This program is free software; you can redistribute it and/or modify it
299 under the terms of the the Artistic License (2.0). You may obtain a
300 copy of the full license at:
301  
302 L<http://www.perlfoundation.org/artistic_license_2_0>
303  
304 Any use, modification, and distribution of the Standard or Modified
305 Versions is governed by this Artistic License. By using, modifying or
306 distributing the Package, you accept this license. Do not use, modify,
307 or distribute the Package, if you do not accept this license.
308  
309 If your Modified Version has been derived from a Modified Version made
310 by someone other than you, you are nevertheless required to ensure that
311 your Modified Version complies with the requirements of this license.
312  
313 This license does not grant you the right to use any trademark, service
314 mark, tradename, or logo of the Copyright Holder.
315  
316 This license includes the non-exclusive, worldwide, free-of-charge
317 patent license to make, have made, use, offer to sell, sell, import and
318 otherwise transfer the Package with respect to any patent claims
319 licensable by the Copyright Holder that are necessarily infringed by the
320 Package. If you institute patent litigation (including a cross-claim or
321 counterclaim) against any party alleging that the Package constitutes
322 direct or contributory patent infringement, then this Artistic License
323 to you shall terminate on the date that such litigation is filed.
324  
325 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
326 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
327 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
328 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
329 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
330 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
331 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
332 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
333  
334  
335 =cut
336  
337 1 1;
↑ Summary