Geocoding using redis?

1,229 views
Skip to first unread message

Mason Jones

unread,
Nov 18, 2009, 12:45:02 AM11/18/09
to redi...@googlegroups.com
This is rather a long shot, but it never hurts to ask... Has anyone
out there done any work using redis in conjunction with a
Geolocation/geocoding application? For example, working with Geocoder
(http://github.com/geocommons/geocoder) or the like. For that matter,
it seems to be hard to find any Ruby-based geocoding libraries, and
Geocoder seems to be U.S.-only, as well.

Anyway, I'm looking at a use-case where we might want to find all of
the stores near a certain location, based on tags, for example all
book stores. Redis sets are well-suited for the tags, but it would be
a shame to have to do the geocoding using one database, and then turn
around and check redis for each match against a set. So I wondered if
anyone else has looked at redis and geocoding?


-- Mason

Salvatore Sanfilippo

unread,
Nov 18, 2009, 6:35:51 AM11/18/09
to redi...@googlegroups.com
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

Salvatore Sanfilippo

unread,
Nov 18, 2009, 7:22:16 AM11/18/09
to redi...@googlegroups.com
@berryg on Twitter pointed me to this article:

http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves

That's very cool and very useful as the trivial algorithm I
illustrated is not good enough when there are hot spots with many many
data inside and other places almost empty. Really a must-read.

Cheers,
Salvatore

Mason Jones

unread,
Nov 18, 2009, 1:07:42 PM11/18/09
to redi...@googlegroups.com
Great, thank you for the thoughts (and the link to that other article,
which I'll go and read now!). I was thinking about something like
this, which is certainly interesting... I will start coding something
to test it out for us. Thanks again.
> --
>
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To post to this group, send email to redi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redis-db?hl=.
>
>
>

Luca Guidi

unread,
Dec 16, 2009, 6:59:58 PM12/16/09
to redis-db
Reply all
Reply to author
Forward
0 new messages