blib/lib/Geo/ShapeFile/Shape/Index.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 116 CC 5 SCAR 14.6
CC
5
Cov
0%
query_point
12.0
new
6.0
snap_to_index
6.0
CRAP 4.3
1 package Geo::ShapeFile::Shape::Index;
2 #use 5.010; # not yet
3 use strict;
4 use warnings;
5 use POSIX qw /floor/;
6 use Carp;
7 use autovivification;
8  
9 our $VERSION = '3.02';
10  
11 # should also handle X cells
12 sub new {
13 my ($class, $n, $x_min, $y_min, $x_max, $y_max) = @_;
14  
15 my $self = bless {}, $class;
16  
17 $n ||= 10; # need a better default?
18 $n = int $n;
19 die 'Number of blocks must be positive and >=1'
20 if $n <= 0;
21  
22 my $y_range = abs ($y_max - $y_min);
23 my $y_tol = $y_range / 1000;
24 $y_range += 2 * $y_tol;
25 $y_min -= $y_tol;
26 $y_max += $y_tol;
27  
28 my $block_ht = $y_range / $n;
29  
30 $self->{x_min} = $x_min;
31 $self->{y_min} = $y_min;
32 $self->{x_max} = $x_max;
33 $self->{y_max} = $y_max;
34 $self->{y_res} = $block_ht;
35 $self->{y_n} = $n;
36 $self->{x_n} = 1;
37  
38 my %blocks;
39 my $y = $y_min;
40 foreach my $i (1 .. $n) {
41 my $key = $self->snap_to_index($x_min, $y); # index by lower left
42 $blocks{$key} = [];
43 $y += $block_ht;
44 }
45 $self->{containers} = \%blocks;
46  
47 return $self;
48 }
49  
50 sub get_x_min {$_[0]->{x_min}}
51 sub get_x_max {$_[0]->{x_max}}
52 sub get_y_min {$_[0]->{y_min}}
53 sub get_y_max {$_[0]->{y_max}}
54 sub get_y_res {$_[0]->{y_res}}
55  
56 # return an anonymous array if we are out of the index bounds
57 sub _get_container_ref {
58 my ($self, $id) = @_;
59  
60 no autovivification;
61  
62 my $containers = $self->{containers};
63 my $container = $containers->{$id} || [];
64  
65 return $container;
66 };
67  
68 # need to handle X coords as well
69 sub snap_to_index {
70 my ($self, $x, $y) = @_;
71  
72 #my $x_min = $self->get_x_min;
73 my $y_min = $self->get_y_min;
74 my $y_res = $self->get_y_res;
75  
76 # take the floor, but add a small tolerance to
77 # avoid precision issues with snapping
78 my $partial = ($y - $y_min) / $y_res;
79 my $y_block = floor ($partial * 1.001);
80  
81 return wantarray ? (0, $y_block) : "0:$y_block";
82 }
83  
84 # inserts into whichever blocks overlap the bounding box
85 sub insert {
86 my ($self, $item, @bbox) = @_;
87  
88 my @index_id1 = $self->snap_to_index (@bbox[0, 1]);
89 my @index_id2 = $self->snap_to_index (@bbox[2, 3]);
90  
91 my $insert_count = 0;
92 foreach my $y ($index_id1[1] .. $index_id2[1]) {
93 my $index_id = "0:$y"; # hackish
94 my $container = $self->_get_container_ref ($index_id);
95 push @$container, $item;
96 $insert_count++;
97 }
98  
99 return $insert_count;
100 }
101  
102 # $storage ref arg is for Tree::R compat - still needed?
103 sub query_point {
104 my ($self, $x, $y, $storage_ref) = @_;
105  
106 my $index_id = $self->snap_to_index ($x, $y);
107 my $container = $self->_get_container_ref ($index_id);
108  
109 if ($storage_ref) {
110 push @$storage_ref, @$container;
111 }
112  
113 return wantarray ? @$container : [@$container];
114 }
115  
116  
117 1;
118  
119 __END__