How to use redis-py to store Geo location data

created at 07-22-2021 views: 18

One of the important updates of Redis 3.2 is to provide support for geographic location (GEO) data. This feature allows users to store geographic location information in the Redis database, and perform distance calculations, range lookups, and other operations on them.

Fortunately, today I discovered that the latest version of redis-py has added support for GEO features, so today let us take a look at how to process geographic location data in redis-py.

redis-py github:https://github.com/andymccurdy/redis-py

Currently redis-py only supports geographic location (GEO) data in version 2.10.6. If you need to use GEO, please update to version 2.10.6.

If redis-py is installed

pip uninstall redis # remove current redis

pip install redis

Load the redis-py library:

from redis import  Redis
conn = Redis()

By accessing the properties of the Redis() object, we can confirm that each GEO command has a corresponding method in redis-py:

>>>>>>: for i in dir(conn):
   ...:     if i.startswith('geo'):
   ...:         print i
   ...:

geoadd
geodist
geohash
geopos
georadius
georadiusbymember

Add geographic location

use geoadd() to add locations:

geoadd(self, name, *values) 

>>> conn.geoadd("guangdong", 114.07, 22.62, "shenzhen", 113.23, 23.16, "guangzhou", 113.11, 23.05, "foshan")
3

Get geographic location

use geopos() to get position:

geopos(self, name, *values)

>>> conn.geopos('guangdong','guangzhou')
 [(113.22999805212021, 23.15999943763535)]

Calculate distance

Calculate distance between two places with geodist():

geodist(self, name, place1, place2, unit=None)

>>> conn.geodist('guangdong','guangzhou','foshan')
17331.9291

The GEODIST command uses meters as the unit by default, so it returns 17331.9291 meters as the result. To make this result more intuitive, we can change the unit of the GEODIST command from meters to kilometers (km)

>>> conn.geodist('guangdong','guangzhou','foshan',unit='km')
17.3319

Now you can see that the distance between Guangzhou and Foshan is 17.3319km

Range lookup

georadius() and georadiusbymember()

# With the given latitude and longitude as the center, find the elements within a certain radius
georadius(self, name, longitude, latitude, radius, unit=None, withdist=False, withcoord=False, withhash=False, count=None, sort=None, store=None, store_dist=None)

# Find the element located in the specified range, the center point is determined by the given position element
georadiusbymember(self, name, member, radius, unit=None, withdist=False, withcoord=False, withhash=False, count=None, sort=None, store=None, store_dist=None)

georadius()

# Find a city within 120 kilometers of Shenzhen's coordinates
>>> conn.georadius("Guangdong", 113.23, 23.16, 100, unit="km", withdist=True)

[['foshan', 17.3321], ['shenzhen', 0.0002], ['guangzhou', 104.9567]]

georadiusbymember()

>>> conn.georadiusbymember("guangdong", "guangzhou", 30, unit="km", withdist=True)
[['guangzhou', 0.0], ['foshan', 17.3319]]

Get geohash

# Return the Geohash representation of one or more location elements
geohash(self, name, *values)


>>> conn.geohash("guangdong", "guangzhou", "foshan", "shenzhou")
['ws0eb85sf00', 'ws07juh5yp0', 'ws10etz5p90']
created at:07-22-2021
edited at: 07-22-2021: