Geocoding.pm

filter: Statementstmt 63.0% 41 / 65 Branchbran 35.7% 5 / 14 Conditioncond 23.0% 3 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 53.2% 57 / 107 CC 14 SCAR 38.0
CC
14
Cov
53%
query
63.6
BEGIN
1.0
BEGIN
1.0
CRAP 44.8
↑ Summary Geocoding/Result.pm »
1 package Geo::USCensus::Geocoding;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Geo::USCensus::Geocoding::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Geo::USCensus::Geocoding - The U.S. Census Bureau geocoding service
14  
15 =head1 VERSION
16  
17 Version 0.01
18  
19 =cut
20  
21 3 our $VERSION = '0.01';
22 3 our $DEBUG = 0;
23  
24 =head1 SYNOPSIS
25  
26 use Geo::USCensus::Geocoding;
27  
28 my $request = {
29 # required fields
30 street => '123 Main Street',
31 city => 'San Francisco', # city
32 state => 'CA', # state
33 # optional fields
34 zip => '93102', # zip code
35 benchmark => 'Public_AR_ACS2013', # default is "Public_AR_Current"
36 vintage => 'Census2010_ACS2013', # default is "Current_Current"
37  
38 debug => 1, # will print the URL and some other info
39 };
40 my $result = Geo::USCensus::Geocoding->query($request);
41  
42 if ($result->is_match) {
43 print $result->address,"\n",
44 $result->latitude,", ",$result->longitude,"\n",
45 $result->censustract,"\n";
46 } else {
47 print "No match.\n";
48 }
49  
50 =head1 CLASS METHODS
51  
52 =head2 query HASHREF
53  
54 Send a request to the web service. See
55 L<http://geocoding.geo.census.gov/geocoder> for API documentation. This
56 package will always use the batch method (which seems to be more reliable,
57 as of 2015) and the Geographies return type.
58  
59 Returns an object of class Geo::USCensus::Geocoding::Result.
60  
61 =cut
62  
63 3 my $ua = LWP::UserAgent->new;
64 3 my $url = 'http://geocoding.geo.census.gov/geocoder/geographies/addressbatch';
65  
66 3 my $csv = Text::CSV->new({eol => "\n", binary => 1});
67  
68 # for a current list of benchmark/vintage IDs, download
69 # http://geocoding.geo.census.gov/geocoder/benchmarks
70 # http://geocoding.geo.census.gov/geocoder/vintages?benchmark=<id>
71 # with Accept: application/json
72  
73 2 ▶sub query {
Subroutine query: called CC=14 SCAR=41.5
Pod documented
74 2 my $class = shift;
75 2 my %opt = (
76 returntype => 'geographies',
77 benchmark => 4, # "Current"
78 vintage => 4, # "Current"
79 );
80 2 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
81 0 %opt = (%opt, %{ $_[0] });
82 } else {
83 2 %opt = (%opt, @_);
84 }
85  
86 2 ▶ $DEBUG = $opt{debug} || 0;
Condition50%1/2
$opt{'debug'} || 0
l!l
02
MC/DC0%0/1
$opt{'debug'} || 0
$opt{'debug'}
Truth table50%1/2
A
A= $opt{'debug'}
Aresult
11
00
87  
88 2 my $result = Geo::USCensus::Geocoding::Result->new;
89  
90 2 my @row = ( 1 ); # first element = row identifier
91 # at some point support multiple rows in a single query?
92 2 ▶ if (!$opt{street}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"street"}) 0 2 2
93 0 $result->error_message("Street address is required.");
94 0 return $result;
95 }
96 2 ▶ if (!$opt{zip} and (!$opt{city} or !$opt{state})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"zip"} or $opt{"city"} and $opt{"state"}) 0 2 2
Condition16%1/6
$opt{"zip"} or $opt{"city"} and $opt{"state"}
l!l&&r!l&&!r
200
$opt{"city"} and $opt{"state"}
!ll&&!rl&&r
000
MC/DC0%0/3
$opt{"zip"} or $opt{"city"} and $opt{"state"}
$opt{"zip"} $opt{"city"} $opt{"state"}
Truth table25%1/4
A or B and C
A= $opt{"zip"}B= $opt{"city"}C= $opt{"state"}
ABCresult
1XX1
0111
00X0
0100
97 0 $result->error_message("Either city/state or zip code is required.");
98 0 return $result;
99 }
100 2 foreach (qw(street city state zip)) {
101 8 ▶ push @row, $opt{$_} || '';
Condition50%1/2
$opt{$_} || ''
l!l
80
MC/DC0%0/1
$opt{$_} || ''
$opt{$_}
Truth table50%1/2
A
A= $opt{$_}
Aresult
11
00
102 }
103  
104 2 $csv->combine(@row);
105 2 ▶ warn "Sending:\n".$csv->string."\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Geo::USCensus::Geocoding::DEBUG 0 2 2
106  
107 # they are not picky about content types, Accept headers, etc., but
108 # the uploaded file must have a _name_.
109 my $resp = $ua->request(POST $url,
110 'Content_Type' => 'form-data',
111 'Content' => [ benchmark => $opt{benchmark},
112 vintage => $opt{vintage},
113 returntype => $opt{returntype},
114 2 addressFile => [ undef, 'upload.csv',
115 Content => $csv->string
116 ],
117 ],
118 );
119 2 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 2 2
120 0 $result->content($resp->content);
121 0 my $status = $csv->parse($resp->content);
122 0 my @fields = $csv->fields;
123 0 ▶ if (!$status or @fields < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $status or @fields < 3) 0 0 0
Condition0%0/3
not $status or @fields < 3
l!l&&r!l&&!r
000
MC/DC0%0/2
not $status or @fields < 3
not $status @fields < 3
Truth table0%0/3
not(A) or B
A= not $statusB= @fields < 3
ABresult
1X1
011
000
124 0 $result->error_message("Unable to parse response:\n" . $resp->content);
125 0 return $result;
126 }
127 0 ▶ if ( $fields[2] eq 'Match' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[2] eq 'Match') { } 0 0 0
128 0 $result->is_match(1);
129 0 $result->match_level($fields[3]);
130 0 $result->address($fields[4]);
131 0 my ($long, $lat) = split(',', $fields[5]);
132 0 $result->longitude($long);
133 0 $result->latitude($lat);
134 0 $result->state($fields[8]);
135 0 $result->county($fields[9]);
136 0 $result->tract($fields[10]);
137 0 $result->block($fields[11]);
138 } else {
139 0 $result->is_match(0);
140 }
141 } else {
142 2 $result->error_message( $resp->status_line );
143 }
144  
145 2 return $result;
146 }
147  
148 =head1 AUTHOR
149  
150 Mark Wells, C<< <mark at freeside.biz> >>
151  
152 =head1 SUPPORT
153  
154 Commercial support for this module is available from Freeside Internet
155 Services:
156  
157 L<http://www.freeside.biz/>
158  
159 =back
160  
161  
162 =head1 LICENSE AND COPYRIGHT
163  
164 Copyright (C) 2014 Mark Wells.
165  
166 This program is free software; you can redistribute it and/or modify it
167 under the terms of either: the GNU General Public License as published
168 by the Free Software Foundation; or the Artistic License.
169  
170 See http://dev.perl.org/licenses/ for more information.
171  
172 =cut
173  
174 3 1;
↑ Summary Geocoding/Result.pm »