blib/lib/Geo/UK/Postcode/CodePointOpen.pm

filter: Statementstmt 94.3% 101 / 107 Branchbran 79.4% 27 / 34 Conditioncond 68.0% 17 / 25 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 94.1% 16 / 17 Podpod 100% 5 / 5 totaltotal 84.7% 172 / 203 CC 27 SCAR 18.7
CC
27
Cov
87%
__ANON__
14.5
read_iterator
5.1
_build_column_headers
3.1
CRAP 6.5
↑ Summary
1 package Geo::UK::Postcode::CodePointOpen;
2  
3 6 our $VERSION = '0.007';
4  
5 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Types::Path::Tiny qw/ Dir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Geo::UK::Postcode::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Geo::Coordinates::OSGB qw/ grid_to_ll /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use List::MoreUtils qw/ uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 has path => ( is => 'ro', isa => Dir, coerce => Dir->coercion );
14 6 has pc_re => ( is => 'lazy' );
15 6 has column_headers => ( is => 'lazy' );
16 6 has csv => ( is => 'lazy' );
17 6 has metadata => ( is => 'lazy' );
18  
19 0 ▶sub _build_pc_re {
Subroutine _build_pc_re: not called CC=1 SCAR=6.9
20 0 Geo::UK::Postcode::Regex->strict_regex;
21 }
22  
23 3 ▶sub _build_column_headers {
Subroutine _build_column_headers: called CC=3 SCAR=11.3
24 3 my $self = shift;
25  
26 3 my $fh = $self->doc_dir->child('Code-Point_Open_Column_Headers.csv')
27 ->filehandle('<');
28  
29 3 ▶ my $short = $self->csv->getline($fh)
Branch: 1 branch
BranchTrueFalseTotal
my $short = $self->csv->getline($fh) or die "Unable to read short column headers" 3 0 3
30 or die "Unable to read short column headers";
31 3 ▶ my $long = $self->csv->getline($fh)
Branch: 1 branch
BranchTrueFalseTotal
my $long = $self->csv->getline($fh) or die "Unable to read long column headers" 3 0 3
32 or die "Unable to read long column headers";
33  
34 return {
35 3 short => $short,
36 long => $long
37 };
38 }
39  
40 3 ▶sub _build_csv {
Subroutine _build_csv: called CC=2 SCAR=7.2
41 3 ▶ my $csv = Text::CSV->new( { binary => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
my $csv = "Text::CSV"->new({"binary", 1}) or die "Text::CSV"->error_diag 3 0 3
42 or die Text::CSV->error_diag();
43 3 return $csv;
44 }
45  
46 # <author>
47 # PRODUCT: <product>
48 # DATASET VERSION NUMBER: <version>
49 # COPYRIGHT DATE: <YYYYMMDD>
50 # RM UPDATE DATE: <YYYYMMDD>
51 # XX\t123
52 1 ▶sub _build_metadata {
Subroutine _build_metadata: called CC=2 SCAR=6.9
53 1 my $self = shift;
54  
55 1 my $metadata_file = $self->doc_dir->child('metadata.txt');
56  
57 1 my @lines = $metadata_file->lines( { chomp => 1 } );
58  
59 1 my $author = shift @lines;
60  
61 1 my @headers = grep {/:/} @lines;
62 1 my @counts = grep {/\t/} @lines;
63  
64 return {
65 AUTHOR => $author,
66 4 ( map { split /\s*:\s*/ } @headers ),
67 counts =>
68 1 ▶ { map { /\s+([A-Z]{1,2})\t(\d+)/ ? ( $1, $2 ) : () } @counts },
Branch: 1 branch
BranchTrueFalseTotal
/\s+([A-Z]{1,2})\t(\d+)/ ? : 2 0 2
69 };
70 }
71  
72 4 ▶sub doc_dir {
Subroutine doc_dir: called CC=1 SCAR=0.0
Pod documented
73 4 shift->path->child('Doc');
74 }
75  
76 8 ▶sub data_dir {
Subroutine data_dir: called CC=1 SCAR=0.0
Pod documented
77 8 shift->path->child('Data/CSV');
78 }
79  
80 8 ▶sub data_files {
Subroutine data_files: called CC=3 SCAR=11.2
Pod documented
81 8 my ( $self, @outcodes ) = @_;
82  
83 my $areas
84 8 ▶ = join( '|', uniq grep {$_} map { /^([A-Z]+)/i && lc $1 } @outcodes );
Condition33%1/3
/^([A-Z]+)/i && lc $1
!ll&&!rl&&r
005
MC/DC0%0/2
/^([A-Z]+)/i && lc $1
/^([A-Z]+)/i lc $1
Truth table33%1/3
A && B
A= /^([A-Z]+)/iB= lc $1
ABresult
0X0
100
111
85  
86 8 ▶ return sort $self->data_dir->children(
Branch: 1 branch
BranchTrueFalseTotal
$areas ? : 3 5 8
87 $areas ? qr/^(?:$areas)\.csv$/ #
88 : qr/\.csv$/
89 );
90 }
91  
92 5 ▶sub read_iterator {
Subroutine read_iterator: called CC=5 SCAR=16.3
Pod documented
93 5 my ( $self, %args ) = @_;
94  
95 5 my ( @col_names, $lat_col, $lon_col, $out_col, $in_col );
96 5 ▶ if ( $args{short_column_names} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'short_column_names'}) { } 0 5 5
97 0 @col_names = @{ $self->column_headers->{short} };
98 0 ( $lat_col, $lon_col ) = ( 'LA', 'LO' );
99 0 ( $out_col, $in_col ) = ( 'OC', 'IC' );
100 } else {
101 5 @col_names = @{ $self->column_headers->{long} };
102 5 ( $lat_col, $lon_col ) = ( 'Latitude', 'Longitude' );
103 5 ( $out_col, $in_col ) = ( 'Outcode', 'Incode' );
104 }
105  
106 5 ▶ my @outcodes = @{ $args{outcodes} || [] };
Condition100%2/2
$args{'outcodes'} || []
l!l
14
MC/DC100%1/1
$args{'outcodes'} || []
$args{'outcodes'}
Truth table100%2/2
A
A= $args{'outcodes'}
Aresult
11
00
107 5 my @data_files = $self->data_files(@outcodes);
108  
109 5 ▶ my $match = @outcodes ? join( '|', map {uc} @outcodes ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@outcodes ? : 1 4 5
110 5 ▶ $match = qr/^(?:$match)$/ if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 1 4 5
111  
112 # Create iterator coderef
113 5 my $fh2;
114 5 my $csv = $self->csv;
115  
116 95 ▶ my $iterator = sub {
Subroutine __ANON__: called CC=14 SCAR=26.8
117  
118 95 my %pc;
119 95 while (1) {
120  
121 95 ▶ unless ( $fh2 && !eof $fh2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh2 and not eof $fh2) 14 81 95
Condition100%3/3
$fh2 and not eof $fh2
!ll&&!rl&&r
5981
MC/DC100%2/2
$fh2 and not eof $fh2
$fh2 not eof $fh2
Truth table100%3/3
A and not(B)
A= $fh2B= not eof $fh2
ABresult
0X0
100
111
122 14 ▶ my $file = shift @data_files or return; # none left
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift @data_files or return 9 5 14
123 9 $fh2 = $file->filehandle('<');
124 }
125  
126 # Expects:
127 # Postcode,Positional_quality_indicator,Eastings,Northings,...
128 90 my $row = $csv->getline($fh2);
129  
130 90 my $i = 0;
131 90 %pc = map { $_ => $row->[ $i++ ] } @col_names;
132  
133 90 ▶ if ( $args{include_lat_long} && $pc{Eastings} && $pc{Northings} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"include_lat_long"} and $pc{"Eastings"} and $pc{"Northings"}) 20 70 90
Condition66%4/6
$args{"include_lat_long"} and $pc{"Eastings"} and $pc{"Northings"}
!ll&&!rl&&r
70020
$args{"include_lat_long"} and $pc{"Eastings"}
!ll&&!rl&&r
70020
MC/DC33%1/3
$args{"include_lat_long"} and $pc{"Eastings"} and $pc{"Northings"}
$args{"include_lat_long"} $pc{"Eastings"} $pc{"Northings"}
Truth table50%2/4
A and B and C
A= $args{"include_lat_long"}B= $pc{"Eastings"}C= $pc{"Northings"}
ABCresult
0XX0
10X0
1100
1111
134 20 my ( $lat, $lon ) = grid_to_ll( $pc{Eastings}, $pc{Northings} );
135  
136 20 $pc{$lat_col} = sprintf( "%.5f", $lat );
137 20 $pc{$lon_col} = sprintf( "%.5f", $lon );
138 }
139  
140 90 ▶ if ( $args{split_postcode} || $match ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"split_postcode"} or $match) 30 60 90
Condition100%3/3
$args{"split_postcode"} or $match
l!l&&r!l&&!r
201060
MC/DC100%2/2
$args{"split_postcode"} or $match
$args{"split_postcode"} $match
Truth table100%3/3
A or B
A= $args{"split_postcode"}B= $match
ABresult
1X1
011
000
141  
142 30 $pc{Postcode} =~ s/\s+/ /;
143  
144 my ( $area, $district, $sector, $unit )
145 30 = eval { $pc{Postcode} =~ $self->pc_re };
146  
147 30 ▶ if ( $@ || !$unit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $unit) { } 0 30 30
Condition33%1/3
$@ or not $unit
l!l&&r!l&&!r
0030
MC/DC0%0/2
$@ or not $unit
$@ not $unit
Truth table33%1/3
A or not(B)
A= $@B= not $unit
ABresult
1X1
011
000
148 die "Unable to parse '"
149 . $pc{Postcode}
150 0 . "' : Please report via "
151 . "https://github.com/mjemmeson/Geo-UK-Postcode-Regex/issues\n";
152  
153 } else {
154  
155 30 ▶ next if $match && ( $area . $district ) !~ $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match and not +($area . $district) =~ /$match/ 0 30 30
Condition66%2/3
$match and not +($area . $district) =~ /$match/
!ll&&!rl&&r
20100
MC/DC0%0/2
$match and not +($area . $district) =~ /$match/
$match not +($area . $district) =~ /$match/
Truth table66%2/3
A and not(B)
A= $matchB= not +($area . $district) =~ /$match/
ABresult
0X0
100
111
156  
157 30 ▶ if ( $args{split_postcode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"split_postcode"}) 20 10 30
158 20 $pc{$out_col} = $area . $district;
159 20 $pc{$in_col} = $sector . $unit;
160 }
161 }
162 }
163  
164 90 last;
165 }
166  
167 90 return \%pc;
168 5 };
169  
170 5 return $iterator;
171 }
172  
173 1 ▶sub batch_iterator {
Subroutine batch_iterator: called CC=2 SCAR=6.9
Pod documented
174 1 my ( $self, %args ) = @_;
175  
176 1 ▶ my $batch_size = $args{batch_size} || 100;
Condition50%1/2
$args{'batch_size'} || 100
l!l
10
MC/DC0%0/1
$args{'batch_size'} || 100
$args{'batch_size'}
Truth table50%1/2
A
A= $args{'batch_size'}
Aresult
11
00
177  
178 1 my $read_iterator = $self->read_iterator(%args);
179  
180 6 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
181  
182 6 my $i = 1;
183 6 my @postcodes;
184  
185 6 ▶ while ( my $pc = $read_iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pc = &$read_iterator()) 20 1 21
186 20 push @postcodes, $pc;
187 20 ▶ last if ++$i > $batch_size;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i > $batch_size 5 15 20
188 }
189  
190 6 return @postcodes;
191 1 };
192 }
193  
194 6 1;
195  
196 __END__
↑ Summary