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

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
1 package Geo::UK::Postcode::CodePointOpen;
2  
3 our $VERSION = '0.005';
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 shift_ll_into_WGS84 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use List::MoreUtils qw/ uniq /;
11 use Text::CSV;
12  
13 has path => ( is => 'ro', isa => Dir, coerce => Dir->coercion );
14 has pc_re => ( is => 'lazy' );
15 has column_headers => ( is => 'lazy' );
16 has csv => ( is => 'lazy' );
17 has metadata => ( is => 'lazy' );
18  
19 sub _build_pc_re {
20 Geo::UK::Postcode::Regex->strict_regex;
21 }
22  
23 sub _build_column_headers {
24 my $self = shift;
25  
26 my $fh = $self->doc_dir->child('Code-Point_Open_Column_Headers.csv')
27 ->filehandle('<');
28  
29 my $short = $self->csv->getline($fh)
30 or die "Unable to read short column headers";
31 my $long = $self->csv->getline($fh)
32 or die "Unable to read long column headers";
33  
34 return {
35 short => $short,
36 long => $long
37 };
38 }
39  
40 sub _build_csv {
41 my $csv = Text::CSV->new( { binary => 1 } )
42 or die Text::CSV->error_diag();
43 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 sub _build_metadata {
53 my $self = shift;
54  
55 my $metadata_file = $self->doc_dir->child('metadata.txt');
56  
57 my @lines = $metadata_file->lines( { chomp => 1 } );
58  
59 my $author = shift @lines;
60  
61 my @headers = grep {/:/} @lines;
62 my @counts = grep {/\t/} @lines;
63  
64 return {
65 AUTHOR => $author,
66 ( map { split /\s*:\s*/ } @headers ),
67 counts =>
68 { map { /\s+([A-Z]{1,2})\t(\d+)/ ? ( $1, $2 ) : () } @counts },
69 };
70 }
71  
72 sub doc_dir {
73 shift->path->child('Doc');
74 }
75  
76 sub data_dir {
77 shift->path->child('Data/CSV');
78 }
79  
80 sub data_files {
81 my ( $self, @outcodes ) = @_;
82  
83 my $areas
84 = join( '|', uniq grep {$_} map { /^([A-Z]+)/i && lc $1 } @outcodes );
85  
86 return sort $self->data_dir->children(
87 $areas ? qr/^(?:$areas)\.csv$/ #
88 : qr/\.csv$/
89 );
90 }
91  
92 sub read_iterator {
93 my ( $self, %args ) = @_;
94  
95 my ( @col_names, $lat_col, $lon_col, $out_col, $in_col );
96 if ( $args{short_column_names} ) {
97 @col_names = @{ $self->column_headers->{short} };
98 ( $lat_col, $lon_col ) = ( 'LA', 'LO' );
99 ( $out_col, $in_col ) = ( 'OC', 'IC' );
100 } else {
101 @col_names = @{ $self->column_headers->{long} };
102 ( $lat_col, $lon_col ) = ( 'Latitude', 'Longitude' );
103 ( $out_col, $in_col ) = ( 'Outcode', 'Incode' );
104 }
105  
106 my @outcodes = @{ $args{outcodes} || [] };
107 my @data_files = $self->data_files(@outcodes);
108  
109 my $match = @outcodes ? join( '|', map {uc} @outcodes ) : undef;
110 $match = qr/^(?:$match)$/ if $match;
111  
112 # Create iterator coderef
113 my $fh2;
114 my $csv = $self->csv;
115  
116 my $iterator = sub {
117  
118 my %pc;
119 while (1) {
120  
121 unless ( $fh2 && !eof $fh2 ) {
122 my $file = shift @data_files or return; # none left
123 $fh2 = $file->filehandle('<');
124 }
125  
126 # Expects:
127 # Postcode,Positional_quality_indicator,Eastings,Northings,...
128 my $row = $csv->getline($fh2);
129  
130 my $i = 0;
131 %pc = map { $_ => $row->[ $i++ ] } @col_names;
132  
133 if ( $args{include_lat_long} && $pc{Eastings} && $pc{Northings} ) {
134 my ( $lat, $lon )
135 = shift_ll_into_WGS84(
136 grid_to_ll( $pc{Eastings}, $pc{Northings} ) );
137  
138 $pc{$lat_col} = sprintf( "%.5f", $lat );
139 $pc{$lon_col} = sprintf( "%.5f", $lon );
140 }
141  
142 if ( $args{split_postcode} || $match ) {
143  
144 $pc{Postcode} =~ s/\s+/ /;
145  
146 my ( $area, $district, $sector, $unit )
147 = eval { $pc{Postcode} =~ $self->pc_re };
148  
149 if ( $@ || !$unit ) {
150 die "Unable to parse '"
151 . $pc{Postcode}
152 . "' : Please report via "
153 . "https://github.com/mjemmeson/Geo-UK-Postcode-Regex/issues\n";
154  
155 } else {
156  
157 next if $match && ($area . $district) !~ $match;
158  
159 if ( $args{split_postcode} ) {
160 $pc{$out_col} = $area . $district;
161 $pc{$in_col} = $sector . $unit;
162 }
163 }
164 }
165  
166 last;
167 }
168  
169 return \%pc;
170 };
171  
172 return $iterator;
173 }
174  
175 sub batch_iterator {
176 my ( $self, %args ) = @_;
177  
178 my $batch_size = $args{batch_size} || 100;
179  
180 my $read_iterator = $self->read_iterator(%args);
181  
182 return sub {
183  
184 my $i = 1;
185 my @postcodes;
186  
187 while ( my $pc = $read_iterator->() ) {
188 push @postcodes, $pc;
189 last if ++$i > $batch_size;
190 }
191  
192 return @postcodes;
193 };
194 }
195  
196 1;
197  
198 __END__
« Build ↑ Summary