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