Record.pm

filter: Statementstmt 58.8% 73 / 124 Branchbran 23.0% 6 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 40.9% 18 / 44 Podpod 36.3% 12 / 33 totaltotal 44.1% 109 / 247 CC 18 SCAR 17.4
CC
18
Cov
49%
country
20.0
admin1
20.0
admin2
20.0
CRAP 5.7
« File.pm ↑ Summary DB/SQLite.pm »
1 package Geo::GeoNames::Record;
2  
3 =head1 NAME
4  
5 Geo::GeoNames::Record - Perl module for handling GeoNames.org records
6  
7 =head1 DESCRIPTION
8  
9 Provides a Perl extension for handling GeoNames.org records.
10  
11 =head1 AUTHOR
12  
13 Xiangrui Meng <mengxr@stanford.edu>
14  
15 =head1 LINKS
16  
17 GoeNames:
18 http://www.geonames.org/
19  
20 This package is part of the metadata generation and remediation suite:
21 http://cads.stanford.edu/
22  
23 =head1 COPYRIGHT
24  
25 Copyright (C) 2009 by Xiangrui Meng
26  
27 This library is free software; you can redistribute it and/or modify
28 it under the same terms as Perl itself, either Perl version 5.8.8 or,
29 at your option, any later version of Perl 5 you may have available.
30  
31 =cut
32  
33 3 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 3 require Exporter;
38  
39 3 our @ISA = qw(Exporter);
40  
41 3 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
42  
43 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
44  
45 3 our @EXPORT = qw( );
46  
47 3 our $VERSION = '0.11';
48  
49 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 ## overloadings ##
55  
56 use overload
57 3 ▶ q("") => \&as_string,
Subroutine BEGIN: called CC=1 SCAR=0.0
58 q(eq) => \&op_eq,
59 q(==) => \&op_eq,
60 3 ;
61  
62 ## public static variables ##
63  
64 3 our @fields = qw( geonameid name asciiname alternatenames latitude
65 longitude feature_class feature_code country_code cc2
66 admin1_code admin2_code admin3_code admin4_code
67 population elevation gtopo30 timezone modification_date
68 );
69  
70 3 our $n_fields = @fields;
71  
72 =head1 VARIABLES
73  
74 Each Geo::GeoNames::Record instance has the following fields defined in
75 http://download.geonames.org/export/dump/readme.txt:
76  
77 geonameid : integer id of record in geonames database
78  
79 name : name of geographical point (utf8) varchar(200)
80  
81 asciiname : name of geographical point in plain ascii characters, varchar(200)
82  
83 alternatenames : alternatenames, comma separated varchar(4000) (varchar(5000) for SQL Server)
84  
85 latitude : latitude in decimal degrees (wgs84)
86  
87 longitude : longitude in decimal degrees (wgs84)
88  
89 feature_class : see http://www.geonames.org/export/codes.html, char(1)
90  
91 feature_code : see http://www.geonames.org/export/codes.html, varchar(10)
92  
93 country_code : ISO-3166 2-letter country code, 2 characters
94  
95 cc2 : alternate country codes, comma separated, ISO-3166 2-letter country code, 60 characters
96  
97 admin1_code : fipscode (subject to change to iso code), isocode for the us and ch, see file admin1Codes.txt for display names of this code; varchar(20)
98  
99 admin2_code : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
100  
101 admin3_code : code for third level administrative division, varchar(20)
102  
103 admin4_code : code for fourth level administrative division, varchar(20)
104  
105 population : bigint (4 byte int)
106  
107 elevation : in meters, integer
108  
109 gtopo30 : average elevation of 30'x30' (ca 900mx900m) area in meters, integer
110  
111 timezone : the timezone id (see file timeZone.txt)
112  
113 modification_date : date of last modification in yyyy-MM-dd format
114  
115 For each member variable, we defined a member function to save the curly
116 brackets from your code. For example, you can get the value of
117 $rec->{geonameid} by
118  
119 $rec->geonameid;
120  
121 or set its value by
122  
123 $rec->geonameid = 123456;
124  
125 =cut
126  
127 ## public variables and corresponding member functions ##
128  
129 3 my @_PUBLIC_VARIABLES = @fields;
130  
131 3 foreach (@_PUBLIC_VARIABLES)
132 {
133 0 ▶ eval "sub $_ : lvalue { shift->{$_}; }";
Subroutine admin1_code: not called CC=1 SCAR=5.5
Subroutine admin2_code: not called CC=1 SCAR=5.5
Subroutine admin3_code: not called CC=1 SCAR=5.5
Subroutine admin4_code: not called CC=1 SCAR=5.5
Subroutine alternatenames: not called CC=1 SCAR=5.5
Subroutine asciiname: not called CC=1 SCAR=5.5
Subroutine cc2: not called CC=1 SCAR=5.5
Subroutine country_code: not called CC=1 SCAR=5.5
Subroutine elevation: not called CC=1 SCAR=5.5
Subroutine feature_class: not called CC=1 SCAR=5.5
Subroutine feature_code: not called CC=1 SCAR=5.5
Subroutine geonameid: not called CC=1 SCAR=5.5
Subroutine gtopo30: not called CC=1 SCAR=5.5
Subroutine latitude: not called CC=1 SCAR=5.5
Subroutine longitude: not called CC=1 SCAR=5.5
Subroutine modification_date: not called CC=1 SCAR=5.5
Subroutine name: called CC=1 SCAR=5.5
Subroutine population: not called CC=1 SCAR=5.5
Subroutine timezone: not called CC=1 SCAR=5.5
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
134 }
135  
136 =head1 VARIABLE ALIASES
137  
138 We defined several aliases as followed:
139  
140 id : geonameid;
141  
142 coordinates : (latitude,longitude);
143  
144 =cut
145  
146 3 my %_PUBLIC_VARIABLE_ALIASES = ( id => 'geonameid' );
147  
148 3 foreach (keys %_PUBLIC_VARIABLE_ALIASES)
149 {
150 25 ▶ eval "sub $_ : lvalue { shift->{$_PUBLIC_VARIABLE_ALIASES{$_}}; }";
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
151 }
152  
153 0 ▶sub coordinates : lvalue
Subroutine coordinates: not called CC=1 SCAR=6.9
Pod undocumented
154 {
155 0 my $self = shift;
156 0 ($self->{latitude}, $self->{longitude});
157 }
158  
159 ## private static variables ##
160  
161 3 my $_admins_loaded;
162 my $_admin_code_to_record;
163  
164 =head1 METHODS
165  
166 =over
167  
168 =item new()
169  
170 Constructor for Geo::GeoNames::Record.
171  
172 my $rec = Geo::GeoNames::Record->new();
173  
174 It returns an empty Geo::GeoNames::Record object.
175  
176 my $rec = Geo::GeoNames::Record->new( $str_record );
177  
178 You may also pass a GeoNames record string. It returns the corresponding
179 Geo::GeoNames::Record object or undef if the input is incorrect.
180  
181 =cut
182  
183 sub new
184 31 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
185 31 my $class = shift;
186  
187 31 my $self = bless {}, $class;
188  
189 31 @{$self}{@fields} = ();
190  
191 31 ▶ if ( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 31 0 31
192 {
193 31 $self->parse( @_ );
194 }
195  
196 31 return $self;
197 }
198  
199 =item parse()
200  
201 $rec->parse( $line );
202  
203 Parses a record line from a GeoNames.org data file and updates the current
204 object.
205  
206 =cut
207  
208 sub parse
209 31 ▶{
Subroutine parse: called CC=3 SCAR=11.1
Pod documented
210 31 my ( $self, $str ) = @_;
211  
212 31 chomp($str);
213  
214 31 my @data = split /\t/, $str;
215  
216 31 ▶ if ( @data == $n_fields )
Branch: 1 branch
BranchTrueFalseTotal
if (@data == $Geo::GeoNames::Record::n_fields) { } 31 0 31
217 {
218 31 ▶ for ( my $i = 0; $i < $n_fields; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $Geo::GeoNames::Record::n_fields) 589 31 620
219 {
220 589 $self->{$fields[$i]} = $data[$i];
221 }
222 }
223 else
224 {
225 0 Carp::croak "Wrong number of GeoNames fields";
226 }
227  
228 31 return $self;
229 }
230  
231 =item names()
232  
233 Returns all unique names.
234  
235 =cut
236  
237 sub names
238 14 ▶{
Subroutine names: called CC=1 SCAR=0.0
Pod documented
239 14 my $self = shift;
240  
241 14 my %saw;
242 14 @saw{ ( $self->{name}, $self->{asciiname}, split(/,/, $self->{alternatenames}) ) } = ();
243  
244 14 return keys %saw;
245 }
246  
247 =item has_name( $name )
248  
249 Returns true if the record has a name matching the $name argument.
250  
251 =cut
252  
253 sub has_name
254 3 ▶{
Subroutine has_name: called CC=2 SCAR=6.9
Pod documented
255 3 my ($self, $name) = @_;
256 3 ▶ return ( grep { $_ eq $name; } $self->names() ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $name;} $self->names) ? : 2 1 3
257 }
258  
259 =item country()
260  
261 Return the country of the record as a Geo::GeoNames::Record object.
262  
263 =cut
264  
265 sub country
266 0 ▶{
Subroutine country: not called CC=4 SCAR=30.0
Pod documented
267 0 ▶ _load_admins() unless $_admins_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $_admins_loaded 0 0 0
268  
269 0 my $self = shift;
270
271 0 ▶ if ( $self->{country_code} && exists( $_admin_code_to_record->{$self->{country_code}} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"country_code"} and exists $$_admin_code_to_record{$self->{"country_code"}}) 0 0 0
Condition0%0/3
$self->{"country_code"} and exists $$_admin_code_to_record{$self->{"country_code"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"country_code"} and exists $$_admin_code_to_record{$self->{"country_code"}}
$self->{"country_code"} exists $$_admin_code_to_record{$self->{"country_code"}}
Truth table0%0/3
A and B
A= $self->{"country_code"}B= exists $$_admin_code_to_record{$self->{"country_code"}}
ABresult
0X0
100
111
272 {
273 0 return $_admin_code_to_record->{$self->{country_code}};
274 }
275
276 0 return;
277 }
278  
279 =item admin1()
280  
281 Return the admin1 of the record as a Geo::GeoNames::Record object.
282  
283 =cut
284  
285 sub admin1
286 0 ▶{
Subroutine admin1: not called CC=4 SCAR=30.0
Pod documented
287 0 ▶ _load_admins() unless $_admins_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $_admins_loaded 0 0 0
288  
289 0 my $self = shift;
290
291 0 ▶ if ( $self->{admin1_code} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"admin1_code"}) 0 0 0
292 {
293 0 my $admin1_key = $self->{country_code} . "." . $self->{admin1_code};
294  
295 0 ▶ if ( exists( $_admin_code_to_record->{$admin1_key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_admin_code_to_record->{$admin1_key}) 0 0 0
296 {
297 0 return $_admin_code_to_record->{$admin1_key};
298 }
299 }
300  
301 0 return;
302 }
303  
304 =item admin2
305  
306 Return the admin2 of the record as a Geo::GeoNames::Record object.
307  
308 =cut
309  
310 sub admin2
311 0 ▶{
Subroutine admin2: not called CC=4 SCAR=30.0
Pod documented
312 0 ▶ _load_admins() unless $_admins_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $_admins_loaded 0 0 0
313  
314 0 my $self = shift;
315  
316 0 ▶ if ( $self->{admin2_code} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"admin2_code"}) 0 0 0
317 {
318 0 my $admin2_key = $self->{country_code} . "." . $self->{admin1_code} . "." . $self->{admin2_code};
319  
320 0 ▶ if ( exists( $_admin_code_to_record->{$admin2_key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_admin_code_to_record->{$admin2_key}) 0 0 0
321 {
322 0 return $_admin_code_to_record->{$admin2_key};
323 }
324 }
325  
326 0 return;
327 }
328  
329 =item as_string()
330  
331 Convert the record to a GeoNames.org record line.
332  
333 =cut
334  
335 sub as_string
336 14 ▶{
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
337 14 my $self = shift;
338  
339 14 return join( "\t", @{$self}{@fields} );
340 }
341  
342 =item op_eq()
343  
344 Compare records based on their geonameids.
345  
346 =cut
347  
348 sub op_eq
349 1 ▶{
Subroutine op_eq: called CC=1 SCAR=0.0
Pod documented
350 1 return ($_[0]->id eq $_[1]->id);
351 }
352  
353 =item is_country()
354  
355 Return true if the record is a country.
356  
357 =cut
358  
359 sub is_country
360 0 ▶{
Subroutine is_country: not called CC=2 SCAR=17.9
Pod documented
361 0 my $self = shift;
362 0 ▶ return ( $self->{feature_class} eq 'A' ) && ( $self->{feature_code} =~ /^P/ );
Condition0%0/3
$self->{'feature_class'} eq 'A' && $self->{'feature_code'} =~ /^P/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'feature_class'} eq 'A' && $self->{'feature_code'} =~ /^P/
$self->{'feature_class'} eq 'A' $self->{'feature_code'} =~ /^P/
Truth table0%0/3
A && B
A= $self->{'feature_class'} eq 'A'B= $self->{'feature_code'} =~ /^P/
ABresult
0X0
100
111
363 }
364  
365 =item is_admin1()
366  
367 Return true if the record is a primary administrative division.
368  
369 =cut
370  
371 sub is_admin1
372 0 ▶{
Subroutine is_admin1: not called CC=2 SCAR=17.9
Pod documented
373 0 my $self = shift;
374 0 ▶ return ( $self->{feature_class} eq 'A' ) && ( $self->{feature_code} eq 'ADM1' );
Condition0%0/3
$self->{'feature_class'} eq 'A' && $self->{'feature_code'} eq 'ADM1'
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'feature_class'} eq 'A' && $self->{'feature_code'} eq 'ADM1'
$self->{'feature_class'} eq 'A' $self->{'feature_code'} eq 'ADM1'
Truth table0%0/3
A && B
A= $self->{'feature_class'} eq 'A'B= $self->{'feature_code'} eq 'ADM1'
ABresult
0X0
100
111
375 }
376  
377 =item is_admin2()
378  
379 Return true if the record is a second-order administrative division.
380  
381 =cut
382  
383 sub is_admin2
384 0 ▶{
Subroutine is_admin2: not called CC=2 SCAR=17.9
Pod documented
385 0 my $self = shift;
386 0 ▶ return ( $self->{feature_class} eq 'A' ) && ( $self->{feature_code} eq 'ADM2' );
Condition0%0/3
$self->{'feature_class'} eq 'A' && $self->{'feature_code'} eq 'ADM2'
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'feature_class'} eq 'A' && $self->{'feature_code'} eq 'ADM2'
$self->{'feature_class'} eq 'A' $self->{'feature_code'} eq 'ADM2'
Truth table0%0/3
A && B
A= $self->{'feature_class'} eq 'A'B= $self->{'feature_code'} eq 'ADM2'
ABresult
0X0
100
111
387 }
388  
389 ## private functions ##
390  
391 # load data files for decoding country_code, admin1_code and admin2_code
392  
393 sub _load_admins
394 0 ▶{
Subroutine _load_admins: not called CC=2 SCAR=9.6
395 3 ▶ use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
396 3 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
397
398 0 my $admins_filename = File::Spec->catfile( File::HomeDir->my_home(),
399 ".Geo-GeoNames-Record",
400 "admin_code_to_record.hash" );
401
402 0 ▶ if( -e $admins_filename )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $admins_filename) { } 0 0 0
403 {
404 0 $_admin_code_to_record = Storable::retrieve( $admins_filename );
405 0 $_admins_loaded = 1;
406 }
407 else
408 {
409 0 Carp::croak( "$admins_filename doesn't exist. Please run gn_update_admins first." );
410 }
411 }
412  
413 =back
414  
415 =cut
416  
417 3 1;
418 __END__
« File.pm ↑ Summary DB/SQLite.pm »