user1597670
user1597670

Reputation: 21

Geohash in redis with php

I am using Redis with php and its library phpredis.

I have the following structure:

city:1 {
 lat->14.02,
 lon->10.00,
 hash->sp2f1h60w5j
}

city:2 {
 lat->14.03,
 lon->10.1,
 hash->sp2f1h60w5m
}

But I haven't found a way to search by hash. I would like to search for example the exact same hash or approximately the same hash. Should I change my structure?

Thanks.

Upvotes: 2

Views: 1971

Answers (3)

Faisal
Faisal

Reputation: 18988

Do you want to find all items near a specific point? Since you're looking for exact or near match, I assume that's the case. Redis just released brand new Geo functionality yesterday (http://redis.io/commands#geo).

GeoRadius and GeoRadiusByMember are the ones you're looking for.

Upvotes: 0

Jacob Christensen
Jacob Christensen

Reputation: 141

Why you don't use the key in redis to get the elements.

like:

city-sp2f1h60w5j = 1

city-sp2f1h60w5m = 2

then if you wish the closets: get by "key": city-sp2f1h60w* if will return "array" of keys... or city id's. Can't remember the exact return data.

cheers

Upvotes: 1

Linus Thiel
Linus Thiel

Reputation: 39223

You could follow Josiah Carlson's advice from this thread, and convert the hash into a number, and use that as a score in a sorted set. Something like this:

city:1 {
 lat->14.02,
 lon->10.00,
 hash->sp2f1h60w5j
}

city:2 {
 lat->14.03,
 lon->10.1,
 hash->sp2f1h60w5m
}

To use the geohashes as numbers, you need to decode them from base 32 using a particular character map -- see Geohash on Wikipedia. I'll use example numbers below.

cities { (1, 4711), (2, 4712) }

Now you can use zrangebyscore to find cities in an area:

zrangebyscore cities 4000 5000

Upvotes: 4

Related Questions