Welcome to the Monastery PerlMonks

### Re: Sorting by geographical proximity / clumping groups of items based on X and Y

by dws (Chancellor)
 on Jul 17, 2002 at 23:51 UTC ( #182650=note: 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.

• Comment on Re: Sorting by geographical proximity / clumping groups of items based on X and Y

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://182650]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2022-12-05 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?