Pathologically Eclectic Rubbish Lister  
PerlMonks 
comment on 
( #3333=superdoc: print w/replies, xml )  Need Help?? 
One simple approach to geographic searching is to precalculate a "bucket" for each coordinate, and then organize the data such that the bucket number can be used as a key to quickly lookup all coordinates that fall within that bucket bucket. This quickly culls down the search space, allowing you to make the pairwise distance calculation on a subset of the data.
The initial filtering to find all points within X feet of a given point by start by considering how many additional buckets along one vector (say, along the X axis) need to be considered such that the far side of one bucket to the far side of the end bucket is >= X feet. This number forms an integral radius for a sweep around the bucket space (or, more simply, to determine the square grid of buckets to be searched). If you bucket sizes selected such that most searches can be done by considering a bucket and its immediate neighbors, the SQL query is pretty simple. Prepare the query select * from complaint where bucket in (?,?,?,?,?,?,?,?,?) and then plug in the bucket ids when you execute. If you need to consider more buckets, write the bucket numbers into a temporary table, using that table to JOIN against the complaint table. Then perform the distance calculation on whatever comes back, and sort accordingly.
In reply to Re: Sorting by geographical proximity / clumping groups of items based on X and Y
by dws

