Geospatial data in Redis is represented in terms of longitude and latitude. The geospatial data is stored as a sorted set, as it makes it possible to query the items.
Some of the most frequently used commands for geospatial data in Redis are as follows:
GEOADD
commandThe GEOADD
command is used to add the specified geospatial data to the given key.
The syntax is as follows:
GEOADD key longitude latitude member [ longitude latitude member ...]
where:
key
is the name under which the sorted set for geospatial data is registered.longitude
is the longitude of the location.latitude
is the latitude of the location.member
is the member to store.GEODIST
commandThe GEODIST
command is used to get the distance between two geospatial members or points in the specified unit.
The syntax is as follows:
GEODIST key member1 member2 [ M | KM | FT | MI]
where:
key
is the name under which the sorted set for geospatial data is registered.member1
is the first geospatial member or point.member2
is the second geospatial member or point.The following units of distance are supported.
The unit must be one of the following and defaults to meters:
m
for meters.km
for kilometers.mi
for miles.ft
for feet.GEOSEARCH
commandThe GEOSEARCH
command is used to return the members of a geospatial populated sorted set that are within the borders of the area specified by a given shape. This command supports searching in circular as well as rectangular areas.
The syntax is as follows:
GEOSEARCH key FROMMEMBER member | FROMLONLAT longitude latitude BYRADIUS radius M | KM | FT | MI | BYBOX width height M | KM | FT | MI [ ASC | DESC]
where:
key
is the name under which the sorted set for geospatial data is registered.The reference center point for querying is provided by either of the following mandatory options:
FROMMEMBER
: This option indicates to use the given member in the sorted set.FROMLONLAT
: This option indicates to use the given longitude and latitude.The search shape for querying is provided by either of the following mandatory options:
BYRADIUS
: Search in a circular area according to the given radius.BYBOX
: Search in a rectangle according to the given height and width.To sort the returned results, use one of the following options:
ASC
: This option indicates that the results should be sorted in relation to the central point from nearest to farthest.DESC
: This option indicates that the results should be sorted in relation to the central point from farthest to nearest.GEOPOS
commandThe GEOPOS
command is used to get the positions (longitude, latitude) of all the specified members.
The syntax is as follows:
GEOPOS key member [member ...]
where:
key
is the name under which the sorted set for geospatial data is registered.member
is the member whose position has to be retrieved.