Simon
Simon

Reputation: 45

Redis store geo infomation,

I just meet a problem. I use redis to store the geo information. for example:

hset 10001 la 41.000333
hset 10001 lo 121.999999

or

zadd la 41.xxxxx pk-value
zadd lo 121.xxxxx pk-value

about 40000 key-values the key is for the terminal id, and value is set, storing the termianl gps info. I have a requirement to computing the around terminal. for example, my location is 41.000123, 121.999988, and I want to the fastest compute the terminal around my location, I have idea how to compute the two location's distance. All I want is to think a way to fast iterate all data. In Redis 2.6 there is lua support. Can it help to resolve my problem?

Upvotes: 2

Views: 1490

Answers (2)

anydot
anydot

Reputation: 1539

You probably want to use geohashes, then you will be able to store (and search by) lon/lat with any precision you want, also it's relatively easy to get points which are in given bounding box.

For implementation with redis, have a look at geodis.

Upvotes: 2

Linus Thiel
Linus Thiel

Reputation: 39261

As I understand your question, you want to find all values close to some coordinates? One way would be to use Lua scripting, another would be to store one sorted set for each approximate latitude/longitude (if you know in advance which granularity you require). Example:

zadd la.41 41.000333 pk-value
zadd lo.121 121.999999 pk-value

Then, when you need to find something close to some coords (let's say (42.01, 122.03)), you would do something like:

lat = 42.01
lon = 122.03
lat_min, lat_mid, lat_max = round(lat - 1), round(lat), round(lat + 1)
lon_min, lon_mid, lon_max = round(lon - 1), round(lon), round(lon + 1)

Thus, you would look in the sorted sets la.41, la.42, la.43, lo.121, lo.122, lo.123:

zinterstore close.${lat},${lon} 6 la.${lat_min}, la.${lat_mid}, la.${lat_max}, lo.${lon_min}, lo.${lon_mid}, lo.${lon_max}

Now, close.${lat},${lon} should contain the id of every terminal close to the supplied coordinates.

Obviously, you could store each coordinate with greater granularity, like la.41.0, lo.121.0 and look only for terminals that close. Optionally, you could further filter the result in your client code.

Upvotes: 1

Related Questions