2017-09-21 15:17:45 +01:00
package Pear::LocalLoop::Command::codepoint_open ;
use Mojo::Base 'Mojolicious::Command' ;
use Mojo::Util 'getopt' ;
use Geo::UK::Postcode::CodePointOpen ;
has description = > 'Manage Codepoint Open Data' ;
has usage = > sub { shift - > extract_usage } ;
sub run {
my ( $ self , @ args ) = @ _ ;
2017-09-21 15:52:01 +01:00
getopt \ @ args ,
2017-09-21 16:57:14 +01:00
'o|outcodes=s' = > \ my @ outcodes ,
2017-09-21 15:52:01 +01:00
'q|quiet' = > \ my $ quiet_mode ;
2017-09-21 15:17:45 +01:00
my $ cpo_dir = $ self - > app - > home - > child ( 'etc' ) - > child ( 'code-point-open' ) ;
my $ zip_file = $ cpo_dir - > child ( 'codepo_gb.zip' ) - > realpath - > to_string ;
my $ output_dir = $ cpo_dir - > child ( 'codepo_gb' ) - > realpath - > to_string ;
unless ( - d $ output_dir ) {
2017-09-21 15:52:01 +01:00
print "Unzipping code-point-open data\n" unless $ quiet_mode ;
2019-07-14 15:15:14 +01:00
eval { system ( 'unzip' , '-q' , $ zip_file , '-d' , $ output_dir ) } ;
if ( my $ err = $@ ) {
print "Error extracting zip: " . $ err . "\n" ;
print "Manually create etc/code-point-open/codepo_gb directory and extract zip into it" ;
die ;
}
2017-09-21 15:17:45 +01:00
}
my $ cpo = Geo::UK::Postcode::CodePointOpen - > new ( path = > $ output_dir ) ;
2017-09-21 16:57:14 +01:00
printf ( "Importing data for %s outcode(s)\n" , @ outcodes ? join ( ' ' , @ outcodes ) : 'all' )
2017-09-21 15:52:01 +01:00
unless $ quiet_mode ;
2017-09-21 16:57:14 +01:00
2017-09-21 15:17:45 +01:00
my $ iter = $ cpo - > read_iterator (
2017-09-21 15:52:01 +01:00
outcodes = > \ @ outcodes ,
2017-09-21 15:17:45 +01:00
include_lat_long = > 1 ,
split_postcode = > 1 ,
) ;
2017-09-21 16:57:14 +01:00
my $ pc_rs = $ self - > app - > schema - > resultset ( 'GbPostcode' ) ;
2017-09-21 15:52:01 +01:00
while ( my $ pc = $ iter - > ( ) ) {
2017-09-21 16:57:14 +01:00
$ pc_rs - > find_or_create (
{
outcode = > $ pc - > { Outcode } ,
incode = > $ pc - > { Incode } ,
latitude = > $ pc - > { Latitude } ,
longitude = > $ pc - > { Longitude } ,
} ,
{ key = > 'primary' } ,
) ;
2017-09-21 15:52:01 +01:00
}
2017-09-21 15:17:45 +01:00
}
= head1 SYNOPSIS
Usage: APPLICATION codepoint_open [ OPTIONS ]
Options:
2017-09-21 15:52:01 +01:00
- o | - - outcodes <outcode> : limit to specified outcodes ( can be defined
multiple times )
2017-09-21 15:17:45 +01:00
= cut
1 ;