| 1 | package Geo::GNS::Parser; | ||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | require Exporter; | |||||||||
| 5 | 1 | our @ISA = qw(Exporter); | |||||||||
| 6 | 1 | our @EXPORT_OK = qw/parse_file @fields/; | |||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 1 | our $data_dir = '/home/ben/data/gns'; | |||||||||
| 11 | |||||||||||
| 12 | # See L<http://geonames.nga.mil/gns/html/gis_countryfiles.html> for | ||||||||||
| 13 | # explanations. | ||||||||||
| 14 | |||||||||||
| 15 | 1 | our @fields = qw/RC UFI UNI LAT LONG DMS_LAT DMS_LONG MGRS JOG FC DSG | |||||||||
| 16 | PC CC1 ADM1 POP ELEV CC2 NT LC SHORT_FORM GENERIC SORT_NAME_RO | ||||||||||
| 17 | FULL_NAME_RO FULL_NAME_ND_RO SORT_NAME_RG FULL_NAME_RG FULL_NAME_ND_RG | ||||||||||
| 18 | NOTE MODIFY_DATE/; | ||||||||||
| 19 | |||||||||||
| 20 | sub parse_file | ||||||||||
| 21 | 0 | ▶ | { | ||||||||
|
Subroutine
parse_file: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||
| 22 | 0 | my (%options) = @_; | |||||||||
| 23 | 0 | my $file = $options{file}; | |||||||||
| 24 | 0 | my $data = $options{data}; | |||||||||
| 25 | 0 | my $callback = $options{callback}; | |||||||||
| 26 | 0 | my $callback_data = $options{callback_data}; | |||||||||
| 27 | 0 | ▶ | if (! $file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | croak "Specify a file with 'file =>'"; | |||||||||
| 29 | } | ||||||||||
| 30 | 0 | ▶ | if ($file !~ m!/!) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 0 | $file = "$data_dir/$file"; | |||||||||
| 32 | } | ||||||||||
| 33 | 0 | ▶ | open my $input, "<:encoding(utf8)", $file or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | ▶ | while (<$input>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | my @parts = split /\t/; | |||||||||
| 36 | 0 | ▶ | if (@parts != 29) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | die "$file:$.: bad line containing " . scalar (@parts) . " parts.\n"; | |||||||||
| 38 | } | ||||||||||
| 39 | 0 | my %line; | |||||||||
| 40 | 0 | @line{@fields} = @parts; | |||||||||
| 41 | 0 | my $ufi = $line{UFI}; | |||||||||
| 42 | 0 | ▶ | if ($callback) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | &{$callback} ($callback_data, \%line); | |||||||||
| 44 | } | ||||||||||
| 45 | 0 | ▶ | if ($data) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | push @$data, \%line; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | 0 | ▶ | close $input or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 1 | 1; | |||||||||