views:

49

answers:

3

I need to find all the zipcodes with a certain range from a zipcode. I have all the zipcode lat/lon in a DB.

I have found two formulas online that vary slightly from each other. Which one is correct?

Formula 1:

def latRange = range/69.172
def lonRange = Math.abs(range/(Math.cos(Math.toRadians(zip.latitude)) * 69.172));
def minLat = zip.latitude - latRange
def maxLat = zip.latitude + latRange
def minLon = zip.longitude - lonRange
def maxLon = zip.longitude + lonRange

Formula 2: (is identical to formula one except for the following:)

def lonRange = Math.abs(range/(Math.cos(zip.latitude) * 69.172));

(The second one does not have Math.toRadians ) After I get the min/max Lat/Lon, I intend to search a DB table using a between criteria. Which formula is correct?

+1  A: 

It depends on the units of your lat/long. If they are in degrees, you need to convert to radians.

Lou Franco
I dont think the data is in radians. I got it here: http://www.micahcarrick.com/php-zip-code-range-and-distance-calculation.html
Tihom
@Tihom - should be easy to tell. If you have any values over Pi, it's in degrees.
bemace
A: 

If your latitude and longitude data isn't already in radians then you'll need to use the one that converts. You should be aware though that the way things are set up now you'll end up with a square range.

You'll be better off doing the distance calculation in your mysql query, using the Pythagorean theorem to find the distance so you end up with a circular range. This question should help you get started with that: http://stackoverflow.com/questions/4011944/mysql-select-query-based-on-lat-long-data-from-zipcode .

If you need to improve it's performance you can index it using Sphinx.

bemace
A: 

I would suggest letting the db do all the heavy lifting. Several DBs have geo add-ons, but here are a couple examples: MongoDB, postgres+postgis

Ben Taitelbaum