Zip Code Ranges

I have a DB with ZipCodes and their Lat/long.

What I hear is that basically, we need to compute distance between all points (~70K) calculations before returning a result. Isn't there a better way to reduce the set of calculations to determine say, zipcodes < 50 miles from x.

Rajat

I have a DB with ZipCodes and their Lat/long.

What I hear is that basically, we need to compute distance between all points (~70K) calculations before returning a result. Isn't there a better way to reduce the set of calculations to determine say, zipcodes < 50 miles from x.

Precompute it, rather than trying to compute it on the fly. Disk is cheap, indexes are good.

Rajat

--Greg

For 25 million pairs of zip codes? Disk is cheap, but that seems a bit extreme.

As an alternative, you could index the zip codes by degrees of latitude and longitude. Then to find zip codes within 50 miles of a given zip code, you could simply retrieve all the zip codes within one degree of latitude or longitude.

A better idea: index all the zip codes by latitude and longitude to two decimal places. Then if your database has trigonometric functions you can get a result with a single database query, and no ruby code.

There is a great tutorial with CODE to calculate both distance between zip codes and zip codes that fall within a specified range.

free-zipcodes.com