lib/Geo/ShapeFile/Shape.pm

filter: Statementstmt 70.7% 268 / 379 Branchbran 62.2% 56 / 90 Conditioncond 47.6% 10 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 70.3% 45 / 64 Podpod 82.7% 24 / 29 totaltotal 68.2% 407 / 596 CC 74 SCAR 21.0
CC
74
Cov
68%
dump
56.0
has_point
20.0
area_centroid
20.0
CRAP 8.2
« blib/lib/Geo/ShapeFile/Shape.pm ↑ Summary lib/Geo/ShapeFile/Point.pm »
1 package Geo::ShapeFile::Shape;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Tree::R;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::Util qw /min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Geo::ShapeFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Geo::ShapeFile::Point;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Geo::ShapeFile::Shape::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use parent qw /Geo::ShapeFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '3.02';
15  
16 2 my $little_endian_sys = unpack 'b', (pack 'S', 1 );
17  
18 2 my $index_class = 'Geo::ShapeFile::Shape::Index';
19  
20 sub new {
21 7416 ▶ my $proto = shift;
Subroutine new: called CC=3 SCAR=11.6
Pod documented
22 7416 ▶ my $class = ref ($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
074160
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
23 7416 my %args = @_;
24  
25 7416 my $self = {
26 shp_content_length => 0,
27 source => undef,
28 shp_points => [],
29 shp_num_points => 0,
30 shp_parts => [],
31 shp_record_number => undef,
32 shp_shape_type => undef,
33 shp_num_parts => 0,
34 shp_x_min => undef,
35 shp_x_max => undef,
36 shp_y_min => undef,
37 shp_y_max => undef,
38 shp_z_min => undef,
39 shp_z_max => undef,
40 shp_m_min => undef,
41 shp_m_max => undef,
42 shp_data => undef,
43 };
44  
45 7416 foreach (keys %args) {
46 0 $self->{$_} = $args{$_};
47 }
48  
49 7416 bless $self, $class;
50  
51 7416 return $self;
52 }
53  
54 sub parse_shp {
55 7416 ▶ my $self = shift;
Subroutine parse_shp: called CC=4 SCAR=16.3
Pod undocumented
56  
57 7416 $self->{source} = $self->{shp_data} = shift;
58  
59 7416 $self->_extract_ints('big', 'shp_record_number', 'shp_content_length');
60 7416 $self->_extract_ints('little', 'shp_shape_type');
61  
62 7416 my $parser = '_parse_shp_' . $self->type($self->{shp_shape_type});
63  
64 7416 ▶ croak "Can't parse shape_type $self->{shp_shape_type}"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($parser) 0 7416 7416
65 if !$self->can($parser);
66  
67 7416 $self->$parser();
68  
69 7416 ▶ if (length($self->{shp_data})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$self{"shp_data"}) 0 7416 7416
70 0 my $len = length($self->{shp_data});
71 0 ▶ my $byte_plural = $len > 1 ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
$len > 1 ? : 0 0 0
72 0 carp "$len byte$byte_plural remaining in buffer after parsing "
73 . $self->shape_type_text()
74 . ' #'
75 . $self->shape_id();
76 }
77 }
78  
79 sub _parse_shp_Null {
80 0 ▶ my $self = shift;
Subroutine _parse_shp_Null: not called CC=1 SCAR=6.9
81 }
82  
83 # TODO - document this
84 sub add_point {
85 0 ▶ my $self = shift;
Subroutine add_point: not called CC=3 SCAR=24.8
Pod undocumented
86  
87 0 ▶ if(@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
88 0 my $point = shift;
89 0 ▶ if($point->isa('Geo::ShapeFile::Point')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($point->isa("Geo::ShapeFile::Point")) 0 0 0
90 0 push @{$self->{shp_points}}, $point;
91 }
92 }
93 else {
94 0 my %point_opts = @_;
95  
96 0 push @{$self->{shp_points}}, Geo::ShapeFile::Point->new(%point_opts);
97 0 $self->{shp_num_points}++;
98 }
99 }
100  
101 # TODO - document this
102 sub add_part {
103 0 ▶ my $self = shift;
Subroutine add_part: not called CC=1 SCAR=6.9
Pod undocumented
104  
105 0 push @{$self->{shp_parts}}, $self->{shp_num_parts}++;
106 }
107  
108 # TODO - finish me
109 sub calculate_bounds {
110 0 ▶ my $self = shift;
Subroutine calculate_bounds: not called CC=2 SCAR=17.9
Pod undocumented
111  
112 0 my %bounds = $self->find_bounds($self->points);
113 0 foreach (keys %bounds) {
114 0 $self->{'shp_' . $_} = $bounds{$_};
115 }
116 0 return %bounds;
117 }
118  
119 sub _parse_shp_Point {
120 2553 ▶ my $self = shift;
Subroutine _parse_shp_Point: called CC=1 SCAR=0.0
121 2553 $self->_extract_doubles('shp_X', 'shp_Y');
122 $self->{shp_points} = [Geo::ShapeFile::Point->new(
123 X => $self->{shp_X},
124 Y => $self->{shp_Y},
125 2553 )];
126 2553 $self->{shp_num_points} = 1;
127 2553 $self->{shp_x_min} = $self->{shp_X};
128 2553 $self->{shp_x_max} = $self->{shp_X};
129 2553 $self->{shp_y_min} = $self->{shp_Y};
130 2553 $self->{shp_y_max} = $self->{shp_Y};
131 }
132 # Point
133 # Double X // X coordinate
134 # Double Y // Y coordinate
135  
136 sub _parse_shp_PolyLine {
137 2253 ▶ my $self = shift;
Subroutine _parse_shp_PolyLine: called CC=1 SCAR=0.0
138  
139 2253 $self->_extract_bounds();
140 2253 $self->_extract_parts_and_points();
141 }
142 # PolyLine
143 # Double[4] Box // Bounding Box
144 # Integer NumParts // Number of parts
145 # Integer NumPoints // Number of points
146 # Integer[NumParts] Parts // Index to first point in part
147 # Point[NumPoints] Points // Points for all parts
148  
149 sub _parse_shp_Polygon {
150 2607 ▶ my $self = shift;
Subroutine _parse_shp_Polygon: called CC=1 SCAR=0.0
151  
152 2607 $self->_extract_bounds();
153 2607 $self->_extract_parts_and_points();
154 }
155 # Polygon
156 # Double[4] Box // Bounding Box
157 # Integer NumParts // Number of Parts
158 # Integer NumPoints // Total Number of Points
159 # Integer[NumParts] Parts // Index to First Point in Part
160 # Point[NumPoints] Points // Points for All Parts
161  
162 sub _parse_shp_MultiPoint {
163 3 ▶ my $self = shift;
Subroutine _parse_shp_MultiPoint: called CC=1 SCAR=0.0
164  
165 3 $self->_extract_bounds();
166 3 $self->_extract_ints('little', 'shp_num_points');
167 3 $self->_extract_points($self->{shp_num_points}, 'shp_points');
168 }
169 # MultiPoint
170 # Double[4] Box // Bounding Box
171 # Integer NumPoints // Number of Points
172 # Point[NumPoints] Points // The points in the set
173  
174 sub _parse_shp_PointZ {
175 2445 ▶ my $self = shift;
Subroutine _parse_shp_PointZ: called CC=1 SCAR=0.0
176  
177 2445 $self->_parse_shp_Point();
178 2445 $self->_extract_doubles('shp_Z', 'shp_M');
179 2445 $self->{shp_points}->[0]->Z($self->{shp_Z});
180 2445 $self->{shp_points}->[0]->M($self->{shp_M});
181 }
182 # PointZ
183 # Point +
184 # Double Z
185 # Double M
186  
187 sub _parse_shp_PolyLineZ {
188 366 ▶ my $self = shift;
Subroutine _parse_shp_PolyLineZ: called CC=1 SCAR=0.0
189  
190 366 $self->_parse_shp_PolyLine();
191 366 $self->_extract_z_data();
192 366 $self->_extract_m_data();
193 }
194 # PolyLineZ
195 # PolyLine +
196 # Double[2] Z Range
197 # Double[NumPoints] Z Array
198 # Double[2] M Range
199 # Double[NumPoints] M Array
200  
201 sub _parse_shp_PolygonZ {
202 0 ▶ my $self = shift;
Subroutine _parse_shp_PolygonZ: not called CC=1 SCAR=6.9
203  
204 0 $self->_parse_shp_Polygon();
205 0 $self->_extract_z_data();
206 0 $self->_extract_m_data();
207 }
208 # PolygonZ
209 # Polygon +
210 # Double[2] Z Range
211 # Double[NumPoints] Z Array
212 # Double[2] M Range
213 # Double[NumPoints] M Array
214  
215 sub _parse_shp_MultiPointZ {
216 0 ▶ my $self = shift;
Subroutine _parse_shp_MultiPointZ: not called CC=1 SCAR=6.9
217  
218 0 $self->_parse_shp_MultiPoint();
219 0 $self->_extract_z_data();
220 0 $self->_extract_m_data();
221 }
222 # MultiPointZ
223 # MultiPoint +
224 # Double[2] Z Range
225 # Double[NumPoints] Z Array
226 # Double[2] M Range
227 # Double[NumPoints] M Array
228  
229 sub _parse_shp_PointM {
230 0 ▶ my $self = shift;
Subroutine _parse_shp_PointM: not called CC=1 SCAR=6.9
231  
232 0 $self->_parse_shp_Point();
233 0 $self->_extract_doubles('shp_M');
234 0 $self->{shp_points}->[0]->M($self->{shp_M});
235 }
236 # PointM
237 # Point +
238 # Double M // M coordinate
239  
240 sub _parse_shp_PolyLineM {
241 0 ▶ my $self = shift;
Subroutine _parse_shp_PolyLineM: not called CC=1 SCAR=6.9
242  
243 0 $self->_parse_shp_PolyLine();
244 0 $self->_extract_m_data();
245 }
246 # PolyLineM
247 # PolyLine +
248 # Double[2] MRange // Bounding measure range
249 # Double[NumPoints] MArray // Measures for all points
250  
251 sub _parse_shp_PolygonM {
252 0 ▶ my $self = shift;
Subroutine _parse_shp_PolygonM: not called CC=1 SCAR=6.9
253  
254 0 $self->_parse_shp_Polygon();
255 0 $self->_extract_m_data();
256 }
257 # PolygonM
258 # Polygon +
259 # Double[2] MRange // Bounding Measure Range
260 # Double[NumPoints] MArray // Measures for all points
261  
262 sub _parse_shp_MultiPointM {
263 0 ▶ my $self = shift;
Subroutine _parse_shp_MultiPointM: not called CC=1 SCAR=6.9
264  
265 0 $self->_parse_shp_MultiPoint();
266 0 $self->_extract_m_data();
267 }
268 # MultiPointM
269 # MultiPoint
270 # Double[2] MRange // Bounding measure range
271 # Double[NumPoints] MArray // Measures
272  
273 sub _parse_shp_MultiPatch {
274 0 ▶ my $self = shift;
Subroutine _parse_shp_MultiPatch: not called CC=1 SCAR=6.9
275  
276 0 $self->_extract_bounds();
277 0 $self->_extract_parts_and_points();
278 0 $self->_extract_z_data();
279 0 $self->_extract_m_data();
280 }
281 # MultiPatch
282 # Double[4] BoundingBox
283 # Integer NumParts
284 # Integer NumPoints
285 # Integer[NumParts] Parts
286 # Integer[NumParts] PartTypes
287 # Point[NumPoints] Points
288 # Double[2] Z Range
289 # Double[NumPoints] Z Array
290 # Double[2] M Range
291 # Double[NumPoints] M Array
292  
293 sub _extract_bounds {
294 4863 ▶ my $self = shift;
Subroutine _extract_bounds: called CC=1 SCAR=0.0
295  
296 4863 $self->_extract_doubles(qw/shp_x_min shp_y_min shp_x_max shp_y_max/);
297 }
298  
299 sub _extract_ints {
300 19695 ▶ my $self = shift;
Subroutine _extract_ints: called CC=2 SCAR=6.9
301 19695 my $end = shift;
302 19695 my @what = @_;
303  
304 19695 ▶ my $template = ($end =~ /^l/i) ? 'V' :'N';
Branch: 1 branch
BranchTrueFalseTotal
$end =~ /^l/i ? : 12279 7416 19695
305  
306 19695 $self->_extract_and_unpack(4, $template, @what);
307 }
308  
309 sub _extract_count_ints {
310 4860 ▶ my $self = shift;
Subroutine _extract_count_ints: called CC=2 SCAR=7.0
311 4860 my $count = shift;
312 4860 my $end = shift;
313 4860 my $label = shift;
314  
315 4860 ▶ my $template = ($end =~ /^l/i) ? 'V' :'N';
Branch: 1 branch
BranchTrueFalseTotal
$end =~ /^l/i ? : 4860 0 4860
316  
317 4860 my $tmp = substr $self->{shp_data}, 0, ($count * 4), '';
318 4860 my @tmp = unpack $template . $count, $tmp;
319 #my @tmp = unpack($template."[$count]",$tmp);
320  
321 4860 $self->{$label} = [@tmp];
322 }
323  
324 sub _extract_doubles {
325 10593 ▶ my $self = shift;
Subroutine _extract_doubles: called CC=3 SCAR=11.0
326 10593 my @what = @_;
327 10593 my $size = 8;
328 10593 my $template = 'd';
329  
330 10593 foreach ( @what ) {
331 30912 my $tmp = substr $self->{shp_data}, 0, $size, '';
332 30912 ▶ $self->{ $_ } = $little_endian_sys
Branch: 1 branch
BranchTrueFalseTotal
$little_endian_sys ? : 30912 0 30912
333 ? (unpack $template, $tmp )
334 : (unpack $template, scalar reverse $tmp );
335 }
336 }
337  
338 sub _extract_count_doubles {
339 732 ▶ my $self = shift;
Subroutine _extract_count_doubles: called CC=2 SCAR=7.0
340 732 my $count = shift;
341 732 my $label = shift;
342  
343 732 my $tmp = substr $self->{shp_data}, 0, $count*8, '';
344 732 ▶ my @tmp = $little_endian_sys
Branch: 1 branch
BranchTrueFalseTotal
$little_endian_sys ? : 732 0 732
345 ? (unpack 'd'.$count, $tmp )
346 : (reverse unpack( 'd' . $count, scalar ( reverse( $tmp ) ) ) );
347  
348 732 $self->{$label} = [@tmp];
349 }
350  
351 sub _extract_points {
352 4863 ▶ my $self = shift;
Subroutine _extract_points: called CC=3 SCAR=11.0
353 4863 my $count = shift;
354 4863 my $label = shift;
355  
356 4863 my $data = substr $self->{shp_data}, 0, $count * 16, '';
357  
358 4863 ▶ my @ps = $little_endian_sys
Branch: 1 branch
BranchTrueFalseTotal
$little_endian_sys ? : 4863 0 4863
359 ? (unpack 'd*', $data )
360 : (reverse unpack 'd*', scalar reverse $data );
361  
362 4863 my @p = (); # points
363 4863 ▶ while(@ps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ps) 80764 4863 85627
364 80764 my ($x, $y) = (shift @ps, shift @ps);
365 80764 push @p, Geo::ShapeFile::Point->new(X => $x, Y => $y);
366 }
367 4863 $self->{$label} = [@p];
368 }
369  
370 sub _extract_and_unpack {
371 19695 ▶ my $self = shift;
Subroutine _extract_and_unpack: called CC=3 SCAR=11.2
372 19695 my $size = shift;
373 19695 my $template = shift;
374 19695 my @what = @_;
375  
376 19695 foreach(@what) {
377 31971 my $tmp = substr $self->{shp_data}, 0, $size, '';
378 31971 ▶ if ( $template eq 'd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template eq "d") 0 31971 31971
379 0 $tmp = Geo::ShapeFile->byteswap( $tmp );
380 }
381 31971 $self->{$_} = unpack $template, $tmp;
382 }
383 }
384  
385 4644 ▶sub num_parts { shift()->{shp_num_parts}; }
Subroutine num_parts: called CC=1 SCAR=0.0
Pod documented
386 sub parts {
387 4751 ▶ my $self = shift;
Subroutine parts: called CC=3 SCAR=11.0
Pod undocumented
388  
389 4751 my $parts = $self->{shp_parts};
390  
391 4751 ▶ return wantarray ? @{$parts || []} : $parts;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 4666 85 4751
$parts or [] 0 4666 4666
392 }
393  
394 4626 ▶sub num_points { shift()->{shp_num_points}; }
Subroutine num_points: called CC=1 SCAR=0.0
Pod documented
395 sub points {
396 4711 ▶ my $self = shift;
Subroutine points: called CC=3 SCAR=11.0
Pod documented
397  
398 4711 my $points = $self->{shp_points};
399  
400 4711 ▶ return wantarray ? @{$points || []} : $points;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 4626 85 4711
$points or [] 0 4626 4626
401 }
402  
403 sub get_part {
404 85 ▶ my $self = shift;
Subroutine get_part: called CC=7 SCAR=19.6
Pod documented
405 85 my $index = shift;
406  
407 85 ▶ croak 'index passed to get_part must be >0'
Branch: 1 branch
BranchTrueFalseTotal
if $index <= 0 0 85 85
408 if $index <= 0;
409  
410 85 $index -= 1; # shift to a 0 index
411  
412 # $parts is an array of starting indexes in the $points array
413 85 my $parts = $self->parts;
414 85 ▶ croak 'index exceeds number of parts'
Branch: 1 branch
BranchTrueFalseTotal
if $index > $parts->$#* 0 85 85
415 if $index > $#$parts;
416  
417 85 my $points = $self->points;
418 85 ▶ my $beg = $parts->[$index] || 0;
Condition100%2/2
$$parts[$index] || 0
l!l
5827
MC/DC100%1/1
$$parts[$index] || 0
$$parts[$index]
Truth table100%2/2
A
A= $$parts[$index]
Aresult
11
00
419 85 ▶ my $end = $parts->[$index+1] || 0; # if we use 5.010 then we can use the // operator here
Condition100%2/2
$$parts[$index + 1] || 0
l!l
5827
MC/DC100%1/1
$$parts[$index + 1] || 0
$$parts[$index + 1]
Truth table100%2/2
A
A= $$parts[$index + 1]
Aresult
11
00
420 85 $end -= 1;
421 85 ▶ if ($end < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 27 58 85
422 27 $end = $#$points;
423 }
424  
425 85 ▶ return wantarray ? @$points[$beg .. $end] : [@$points[$beg .. $end]];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 85 85
426 }
427  
428 sub shape_type {
429 4626 ▶ my $self = shift;
Subroutine shape_type: called CC=1 SCAR=0.0
Pod documented
430 4626 return $self->{shp_shape_type};
431 }
432  
433 sub shape_id {
434 604 ▶ my $self = shift;
Subroutine shape_id: called CC=1 SCAR=0.0
Pod documented
435 604 return $self->{shp_record_number};
436 }
437  
438 sub _extract_z_data {
439 366 ▶ my $self = shift;
Subroutine _extract_z_data: called CC=2 SCAR=6.9
440  
441 366 $self->_extract_doubles('shp_z_min', 'shp_z_max');
442 366 $self->_extract_count_doubles($self->{shp_num_points}, 'shp_z_data');
443 366 my @zdata = @{delete $self->{shp_z_data}};
444 366 for (0 .. $#zdata) {
445 5208 $self->{shp_points}->[$_]->Z($zdata[$_]);
446 }
447 }
448  
449 sub _extract_m_data {
450 366 ▶ my $self = shift;
Subroutine _extract_m_data: called CC=2 SCAR=7.0
451  
452 366 $self->_extract_doubles ('shp_m_min', 'shp_m_max');
453 366 $self->_extract_count_doubles($self->{shp_num_points}, 'shp_m_data');
454 366 my @mdata = @{delete $self->{shp_m_data}};
455 366 for (0 .. $#mdata) {
456 0 $self->{shp_points}->[$_]->M($mdata[$_]);
457 }
458 }
459  
460 sub _extract_parts_and_points {
461 4860 ▶ my $self = shift;
Subroutine _extract_parts_and_points: called CC=1 SCAR=0.0
462  
463 4860 $self->_extract_ints('little', 'shp_num_parts', 'shp_num_points');
464 4860 $self->_extract_count_ints($self->{shp_num_parts}, 'little', 'shp_parts');
465 4860 $self->_extract_points($self->{shp_num_points}, 'shp_points');
466 }
467  
468  
469 # these could be factory generated
470 5164 ▶sub x_min { shift()->{shp_x_min}; }
Subroutine x_min: called CC=1 SCAR=0.0
Pod documented
471 5164 ▶sub x_max { shift()->{shp_x_max}; }
Subroutine x_max: called CC=1 SCAR=0.0
Pod documented
472 5164 ▶sub y_min { shift()->{shp_y_min}; }
Subroutine y_min: called CC=1 SCAR=0.0
Pod documented
473 5164 ▶sub y_max { shift()->{shp_y_max}; }
Subroutine y_max: called CC=1 SCAR=0.0
Pod documented
474 0 ▶sub z_min { shift()->{shp_z_min}; }
Subroutine z_min: not called CC=1 SCAR=6.9
Pod documented
475 0 ▶sub z_max { shift()->{shp_z_max}; }
Subroutine z_max: not called CC=1 SCAR=6.9
Pod documented
476 0 ▶sub m_min { shift()->{shp_m_min}; }
Subroutine m_min: not called CC=1 SCAR=6.9
Pod documented
477 0 ▶sub m_max { shift()->{shp_m_max}; }
Subroutine m_max: not called CC=1 SCAR=6.9
Pod documented
478  
479 sub bounds {
480 5 ▶ my $self = shift;
Subroutine bounds: called CC=2 SCAR=7.1
Pod documented
481  
482 5 my @results = (
483 $self->x_min,
484 $self->y_min,
485 $self->x_max,
486 $self->y_max,
487 );
488  
489 5 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
490 }
491  
492 sub has_point {
493 0 ▶ my $self = shift;
Subroutine has_point: not called CC=4 SCAR=30.0
Pod documented
494 0 my $point = shift;
495  
496 0 ▶ return 0 if !$self->bounds_contains_point($point);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bounds_contains_point($point) 0 0 0
497  
498 0 foreach my $check_pt ($self->points) {
499 0 ▶ return 1 if $check_pt == $point;
Branch: 1 branch
BranchTrueFalseTotal
if $check_pt == $point 0 0 0
500 }
501  
502 0 return 0;
503 }
504  
505 sub contains_point {
506 59 ▶ my ( $self, $point, $index_res ) = @_;
Subroutine contains_point: called CC=10 SCAR=23.0
Pod documented
507  
508 59 ▶ return $self->_contains_point_use_index ($point, $index_res)
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_spatial_index or defined $index_res 37 22 59
Condition66%2/3
$self->get_spatial_index or defined $index_res
l!l&&r!l&&!r
37022
MC/DC50%1/2
$self->get_spatial_index or defined $index_res
$self->get_spatial_index defined $index_res
Truth table66%2/3
A or B
A= $self->get_spatial_indexB= defined $index_res
ABresult
1X1
011
000
509 if $self->get_spatial_index || defined $index_res;
510  
511 22 ▶ return 0 if !$self->bounds_contains_point( $point );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bounds_contains_point($point) 4 18 22
512  
513 18 my $a = 0;
514 18 my ( $x0, $y0 ) = ( $point->get_x, $point->get_y );
515  
516 # one day we will track the bounds of the parts
517 # so we can more easily skip parts of multipart polygons
518 18 my $num_parts = $self->num_parts;
519  
520 # $x1, $x2, $y1 and $y2 are offsets from the point we are checking
521 18 foreach my $part_num (1 .. $num_parts) {
522 66 my $points = $self->get_part( $part_num );
523  
524 66 my $p_start = shift @$points; # $points is a copy, so no harm in shifting
525 66 my $x1 = $p_start->get_x - $x0;
526 66 my $y1 = $p_start->get_y - $y0;
527  
528 66 foreach my $p2 ( @$points ) {
529 1986 my $x2 = $p2->get_x - $x0;
530 1986 my $y2 = $p2->get_y - $y0;
531  
532 # does the ray intersect the segment?
533 1986 ▶ if (($y2 >= 0) != ($y1 >= 0)) { # $y0 is between $y1 and $y2
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= 0 != $y1 >= 0) 52 1934 1986
534 52 my $isl = $x1 * $y2 - $y1 * $x2; # is left of $p2
535 52 ▶ if ( $y2 > $y1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 > $y1) { } 26 26 52
536 26 ▶ if ($isl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isl > 0) 9 17 26
537 9 $a--;
538 }
539 }
540 else {
541 26 ▶ if ($isl < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isl < 0) 18 8 26
542 18 $a++;
543 }
544 }
545 }
546 1986 ( $x1, $y1 ) = ( $x2, $y2 );
547 }
548 }
549  
550 18 return $a;
551 }
552  
553 sub _contains_point_use_index {
554 37 ▶ my ( $self, $point, $index_res ) = @_;
Subroutine _contains_point_use_index: called CC=10 SCAR=23.0
555  
556 37 ▶ return 0 if !$self->bounds_contains_point( $point );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bounds_contains_point($point) 4 33 37
557  
558 33 ▶ my $sp_index_hash = $self->get_spatial_index || $self->build_spatial_index ($index_res);
Condition33%1/3
$self->get_spatial_index || $self->build_spatial_index($index_res)
l!l&&r!l&&!r
3300
MC/DC0%0/2
$self->get_spatial_index || $self->build_spatial_index($index_res)
$self->get_spatial_index $self->build_spatial_index($index_res)
Truth table33%1/3
A || B
A= $self->get_spatial_indexB= $self->build_spatial_index($index_res)
ABresult
1X1
011
000
559  
560 33 my $a = 0;
561 33 my ( $x0, $y0 ) = ( $point->get_x, $point->get_y );
562  
563 33 my @parts = $self->parts;
564 33 my $num_parts = scalar @parts;
565  
566 # $x1, $x2, $y1 and $y2 are offsets from the point we are checking
567 PART:
568 33 foreach my $part_index (1 .. $num_parts) {
569 81 my $sp_index = $sp_index_hash->{$part_index};
570  
571 81 my @results = $sp_index->query_point($x0, $y0);
572  
573 # skip if not in this part's bounding box
574 81 ▶ next PART if !scalar @results;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @results 42 39 81
575  
576 # segments spanning the index's bounding box
577 39 for my $segment (@results) {
578  
579 # index stores bare x and y coords to avoid method overhead here
580 22962 my $x1 = $segment->[0][0] - $x0;
581 22962 my $y1 = $segment->[0][1] - $y0;
582 22962 my $x2 = $segment->[1][0] - $x0;
583 22962 my $y2 = $segment->[1][1] - $y0;
584  
585 # does the ray intersect the segment?
586 22962 ▶ if (($y2 >= 0) != ($y1 >= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= 0 != $y1 >= 0) 82 22880 22962
587 82 my $isl = $x1 * $y2 - $y1 * $x2;
588 82 ▶ if ( $y2 > $y1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 > $y1) { } 41 41 82
589 41 ▶ if ($isl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isl > 0) 9 32 41
590 9 $a--;
591 }
592 }
593 else {
594 41 ▶ if ($isl < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isl < 0) 33 8 41
595 33 $a++;
596 }
597 }
598 }
599 }
600 }
601  
602 33 return $a;
603 }
604  
605  
606 # We could trigger a build if undefined,
607 # but save that for later.
608 sub get_spatial_index {
609 92 ▶ my $self = shift;
Subroutine get_spatial_index: called CC=1 SCAR=0.0
Pod documented
610  
611 92 return $self->{_spatial_indexes};
612 }
613  
614 # Add the polygon's segments to a spatial index
615 # where the index boxes are as wide as the part
616 # they are in.
617 # The set of spatial indexes is a hash keyed by
618 # the part ID.
619 # $n is the number of boxes - need an automatic way of calculating, poss f(y_range / x_range)
620 sub build_spatial_index {
621 7 ▶ my $self = shift;
Subroutine build_spatial_index: called CC=8 SCAR=20.8
Pod documented
622 7 ▶ my $n = shift || 10;
Condition100%2/2
shift() || 10
l!l
52
MC/DC100%1/1
shift() || 10
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
623  
624 7 $n = int $n;
625  
626 7 ▶ croak 'Cannot build spatial index with <1 boxes'
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 0 7 7
627 if $n < 1;
628  
629 7 my %sp_indexes;
630  
631 7 my @parts = $self->parts;
632  
633 7 my ($x_min, $x_max, $y_min, $y_max);
634  
635 7 my $part_id = 0;
636 7 foreach my $part (@parts) {
637 12 $part_id ++; # parts are indexed base 1
638  
639 12 my $segments = $self->get_segments ($part_id);
640  
641 12 ▶ if (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) { } 7 5 12
642 7 my %bounds = $self->_get_part_bounds ($part_id);
643 7 ($x_min, $y_min, $x_max, $y_max) = @bounds{qw /x_min y_min x_max y_max/};
644 }
645 else {
646 5 ($x_min, $y_min, $x_max, $y_max) = $self->bounds; # faster than searching all points
647 }
648  
649 12 ▶ my $n_boxes = @$segments > 20 ? $n : 1;
Branch: 1 branch
BranchTrueFalseTotal
@$segments > 20 ? : 7 5 12
650 12 my $sp_index = $index_class->new ($n_boxes, $x_min, $y_min, $x_max, $y_max);
651  
652 12 foreach my $segment (@$segments) {
653 7289 my $p1 = $segment->[0];
654 7289 my $p2 = $segment->[1];
655  
656 # bare metal version
657 7289 my $coords = [
658 [$p1->get_x, $p1->get_y],
659 [$p2->get_x, $p2->get_y],
660 ];
661  
662 7289 my @bbox = ($x_min, $y_min, $x_max, $y_max);
663 7289 $sp_index->insert($coords, @bbox);
664 }
665  
666 12 $sp_indexes{$part_id} = $sp_index;
667 }
668  
669 7 $self->{_spatial_indexes} = \%sp_indexes;
670  
671 7 ▶ return wantarray ? %sp_indexes : \%sp_indexes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 2 7
672 }
673  
674 sub _get_part_bounds {
675 7 ▶ my $self = shift;
Subroutine _get_part_bounds: called CC=3 SCAR=11.0
676 7 my $part = shift;
677  
678 7 my $points = $self->get_part($part);
679  
680 7 my $pt1 = shift @$points;
681 7 my ($x_min, $y_min) = ($pt1->get_x, $pt1->get_y);
682 7 my ($x_max, $y_max) = ($x_min, $y_min);
683  
684 7 foreach my $pt (@$points) {
685 229 my $x = $pt->get_x;
686 229 my $y = $pt->get_y;
687  
688 229 $x_min = min ($x_min, $x);
689 229 $y_min = min ($y_min, $y);
690 229 $x_max = max ($x_max, $x);
691 229 $y_max = max ($y_max, $y);
692 }
693  
694 7 my %bounds = (
695 x_min => $x_min,
696 x_max => $x_max,
697 y_min => $y_min,
698 y_max => $y_max,
699 );
700  
701 7 ▶ return wantarray ? %bounds : \%bounds;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 0 7
702 }
703  
704 sub get_segments {
705 12 ▶ my $self = shift;
Subroutine get_segments: called CC=3 SCAR=11.0
Pod documented
706 12 my $part = shift;
707  
708 12 my $points = $self->get_part($part);
709  
710 12 my @segments;
711 12 foreach my $i (0 .. $#$points - 1) {
712 7289 push @segments, [$points->[$i], $points->[$i+1]];
713 }
714  
715 12 ▶ return wantarray ? @segments : \@segments;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 12 12
716 }
717  
718 sub vertex_centroid {
719 0 ▶ my $self = shift;
Subroutine vertex_centroid: not called CC=3 SCAR=24.8
Pod documented
720 0 my $part = shift;
721  
722 0 my ($cx, $cy) = (0, 0);
723  
724 0 my @points = ();
725 0 ▶ if ($part) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part) { } 0 0 0
726 0 @points = $self->get_part($part);
727 }
728 else {
729 0 @points = $self->points;
730 }
731  
732 0 foreach (@points) {
733 0 $cx += $_->X;
734 0 $cy += $_->Y;
735 }
736  
737 Geo::ShapeFile::Point->new(
738 0 X => $cx / @points,
739 Y => $cy / @points,
740 );
741 }
742 2 *centroid = \&vertex_centroid;
743  
744 sub area_centroid {
745 0 ▶ my ( $self, $part ) = @_;
Subroutine area_centroid: not called CC=4 SCAR=30.0
Pod documented
746  
747 0 my ( $cx, $cy ) = ( 0, 0 );
748 0 my $A = 0;
749  
750 0 my (@points, @parts);
751  
752 0 ▶ if ( defined $part ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $part) { } 0 0 0
753 0 @parts = ( $part );
754 }
755 else {
756 0 @parts = (1 .. $self->num_parts);
757 }
758  
759 0 for my $part ( @parts ) {
760 0 my ( $p0, @pts ) = $self->get_part( $part );
761 0 my ( $x0, $y0 ) = ( $p0->X, $p0->Y );
762 0 my ( $x1, $y1 ) = ( 0, 0 );
763 0 my ( $cxp, $cyp ) = ( 0, 0 );
764 0 my $Ap = 0;
765  
766 0 for ( @pts ) {
767 0 my $x2 = $_->X - $x0;
768 0 my $y2 = $_->Y - $y0;
769 0 $Ap += ( my $a = $x2*$y1 - $x1*$y2 );
770 0 $cxp += $a * ( $x2 + $x1 ) / 3;
771 0 $cyp += $a * ( $y2 + $y1 ) / 3;
772 0 ( $x1, $y1 ) = ( $x2, $y2 );
773 }
774  
775 0 $cx += $Ap * $x0 + $cxp;
776 0 $cy += $Ap * $y0 + $cyp;
777 0 $A += $Ap;
778 }
779  
780 0 return Geo::ShapeFile::Point->new(
781 X => $cx / $A,
782 Y => $cy / $A,
783 );
784 }
785  
786 sub dump {
787 0 ▶ my $self = shift;
Subroutine dump: not called CC=7 SCAR=40.3
Pod documented
788  
789 0 my $return = '';
790  
791 #$self->points();
792 #$self->get_part();
793 #$self->x_min, x_max, y_min, y_max, z_min, z_max, m_min, m_max
794  
795 0 $return .= sprintf
796 "Shape Type: %s (id: %d) Parts: %d Points: %d\n",
797 $self->shape_type_text(),
798 $self->shape_id(),
799 $self->num_parts(),
800 $self->num_points();
801  
802 0 $return .= sprintf
803 "\tX bounds(min=%s, max=%s)\n",
804 $self->x_min(),
805 $self->x_max();
806  
807 0 $return .= sprintf
808 "\tY bounds(min=%s, max=%s)\n",
809 $self->y_min(),
810 $self->y_max();
811  
812 0 ▶ if (defined $self->z_min() && defined $self->z_max()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->z_min and defined $self->z_max) 0 0 0
Condition0%0/3
defined $self->z_min and defined $self->z_max
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->z_min and defined $self->z_max
defined $self->z_min defined $self->z_max
Truth table0%0/3
A and B
A= defined $self->z_minB= defined $self->z_max
ABresult
0X0
100
111
813 0 $return .= sprintf
814 "\tZ bounds(min=%s, max=%s)\n",
815 $self->z_min(),
816 $self->z_max();
817 }
818  
819 0 ▶ if (defined $self->m_min() && defined $self->m_max()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->m_min and defined $self->m_max) 0 0 0
Condition0%0/3
defined $self->m_min and defined $self->m_max
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->m_min and defined $self->m_max
defined $self->m_min defined $self->m_max
Truth table0%0/3
A and B
A= defined $self->m_minB= defined $self->m_max
ABresult
0X0
100
111
820 0 $return .= sprintf
821 "\tM bounds(min=%s, max=%s)\n",
822 $self->m_min(),
823 $self->m_max();
824 }
825  
826 0 for (1 .. $self->num_parts()) {
827 0 $return .= "\tPart $_:\n";
828 0 foreach ($self->get_part($_)) {
829 0 $return .= "\t\t$_\n";
830 }
831 }
832  
833 0 $return .= "\n";
834  
835 0 return $return;
836 }
837  
838 2 1;
839 __END__
« blib/lib/Geo/ShapeFile/Shape.pm ↑ Summary lib/Geo/ShapeFile/Point.pm »