It seems that every day it
becomes harder to locate simple text files and programs to manage
those files. More and more those files are being offered for sale
with some software which amounts to a few lines of code to search
a plain text file.
To make life easier for the
perl developers I am posting some basic zipcode manipulation
tools here as well as the us zipcode databases needed in plain
The object of the zipcode
tools is to help you understand distance calculations and zipcode
data lookups and provide simple solutions that you can integrate
into larger programs like classifieds and social networks.
Caluculating distance is basic
geometry. The earth is divided into latitude and lognitude
coordinates and the distance between them is just like
calculating the disttance between any 2 points on a sphere, but
our has a diameter of about 4000 miles. As long as we know the
coordinates of each point we can determine the distance between
those points with a simple equation.
In math it would look
something like this:
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin^2(dlat/2) + cos(lat1) * cos(lat2) * sin^2(dlon/2)
c = 2 * arcsin(min(1,sqrt(a)))
d = R * c
Now lets convert that to perl,
something we can all understand.
# Find the distance
$temp = sin($delta_lat/2.0)**2 + cos($lat_1) * cos($lat_2) * sin($delta_lon/2.0)**2;
$distance = 3956 * 2 * atan2(sqrt($temp),sqrt(1-$temp));
Now that makes a little more
sense. At least to me it does.
What we have done is to use
the math formula and replaced the varriable with perl varriables.
The math is still the same, but the formula is in the program
where it can run. The "sin, cos, atan and sqrt" are all
math functions of perl which are used just like multiplication
Try the zipcode distance calculator demo
Download the zipcode distanace calculator
script - you will also
need the zipcode latatude and logitude text file below. There is
nothing to configure, just upload the script and datafile in a
directory, chmod the zipdistance.cgi to 0755 and it should work.
I will warn you that opening a
1 meg file is not the most efficient use of resources, but I am
providing the simplest way to calculte the distance and not
necessisarily the most efficient. The most efficeient would be to
use 40,000 individual files and only open the 2 files you need,
but that would be a mess to download and install. You should also
realize these are straight line distances and not driving
distance, but it is still an accurate way to sort by distance.
Zipcode lookups are simple,
simple simple. You basically have a chart and return the value
that matches the zipcode in the chart. But here we use perl to
read the chart and tell us the answer.
The program is almost
identical to the zipcode distance calculator, but without all the
math. Just a simple read the file and return the match.
Using the text file zipcodes.txt,
the program will open the file and find the line with the
matching zipcode and print the data.
Zipcode Lookup for PERL Demo
Download Zipcode Lookup for PERL - You will also need the text file below for
zipcodes with city and state. There is nothing to configure, just
upload the script and datafile in a directory, chmod the
ziplookup.cgi to 0755 and it should work.
The same priciple can be
applied to specify a city and lookup the zipcode for that city.
Just tweak the program to match a different field and you have a
lookup by city.
These are single files pipe delimited with 40,000+ lines. Each is
just over 1 meg, so using them efficiently will require breaking
them into smaller files. They are probably not the most up to
date but a great starting point.
Zipcodes with Latatude and Longitude
Zipcodes with State and City
These zipcode tools are as simple as they
get. But it will give you what you need to understand how the
tools work and how they can be utilized in your own programs.