
Automatically Geocode U.S. Addresses #80
Chapter 7, Names and Places
|
407
HACK
my $where = shift @ARGV
or die "Usage: $0 \"111 Main St, Anytown, KS\"\n";
my $addr = uri_escape($where);
print get "http://rpc.geocoder.us/service/rest?address=$addr";
Call the program by putting an address on the command line:
./simplest_rest.pl "1005 Gravenstein Hwy N, Sebastopol, CA"
You can also substitute + for spaces and skip the quotes:
./simplest_rest.pl 1005+Gravenstein+Hwy+N+Sebastopol+CA
The full RDF document as shown in Figure 7-3 is returned. This can be
parsed with the Perl module
RDF::Simple::Parser.
Geocoding a List of Addresses
The Monterey Express is a dive boat in Monterey, California. A list of dive-
related resources for the boat is maintained at http://www.montereyexpress.
com/DiveLinks.htm. A real-world application would be to geocode these
addresses in order to create a “find your closest dive resource” application.
This sample Perl code fetches the list, does a simplistic (and demonstrably
wrong in some cases) parse to get the addresses, geocodes the addresses, and
returns the results:
#!/usr/bin/perl
# divecode.pl - Geocode the Monterey Express dive resources list
use LWP::Simple;
use XMLRPC::Lite;
my $lines;
#$lines = get "http://www.montereyexpress.com/DiveLinks.htm";
#or use STDIN
{local $/; undef $/; $lines = <>;}
my ($shop_name, $shop_address);
while ($lines =~ s/(.+)<br>//m) {
$st = $1;
chomp $st; ...