View Full Version : Distance Between 2 Zip Codes
GrimChild
07-24-2005, 05:21 PM
Wow, Been Quite Some Time Since I've Been Here. The Forums Look Great !
So Anyhow;
Im Running My Own Servers Now, One Of Which Is The Apache Web Server With Indigo Perl Installed.
Im Looking For A Way To Find The Distance Between 2 Zip Codes No Matter What Language It May Be In. But Most Prefferably CGI/Perl Or Java
I Figure Something Like This May Need A Big Database Storing The Latitude And Longitudes For All Zip Codes, And If That Be The Case, I Have Microsoft Office XP With Microsoft Access And Excell I Could Use...
Thanx To Any/All Who Reply
mlseim
07-24-2005, 05:52 PM
This has been a topic on here before ... although I can't find the thread.
You can purchase a copy of the database real cheap, or maybe find one to download for free:
http://www.google.com/search?q=zipcode+gps+database&btnG=Search
GrimChild
07-24-2005, 08:29 PM
Thx Mlseim.
I Have Done Some Searching Before I Posted This Thread, And I Planned On getting The Database From The Census Bureau At http://www.census.gov/cgi-bin/gazetteer
But Unfortunatally, The Link to The Database Files Is Broken.
I Was Just Wondering If There Was A Smarter More Efficient Way Of Doing This.
]|V|[agnus
07-26-2005, 06:45 PM
You need a ZIP codes database and then some code do to roughly the following:
First, check if the ZIP you're searching for exists and grab the latitude and longitude:
SELECT DISTINCT ZIPcode,latitude,longitude FROM zipcodes
WHERE ZIPcode = '#zip#'
Then, using some math, we can query for all zip codes within' a certain radius.
SELECT DISTINCT ZIPcode,
ROUND((ACOS((SIN(#latitude#/57.2958) * SIN(latitude/57.2958)) +
(COS(#latitude#/57.2958) * COS(latitude/57.2958) * COS(longitude/57.2958 - #longitude#/57.2958)))) * 3963)
AS distance
FROM zipcodes
WHERE (latitude >= #latitude# - (#radius#/111))
AND (latitude <= #latitude# + (#radius#/111))
AND (longitude >= #longitude# - (#radius#/111))
AND (longitude <= #longitude# + (#radius#/111))
ORDER BY distance
#zip# and #radius# should be user input. #latitude# and #longitude# in the second query are referring to selected values from the first.
So, you could then make that query even more complex and attempt to grab the relevant data you need in one shot, your you could loop over the returned zip codes and do more queries to get the right related info.
I don't know Perl/CGI though. My code samples are from ColdFusion, but the math is of course the same.
GrimChild
07-26-2005, 10:13 PM
Thx ....]|V|[agnus
I Got The Database; Found it Deep Within The Census Bureau.
I Think Im All Set Now :cool:
]|V|[agnus
07-26-2005, 10:28 PM
Got a link for that census bureau DB? We ended up purchasing on where I work. It was a reasonable price, but free is excellent. :)
GrimChild
07-30-2005, 05:30 AM
Heres The Link To The Files....
http://www.census.gov/geo/www/gazetteer/places2k.html
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.