blib/lib/Geo/Shapefile/Writer.pm

filter: Statementstmt 98.5% 131 / 133 Branchbran 79.1% 19 / 24 Conditioncond 78.5% 11 / 14 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 92.2% 179 / 194 CC 20 SCAR 16.0
CC
20
Cov
94%
add_shape
9.1
_get_attr_format
6.0
new
4.0
CRAP 5.0
↑ Summary
1 package Geo::Shapefile::Writer;
2 {
3 2 $Geo::Shapefile::Writer::VERSION = '0.005';
4 }
5  
6 # $Id: Writer.pm 16 2014-07-30 08:16:24Z xliosha@gmail.com $
7  
8 # NAME: Geo::Shapefile::Writer
9 # ABSTRACT: simple pureperl shapefile writer
10  
11  
12 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use XBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use List::Util qw/ min max /;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24  
25 2 my %shape_type = (
26 # extend
27 NULL => 0,
28 POINT => 1,
29 POLYLINE => 3,
30 POLYGON => 5,
31 );
32  
33  
34  
35 {
36 2 my @default_attr_format = ( C => 64 );
37  
38 sub _get_attr_format {
39 5 ▶ my ($format) = @_;
Subroutine _get_attr_format: called CC=6 SCAR=17.9
40  
41 my @descr = !ref $format ? ($format)
42 : ref $format eq 'ARRAY' ? @$format
43 5 ▶ : ref $format eq 'HASH' ? @$format{ qw/ name type length decimals / }
Branch: 3 branches
BranchTrueFalseTotal
!ref($format) ? : 2 3 5
ref $format eq 'ARRAY' ? : 2 1 3
ref $format eq 'HASH' ? : 1 0 1
44 : ();
45  
46 5 ▶ croak 'Bad format description' if !$descr[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $descr[0] 1 4 5
47  
48 4 ▶ @descr[1,2] = @default_attr_format if !$descr[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $descr[1] 2 2 4
49 4 return \@descr;
50 }
51 }
52  
53 2 sub new {
54 4 ▶ my ($class, $name, $type, @attrs) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
55  
56 4 ▶ my $shape_type = $shape_type{ uc($type || q{}) };
Condition50%1/2
$type || ''
l!l
40
MC/DC0%0/1
$type || ''
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
57 4 ▶ croak "Invalid shape type: $type" if !defined $shape_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shape_type 1 3 4
58  
59 3 my $self = bless {
60 NAME => $name,
61 TYPE => $shape_type,
62 RCOUNT => 0,
63 SHP_SIZE => 50,
64 SHX_SIZE => 50,
65 }, $class;
66  
67 3 my $header_data = $self->_get_header('SHP');
68  
69 3 open $self->{SHP}, '>:raw', "$name.shp";
70 3 print {$self->{SHP}} $header_data;
71  
72 3 open $self->{SHX}, '>:raw', "$name.shx";
73 3 print {$self->{SHX}} $header_data;
74  
75 3 ▶ unlink "$name.dbf" if -f "$name.dbf";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$name.dbf" 0 3 3
76  
77 3 my @fields = map { _get_attr_format($_) } @attrs;
78 $self->{DBF} = XBase->create(
79 name => "$name.dbf",
80 4 field_names => [ map { $_->[0] } @fields ],
81 4 field_types => [ map { $_->[1] } @fields ],
82 4 field_lengths => [ map { $_->[2] } @fields ],
83 2 field_decimals => [ map { $_->[3] } @fields ],
84 );
85  
86 2 return $self;
87 }
88  
89  
90 {
91 2 my $header_size = 100;
92 # position, pack_type, object_field, default
93 2 my @header_fields = (
94 [ 0, 'N', undef, 9994 ], # magic
95 [ 24, 'N', _SIZE => $header_size / 2 ], # file size in 16-bit words
96 [ 28, 'L', undef, 1000 ], # version
97 [ 32, 'L', 'TYPE' ],
98 [ 36, 'd', 'XMIN' ],
99 [ 44, 'd', 'YMIN' ],
100 [ 52, 'd', 'XMAX' ],
101 [ 60, 'd', 'YMAX' ],
102 );
103  
104 sub _get_header {
105 7 ▶ my ($self, $file_type) = @_;
Subroutine _get_header: called CC=4 SCAR=13.9
106  
107 my @use_fields =
108 56 grep { defined $_->[2] }
109 7 ▶ map {[ $_->[0], $_->[1], $_->[2] && ($self->{$_->[2]} // $self->{"$file_type$_->[2]"}) // $_->[3] ]}
Condition100%9/9
$_->[2] && ($$self{$_->[2]} // $$self{"$file_type$_->[2]"}) // $_->[3]
l!l&&r!l&&!r
301412
$_->[2] and $$self{$_->[2]} // $$self{"$file_type$_->[2]"}
!ll&&!rl&&r
14357
$$self{$_->[2]} // $$self{"$file_type$_->[2]"}
l!l&&r!l&&!r
23712
MC/DC50%2/4
$_->[2] && ($$self{$_->[2]} // $$self{"$file_type$_->[2]"}) // $_->[3]
$_->[2] $$self{$_->[2]} $$self{"$file_type$_->[2]"} $_->[3]
Truth table66%4/6
A and B // C // D
A= $_->[2]B= $$self{$_->[2]}C= $$self{"$file_type$_->[2]"}D= $_->[3]
ABCDresult
11XX1
101X1
0XX11
10011
0XX00
10000
110 @header_fields;
111  
112 7 my $pack_string = join q{ }, map { sprintf '@%d%s', @$_ } (@use_fields, [$header_size, q{}]);
113 7 return pack $pack_string, map { $_->[2] } @use_fields;
114 }
115 }
116  
117  
118  
119 2 sub add_shape {
120 4 ▶ my ($self, $data, @attributes) = @_;
Subroutine add_shape: called CC=9 SCAR=22.1
Pod documented
121  
122 4 my ($xmin, $ymin, $xmax, $ymax);
123  
124 4 my $rdata;
125 4 my $type = $self->{TYPE};
126  
127 4 ▶ if ($type == $shape_type{NULL} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == $shape_type{'NULL'}) { } 0 4 4
elsif ($type == $shape_type{'POINT'}) { } 2 2 4
elsif ($type == $shape_type{'POLYLINE'} or $type == $shape_type{'POLYGON'}) { } 2 0 2
128 0 $rdata = pack( 'L', $self->{TYPE} );
129 }
130 elsif ($type == $shape_type{POINT} ) {
131 2 $rdata = pack( 'Ldd', $self->{TYPE}, @$data );
132 2 ($xmin, $ymin, $xmax, $ymax) = ( @$data, @$data );
133 }
134 ▶ elsif ($type == $shape_type{POLYLINE} || $type == $shape_type{POLYGON} ) {
Condition33%1/3
$type == $shape_type{"POLYLINE"} or $type == $shape_type{"POLYGON"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$type == $shape_type{"POLYLINE"} or $type == $shape_type{"POLYGON"}
$type == $shape_type{"POLYLINE"} $type == $shape_type{"POLYGON"}
Truth table33%1/3
A or B
A= $type == $shape_type{"POLYLINE"}B= $type == $shape_type{"POLYGON"}
ABresult
1X1
011
000
135 2 my $rpart = q{};
136 2 my $rpoint = q{};
137 2 my $ipoint = 0;
138  
139 2 for my $line ( @$data ) {
140 3 $rpart .= pack 'L', $ipoint;
141 3 for my $point ( @$line ) {
142 7 my ($x, $y) = @$point;
143 7 $rpoint .= pack 'dd', $x, $y;
144 7 $ipoint ++;
145 }
146 }
147  
148 2 $xmin = min map {$_->[0]} map {@$_} @$data;
149 2 $ymin = min map {$_->[1]} map {@$_} @$data;
150 2 $xmax = max map {$_->[0]} map {@$_} @$data;
151 2 $ymax = max map {$_->[1]} map {@$_} @$data;
152  
153 2 $rdata = pack 'LddddLL', $self->{TYPE}, $xmin, $ymin, $xmax, $ymax, scalar @$data, $ipoint;
154 2 $rdata .= $rpart . $rpoint;
155 }
156
157  
158 4 my $attr0 = $attributes[0];
159 4 ▶ if ( ref $attr0 eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $attr0 eq 'HASH') { } 1 3 4
elsif (ref $attr0 eq 'ARRAY') { } 0 3 3
160 1 $self->{DBF}->set_record_hash( $self->{RCOUNT}, map {( uc($_) => $attr0->{$_} )} keys %$attr0 );
161 }
162 elsif ( ref $attr0 eq 'ARRAY' ) {
163 0 $self->{DBF}->set_record( $self->{RCOUNT}, @$attr0 );
164 }
165 else {
166 3 $self->{DBF}->set_record( $self->{RCOUNT}, @attributes );
167 }
168  
169 4 $self->{RCOUNT} ++;
170  
171 4 print {$self->{SHX}} pack 'NN', $self->{SHP_SIZE}, length($rdata)/2;
172 4 $self->{SHX_SIZE} += 4;
173  
174 4 print {$self->{SHP}} pack 'NN', $self->{RCOUNT}, length($rdata)/2;
175 4 print {$self->{SHP}} $rdata;
176 4 $self->{SHP_SIZE} += 4+length($rdata)/2;
177  
178 4 $self->{XMIN} = min grep {defined} ($xmin, $self->{XMIN});
179 4 $self->{YMIN} = min grep {defined} ($ymin, $self->{YMIN});
180 4 $self->{XMAX} = max grep {defined} ($xmax, $self->{XMAX});
181 4 $self->{YMAX} = max grep {defined} ($ymax, $self->{YMAX});
182  
183 4 return $self;
184 }
185  
186  
187  
188 sub finalize {
189 2 ▶ my $self = shift;
Subroutine finalize: called CC=1 SCAR=0.0
Pod documented
190  
191 2 my $shp = $self->{SHP};
192 2 seek $shp, 0, 0;
193 2 print {$shp} $self->_get_header('SHP');
194 2 close $shp;
195  
196 2 my $shx = $self->{SHX};
197 2 seek $shx, 0, 0;
198 2 print {$shx} $self->_get_header('SHX');
199 2 close $shx;
200  
201 2 $self->{DBF}->close();
202  
203 2 return;
204 }
205  
206 2 1;
207  
208 __END__
↑ Summary