Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 679

# Zip code radius forumula/module with Perl

Can someone provide a formula or sample code as to how to find all of the zip codes within a radius of a base zip code efficiently?  Say I enter 90210 and I enter 10 miles.  I want to find all the zip codes that are located within 10 miles of 90210.  I already have a database with all the zip and their long/lat.  I can loop through all the zip and see if the distance to the base zip is within 10 miles but I'm think that can't be efficient and fast with tens of thousands of records.  Any other ideas?  I'm a bit stuck!  thanks
0
regulatorz
• 4
1 Solution

Commented:
At first you need database with zipcode geographic coordinates (latitude and longtitude). It cannot be computed from zipcode number. At second - this code (function 'distance') calculates the distance (in miles) between two points:

use constant PI => 3.14159265358979;
sub d2r(\$) { \$_[0]*PI/180.0 }
sub r2d(\$) { \$_[0]*180.0/PI }
sub acos(\$) { atan2( sqrt(1 - \$_[0] * \$_[0]), \$_[0] ) }
sub distance(\$\$\$\$){
my (\$t1,\$n1,\$t2,\$n2)=@_;
my \$L=abs(\$n1-\$n2);
\$L=360.0-\$L if \$L>180.0;
return r2d(acos((sin(d2r(\$t1))*sin(d2r(\$t2))) + (cos(d2r(\$t1))*cos(d2r(\$t2))*cos(d2r(\$L)))))*69.041;
}

parameters: (latitude1, longtitude1, latitude2, longtitude2)

--
Andrey
0

Author Commented:
i have all that already but the problem is i want to find all the zip codes that are say within 10 miles of 90210.  i can use the function to find out the distance from each zip to 90210 but then that means i have to go through all the zip codes everytime.  is that the only way?  seems that way.
0

Commented:
One more function for you:

sub square {
my (\$lat,\$lng,\$dist)=@_;
if (!\$lat || !\$lng || !\$dist) {
return (undef,undef,undef,undef);
}
my \$minlat = \$lat - \$dist / 69.8131700797731;
my \$maxlat = \$lat + \$dist / 69.8131700797731;
my \$minlng = \$lng - \$dist / (69.8131700797731 * cos (\$lat * 0.0174532925199433));
my \$maxlng = \$lng + \$dist / (69.8131700797731 * cos (\$lat * 0.0174532925199433));
return (\$minlat,\$maxlat,\$minlng,\$maxlng);
}

there are three parameters: latitude of point, longitude of point, and maximum distance(in miles). It will returns minimus and maximum latitudes, and minimum and maximum longtitudes. These values can be used for initial search for possible candidates (for example SELECT zip, lat, lng FROM zipcodes WHERE lat >= minlat AND lat <= maxlat AND lng >= minlng AND lng <= maxlng), but not all zipcodes located in this "square" will have distanse less than given value. Some of them may be located in "corners" of this "square". In any case it may give significant speedup.

--
Andrey
0

Commented:
Hi all,

Can anyone please explain the square function to me. What are the values 69.8131700797731 and 0.0174532925199433. And I believe the minlat, maxlat etc are in radians so I multiplied them by 180/pi to get the degrees. I tried to use this but got wrong results. For example a 1 mile radius search around zip code 35004 gave me the values

min latt = 32.7856774126, min long = -87.3232365932, max latt = 34.4270805874, max long = -85.6817474068

which are quite wrong.

Thanks a lot,
Muazzam
0

Commented:
> What are the values 69.8131700797731

the length of 1 degree horde on Earth equator (in miles)  = 2 * PI * R / 360

> and 0.0174532925199433

radians in one degree = PI / 180

> And I believe the minlat, maxlat etc are in radians

all values in degrees

> For example a 1 mile radius search around zip code 35004 gave me the values
> min latt = 32.7856774126, min long = -87.3232365932,
> max latt = 34.4270805874, max long = -85.6817474068

Searching for 34004 zip code at:
http://www.zipinfo.com/search/zipcode.htm
gave lat = 33.6013, long = -86.5003

and square(33.6013, -86.5003, 1)) returns:
33.5869760551217, 33.6156239448783, -86.5174974973318, -86.4831025026682

--
Andrey
0

Commented:
but http://www.census.gov/cgi-bin/gazetteer?city=&state=&zip=35004
gave lat = 33.584132, long = -86.51557

and square returns:
33.5698080551217, 33.5984559448783, -86.5327640749543, -86.4983759250457

--
Andrey
0

## Featured Post

• 4
Tackle projects and never again get stuck behind a technical roadblock.