blib/lib/Geo/Coder/US/Census.pm

filter: Statementstmt 52.3% 45 / 86 Branchbran 18.7% 6 / 32 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 80.0% 12 / 15 Podpod 100% 5 / 5 totaltotal 47.2% 69 / 146 SCAR 43.9
CC
18
Cov
42%
geocode
136.4
ua
6.0
run
6.0
CRAP 80.3
↑ Summary
1 package Geo::Coder::US::Census;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Geo::StreetAddress::US;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Geo::Coder::US::Census - Provides a Geo-Coding functionality for the US using L<https://geocoding.geo.census.gov>
18  
19 =head1 VERSION
20  
21 Version 0.06
22  
23 =cut
24  
25 5 our $VERSION = '0.06';
26  
27 =head1 SYNOPSIS
28  
29 use Geo::Coder::US::Census;
30  
31 my $geo_coder = Geo::Coder::US::Census->new();
32 my $location = $geo_coder->geocode(location => '4600 Silver Hill Rd., Suitland, MD');
33 # Sometimes the server gives a 500 error on this
34 $location = $geo_coder->geocode(location => '4600 Silver Hill Rd., Suitland, MD, USA');
35  
36 =head1 DESCRIPTION
37  
38 Geo::Coder::US::Census provides an interface to geocoding.geo.census.gov. Geo::Coder::US no longer seems to work.
39  
40 =head1 METHODS
41  
42 =head2 new
43  
44 $geo_coder = Geo::Coder::US::Census->new();
45 my $ua = LWP::UserAgent->new();
46 $ua->env_proxy(1);
47 $geo_coder = Geo::Coder::US::Census->new(ua => $ua);
48  
49 =cut
50  
51 sub new {
52 2 ▶ my($class, %param) = @_;
Subroutine new: called CC=3 SCAR=11.2
Pod documented
53  
54 2 my $ua = $param{ua};
55 2 ▶ if(!defined($ua)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 2 0 2
56 2 $ua = LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
57 2 $ua->default_header(accept_encoding => 'gzip,deflate');
58 }
59 2 ▶ my $host = $param{host} || 'geocoding.geo.census.gov/geocoder/locations/address';
Condition50%1/2
$param{'host'} || 'geocoding.geo.census.gov/geocoder/locations/address'
l!l
02
MC/DC0%0/1
$param{'host'} || 'geocoding.geo.census.gov/geocoder/locations/address'
$param{'host'}
Truth table50%1/2
A
A= $param{'host'}
Aresult
11
00
60  
61 2 return bless { ua => $ua, host => $host }, $class;
62 }
63  
64 =head2 geocode
65  
66 $location = $geo_coder->geocode(location => $location);
67 # @location = $geo_coder->geocode(location => $location);
68  
69 print 'Latitude: ', $location->{'latt'}, "\n";
70 print 'Longitude: ', $location->{'longt'}, "\n";
71  
72 =cut
73  
74 sub geocode {
75 1 ▶ my $self = shift;
Subroutine geocode: called CC=14 SCAR=49.2
Pod documented
76 1 my %param;
77  
78 1 ▶ if(ref($_[0]) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 1 1
elsif (ref $_[0]) { } 0 1 1
elsif (@_ % 2 == 0) { } 1 0 1
79 0 %param = %{$_[0]};
80 } elsif(ref($_[0])) {
81 0 Carp::croak('Usage: geocode(location => $location)');
82 } elsif(@_ % 2 == 0) {
83 1 %param = @_;
84 } else {
85 0 $param{location} = shift;
86 }
87  
88 my $location = $param{location}
89 1 ▶ or Carp::croak('Usage: geocode(location => $location)');
Branch: 1 branch
BranchTrueFalseTotal
my $location = $param{"location"} or Carp::croak("Usage: geocode(location => \$location)") 1 0 1
90  
91 0 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 0 0
92 0 $location = Encode::encode_utf8($location);
93 }
94  
95 0 ▶ if($location =~ /,?(.+),\s*(United States|US|USA)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /,?(.+),\s*(United States|US|USA)$/i) 0 0 0
96 0 $location = $1;
97 }
98  
99 # Remove county from the string, if that's included
100 # Assumes not more than one town in a state with the same name
101 # in different counties - but the census Geo-Coding doesn't support that
102 # anyway
103 # Some full state names include spaces, e.g South Carolina
104 # Some roads include full stops, e.g. S. West Street
105 0 ▶ if($location =~ /^(\d+\s+[\w\s\.]+),\s*([\w\s]+),\s*[\w\s]+,\s*([A-Za-z\s]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /^(\d+\s+[\w\s\.]+),\s*([\w\s]+),\s*[\w\s]+,\s*([A-Za-z\s]+)$/) 0 0 0
106 0 $location = "$1, $2, $3";
107 }
108  
109 0 my $uri = URI->new("https://$self->{host}");
110 0 my $hr = Geo::StreetAddress::US->parse_address($location);
111  
112 0 ▶ if((!defined($hr->{'city'})) || (!defined($hr->{'state'}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hr->{"city"} and defined $hr->{"state"}) 0 0 0
Condition0%0/3
defined $hr->{"city"} and defined $hr->{"state"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hr->{"city"} and defined $hr->{"state"}
defined $hr->{"city"} defined $hr->{"state"}
Truth table0%0/3
A and B
A= defined $hr->{"city"}B= defined $hr->{"state"}
ABresult
0X0
100
111
113 0 Carp::carp(__PACKAGE__ . ": city and state are mandatory ($location)");
114 0 return;
115 }
116  
117 my %query_parameters = (
118 'benchmark' => 'Public_AR_Current',
119 'city' => $hr->{'city'},
120 'format' => 'json',
121 0 'state' => $hr->{'state'},
122 );
123 0 ▶ if($hr->{'street'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{"street"}) 0 0 0
124 0 ▶ if($hr->{'number'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{'number'}) { } 0 0 0
125 0 $query_parameters{'street'} = $hr->{'number'} . ' ' . $hr->{'street'} . ' ' . $hr->{'type'};
126 } else {
127 0 $query_parameters{'street'} = $hr->{'street'} . ' ' . $hr->{'type'};
128 }
129 0 ▶ if($hr->{'suffix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{"suffix"}) 0 0 0
130 0 $query_parameters{'street'} .= ' ' . $hr->{'suffix'};
131 }
132 }
133  
134 0 $uri->query_form(%query_parameters);
135 0 my $url = $uri->as_string();
136  
137 0 my $res = $self->{ua}->get($url);
138  
139 0 ▶ if($res->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
140 0 Carp::carp("$url API returned error: " . $res->status_line());
141 0 return;
142 }
143  
144 0 my $json = JSON::MaybeXS->new->utf8();
145 0 return $json->decode($res->decoded_content());
146  
147 # my @results = @{ $data || [] };
148 # wantarray ? @results : $results[0];
149 }
150  
151 =head2 ua
152  
153 Accessor method to get and set UserAgent object used internally. You
154 can call I<env_proxy> for example, to get the proxy information from
155 environment variables:
156  
157 $geo_coder->ua()->env_proxy(1);
158  
159 You can also set your own User-Agent object:
160  
161 $geo_coder->ua(LWP::UserAgent::Throttled->new());
162  
163 =cut
164  
165 sub ua {
166 0 ▶ my $self = shift;
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
167 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
168 0 $self->{ua} = shift;
169 }
170 0 $self->{ua};
171 }
172  
173 =head2 reverse_geocode
174  
175 # $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732');
176  
177 # Similar to geocode except it expects a latitude/longitude parameter.
178  
179 Not supported.
180  
181 =cut
182  
183 sub reverse_geocode {
184 # my $self = shift;
185  
186 # my %param;
187 # if (@_ % 2 == 0) {
188 # %param = @_;
189 # } else {
190 # $param{latlng} = shift;
191 # }
192  
193 # my $latlng = $param{latlng}
194 # or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)");
195  
196 # return $self->geocode(location => $latlng, reverse => 1);
197 0 ▶ Carp::croak('Reverse geocode is not supported');
Subroutine reverse_geocode: not called CC=1 SCAR=6.9
Pod documented
198 }
199  
200 =head2 run
201  
202 You can also run this module from the command line:
203  
204 perl Census.pm 1600 Pennsylvania Avenue NW, Washington DC
205  
206 =cut
207  
208 5 ▶__PACKAGE__->run(@ARGV) unless caller();
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 5 5
209  
210 sub run {
211 0 ▶ require Data::Dumper;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
212  
213 0 my $class = shift;
214  
215 0 my $location = join(' ', @_);
216  
217 0 my @rc = $class->new()->geocode($location);
218  
219 0 ▶ die "$0: geocoding failed" unless(scalar(@rc));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @rc 0 0 0
220  
221 0 print Data::Dumper->new([\@rc])->Dump();
222 }
223  
224 =head1 AUTHOR
225  
226 Nigel Horne <njh@bandsman.co.uk>
227  
228 Based on L<Geo::Coder::GooglePlaces>.
229  
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.
232  
233 Lots of thanks to the folks at geocoding.geo.census.gov.
234  
235 =head1 BUGS
236  
237 Should be called Geo::Coder::NA for North America.
238  
239 =head1 SEE ALSO
240  
241 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>
242  
243 https://www.census.gov/data/developers/data-sets/Geocoding-services.html
244  
245 =head1 LICENSE AND COPYRIGHT
246  
247 Copyright 2017-2023 Nigel Horne.
248  
249 This program is released under the following licence: GPL2
250  
251 =cut
252  
253 5 1;
↑ Summary