lib/Geo/ShapeFile/Point.pm

filter: Statementstmt 57.7% 86 / 149 Branchbran 48.0% 25 / 52 Conditioncond 35.9% 14 / 39 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 44.6% 21 / 47 Podpod 73.0% 19 / 26 totaltotal 49.5% 167 / 337 CC 42 SCAR 20.9
CC
42
Cov
52%
eq
24.8
_add_point_point
12.0
_multiply_point_point
12.0
CRAP 8.0
« lib/Geo/ShapeFile/Shape.pm ↑ Summary lib/Geo/ShapeFile.pm »
1 package Geo::ShapeFile::Point;
2 # TODO - add dimension operators (to specify if 2 or 3 dimensional point)
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 Math::Trig 1.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '3.02';
9  
10 use overload
11 2 '==' => 'eq',
12 'eq' => 'eq',
13 '""' => 'stringify',
14 '+' => \&add,
15 '-' => \&subtract,
16 '*' => \&multiply,
17 '/' => \&divide,
18 fallback => 1,
19 2 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 my %config = (
22 comp_includes_z => 1,
23 comp_includes_m => 1,
24 );
25  
26 sub new {
27 83413 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.2
Pod documented
28 83413 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0834130
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29  
30 83413 my $self = {@_};
31  
32 83413 bless $self, $class;
33  
34 83413 return $self;
35 }
36  
37 sub _var {
38 107360 ▶ my $self = shift;
Subroutine _var: called CC=2 SCAR=6.9
39 107360 my $var = shift;
40  
41 107360 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 10098 97262 107360
42 10098 return $self->{$var} = shift;
43 }
44 else {
45 97262 return $self->{$var};
46 }
47 }
48  
49 # these could be factory generated
50 48490 ▶sub X { shift()->_var('X', @_); }
Subroutine X: called CC=1 SCAR=0.0
Pod documented
51 48490 ▶sub Y { shift()->_var('Y', @_); }
Subroutine Y: called CC=1 SCAR=0.0
Pod documented
52 7791 ▶sub Z { shift()->_var('Z', @_); }
Subroutine Z: called CC=1 SCAR=0.0
Pod documented
53 2589 ▶sub M { shift()->_var('M', @_); }
Subroutine M: called CC=1 SCAR=0.0
Pod documented
54  
55 0 ▶sub x_min { $_[0]->_var('X'); }
Subroutine x_min: not called CC=1 SCAR=6.9
Pod documented
56 0 ▶sub x_max { $_[0]->_var('X'); }
Subroutine x_max: not called CC=1 SCAR=6.9
Pod documented
57 0 ▶sub y_min { $_[0]->_var('Y'); }
Subroutine y_min: not called CC=1 SCAR=6.9
Pod documented
58 0 ▶sub y_max { $_[0]->_var('Y'); }
Subroutine y_max: not called CC=1 SCAR=6.9
Pod documented
59 0 ▶sub z_min { $_[0]->_var('Z'); }
Subroutine z_min: not called CC=1 SCAR=6.9
Pod documented
60 0 ▶sub z_max { $_[0]->_var('Z'); }
Subroutine z_max: not called CC=1 SCAR=6.9
Pod documented
61 0 ▶sub m_min { $_[0]->_var('M'); }
Subroutine m_min: not called CC=1 SCAR=6.9
Pod documented
62 0 ▶sub m_max { $_[0]->_var('M'); }
Subroutine m_max: not called CC=1 SCAR=6.9
Pod documented
63  
64 16984 ▶sub get_x { $_[0]->{X} }
Subroutine get_x: called CC=1 SCAR=0.0
Pod documented
65 16984 ▶sub get_y { $_[0]->{Y} }
Subroutine get_y: called CC=1 SCAR=0.0
Pod documented
66 0 ▶sub get_z { $_[0]->{Z} }
Subroutine get_z: not called CC=1 SCAR=6.9
Pod documented
67 0 ▶sub get_m { $_[0]->{M} }
Subroutine get_m: not called CC=1 SCAR=6.9
Pod documented
68  
69  
70 sub import {
71 4 ▶ my $self = shift;
Subroutine import: called CC=2 SCAR=7.2
72 4 my %args = @_;
73  
74 4 foreach(keys %args) { $config{$_} = $args{$_}; }
75 }
76  
77 sub eq {
78 8 ▶ my $left = shift;
Subroutine eq: called CC=14 SCAR=32.1
Pod undocumented
79 8 my $right = shift;
80  
81 8 ▶ if ($config{comp_includes_z} && (defined $left->Z || defined $right->Z)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"comp_includes_z"} and defined $left->Z || defined $right->Z) 1 7 8
Condition50%3/6
$config{"comp_includes_z"} and defined $left->Z || defined $right->Z
!ll&&!rl&&r
080
defined $left->Z or defined $right->Z
l!l&&r!l&&!r
107
MC/DC0%0/3
$config{"comp_includes_z"} and defined $left->Z || defined $right->Z
$config{"comp_includes_z"} defined $left->Z defined $right->Z
Truth table25%1/4
A and B or C
A= $config{"comp_includes_z"}B= defined $left->ZC= defined $right->Z
ABCresult
0XX0
1000
11X1
1011
82 1 ▶ return 0 unless defined $left->Z && defined $right->Z;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left->Z and defined $right->Z 0 1 1
Condition33%1/3
defined $left->Z and defined $right->Z
!ll&&!rl&&r
001
MC/DC0%0/2
defined $left->Z and defined $right->Z
defined $left->Z defined $right->Z
Truth table33%1/3
A and B
A= defined $left->ZB= defined $right->Z
ABresult
0X0
100
111
83 1 ▶ return 0 unless $left->Z == $right->Z;
Branch: 1 branch
BranchTrueFalseTotal
unless $left->Z == $right->Z 0 1 1
84 }
85 8 ▶ if ($config{comp_includes_m} && (defined $left->M || defined $right->M)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"comp_includes_m"} and defined $left->M || defined $right->M) 3 5 8
Condition50%3/6
$config{"comp_includes_m"} and defined $left->M || defined $right->M
!ll&&!rl&&r
080
defined $left->M or defined $right->M
l!l&&r!l&&!r
305
MC/DC0%0/3
$config{"comp_includes_m"} and defined $left->M || defined $right->M
$config{"comp_includes_m"} defined $left->M defined $right->M
Truth table25%1/4
A and B or C
A= $config{"comp_includes_m"}B= defined $left->MC= defined $right->M
ABCresult
0XX0
1000
11X1
1011
86 3 ▶ return 0 unless defined $left->M && defined $right->M;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left->M and defined $right->M 0 3 3
Condition33%1/3
defined $left->M and defined $right->M
!ll&&!rl&&r
003
MC/DC0%0/2
defined $left->M and defined $right->M
defined $left->M defined $right->M
Truth table33%1/3
A and B
A= defined $left->MB= defined $right->M
ABresult
0X0
100
111
87 3 ▶ return 0 unless $left->M == $right->M;
Branch: 1 branch
BranchTrueFalseTotal
unless $left->M == $right->M 0 3 3
88 }
89  
90 8 ▶ return ($left->X == $right->X && $left->Y == $right->Y);
Condition33%1/3
$left->X == $right->X && $left->Y == $right->Y
!ll&&!rl&&r
008
MC/DC0%0/2
$left->X == $right->X && $left->Y == $right->Y
$left->X == $right->X $left->Y == $right->Y
Truth table33%1/3
A && B
A= $left->X == $right->XB= $left->Y == $right->Y
ABresult
0X0
100
111
91 }
92  
93 sub stringify {
94 107 ▶ my $self = shift;
Subroutine stringify: called CC=3 SCAR=11.0
Pod undocumented
95  
96 107 my @foo = ();
97 107 foreach(qw/X Y Z M/) {
98 428 ▶ if(defined $self->$_()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->$_) 230 198 428
99 230 push @foo, "$_=" . $self->$_();
100 }
101 }
102 107 my $r = 'Point(' . join(',', @foo) . ')';
103 }
104  
105 sub distance_from {
106 0 ▶ my ($p1, $p2) = @_;
Subroutine distance_from: not called CC=1 SCAR=6.9
Pod documented
107  
108 0 my $dp = $p2->subtract($p1);
109 0 return sqrt ( ($dp->X ** 2) + ($dp->Y **2) );
110 }
111  
112 0 ▶sub distance_to { distance_from(@_); }
Subroutine distance_to: not called CC=1 SCAR=6.9
Pod undocumented
113  
114 sub angle_to {
115 8 ▶ my ($p1, $p2) = @_;
Subroutine angle_to: called CC=4 SCAR=13.9
Pod documented
116  
117 8 my $dp = $p2->subtract ($p1);
118  
119 8 my $x_off = $dp->get_x;
120 8 my $y_off = $dp->get_y;
121  
122 8 ▶ return 0 if !($x_off || $y_off);
Branch: 1 branch
BranchTrueFalseTotal
unless $x_off or $y_off 1 7 8
Condition100%3/3
$x_off or $y_off
l!l&&r!l&&!r
521
MC/DC100%2/2
$x_off or $y_off
$x_off $y_off
Truth table100%3/3
A or B
A= $x_offB= $y_off
ABresult
1X1
011
000
123  
124 7 my $bearing = 90 - Math::Trig::rad2deg (Math::Trig::atan2 ($y_off, $x_off));
125 7 ▶ if ($bearing < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bearing < 0) 2 5 7
126 2 $bearing += 360;
127 }
128  
129 7 return $bearing;
130 }
131  
132 0 ▶sub add { _mathemagic('add', @_); }
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
133 8 ▶sub subtract { _mathemagic('subtract', @_); }
Subroutine subtract: called CC=1 SCAR=0.0
Pod undocumented
134 0 ▶sub multiply { _mathemagic('multiply', @_); }
Subroutine multiply: not called CC=1 SCAR=6.9
Pod undocumented
135 0 ▶sub divide { _mathemagic('divide', @_); }
Subroutine divide: not called CC=1 SCAR=6.9
Pod undocumented
136  
137 sub _mathemagic {
138 8 ▶ my ($op, $l, $r, $reverse) = @_;
Subroutine _mathemagic: called CC=9 SCAR=24.7
139  
140 8 ▶ if ($reverse) { # put them back in the right order
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) 0 8 8
141 0 ($l, $r) = ($r, $l);
142 }
143 8 my ($left, $right);
144  
145 8 ▶ if (UNIVERSAL::isa($l, 'Geo::ShapeFile::Point')) { $left = 'point'; }
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($l, "Geo::ShapeFile::Point")) 8 0 8
146 8 ▶ if (UNIVERSAL::isa($r, 'Geo::ShapeFile::Point')) { $right = 'point'; }
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($r, "Geo::ShapeFile::Point")) 8 0 8
147  
148 8 ▶ if ($l =~ /^[\d\.]+$/) { $left = 'number'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^[\d\.]+$/) 0 8 8
149 8 ▶ if ($r =~ /^[\d\.]+$/) { $right = 'number'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /^[\d\.]+$/) 0 8 8
150  
151 8 ▶ unless ($left) { croak "Couldn't identify $l for $op"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($left) 0 8 8
152 8 ▶ unless ($right) { croak "Couldn't identify $r for $op"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($right) 0 8 8
153  
154 8 my $function = '_' . join '_', $op, $left, $right;
155  
156 croak "Don't know how to $op $left and $right"
157 8 ▶ if !defined &{$function};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$function;} 0 8 8
158  
159 8 do {
160 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
161 8 return $function->($l, $r);
162 }
163 }
164  
165 sub _add_point_point {
166 0 ▶ my ($p1, $p2) = @_;
Subroutine _add_point_point: not called CC=3 SCAR=24.8
167  
168 0 my $z;
169 0 ▶ if(defined($p2->Z) && defined($p1->Z)) { $z = ($p2->Z + $p1->Z); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p2->Z and defined $p1->Z) 0 0 0
Condition0%0/3
defined $p2->Z and defined $p1->Z
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p2->Z and defined $p1->Z
defined $p2->Z defined $p1->Z
Truth table0%0/3
A and B
A= defined $p2->ZB= defined $p1->Z
ABresult
0X0
100
111
170  
171 Geo::ShapeFile::Point->new(
172 0 X => ($p2->X + $p1->X),
173 Y => ($p2->Y + $p1->Y),
174 Z => $z,
175 );
176 }
177  
178 sub _add_point_number {
179 0 ▶ my ($p1, $n) = @_;
Subroutine _add_point_number: not called CC=2 SCAR=17.9
180  
181 0 my $z;
182 0 ▶ if (defined($p1->Z)) { $z = ($p1->Z + $n); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p1->Z) 0 0 0
183  
184 Geo::ShapeFile::Point->new(
185 0 X => ($p1->X + $n),
186 Y => ($p1->Y + $n),
187 Z => $z,
188 );
189 }
190 0 ▶sub _add_number_point { add_point_number(@_); }
Subroutine _add_number_point: not called CC=1 SCAR=6.9
191  
192 sub _subtract_point_point {
193 8 ▶ my($p1, $p2) = @_;
Subroutine _subtract_point_point: called CC=3 SCAR=12.3
194  
195 8 my $z;
196 8 ▶ if(defined($p2->Z) && defined($p1->Z)) { $z = ($p2->Z - $p1->Z); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p2->Z and defined $p1->Z) 0 8 8
Condition33%1/3
defined $p2->Z and defined $p1->Z
!ll&&!rl&&r
800
MC/DC0%0/2
defined $p2->Z and defined $p1->Z
defined $p2->Z defined $p1->Z
Truth table33%1/3
A and B
A= defined $p2->ZB= defined $p1->Z
ABresult
0X0
100
111
197  
198 8 my $result = Geo::ShapeFile::Point->new(
199 X => ($p1->X - $p2->X),
200 Y => ($p1->Y - $p2->Y),
201 Z => $z,
202 );
203 8 return $result;
204 }
205  
206 sub _subtract_point_number {
207 0 ▶ my($p1, $n) = @_;
Subroutine _subtract_point_number: not called CC=2 SCAR=17.9
208  
209 0 my $z;
210 0 ▶ if (defined $p1->Z) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p1->Z) 0 0 0
211 0 $z = ($p1->Z - $n);
212 }
213  
214 Geo::ShapeFile::Point->new(
215 0 X => ($p1->X - $n),
216 Y => ($p1->Y - $n),
217 Z => $z,
218 );
219 }
220 0 ▶sub _subtract_number_point { _subtract_point_number(reverse @_); }
Subroutine _subtract_number_point: not called CC=1 SCAR=6.9
221  
222 sub _multiply_point_point {
223 0 ▶ my ($p1, $p2) = @_;
Subroutine _multiply_point_point: not called CC=3 SCAR=24.8
224  
225 0 my $z;
226 0 ▶ if (defined $p2->Z and defined $p1->Z) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p2->Z and defined $p1->Z) 0 0 0
Condition0%0/3
defined $p2->Z and defined $p1->Z
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p2->Z and defined $p1->Z
defined $p2->Z defined $p1->Z
Truth table0%0/3
A and B
A= defined $p2->ZB= defined $p1->Z
ABresult
0X0
100
111
227 0 $z = $p2->Z * $p1->Z;
228 }
229  
230 Geo::ShapeFile::Point->new(
231 0 X => ($p2->X * $p1->X),
232 Y => ($p2->Y * $p1->Y),
233 Z => $z,
234 );
235 }
236 sub _multiply_point_number {
237 0 ▶ my($p1, $n) = @_;
Subroutine _multiply_point_number: not called CC=2 SCAR=17.9
238  
239 0 my $z;
240 0 ▶ if (defined $p1->Z) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p1->Z) 0 0 0
241 0 $z = $p1->Z * $n;
242 }
243  
244 Geo::ShapeFile::Point->new(
245 0 X => ($p1->X * $n),
246 Y => ($p1->Y * $n),
247 Z => $z,
248 );
249 }
250  
251 0 ▶sub _multiply_number_point { _multiply_point_number(reverse @_); }
Subroutine _multiply_number_point: not called CC=1 SCAR=6.9
252  
253 sub _divide_point_point {
254 0 ▶ my($p1, $p2) = @_;
Subroutine _divide_point_point: not called CC=3 SCAR=24.8
255  
256 0 my $z;
257 0 ▶ if (defined $p2->Z and defined $p1->Z) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p2->Z and defined $p1->Z) 0 0 0
Condition0%0/3
defined $p2->Z and defined $p1->Z
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p2->Z and defined $p1->Z
defined $p2->Z defined $p1->Z
Truth table0%0/3
A and B
A= defined $p2->ZB= defined $p1->Z
ABresult
0X0
100
111
258 0 $z = $p1->Z / $p2->Z;
259 }
260  
261 Geo::ShapeFile::Point->new(
262 0 X => ($p1->X / $p2->X),
263 Y => ($p1->Y / $p2->Y),
264 Z => $z,
265 );
266 }
267  
268 sub _divide_point_number {
269 0 ▶ my ($p1, $n) = @_;
Subroutine _divide_point_number: not called CC=2 SCAR=17.9
270  
271 0 my $z;
272 0 ▶ if (defined $p1->Z) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p1->Z) 0 0 0
273 0 $z = $p1->Z / $n;
274 }
275  
276 Geo::ShapeFile::Point->new(
277 0 X => ($p1->X / $n),
278 Y => ($p1->Y / $n),
279 Z => $z,
280 );
281 }
282  
283 0 ▶sub _divide_number_point { divide_point_number(reverse @_); }
Subroutine _divide_number_point: not called CC=1 SCAR=6.9
284  
285 2 1;
286 __END__
« lib/Geo/ShapeFile/Shape.pm ↑ Summary lib/Geo/ShapeFile.pm »