On Wed, Nov 18, 2009 at 6:45 AM, Mason Jones <
maso...@gmail.com> wrote:
> This is rather a long shot, but it never hurts to ask... Has anyone
Hello Mason,
I did it for a customer for an iPhone application I wrote.
This was my design. For instance given a longitude / latitude
coordinate you save the ID of such a store in a Redis Set stored at
key with name given by the following PHP code:
define("SQUARESTEP",0.001);
setlocale(LC_ALL,"C");
function squareCoord($c,$step=SQUARESTEP) {
$i = floor($c*1000000);
$i += ($step/2)*1000000;
$res = ($i-($i%($step*1000000)))/1000000;
return $res;
}
function keyFromLatLong($lat,$long) {
$lat = squareCoord($lat);
$long = squareCoord($long);
return "sq:$lat:$long";
}
It's trivial to translate this code, but this was interfacing with the
customer's PHP application so I want to give you live code instead of
pseudocode.
So basically what you are doing is to divide all the world into
squares. The size of this squares is given by SQUARESTEP. When you
want to query this squares, translate your coordinates and get squares
near to the target one as well, accordingly to the zoom level.
Using Set intersection you can also filter for category, and given
that Redis is smart enough to order the sets in the intersection stage
and given that one of the two sets is small, it will be pretty fast.
Hope this helps,
Cheers,
Salvatore
--
Salvatore 'antirez' Sanfilippo
http://invece.org
"Once you have something that grows faster than education grows,
you’re always going to get a pop culture.", Alan Kay