?
Solved

Zip code radius forumula/module with Perl

Posted on 2003-03-25
6
Medium Priority
?
670 Views
Last Modified: 2012-06-21
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
Comment
Question by:regulatorz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 5

Expert Comment

by:Sapa
ID: 8210939
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 Comment

by:regulatorz
ID: 8214256
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
 
LVL 5

Accepted Solution

by:
Sapa earned 80 total points
ID: 8219482
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:Attiquzzaman
ID: 8794827
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
 
LVL 5

Expert Comment

by:Sapa
ID: 8801657
> 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
 
LVL 5

Expert Comment

by:Sapa
ID: 8801683
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question