Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Zip Codes in a Specific Radius

Tags:

php

mysql

I have found this popular PHP/MySQL Script called Zip Location by SaniSoft and it works great besides one thing: It doesn't in some instances.

It seems that any radius under 20 miles returns the same amount of zip codes as 20 miles. I have searched all over google, but to no avail and I was wondering if someone had some insight on this situation.

I would rather figure out this problem before having to pay for a program, and I could also use the learning experience. The database is a list of zip codes and longitudes and latitudes of each zip code. The script uses a method that determines the distance around the zip code entered and returns the zip codes in that radius based on their lon/lat.

Thank you!!

Edit: From using the distance function that the script provides I have discovered that the distance between the Zip Codes that the program gives me and my zip code are coming up as 0 miles.

MAJOR UPDATE

From research it turns out that the database has duplicate lat/lon values. Please be aware of this when using Zip Locator. Although the PHP does its job, you will need to find a new Database of zip codes. I will post my findings at a later date.

like image 562
Chris Bier Avatar asked Aug 07 '09 18:08

Chris Bier


2 Answers

The following approximate distance calculations are relatively simple, but can produce distance errors of 10% of more. These approximate calculations are performed using latitude and longitude values in degrees. The first approximation requires only simple math functions:

Approximate distance in miles = sqrt(x * x + y * y)

where
      x = 69.1 * (lat2 - lat1)
and   
      y = 53 * (lon2 - lon1)

You can improve the accuracy of this approximate distance calculation by adding the cosine math function:

Approximate distance in miles = sqrt(x * x + y * y)

where
      x = 69.1 * (lat2 - lat1)
and   
      y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

If you need greater accuracy, you must use the exact distance calculation. The exact distance calculation requires use of spherical geometry, since the Earth is a sphere. The exact distance calculation also requires a high level of floating point mathematical accuracy - about 15 digits of accuracy (sometimes called "double-precision"). In addition, the trig math functions used in the exact calculation require conversion of the latitude and longitude values from degrees to radians. To convert latitude or longitude from degrees to radians, divide the latitude and longitude values in this database by 180/pi, or 57.2958. The radius of the Earth is assumed to be 6,371 kilometers, or 3,958.75 miles.

You must include the degrees-to-radians conversion in the calculation. Substituting degrees for radians, the calculation is:

          Exact distance in miles = 3958.75 * arccos[sin(lat1/57.2958) *
                               sin(lat2/57.2958) + 
                               cos(lat1/57.2958) * 
                               cos(lat2/57.2958) * 
                               cos(lon2/57.2958 - lon1/57.2958)]
like image 195
ChssPly76 Avatar answered Oct 26 '22 15:10

ChssPly76


Second try!

Seeing your edited problem statement, I'd look at how you assign your zip values.

Lots of errors can be introduced if your zip codes are integers instead of strings. The biggest problem is that American zip codes can start with 0.

The examples in ziptest.php aren't good, since the treat zips as integers. When you try to describe my own zip code with an integer:

$zip1 = 02446;

Its interpreted by PHP as the octal value 2446. phpZipLocator the uses that value as a string without any explicit conversion. PHP therefore gives it the decimal value of octal 2446 as a string (1318) which is not a zip code at all.

Instead of notifying that it didn't find a zip code, phpZipLocator does a radius search of all zip codes within a given radius of something that doesn't exist, which it decides should be 1.

If I set the zip code using a string

$zip1 = '02446';

I get the correct result.

IMHO it seems like phpZipLocator could use a little work.

like image 33
Otterfan Avatar answered Oct 26 '22 16:10

Otterfan