
404
|
Chapter 7, Names and Places
#80 Automatically Geocode U.S. Addresses
HACK
geocoder.us can be queried via the XML-RPC and REST-ful interfaces, which
are available to any reasonable programming language. The basic steps are:
1. Get an address from a web form, database, or file.
2. Format that address and create a web-service request.
3. Call the geocoder.
4. Do something interesting with the result.
Geocoding with XML-RPC
XML-RPC is a way of making a request of a remote system (a Remote Proce-
dure Call, or RPC) and receiving the results in XML. The XML response
from geocoder.us is easy to script in Perl by using the
XMLRPC::Lite module.
Most modern languages have a library that will parse XML-RPC and return
results in an easy-to-manage form:
#!/usr/bin/perl
use XMLRPC::Lite;
use Data::Dumper;
use strict;
use warnings;
my $where = shift @ARGV
or die "Usage: $0 \"111 Main St, Anytown, KS\"\n";
my $result = XMLRPC::Lite
-> proxy( 'http://rpc.geocoder.us/service/xmlrpc' )
-> geocode( $where )
-> result;
print Dumper $result;
Before running the code, you need to install the XMLRPC::Lite Perl module.
This can be done via CPAN from the shell by typing
sudo perl -MCPAN -e
"XMLRPC::Lite"
.
Running the Hack
Write the previous script to a file called simplest_xmlrpc.pl and run it like
this:
./simplest_xmlrpc.pl "1005 Gravenstein Highway North, Sebastopol, CA 95472"
It should show you the following data structure:
$VAR1 ...