Geo::Coder::US


Easy Integration to PHP

This perl script is called via the command line and returns a serialized PHP array. Returns non-zero on error, but error message is returned as serialized PHP array with error message in the ”error” key so that scripts can easily handle the errors.

#!/usr/bin/perl -w

use Geo::Coder::US;
use strict;

my $filename = shift @ARGV or die "a:1:{s:5:\"error\";s:22:\"No database specified!\";}";

# Make sure the specified file exists
if(-e $filename){
        Geo::Coder::US->set_db($filename);
}else{
        die("a:1:{s:5:\"error\";s:34:\"Specified database file not found!\";}");
}

my $arg = shift @ARGV or die "a:1:{s:5:\"error\";s:21:\"No address specified!\";}";

# Geocode the address
my ($val) = Geo::Coder::US->geocode( $arg );

# Determine size of array
my $i = 0;
$i += scalar keys %$val;
print "a:$i:{";

# Output the data as a PHP serialized array
while ( (my $key, my $value) = each %$val){
        print "s:".length($key).":\"$key\";s:";

        if (defined($value)){
                print length($value).":\"$value\";";
        }else{
                print "0:\"\";";
        }
}
print "}";

Usage:
perl geocode_address.pl /www/VirtualHosts/tiger/tiger.db “144 bloomfield way, folsom, ca”

Result:
a:10:{s:6:”number”;s:3:”144″;s:3:”lat”;s:9:”38.672122″;s:6:”street”;s:10:”Bloomfield”;s:5:”state”;s:2:”CA”;s:4:”city”;s:6:”Folsom”;s:3:”zip”;s:5:”95630″;s:6:”suffix”;s:0:”";s:4:”long”;s:11:”-121.133587″;s:4:”type”;s:3:”Way”;s:6:”prefix”;s:0:”";}

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>