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

filter: Statementstmt 50.6% 42 / 83 Branchbran 17.8% 5 / 28 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 80.0% 12 / 15 Podpod 100% 5 / 5 totaltotal 47.4% 66 / 139 CC 16 SCAR 37.1
CC
16
Cov
42%
geocode
98.0
ua
6.0
run
6.0
CRAP 40.7
↑ 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;
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.05
22  
23 =cut
24  
25 5 our $VERSION = '0.05';
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=12.0
Pod documented
53  
54 2 ▶ my $ua = delete $param{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition33%1/3
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::US::Census' . "/$Geo::Coder::US::Census::VERSION")
l!l&&r!l&&!r
020
MC/DC0%0/2
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::US::Census' . "/$Geo::Coder::US::Census::VERSION")
delete $param{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::US::Census' . "/$Geo::Coder::US::Census::VERSION")
Truth table33%1/3
A || B
A= delete $param{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::US::Census' . "/$Geo::Coder::US::Census::VERSION")
ABresult
1X1
011
000
55 2 ▶ my $host = delete $param{host} || 'geocoding.geo.census.gov/geocoder/locations/address';
Condition50%1/2
delete $param{'host'} || 'geocoding.geo.census.gov/geocoder/locations/address'
l!l
02
MC/DC0%0/1
delete $param{'host'} || 'geocoding.geo.census.gov/geocoder/locations/address'
delete $param{'host'}
Truth table50%1/2
A
A= delete $param{'host'}
Aresult
11
00
56  
57 2 return bless { ua => $ua, host => $host }, $class;
58 }
59  
60 =head2 geocode
61  
62 $location = $geo_coder->geocode(location => $location);
63 # @location = $geo_coder->geocode(location => $location);
64  
65 print 'Latitude: ', $location->{'latt'}, "\n";
66 print 'Longitude: ', $location->{'longt'}, "\n";
67  
68 =cut
69  
70 sub geocode {
71 1 ▶ my $self = shift;
Subroutine geocode: called CC=12 SCAR=45.8
Pod documented
72 1 my %param;
73  
74 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
75 0 %param = %{$_[0]};
76 } elsif(ref($_[0])) {
77 0 Carp::croak('Usage: geocode(location => $location)');
78 } elsif(@_ % 2 == 0) {
79 1 %param = @_;
80 } else {
81 0 $param{location} = shift;
82 }
83  
84 my $location = $param{location}
85 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
86  
87 0 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 0 0
88 0 $location = Encode::encode_utf8($location);
89 }
90  
91 0 ▶ if($location =~ /,?(.+),\s*(United States|US|USA)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /,?(.+),\s*(United States|US|USA)$/i) 0 0 0
92 0 $location = $1;
93 }
94  
95 # Remove county from the string, if that's included
96 # Assumes not more than one town in a state with the same name
97 # in different counties - but the census Geo-Coding doesn't support that
98 # anyway
99 0 ▶ if($location =~ /^(\d+\s+[\w\s]+),\s*([\w\s]+),\s*[\w\s]+,\s*([A-Za-z]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /^(\d+\s+[\w\s]+),\s*([\w\s]+),\s*[\w\s]+,\s*([A-Za-z]+)$/) 0 0 0
100 0 $location = "$1, $2, $3";
101 }
102  
103 0 my $uri = URI->new("https://$self->{host}");
104 0 $location =~ s/\s/+/g;
105 0 my $hr = Geo::StreetAddress::US->parse_address($location);
106  
107 0 my %query_parameters = ('format' => 'json', 'benchmark' => 'Public_AR_Current');
108 0 ▶ if($hr->{'street'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{"street"}) 0 0 0
109 0 ▶ if($hr->{'number'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{'number'}) { } 0 0 0
110 0 $query_parameters{'street'} = $hr->{'number'} . ' ' . $hr->{'street'} . ' ' . $hr->{'type'};
111 } else {
112 0 $query_parameters{'street'} = $hr->{'street'} . ' ' . $hr->{'type'};
113 }
114 0 ▶ if($hr->{'suffix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr->{"suffix"}) 0 0 0
115 0 $query_parameters{'street'} .= ' ' . $hr->{'suffix'};
116 }
117 }
118 0 $query_parameters{'city'} = $hr->{'city'};
119 0 $query_parameters{'state'} = $hr->{'state'};
120  
121 0 $uri->query_form(%query_parameters);
122 0 my $url = $uri->as_string();
123  
124 0 my $res = $self->{ua}->get($url);
125  
126 0 ▶ if($res->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
127 0 Carp::croak("$url API returned error: " . $res->status_line());
128 0 return;
129 }
130  
131 0 my $json = JSON->new->utf8();
132 0 return $json->decode($res->content());
133  
134 # my @results = @{ $data || [] };
135 # wantarray ? @results : $results[0];
136 }
137  
138 =head2 ua
139  
140 Accessor method to get and set UserAgent object used internally. You
141 can call I<env_proxy> for example, to get the proxy information from
142 environment variables:
143  
144 $geo_coder->ua()->env_proxy(1);
145  
146 You can also set your own User-Agent object:
147  
148 $geo_coder->ua(LWP::UserAgent::Throttled->new());
149  
150 =cut
151  
152 sub ua {
153 0 ▶ my $self = shift;
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
154 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
155 0 $self->{ua} = shift;
156 }
157 0 $self->{ua};
158 }
159  
160 =head2 reverse_geocode
161  
162 # $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732');
163  
164 # Similar to geocode except it expects a latitude/longitude parameter.
165  
166 Not supported.
167  
168 =cut
169  
170 sub reverse_geocode {
171 # my $self = shift;
172  
173 # my %param;
174 # if (@_ % 2 == 0) {
175 # %param = @_;
176 # } else {
177 # $param{latlng} = shift;
178 # }
179  
180 # my $latlng = $param{latlng}
181 # or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)");
182  
183 # return $self->geocode(location => $latlng, reverse => 1);
184 0 ▶ Carp::croak('Reverse geocode is not supported');
Subroutine reverse_geocode: not called CC=1 SCAR=6.9
Pod documented
185 }
186  
187 =head2 run
188  
189 You can also run this module from the command line:
190  
191 perl Census.pm 1600 Pennsylvania Avenue NW, Washington DC
192  
193 =cut
194  
195 5 ▶__PACKAGE__->run(@ARGV) unless caller();
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 5 5
196  
197 sub run {
198 0 ▶ require Data::Dumper;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
199  
200 0 my $class = shift;
201  
202 0 my $location = join(' ', @_);
203  
204 0 my @rc = $class->new()->geocode($location);
205  
206 0 ▶ die "$0: geocoding failed" unless(scalar(@rc));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @rc 0 0 0
207  
208 0 print Data::Dumper->new([\@rc])->Dump();
209 }
210  
211 =head1 AUTHOR
212  
213 Nigel Horne <njh@bandsman.co.uk>
214  
215 Based on L<Geo::Coder::GooglePlaces>.
216  
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.
219  
220 Lots of thanks to the folks at geocoding.geo.census.gov.
221  
222 =head1 BUGS
223  
224 Should be called Geo::Coder::NA for North America.
225  
226 =head1 SEE ALSO
227  
228 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>
229  
230 https://www.census.gov/data/developers/data-sets/Geocoding-services.html
231  
232 =head1 LICENSE AND COPYRIGHT
233  
234 Copyright 2017,2018 Nigel Horne.
235  
236 This program is released under the following licence: GPL2
237  
238 =cut
239  
240 5 1;
↑ Summary