blib/lib/Geo/MapInfo/MIF/Writer/Region.pm

filter: Statementstmt 20.4% 20 / 98 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 6 / 12 Podpod 100% 4 / 4 totaltotal 18.6% 30 / 161 CC 28 SCAR 47.1
CC
28
Cov
14%
save
182.0
_columns
156.0
basename
12.0
CRAP 110.8
↑ Summary
1 package Geo::MapInfo::MIF::Writer::Region;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Path::Class qw{file};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::CSV_XS qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION='0.05';
10  
11 =head1 NAME
12  
13 Geo::MapInfo::MIF::Writer::Region - Perl extension for writing MapInfo Interchange Format (MIF) Region files.
14  
15 =head1 SYNOPSIS
16  
17 use Geo::MapInfo::MIF::Writer::Region;
18 my $map=Geo::MapInfo::MIF::Writer::Region->new(basename=>$basename);
19 $map->addSimpleRegion(
20 data => {col1=>"val1", col2=>"val2"},
21 region => [[$lon1, $lat1], [$lon2, $lat2], [$lon3, $lat3], [$lon4, $lat4]],
22 );
23 $map->addMultipartRegion(
24 data => {col1=>"val1", col2=>"val2"},
25 regions => [ #note the "s" in regions
26 [[$lon1a, $lat1a], [$lon2a, $lat2a], [$lon3a, $lat3a], [$lon4a, $lat4a]],
27 [[$lon1b, $lat1b], [$lon2b, $lat2b], [$lon3b, $lat3b], [$lon4b, $lat4b]],
28 [[$lon1c, $lat1c], [$lon2c, $lat2c], [$lon3c, $lat3c], [$lon4c, $lat4c]],
29 ],
30 );
31 $map->save;
32  
33 =head1 DESCRIPTION
34  
35 Perl extension for writing MapInfo Interchange Format (MIF) Region files.
36  
37 Note: This package stores data in memory before writing so it may not be appropriate for every use.
38  
39 =head1 USAGE
40  
41 =head2 new
42  
43 Creates a new object.
44  
45 my $map=Geo::MapInfo::MIF::Writer::Region->new;
46  
47 =head2 basename
48  
49 Sets and returns the basename of the mid/mif files.
50  
51 $map->basename("basename");
52 $map->basename("./path/basename");
53 $map->basename("/path/basename");
54 $map->basename(undef); #default is "mapinfo-yyyymmddhhmiss"
55  
56 =cut
57  
58 0 ▶sub basename {
Subroutine basename: not called CC=3 SCAR=24.8
Pod documented
59 0 my $self=shift;
60 0 ▶ $self->{"basename"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
61 0 ▶ unless (defined($self->{"basename"})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"basename"}) 0 0 0
62 0 my $dt=DateTime->now;
63 0 $self->{"basename"}=sprintf("mapinfo-%s%s", $dt->ymd(""), $dt->hms(""));
64 }
65 0 return $self->{"basename"};
66 }
67  
68 =head2 save
69  
70 Writes mid and mif files to the name indicated by basename.
71  
72 $map->save;
73  
74 Note: This method overwrites files if they exist.
75  
76 =cut
77  
78 0 ▶sub save {
Subroutine save: not called CC=13 SCAR=52.0
Pod documented
79 0 my $self=shift;
80 0 my @column=$self->_columns; #before we open files
81 0 my @row=$self->_rows; #before we open files
82 0 my $mid=file(join(".", $self->basename, "mid"))->openw;
83 0 my $mif=file(join(".", $self->basename, "mif"))->openw;
84 0 print $mif qq{Version 300\r\n};
85 0 print $mif qq{Charset "WindowsLatin1"\r\n};
86 0 print $mif qq{Delimiter ","\r\n};
87 0 print $mif qq{CoordSys Earth Projection 1, 0\r\n}; #WGS-84 only!
88 0 print $mif sprintf("Columns %s\r\n", scalar(@column));
89 0 foreach my $col (@column) {
90 0 my $name=$col->{"name"};
91 0 my $type=$col->{"type"};
92 $type=sprintf("%s(%s)", $col->{"type"}, $col->{"length"})
93 0 ▶ if $col->{"type"} eq "Char";
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"type"} eq "Char" 0 0 0
94 0 print $mif sprintf(" %s %s\r\n", $name, $type);
95 }
96 0 print $mif "Data\r\n\r\n";
97 0 my $count=1;
98 0 foreach my $row (@row) {
99 #Debug
100 #use Data::Dumper qw{Dumper};
101 #print Dumper($row);
102 #add to mid
103 0 my $csv=Text::CSV_XS->new;
104 0 ▶ my $data=$row->{"data"} || {id=>$count++};
Condition0%0/2
$row->{'data'} || {'id', $count++}
l!l
00
MC/DC0%0/1
$row->{'data'} || {'id', $count++}
$row->{'data'}
Truth table0%0/2
A
A= $row->{'data'}
Aresult
11
00
105 0 my @col=map {$_->{"name"}} @column;
106 0 my $status=$csv->combine(@{$data}{@col});
107 0 ▶ warn sprintf("Text::CSV_XS: %s", $csv->error_input) unless $status == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $status == 1 0 0 0
108 0 print $mid $csv->string."\r\n"; #\r\n per RFC 4180
109 #add to mif
110 0 ▶ my $regions=$row->{"regions"} || [];
Condition0%0/2
$row->{'regions'} || []
l!l
00
MC/DC0%0/1
$row->{'regions'} || []
$row->{'regions'}
Truth table0%0/2
A
A= $row->{'regions'}
Aresult
11
00
111 0 ▶ if (scalar(@$regions) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$regions == 0) { } 0 0 0
112 0 print $mif "none\r\n";
113 } else {
114 0 print $mif sprintf("Region %s\r\n", scalar(@$regions));
115 0 foreach my $region (@$regions) {
116 0 ▶ die("Error: Region must be an array reference.")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $region eq "ARRAY" 0 0 0
117 unless ref($region) eq "ARRAY";
118 0 print $mif sprintf(" %s\r\n", scalar(@$region));
119 0 foreach my $point (@$region) {
120 0 ▶ die("Error: Point must be an array reference.")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $point eq "ARRAY" 0 0 0
121 unless ref($point) eq "ARRAY";
122 0 ▶ die("Error: Point must have two values.")
Branch: 1 branch
BranchTrueFalseTotal
unless @$point == 2 0 0 0
123 unless @$point == 2 ;
124 0 printf $mif "%s %s\r\n", @$point,
125 }
126 }
127 #add optional pen, brush, center to mif
128 }
129 }
130 0 return "1";
131 }
132  
133 # _columns Format
134 # [
135 # name => "", # m/[a-z_][a-z0-9_}{0,30}/i
136 # type => "", # Char, Integer
137 # length => 1-254, # for string types
138 # ]
139  
140 0 ▶sub _columns {
Subroutine _columns: not called CC=12 SCAR=50.5
141 0 my $self=shift;
142 0 my %data=();
143 0 my @column=();
144 0 foreach my $row ($self->_rows) {
145 0 my %column=map {$_->{"name"}=>$_} @column; #updatable reference
146 0 my $data=$row->{"data"};
147 0 ▶ $data={} unless ref($row->{"data"}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row->{"data"} eq "HASH" 0 0 0
148 0 foreach my $key (sort keys %$data) {
149 0 ▶ $data->{$key}="" unless defined $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$key} 0 0 0
150 0 ▶ unless ($key =~ m/[a-z_]{1}[a-z0-9_]{0,30}/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /[a-z_]{1}[a-z0-9_]{0,30}/i) 0 0 0
151 0 warn "MapInfo: A field name can contain only letters, numbers, and '_'. It cannot contain spaces or punctuation marks, and it cannot begin with a number.";
152 0 next;
153 }
154 0 ▶ if (exists $column{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $column{$key}) { } 0 0 0
155 #update max lenght
156 0 my $length=length($data->{$key});
157 0 ▶ $column{$key}->{"length"}=$length if $length>$column{$key}->{"length"};
Branch: 1 branch
BranchTrueFalseTotal
if $length > $column{$key}{"length"} 0 0 0
158 } else {
159 0 my $type="Char";
160 0 ▶ if ($data->{$key}=~m/^[-]?\d{1,10}$/ and abs($data->{$key}) < 2 ** 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$key} =~ /^[-]?\d{1,10}$/ and abs $data->{$key} < "2147483648") 0 0 0
Condition0%0/3
$data->{$key} =~ /^[-]?\d{1,10}$/ and abs $data->{$key} < "2147483648"
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{$key} =~ /^[-]?\d{1,10}$/ and abs $data->{$key} < "2147483648"
$data->{$key} =~ /^[-]?\d{1,10}$/ abs $data->{$key} < "2147483648"
Truth table0%0/3
A and B
A= $data->{$key} =~ /^[-]?\d{1,10}$/B= abs $data->{$key} < "2147483648"
ABresult
0X0
100
111
161 0 $type="Integer";
162 }
163 push @column, {
164 name => $key,
165 type => $type,
166 0 length => length($data->{$key}),
167 };
168 }
169 }
170 }
171 0 ▶ push @column, {name=>"id", type=>"Integer", length=>0} if @column == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @column == 0 0 0 0
172 0 ▶ return wantarray ? @column : \@column;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
173 }
174  
175 =head2 addSimpleRegion
176  
177 Adds a new object to the in memory array.
178  
179 $map->addSimpleRegion(
180 data => {id=>1, col2=>"Foo", col3=>"Bar"}, #default is id=>$index.
181 region => [[$x1,$y1], [$x2,$y2], [$x3,$y3]], #default is "none" which means no geocoded data
182 );
183  
184 =cut
185  
186 0 ▶sub addSimpleRegion {
Subroutine addSimpleRegion: not called CC=1 SCAR=6.9
Pod documented
187 0 my $self=shift;
188 0 my %data=@_;
189 0 $data{"regions"}=[delete($data{"region"})];
190 0 return $self->addMultipartRegion(%data);
191 }
192  
193 =head2 addMultipartRegion
194  
195 Adds a new object to the in memory array.
196  
197 $map->addMultipartRegion(
198 data => {id=>1, col2=>"Foo", col3=>"Bar"},
199 regions => [
200 [[$x1,$y1], [$x2,$y2], [$x3,$y3]],
201 \@r2, #can be island or lake but MapInfo figures that out for you.
202 \@r3,
203 ],
204 );
205  
206 =cut
207  
208 0 ▶sub addMultipartRegion {
Subroutine addMultipartRegion: not called CC=1 SCAR=6.9
Pod documented
209 0 my $self=shift;
210 0 my %data=@_;
211 0 return push @{$self->_rows}, \%data,
212 }
213  
214 0 ▶sub _rows {
Subroutine _rows: not called CC=3 SCAR=24.8
215 0 my $self=shift;
216 0 ▶ $self->{"_rows"}=[] unless ref($self->{"_rows"}) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"_rows"} eq "ARRAY" 0 0 0
217 0 ▶ return wantarray ? @{$self->{"_rows"}} : $self->{"_rows"};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
218 }
219  
220 =head1 LIMITATIONS
221  
222 Currently this package only supports Regions since points and circles are trival to support in MapInfo.
223  
224 Currently we only support string and integer types.
225  
226 =head1 BUGS
227  
228 Please log on RT and send an email to the author.
229  
230 Patches accepted!
231  
232 =head1 SUPPORT
233  
234 DavisNetworks.com supports all Perl applications including this package.
235  
236 =head1 AUTHOR
237  
238 Michael R. Davis
239 CPAN ID: MRDVT
240 DavisNetworks.com
241 davis@davisnetworks.com
242 http://www.davisnetworks.com/
243  
244 =head1 COPYRIGHT
245  
246 This program is free software licensed under the...
247  
248 The BSD License
249  
250 The full text of the license can be found in the LICENSE file included with this module.
251  
252 =head1 SEE ALSO
253  
254 L<Geo::MapInfo::MIF> - MapInfo Interchange Format (MIF) File Reader
255  
256 =cut
257  
258 1 1;
↑ Summary