Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Zip Code Module?

by sifen (Beadle)
on Dec 17, 2004 at 00:35 UTC ( #415520=note: print w/ replies, xml ) Need Help??


in reply to Zip Code Module?

I think Geo::PostalCode would be at least a look. It reads the zipcode/city data from Bearkly DB's that you can get from http://www.census.gov/geo/www/tiger/zip1999.html.

It seems pretty stright forwards, the relivant section from the docs is below:

$gp = Geo::PostalCode->new(db_dir => $db_dir);

Returns a new Geo::PostalCode object using the postalcode.db, latlon.db, and city.db Berkeley Database files in $db_dir.

$record = $gp->lookup_postal_code(postal_code => $postal_code);

Returns a hash reference containing four keys:

* lat - Latitude
* lon - Longitude
* city - City
* state - State two-letter abbreviation.

Hope that's of some use to you. . .


Comment on Re: Zip Code Module?
Select or Download Code
Re^2: Zip Code Module?
by sgifford (Prior) on Dec 17, 2004 at 01:47 UTC
    I use this module frequently, and it works great. I'd highly recommend it. You'll need to download the postal code data before it will complete "make test" succesfully.
      What do you mean, "download the postal code data"?

      I tried this module, I installed it, using WHM (a cPanel Product) with the Install a Perl Module, it appeared to work ok, however, when I use the 'sample' code:
      use Geo::PostalCode; my $gp = Geo::PostalCode->new(db_dir => "."); my $record = $gp->lookup_postal_code(postal_code => '07302'); my $lat = $record->{lat}; my $lon = $record->{lon}; my $city = $record->{city}; my $state = $record->{state}; my $distance = $gp->calculate_distance(postal_codes => ['07302','100 +04']); $record = $gp->lookup_city_state(city => "Jersey City",state => "NJ" +); $lat = $record->{lat}; $lon = $record->{lon}; my $postal_codes = $record->{postal_codes}; $postal_codes = $gp->nearby_postal_codes(lat => $lat, lon => $lon, distance => 50); print qq~<br> City is ~ . $city . qq~<br> State is ~ . $state . qq~<br> Latitude is ~ . $lat . qq~<br> Longitude is ~ . $lon . qq~<br> <br> Nearby Zips are: $postal_codes~;
      (ran as a test) and I had this 'output':
      City is
      State is
      Latitude is
      Longitude is

      Nearby Zips are: ARRAY(0x8674890)

      So, could what you said, be the reason why it's not working?

      I would appreciate your help.
      thx,
      Richard

        I'm really sorry about this. Unlike sgifford, I have actually never used this module. I guess I assumed that it would be straight forward to install.

        I figured out it's not actually.

        I would have attempted to recreate your exact environment so as to figure out what to do, but as I don't have access to cPanel, I used CPAN. It didn't install cleanly and failed most of it's make tests. My solution was to find the build directory made by CPAN, download said data, and run make test and make install by hand.

        That probably wasn't too helpful, so bellow is *actually* what I did:

        sudo cpan install Geo::PostalCode lwp-download http://tjmather.com/Geo-PostalCode_19991101.txt.gz gunzip Geo-PostalCode_19991101.txt.gz cd ~/.cpan/build/Geo-PostalCode-0.06/ sudo cp ~/Geo-PostalCode_19991101.txt ./ sudo ./load.pl sudo make test sudo make install perl -e 'use Geo::PostalCode'

        That got it to install and pass the two sets of test code included with the module. The load.pl script seems to take the downloaded textfile and separate it out into three db files. I also tried downloading the first source I gave you, and although it works, it gives you one DBase db, and I'm not really sure how you would use it ( thought you probably could ).

        Hopefully *this* time I've acutally been some help to you.

        As sifen said, you need to download the postal code database, as explained in the INSTALL file. This is an unfortunate aspect of this module. The author said he'd accept a fix if I emailed him one; perhaps in the next few weeks I can find some time to do that.

        I think part of your problem, though, is that $postal_codes has an array reference, so you need to print it like this:

        print "@$postal_codes";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://415520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-07-13 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (246 votes), past polls