Free US ZIP Code Database as CSV
coolthingoftheday.blogspot.com
coolthingoftheday.blogspot.com
http://therobert.org/stuff/misc/5-digit%20Commercial.csv
7.7MB | February 2008
I need a database that shows the population in each zip code, too. If any good Samaritan can share, that would be great.
A small mirror.
Go to http://factfinder.census.gov/servlet/DCGeoSelectServlet?ds_n...
Click "All 5-Digit ZIP Code Tabulation Areas"
Click "GO"
Click "P1. Total Population"
Click "Add"
Click "Next"
Click "Start Download"
SELECT z.ZIPcode, (sqrt((z.latitude-z2.latitude)*(z.latitude-z2.latitude)+(z.longtitude-z2.longtitude)*(z.longtitude-z2.longtitude)))) AS dist
FROM `zip` AS `z`, `zip` AS `z2`
WHERE `z2`.`ZIPcode = 'YOURZIPCODE'
ORDER BY dist LIMIT 10
Might be faster if you do a query to get the lat and long for the ZIP code you need first, though. Hell, it would probably be fastest to skip the database all together and just import the data and loop the distance formula, recording the smallest distances as you go.Just a note: comparing every zip code and its distance and recording the distance to a DB would create upwards of 12 million records. This might be the breaking point for some.... I was thinking about doing it myself. Once I worked out the numbers I backed away.
If you know the origin and destination long and lat this works well to calc distance. $dist = sin(deg2rad($this->lat1)) * sin(deg2rad($this->lat2)) + cos(deg2rad($this->lat1)) * cos(deg2rad($this->lat2)) * cos(deg2rad($theta));