lib/Geo/ShapeFile.pm

filter: Statementstmt 84.8% 331 / 390 Branchbran 63.6% 84 / 132 Conditioncond 46.0% 23 / 50 MC/DCmcdc 26.9% 7 / 26 Subroutinesub 79.7% 59 / 74 Podpod 91.6% 44 / 48 totaltotal 76.1% 548 / 720 CC 94 SCAR 19.0
CC
94
Cov
77%
__ANON__
30.0
get_dbf_field_info
20.0
shapes_in_area
18.9
CRAP 6.7
« lib/Geo/ShapeFile/Point.pm ↑ Summary blib/lib/Geo/ShapeFile/Shape/Index.pm »
1 package Geo::ShapeFile;
2  
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Geo::ShapeFile::Shape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::Util qw /min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Tree::R;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use constant ON_WINDOWS => ($^O eq 'MSWin32');
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use if ON_WINDOWS, 'Win32::LongPath';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '3.02';
17  
18 2 my $little_endian_sys = unpack 'b', (pack 'S', 1 );
19  
20 # Preloaded methods go here.
21 sub new {
22 43 ▶ my $proto = shift;
Subroutine new: called CC=11 SCAR=24.4
Pod documented
23 43 ▶ my $filebase = shift || croak "Must specify filename!";
Condition33%1/3
shift() || croak('Must specify filename!')
l!l&&r!l&&!r
4300
MC/DC0%0/2
shift() || croak('Must specify filename!')
shift() croak('Must specify filename!')
Truth table33%1/3
A || B
A= shift()B= croak('Must specify filename!')
ABresult
1X1
011
000
24 43 ▶ my $args = shift || {}; # should check it's a haashref
Condition100%2/2
shift() || {}
l!l
2815
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
25  
26 43 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0430
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
27 43 my $self = {};
28  
29 43 $self->{filebase} = $filebase;
30 # should use a proper file name handler
31 # so we can deal with fred.ext referring to fred.ext.shp
32 43 $self->{filebase} =~ s/\.\w{3}$//;
33  
34 $self->{_enable_caching} = {
35 43 shp => 1,
36 dbf => 1,
37 shx => 1,
38 shapes_in_area => 1,
39 };
40 43 $self->{has_shx} = 0;
41 43 $self->{has_shp} = 0;
42 43 $self->{has_dbf} = 0;
43  
44 43 bless $self, $class;
45  
46 # control overall caching
47 43 ▶ if ($args->{no_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$args{"no_cache"}) 14 29 43
48 14 $self->{_no_cache} = 1;
49 }
50  
51 # not sure what this does - possible residual from early plans
52 $self->{_change_cache} = {
53 43 shape_type => undef,
54 records => undef,
55 shp => {},
56 dbf => {},
57 shx => {},
58 };
59 $self->{_object_cache} = {
60 43 shp => {},
61 dbf => {},
62 shx => {},
63 shapes_in_area => {},
64 };
65  
66 43 ▶ if ($self->file_exists ($self->{filebase} . '.shx')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_exists($$self{"filebase"} . '.shx')) 39 4 43
67 39 $self->_read_shx_header();
68 39 $self->{has_shx} = 1;
69 }
70  
71 43 ▶ if ($self->file_exists ($self->{filebase} . '.shp')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_exists($$self{"filebase"} . '.shp')) 39 4 43
72 39 $self->_read_shp_header();
73 39 $self->{has_shp} = 1;
74 }
75  
76 43 ▶ if ($self->file_exists ($self->{filebase} . '.dbf')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_exists($$self{"filebase"} . '.dbf')) 40 3 43
77 40 $self->_read_dbf_header();
78 40 $self->{has_dbf} = 1;
79 }
80  
81 43 ▶ if (!$self->{has_dbf}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"has_dbf"}) 3 40 43
82 croak "$self->{filebase}: shp and/or shx file do not exist or are invalid"
83 3 ▶ if !($self->{has_shp} && $self->{has_shx});
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"has_shp"} and $$self{"has_shx"} 3 0 3
Condition33%1/3
$$self{"has_shp"} and $$self{"has_shx"}
!ll&&!rl&&r
300
MC/DC0%0/2
$$self{"has_shp"} and $$self{"has_shx"}
$$self{"has_shp"} $$self{"has_shx"}
Truth table33%1/3
A and B
A= $$self{"has_shp"}B= $$self{"has_shx"}
ABresult
0X0
100
111
84  
85 0 croak "$self->{filebase}.dbf does not exist or is invalid";
86 }
87  
88 40 return $self;
89 }
90  
91 sub get_file_size {
92 40 ▶ my ($self, $file_name) = @_;
Subroutine get_file_size: called CC=2 SCAR=7.2
Pod undocumented
93  
94 40 my $file_size;
95  
96 40 ▶ if (-e $file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file_name) { } 40 0 40
97 40 $file_size = -s $file_name;
98 }
99 elsif (ON_WINDOWS) {
100 my $stat = statL ($file_name)
101 or die ("unable to get stat for $file_name ($^E)");
102 $file_size = $stat->{size};
103 }
104 else {
105 0 croak "$file_name does not exist or cannot be read, cannot get file size\n";
106 }
107  
108 40 return $file_size;
109 }
110  
111 sub file_exists {
112 129 ▶ my ($self, $file_name) = @_;
Subroutine file_exists: called CC=2 SCAR=6.9
Pod undocumented
113  
114 129 ▶ return 1 if -e $file_name;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file_name 118 11 129
115
116 11 if (ON_WINDOWS) {
117 return testL ('e', $file_name);
118 }
119  
120 11 return;
121 }
122  
123  
124 sub _disable_all_caching {
125 0 ▶ my $self = shift;
Subroutine _disable_all_caching: not called CC=2 SCAR=17.9
126 # a bit nuclear...
127 0 foreach my $type (qw/shp shx dbf shapes_in_area/) {
128 0 $self->{_enable_caching}{$type} = 0;
129 0 $self->{_object_cache} = {};
130 #$self->{_change_cache} = {}; # need to work out what this is for
131 }
132 0 return;
133 }
134  
135 sub caching {
136 27430 ▶ my $self = shift;
Subroutine caching: called CC=2 SCAR=7.2
Pod undocumented
137 27430 my $what = shift;
138 27430 my $flag = shift;
139  
140 27430 ▶ if (defined $flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) 0 27430 27430
141 0 $self->{_enable_caching}->{$what} = $flag;
142 }
143 27430 return $self->{_enable_caching}->{$what};
144 }
145  
146 sub cache {
147 64438 ▶ my ($self, $type, $obj, $cache) = @_;
Subroutine cache: called CC=6 SCAR=18.6
Pod undocumented
148
149 64438 ▶ return if $self->{_no_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_no_cache"} 37008 27430 64438
150  
151 return $self->{_change_cache}->{$type}->{$obj}
152 27430 ▶ if $self->{_change_cache}->{$type} && $self->{_change_cache}->{$type}->{$obj};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_change_cache"}{$type} and $$self{"_change_cache"}{$type}{$obj} 0 27430 27430
Condition33%1/3
$$self{"_change_cache"}{$type} and $$self{"_change_cache"}{$type}{$obj}
!ll&&!rl&&r
0274300
MC/DC0%0/2
$$self{"_change_cache"}{$type} and $$self{"_change_cache"}{$type}{$obj}
$$self{"_change_cache"}{$type} $$self{"_change_cache"}{$type}{$obj}
Truth table33%1/3
A and B
A= $$self{"_change_cache"}{$type}B= $$self{"_change_cache"}{$type}{$obj}
ABresult
0X0
100
111
153  
154 27430 ▶ return if !$self->caching($type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->caching($type) 0 27430 27430
155  
156 27430 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 8371 19059 27430
157 8371 $self->{_object_cache}->{$type}->{$obj} = $cache;
158 }
159 27430 return $self->{_object_cache}->{$type}->{$obj};
160 }
161  
162 # This will trigger the various caching
163 # so we end up with the file in memory.
164 # Not an issue for most files.
165 sub get_all_shapes {
166 8 ▶ my $self = shift;
Subroutine get_all_shapes: called CC=3 SCAR=11.0
Pod documented
167  
168 8 my @shapes;
169  
170 8 foreach my $id (1 .. $self->shapes()) {
171 1427 my $shape = $self->get_shp_record($id);
172 1427 push @shapes, $shape;
173 }
174  
175 8 ▶ return wantarray ? @shapes : \@shapes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 2 8
176 }
177  
178 sub get_shapes_sorted {
179 2 ▶ my $self = shift;
Subroutine get_shapes_sorted: called CC=4 SCAR=13.9
Pod documented
180 2 my $shapes = shift;
181 2 my $sub = shift;
182  
183 2 ▶ if (!defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub) 2 0 2
184 $sub = sub {
185 3593 ▶ my ($s1, $s2) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
186 3593 return $s1->{shp_record_number} <=> $s2->{shp_record_number};
187 2 };
188 }
189  
190 2 ▶ if (!defined $shapes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shapes) 1 1 2
191 1 $shapes = $self->get_all_shapes;
192 }
193  
194 2 my @sorted = sort {$sub->($a, $b)} @$shapes;
195  
196 2 ▶ return wantarray ? @sorted : \@sorted;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
197 }
198  
199 sub get_shapes_sorted_spatially {
200 0 ▶ my $self = shift;
Subroutine get_shapes_sorted_spatially: not called CC=2 SCAR=17.9
Pod documented
201 0 my $shapes = shift;
202 0 my $sub = shift;
203  
204 0 ▶ if (!defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub) 0 0 0
205 $sub = sub {
206 0 ▶ my ($s1, $s2) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
207 return
208 0 ▶ $s1->x_min <=> $s2->x_min
Condition0%0/12
$s1->x_min <=> $s2->x_min || $s1->y_min <=> $s2->y_min || $s1->x_max <=> $s2->x_max || $s1->y_max <=> $s2->y_max || $s1->shape_id <=> $s2->shape_id
l!l&&r!l&&!r
000
$s1->x_min <=> $s2->x_min or $s1->y_min <=> $s2->y_min or $s1->x_max <=> $s2->x_max or $s1->y_max <=> $s2->y_max
l!l&&r!l&&!r
000
$s1->x_min <=> $s2->x_min or $s1->y_min <=> $s2->y_min or $s1->x_max <=> $s2->x_max
l!l&&r!l&&!r
000
$s1->x_min <=> $s2->x_min or $s1->y_min <=> $s2->y_min
l!l&&r!l&&!r
000
MC/DC0%0/5
$s1->x_min <=> $s2->x_min || $s1->y_min <=> $s2->y_min || $s1->x_max <=> $s2->x_max || $s1->y_max <=> $s2->y_max || $s1->shape_id <=> $s2->shape_id
$s1->x_min <=> $s2->x_min $s1->y_min <=> $s2->y_min $s1->x_max <=> $s2->x_max $s1->y_max <=> $s2->y_max $s1->shape_id <=> $s2->shape_id
Truth table0%0/6
A or B or C or D || E
A= $s1->x_min <=> $s2->x_minB= $s1->y_min <=> $s2->y_minC= $s1->x_max <=> $s2->x_maxD= $s1->y_max <=> $s2->y_maxE= $s1->shape_id <=> $s2->shape_id
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
209 || $s1->y_min <=> $s2->y_min
210 || $s1->x_max <=> $s2->x_max
211 || $s1->y_max <=> $s2->y_max
212 || $s1->shape_id <=> $s2->shape_id
213 ;
214 0 };
215 }
216  
217 0 return $self->get_shapes_sorted ($shapes, $sub);
218 }
219  
220 sub build_spatial_index {
221 1 ▶ my $self = shift;
Subroutine build_spatial_index: called CC=2 SCAR=6.9
Pod documented
222  
223 1 my $shapes = $self->get_all_shapes;
224  
225 1 my $rtree = Tree::R->new();
226 1 foreach my $shape (@$shapes) {
227 474 my @bbox = ($shape->x_min, $shape->y_min, $shape->x_max, $shape->y_max);
228 474 $rtree->insert($shape, @bbox);
229 }
230  
231 1 $self->{_spatial_index} = $rtree;
232  
233 1 return $rtree;
234 }
235  
236 sub get_spatial_index {
237 12 ▶ my $self = shift;
Subroutine get_spatial_index: called CC=1 SCAR=0.0
Pod documented
238 12 return $self->{_spatial_index};
239 }
240  
241  
242 sub _read_shx_header {
243 39 ▶ shift()->_read_shx_shp_header('shx', @_);
Subroutine _read_shx_header: called CC=1 SCAR=0.0
244 }
245  
246 sub _read_shp_header {
247 39 ▶ shift()->_read_shx_shp_header('shp', @_);
Subroutine _read_shp_header: called CC=1 SCAR=0.0
248 }
249  
250 sub _read_shx_shp_header {
251 78 ▶ my $self = shift;
Subroutine _read_shx_shp_header: called CC=2 SCAR=7.0
252 78 my $which = shift;
253 78 my $doubles;
254  
255 78 $self->{$which . '_header'} = $self->_get_bytes($which, 0, 100);
256 (
257 $self->{$which . '_file_code'}, $self->{$which . '_file_length'},
258 $self->{$which . '_version'}, $self->{$which . '_shape_type'}, $doubles
259 78 ) = unpack 'N x20 N V2 a64', $self->{$which . '_header'};
260  
261 (
262 $self->{$which . '_x_min'}, $self->{$which . '_y_min'},
263 $self->{$which . '_x_max'}, $self->{$which . '_y_max'},
264 $self->{$which . '_z_min'}, $self->{$which . '_z_max'},
265 78 ▶ $self->{$which . '_m_min'}, $self->{$which . '_m_max'},
Branch: 1 branch
BranchTrueFalseTotal
$little_endian_sys ? : 78 0 78
266 ) = (
267 $little_endian_sys
268 ? (unpack 'd8', $doubles )
269 : (reverse unpack 'd8', scalar reverse $doubles)
270 );
271  
272 78 return 1;
273 }
274  
275 sub type_is {
276 5 ▶ my $self = shift;
Subroutine type_is: called CC=2 SCAR=6.9
Pod documented
277 5 my $type = shift;
278  
279 # numeric code
280 5 ▶ return $self->shape_type == $type
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^[0-9]+$/ 2 3 5
281 if ($type =~ /^[0-9]+$/);
282  
283 3 return (lc $self->type($self->shape_type)) eq (lc $type);
284 }
285  
286 sub get_dbf_field_names {
287 28 ▶ my $self = shift;
Subroutine get_dbf_field_names: called CC=3 SCAR=11.3
Pod documented
288  
289 croak 'dbf field names not loaded yet'
290 28 ▶ if !defined $self->{dbf_field_names};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{"dbf_field_names"} 0 28 28
291  
292 # make sure we return a copy
293 28 my @fld_names = @{$self->{dbf_field_names}};
294  
295 28 ▶ return wantarray ? @fld_names : \@fld_names;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 28 0 28
296 }
297  
298 sub _read_dbf_header {
299 40 ▶ my $self = shift;
Subroutine _read_dbf_header: called CC=11 SCAR=24.2
300  
301 40 $self->{dbf_header} = $self->_get_bytes('dbf', 0, 12);
302 (
303 $self->{dbf_version},
304 $self->{dbf_updated_year},
305 $self->{dbf_updated_month},
306 $self->{dbf_updated_day},
307 $self->{dbf_num_records},
308 $self->{dbf_header_length},
309 $self->{dbf_record_length},
310 40 ) = unpack 'C4 V v v', $self->{dbf_header};
311 # unpack changed from c4 l s s to fix endianess problem
312 # reported by Daniel Gildea
313  
314 my $ls = $self->{dbf_header_length}
315 40 + $self->{dbf_num_records} * $self->{dbf_record_length};
316 40 my $li = $self->get_file_size($self->{filebase} . '.dbf');
317  
318 # some shapefiles (such as are produced by the NOAA NESDIS) don't
319 # have a end-of-file marker in their dbf files, Aleksandar Jelenak
320 # says the ESRI tools don't have a problem with this, so we shouldn't
321 # either
322 40 my $last_byte = $self->_get_bytes('dbf', $li-1, 1);
323 40 ▶ $ls ++ if ord $last_byte == 0x1A;
Branch: 1 branch
BranchTrueFalseTotal
if ord $last_byte == 26 40 0 40
324  
325 40 ▶ croak "dbf: file wrong size (should be $ls, but found $li)"
Branch: 1 branch
BranchTrueFalseTotal
if $ls != $li 0 40 40
326 if $ls != $li;
327  
328 40 my $header = $self->_get_bytes('dbf', 32, $self->{dbf_header_length} - 32);
329 40 my $count = 0;
330 40 $self->{dbf_header_info} = [];
331  
332 40 ▶ while ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 383 0 383
333 383 my $tmp = substr $header, 0, 32, '';
334 383 my $chr = substr $tmp, 0, 1;
335  
336 383 ▶ last if ord $chr == 0x0D;
Branch: 1 branch
BranchTrueFalseTotal
if ord $chr == 13 36 347 383
337 347 ▶ last if length ($tmp) < 32;
Branch: 1 branch
BranchTrueFalseTotal
if length $tmp < 32 4 343 347
338  
339 343 my %tmp = ();
340 (
341 $tmp{name},
342 $tmp{type},
343 $tmp{size},
344 $tmp{decimals}
345 343 ) = unpack 'Z11 Z x4 C2', $tmp;
346  
347 343 $self->{dbf_field_info}->[$count] = {%tmp};
348  
349 343 $count++;
350 }
351  
352 40 $self->{dbf_fields} = $count;
353 40 ▶ croak "dbf: Not enough fields ($count < 1)"
Branch: 1 branch
BranchTrueFalseTotal
if $count < 1 0 40 40
354 if $count < 1;
355  
356 40 my @template = ();
357 40 foreach (@{$self->{dbf_field_info}}) {
358 croak "dbf: Field $_->{name} too short ($_->{size} bytes)"
359 343 ▶ if $_->{size} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $$_{"size"} < 1 0 343 343
360  
361 croak "dbf: Field $_->{name} too long ($_->{size} bytes)"
362 343 ▶ if $_->{size} > 4000;
Branch: 1 branch
BranchTrueFalseTotal
if $$_{"size"} > 4000 0 343 343
363  
364 343 push @template, 'A' . $_->{size};
365 }
366 40 $self->{dbf_record_template} = join ' ', @template;
367  
368 40 my @field_names = ();
369 40 foreach (@{$self->{dbf_field_info}}) {
370 343 push @field_names, $_->{name};
371 }
372 40 $self->{dbf_field_names} = [@field_names];
373  
374 # should return field names?
375 40 return 1;
376 }
377  
378 # needed now there is Geo::ShapeFile::Writer?
379 sub _generate_dbf_header {
380 0 ▶ my $self = shift;
Subroutine _generate_dbf_header: not called CC=1 SCAR=6.9
381  
382 #$self->{dbf_header} = $self->_get_bytes('dbf',0,12);
383 (
384 $self->{dbf_version},
385 $self->{dbf_updated_year},
386 $self->{dbf_updated_month},
387 $self->{dbf_updated_day},
388 $self->{dbf_num_records},
389 $self->{dbf_header_length},
390 $self->{dbf_record_length},
391 0 ) = unpack 'C4 V v v', $self->{dbf_header};
392  
393 $self->{_change_cache}->{dbf_cache}->{header}
394 0 = pack
395 'C4 V v v',
396 3,
397 (localtime)[5],
398 (localtime)[4]+1,
399 (localtime)[3],
400 0, # TODO - num_records,
401 0, # TODO - header_length,
402 0, # TODO - record_length,
403 ;
404 }
405  
406 sub get_dbf_field_info {
407 0 ▶ my $self = shift;
Subroutine get_dbf_field_info: not called CC=4 SCAR=30.0
Pod documented
408
409 0 my $header = $self->{dbf_field_info};
410
411 0 ▶ return if !$header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 0 0 0
412
413 # Return a deep copy to avoid callers
414 # messing up the internals
415 0 my @hdr;
416 0 foreach my $field (@$header) {
417 0 my %h = %$field;
418 0 push @hdr, \%h;
419 }
420  
421 0 ▶ return wantarray ? @hdr : \@hdr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
422 }
423  
424 sub get_dbf_record {
425 9252 ▶ my $self = shift;
Subroutine get_dbf_record: called CC=3 SCAR=11.0
Pod documented
426 9252 my $entry = shift;
427  
428 9252 my $dbf = $self->cache('dbf', $entry);
429  
430 9252 ▶ if (!$dbf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbf) 6939 2313 9252
431 6939 $entry--; # make entry 0-indexed
432  
433 my $record = $self->_get_bytes(
434 'dbf',
435 $self->{dbf_header_length}+($self->{dbf_record_length} * $entry),
436 6939 $self->{dbf_record_length}+1, # +1 for deleted flag
437 );
438 6939 my ($del, @data) = unpack 'c' . $self->{dbf_record_template}, $record;
439  
440 6939 map { s/^\s*//; s/\s*$//; } @data;
441  
442 6939 my %record;
443 6939 @record{@{$self->{dbf_field_names}}} = @data;
444 6939 $record{_deleted} = (ord $del == 0x2A);
445 6939 $dbf = {%record};
446 6939 $self->cache('dbf', $entry + 1, $dbf);
447 }
448  
449 9252 ▶ return wantarray ? %{$dbf} : $dbf;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4626 4626 9252
450 }
451  
452 # needed? not called anywhere
453 sub _set_dbf_record {
454 0 ▶ my $self = shift;
Subroutine _set_dbf_record: not called CC=1 SCAR=6.9
455 0 my $entry = shift;
456 0 my %record = @_;
457  
458 0 $self->{_change_cache}->{dbf}->{$entry} = {%record};
459 }
460  
461 sub _get_shp_shx_header_value {
462 151 ▶ my $self = shift;
Subroutine _get_shp_shx_header_value: called CC=4 SCAR=16.4
463 151 my $val = shift;
464  
465 151 ▶ if (!defined($self->{'shx_' . $val}) && !defined($self->{'shp_' . $val})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$self{'shx_' . $val} or defined $$self{'shp_' . $val}) 0 151 151
Condition33%1/3
defined $$self{'shx_' . $val} or defined $$self{'shp_' . $val}
l!l&&r!l&&!r
15100
MC/DC0%0/2
defined $$self{'shx_' . $val} or defined $$self{'shp_' . $val}
defined $$self{'shx_' . $val} defined $$self{'shp_' . $val}
Truth table33%1/3
A or B
A= defined $$self{'shx_' . $val}B= defined $$self{'shp_' . $val}
ABresult
1X1
011
000
466 0 $self->_read_shx_header(); # ensure we load at least one of the headers
467 }
468  
469 return defined($self->{'shx_' . $val})
470 ? $self->{'shx_' . $val}
471 151 ▶ : $self->{'shp_' . $val};
Branch: 1 branch
BranchTrueFalseTotal
defined $$self{'shx_' . $val} ? : 151 0 151
472 }
473  
474 # factory these
475 29 ▶sub x_min { shift()->_get_shp_shx_header_value('x_min'); }
Subroutine x_min: called CC=1 SCAR=0.0
Pod documented
476 29 ▶sub x_max { shift()->_get_shp_shx_header_value('x_max'); }
Subroutine x_max: called CC=1 SCAR=0.0
Pod documented
477 29 ▶sub y_min { shift()->_get_shp_shx_header_value('y_min'); }
Subroutine y_min: called CC=1 SCAR=0.0
Pod documented
478 29 ▶sub y_max { shift()->_get_shp_shx_header_value('y_max'); }
Subroutine y_max: called CC=1 SCAR=0.0
Pod documented
479 0 ▶sub z_min { shift()->_get_shp_shx_header_value('z_min'); }
Subroutine z_min: not called CC=1 SCAR=6.9
Pod documented
480 0 ▶sub z_max { shift()->_get_shp_shx_header_value('z_max'); }
Subroutine z_max: not called CC=1 SCAR=6.9
Pod documented
481 0 ▶sub m_min { shift()->_get_shp_shx_header_value('m_min'); }
Subroutine m_min: not called CC=1 SCAR=6.9
Pod documented
482 0 ▶sub m_max { shift()->_get_shp_shx_header_value('m_max'); }
Subroutine m_max: not called CC=1 SCAR=6.9
Pod documented
483  
484 sub upper_left_corner {
485 1 ▶ my $self = shift;
Subroutine upper_left_corner: called CC=1 SCAR=0.0
Pod documented
486  
487 1 return Geo::ShapeFile::Point->new(X => $self->x_min, Y => $self->y_max);
488 }
489  
490 sub upper_right_corner {
491 1 ▶ my $self = shift;
Subroutine upper_right_corner: called CC=1 SCAR=0.0
Pod documented
492  
493 1 return Geo::ShapeFile::Point->new(X => $self->x_max, Y => $self->y_max);
494 }
495  
496 sub lower_right_corner {
497 1 ▶ my $self = shift;
Subroutine lower_right_corner: called CC=1 SCAR=0.0
Pod documented
498  
499 1 return Geo::ShapeFile::Point->new(X => $self->x_max, Y => $self->y_min);
500 }
501  
502 sub lower_left_corner {
503 1 ▶ my $self = shift;
Subroutine lower_left_corner: called CC=1 SCAR=0.0
Pod documented
504  
505 1 return Geo::ShapeFile::Point->new(X => $self->x_min, Y => $self->y_min);
506 }
507  
508 sub height {
509 28 ▶ my $self = shift;
Subroutine height: called CC=2 SCAR=6.9
Pod documented
510  
511 28 ▶ return if !$self->records;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->records 2 26 28
512  
513 26 return $self->y_max - $self->y_min;
514 }
515  
516 sub width {
517 28 ▶ my $self = shift;
Subroutine width: called CC=2 SCAR=6.9
Pod documented
518  
519 28 ▶ return if !$self->records;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->records 2 26 28
520  
521 26 return $self->x_max - $self->x_min;
522 }
523  
524 sub corners {
525 0 ▶ my $self = shift;
Subroutine corners: not called CC=1 SCAR=6.9
Pod documented
526  
527 return (
528 0 $self->upper_left_corner,
529 $self->upper_right_corner,
530 $self->lower_right_corner,
531 $self->lower_left_corner,
532 );
533 }
534  
535 sub area_contains_point {
536 59 ▶ my $self = shift;
Subroutine area_contains_point: called CC=4 SCAR=13.9
Pod documented
537 59 my $point = shift;
538  
539 59 my ($x_min, $y_min, $x_max, $y_max) = @_;
540  
541 59 my $x = $point->get_x;
542 59 my $y = $point->get_y;
543  
544 59 ▶ my $result =
Condition88%8/9
$x >= $x_min && $x <= $x_max && $y >= $y_min && $y <= $y_max
!ll&&!rl&&r
4451
$x >= $x_min and $x <= $x_max and $y >= $y_min
!ll&&!rl&&r
2255
$x >= $x_min and $x <= $x_max
!ll&&!rl&&r
0257
MC/DC75%3/4
$x >= $x_min && $x <= $x_max && $y >= $y_min && $y <= $y_max
$x >= $x_min $x <= $x_max $y >= $y_min $y <= $y_max
Truth table80%4/5
A and B and C && D
A= $x >= $x_minB= $x <= $x_maxC= $y >= $y_minD= $y <= $y_max
ABCDresult
0XXX0
10XX0
110X0
11100
11111
545 ($x >= $x_min) &&
546 ($x <= $x_max) &&
547 ($y >= $y_min) &&
548 ($y <= $y_max);
549  
550 59 return $result;
551 }
552  
553 sub bounds_contains_point {
554 59 ▶ my $self = shift;
Subroutine bounds_contains_point: called CC=1 SCAR=0.0
Pod documented
555 59 my $point = shift;
556  
557 59 return $self->area_contains_point (
558 $point,
559 $self->x_min, $self->y_min,
560 $self->x_max, $self->y_max,
561 );
562 }
563  
564 sub file_version {
565 2 ▶ shift()->_get_shp_shx_header_value('version');
Subroutine file_version: called CC=1 SCAR=0.0
Pod documented
566 }
567  
568 sub shape_type {
569 33 ▶ my $self = shift;
Subroutine shape_type: called CC=2 SCAR=7.1
Pod documented
570  
571 return $self->{_change_cache}->{shape_type}
572 33 ▶ if defined $self->{_change_cache}->{shape_type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{"_change_cache"}{"shape_type"} 0 33 33
573  
574 33 return $self->_get_shp_shx_header_value('shape_type');
575 }
576  
577 sub shapes {
578 214 ▶ my $self = shift;
Subroutine shapes: called CC=3 SCAR=11.6
Pod documented
579  
580 return $self->{_change_cache}->{records}
581 214 ▶ if defined $self->{_change_cache}->{records};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{"_change_cache"}{"records"} 0 214 214
582  
583 214 ▶ if (!$self->{shx_file_length}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"shx_file_length"}) 0 214 214
584 0 $self->_read_shx_header();
585 }
586  
587 214 my $filelength = $self->{shx_file_length};
588 214 $filelength -= 50; # don't count the header
589  
590 214 return $filelength / 4;
591 }
592  
593 sub records {
594 197 ▶ my $self = shift;
Subroutine records: called CC=4 SCAR=14.2
Pod documented
595  
596 return $self->{_change_cache}->{records}
597 197 ▶ if defined $self->{_change_cache}->{records};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{"_change_cache"}{"records"} 0 197 197
598  
599 197 ▶ if ($self->{shx_file_length}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$self{'shx_file_length'}) { } 196 1 197
elsif ($$self{'dbf_num_records'}) { } 0 1 1
600 196 my $filelength = $self->{shx_file_length};
601 196 $filelength -= 50; # don't count the header
602 196 return $filelength / 4;
603 }
604 # should perhaps just return dbf_num_records if we get this far?
605 elsif ($self->{dbf_num_records}) {
606 0 return $self->{dbf_num_records};
607 }
608  
609 1 return 0;
610 }
611  
612 sub shape_type_text {
613 28 ▶ my $self = shift;
Subroutine shape_type_text: called CC=1 SCAR=0.0
Pod documented
614  
615 28 return $self->type($self->shape_type());
616 }
617  
618 sub get_shx_record_header {
619 0 ▶ shift()->get_shx_record(@_);
Subroutine get_shx_record_header: not called CC=1 SCAR=6.9
Pod documented
620 }
621  
622 sub get_shx_record {
623 17630 ▶ my $self = shift;
Subroutine get_shx_record: called CC=3 SCAR=11.0
Pod documented
624 17630 my $entry = shift;
625  
626 17630 ▶ croak 'must specify entry index'
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 17630 17630
627 if !$entry;
628  
629 17630 my $shx = $self->cache('shx', $entry);
630  
631 17630 ▶ if (!$shx) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shx) 12520 5110 17630
632 12520 my $record = $self->_get_bytes('shx', (($entry - 1) * 8) + 100, 8);
633 12520 $shx = [unpack 'N N', $record];
634 12520 $self->cache('shx', $entry, $shx);
635 }
636  
637 17630 return @{$shx};
638 }
639  
640 sub get_shp_record_header {
641 4626 ▶ my $self = shift;
Subroutine get_shp_record_header: called CC=1 SCAR=0.0
Pod documented
642 4626 my $entry = shift;
643  
644 4626 my($offset) = $self->get_shx_record($entry);
645  
646 4626 my $record = $self->_get_bytes('shp', $offset * 2, 8);
647 4626 my ($number, $content_length) = unpack 'N N', $record;
648  
649 4626 return ($number, $content_length);
650 }
651  
652  
653 # returns indexes, not objects - need to change that or add method for shape_objects_in_area
654 sub shapes_in_area {
655 12 ▶ my $self = shift;
Subroutine shapes_in_area: called CC=13 SCAR=29.4
Pod documented
656 12 my @area = @_; # x_min, y_min, x_max, y_max,
657  
658 12 ▶ if (my $sp_index = $self->get_spatial_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sp_index = $self->get_spatial_index) 2 10 12
659 2 my $shapes = [];
660 2 $sp_index->query_partly_within_rect (@area, $shapes);
661 2 my @indexes;
662 2 foreach my $shape (@$shapes) {
663 604 push @indexes, $shape->shape_id;
664 }
665 2 ▶ return wantarray ? @indexes : \@indexes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
666 }
667  
668 10 my @results = ();
669 SHAPE:
670 10 foreach my $shp_id (1 .. $self->shapes) {
671 962 my ($offset, $content_length) = $self->get_shx_record($shp_id);
672 962 my $type = unpack 'V', $self->_get_bytes ('shp', $offset * 2 + 8, 4);
673  
674 962 ▶ next SHAPE if $self->type($type) eq 'Null';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type($type) eq "Null" 0 962 962
675  
676 962 ▶ if ($self->type($type) =~ /^Point/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->type($type) =~ /^Point/) { } 0 962 962
elsif ($self->type($type) =~ /^(PolyLine|Polygon|MultiPoint|MultiPatch)/) { } 962 0 962
677 0 my $bytes = $self->_get_bytes('shp', $offset * 2 + 12, 16);
678 0 ▶ my ($x, $y) = (
Branch: 1 branch
BranchTrueFalseTotal
$little_endian_sys ? : 0 0 0
679 $little_endian_sys
680 ? (unpack 'dd', $bytes )
681 : (reverse unpack 'dd', scalar reverse $bytes)
682 );
683 0 my $pt = Geo::ShapeFile::Point->new(X => $x, Y => $y);
684 0 ▶ if ($self->area_contains_point($pt, @area)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->area_contains_point($pt, @area)) 0 0 0
685 0 push @results, $shp_id;
686 }
687 }
688 elsif ($self->type($type) =~ /^(PolyLine|Polygon|MultiPoint|MultiPatch)/) {
689 962 my $bytes = $self->_get_bytes('shp', ($offset * 2) + 12, 32);
690 962 ▶ my @p = (
Branch: 1 branch
BranchTrueFalseTotal
$little_endian_sys ? : 962 0 962
691 $little_endian_sys
692 ? (unpack 'd4', $bytes )
693 : (reverse unpack 'd4', scalar reverse $bytes )
694 );
695 962 ▶ if ($self->check_in_area(@p, @area)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_in_area(@p, @area)) 615 347 962
696 615 push @results, $shp_id;
697 }
698 }
699 else {
700 0 print 'type=' . $self->type($type) . "\n";
701 }
702 }
703  
704 10 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 2 10
705 }
706  
707 sub check_in_area {
708 962 ▶ my $self = shift;
Subroutine check_in_area: called CC=4 SCAR=13.9
Pod documented
709 my (
710 962 $x1_min, $y1_min, $x1_max, $y1_max,
711 $x2_min, $y2_min, $x2_max, $y2_max,
712 ) = @_;
713  
714 962 ▶ my $result = !(
Condition88%8/9
$x1_min > $x2_max || $x1_max < $x2_min || $y1_min > $y2_max || $y1_max < $y2_min
l!l&&r!l&&!r
3470615
$x1_min > $x2_max or $x1_max < $x2_min or $y1_min > $y2_max
l!l&&r!l&&!r
213134615
$x1_min > $x2_max or $x1_max < $x2_min
l!l&&r!l&&!r
2121749
MC/DC75%3/4
$x1_min > $x2_max || $x1_max < $x2_min || $y1_min > $y2_max || $y1_max < $y2_min
$x1_min > $x2_max $x1_max < $x2_min $y1_min > $y2_max $y1_max < $y2_min
Truth table80%4/5
A or B or C || D
A= $x1_min > $x2_maxB= $x1_max < $x2_minC= $y1_min > $y2_maxD= $y1_max < $y2_min
ABCDresult
1XXX1
01XX1
001X1
00011
00000
715 $x1_min > $x2_max
716 or $x1_max < $x2_min
717 or $y1_min > $y2_max
718 or $y1_max < $y2_min
719 );
720  
721 962 return $result;
722 }
723  
724 # SWL: not used anymore - remove?
725 sub _between {
726 0 ▶ my $self = shift;
Subroutine _between: not called CC=3 SCAR=24.8
727 0 my $check = shift;
728  
729 # ensure min then max
730 0 ▶ if ($_[0] > $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] > $_[1]) 0 0 0
731 0 @_ = reverse @_;
732 }
733  
734 0 ▶ return ($check >= $_[0]) && ($check <= $_[1]);
Condition0%0/3
$check >= $_[0] && $check <= $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$check >= $_[0] && $check <= $_[1]
$check >= $_[0] $check <= $_[1]
Truth table0%0/3
A && B
A= $check >= $_[0]B= $check <= $_[1]
ABresult
0X0
100
111
735 }
736  
737 sub bounds {
738 1 ▶ my $self = shift;
Subroutine bounds: called CC=1 SCAR=0.0
Pod documented
739  
740 return (
741 1 $self->x_min, $self->y_min,
742 $self->x_max, $self->y_max,
743 );
744 }
745  
746 # is this ever called?
747 sub _extract_ints {
748 0 ▶ my $self = shift;
Subroutine _extract_ints: not called CC=3 SCAR=24.8
749 0 my $end = shift;
750 0 my @what = @_;
751  
752 0 ▶ my $template = ($end =~ /^l/i) ? 'V': 'N';
Branch: 1 branch
BranchTrueFalseTotal
$end =~ /^l/i ? : 0 0 0
753  
754 0 $self->_extract_and_unpack(4, $template, @what);
755 0 foreach (@what) {
756 0 $self->{$_} = $self->{$_};
757 }
758 }
759  
760 sub get_shp_record {
761 10681 ▶ my $self = shift;
Subroutine get_shp_record: called CC=2 SCAR=6.9
Pod documented
762 10681 my $entry = shift;
763  
764 10681 my $shape = $self->cache('shp', $entry);
765 10681 ▶ if (!$shape) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shape) 7416 3265 10681
766 7416 my($offset, $content_length) = $self->get_shx_record($entry);
767  
768 7416 my $record = $self->_get_bytes('shp', $offset * 2, $content_length * 2 + 8);
769  
770 7416 $shape = Geo::ShapeFile::Shape->new();
771 7416 $shape->parse_shp($record);
772 7416 $self->cache('shp', $entry, $shape);
773 }
774  
775 10681 return $shape;
776 }
777  
778 sub shx_handle {
779 12559 ▶ shift()->_get_handle('shx');
Subroutine shx_handle: called CC=1 SCAR=0.0
Pod documented
780 }
781  
782 sub shp_handle {
783 14005 ▶ shift()->_get_handle('shp');
Subroutine shp_handle: called CC=1 SCAR=0.0
Pod documented
784 }
785  
786 sub dbf_handle {
787 7059 ▶ shift()->_get_handle('dbf');
Subroutine dbf_handle: called CC=1 SCAR=0.0
Pod documented
788 }
789  
790 sub _get_handle {
791 33623 ▶ my $self = shift;
Subroutine _get_handle: called CC=4 SCAR=13.9
792 33623 my $which = shift;
793  
794 33623 my $han = $which . '_handle';
795  
796 33623 ▶ if (!$self->{$han}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{$han}) 118 33505 33623
797 118 my $file = join '.', $self->{filebase}, $which;
798 118 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 118 0 118
799 118 $self->{$han} = IO::File->new;
800 croak "Couldn't get file handle for $file: $!"
801 118 ▶ if not $self->{$han}->open($file, O_RDONLY | O_BINARY);
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{$han}->open($file, 0) 0 118 118
802 }
803 elsif (ON_WINDOWS) {
804 my $fh;
805 openL (\$fh, '<', $file)
806 or croak ("unable to open $file ($^E)");
807 #$fh = IO::File->new_from_fd ($fh);
808 $self->{$han} = $fh;
809 }
810 118 binmode $self->{$han}; # fix windows bug reported by Patrick Dughi
811 }
812  
813 33623 return $self->{$han};
814 }
815  
816 sub _get_bytes {
817 33623 ▶ my $self = shift;
Subroutine _get_bytes: called CC=4 SCAR=14.0
818 33623 my $file = shift;
819 33623 my $offset = shift;
820 33623 my $length = shift;
821  
822 33623 my $handle = $file . '_handle';
823 33623 my $h = $self->$handle();
824 33623 ▶ $h->seek ($offset, 0)
Branch: 1 branch
BranchTrueFalseTotal
$h->seek($offset, 0) or croak("Couldn't seek to $offset for $file") 0 33623 33623
825 || croak "Couldn't seek to $offset for $file";
826  
827 33623 my $tmp;
828 33623 my $res = $h->read($tmp, $length);
829  
830 33623 ▶ croak "Couldn't read $length bytes from $file at offset $offset ($!)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 33623 33623
831 if !defined $res;
832  
833 33623 ▶ croak "EOF reading $length bytes from $file at offset $offset"
Branch: 1 branch
BranchTrueFalseTotal
if $res == 0 0 33623 33623
834 if $res == 0;
835  
836 33623 return $tmp;
837 }
838  
839  
840 sub type {
841 10333 ▶ my $self = shift;
Subroutine type: called CC=1 SCAR=0.0
Pod documented
842 10333 my $shape = shift;
843  
844 # should make this a package lexical
845 10333 my %shape_types = qw(
846 0 Null
847 1 Point
848 3 PolyLine
849 5 Polygon
850 8 MultiPoint
851 11 PointZ
852 13 PolyLineZ
853 15 PolygonZ
854 18 MultiPointZ
855 21 PointM
856 23 PolyLineM
857 25 PolygonM
858 28 MultiPointM
859 31 MultiPatch
860 );
861  
862 10333 return $shape_types{$shape};
863 }
864  
865 sub find_bounds {
866 28 ▶ my $self = shift;
Subroutine find_bounds: called CC=3 SCAR=11.0
Pod documented
867 28 my @objects = @_;
868  
869 28 ▶ return if !scalar @objects;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @objects 2 26 28
870  
871 26 my $obj1 = shift @objects;
872  
873 # assign values from first object to start
874 26 my $x_min = $obj1->x_min();
875 26 my $y_min = $obj1->y_min();
876 26 my $x_max = $obj1->x_max();
877 26 my $y_max = $obj1->y_max();
878  
879  
880 26 foreach my $obj (@objects) {
881 4600 $x_min = min ($x_min, $obj->x_min());
882 4600 $y_min = min ($y_min, $obj->y_min());
883 4600 $x_max = max ($x_max, $obj->x_max());
884 4600 $y_max = max ($y_max, $obj->y_max());
885 }
886  
887 26 my %bounds = (
888 x_min => $x_min,
889 y_min => $y_min,
890 x_max => $x_max,
891 y_max => $y_max,
892 );
893  
894 26 return %bounds;
895 }
896  
897 # XML::Generator::SVG::ShapeFile fails because it is calling this method
898 # and it does not exist in 2.52 and earlier
899 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
900  
901  
902 2 1;
903 __END__
« lib/Geo/ShapeFile/Point.pm ↑ Summary blib/lib/Geo/ShapeFile/Shape/Index.pm »