Redis GEOADD – How to create and add elements in geospatial value
2 months ago Lalit Bhagtani 0
In this tutorial, we will learn about how to create and add elements in a geospatial value stored at the key, by using a COMMAND – GEOADD in redis-cli.
This command is used to add one or more specified geospatial member in the geospatial value stored at a key. A geospatial value is nothing but a sorted set, which is populated using this command. A geospatial member is added in the sorted set in such a way that, which makes it possible to later retrieve a member using a query by radius with the GEORADIUS and GEORADIUSBYMEMBER commands.
The sorted set is populated using a techniques called as Geohash. In this techniques, latitude and longitude bits are interleaved in order to form an unique 52 bit integer. This unique 52 bit integer is stored as a score of name value in sorted set.
A geospatial member contains three values ( information ) about location, its longitude, latitude, and name. So this command takes three arguments for adding one geospatial member, first should be longitude coordinates followed by latitude coordinates and the last should be name value. There are limit to the coordinates ( longitude, latitude ) values that can be added in the geospatial value. The exact limits as specified by EPSG:900913 / EPSG:3785 / OSGEO:41001 are as follows :-
- Valid range of longitudes are from -180 to 180 degrees.
- Valid latitudes are from -85.05112878 to 85.05112878 degrees.
An error is returned, when command tries to add coordinates which is outside of this specified range.
If key exists in the datastore, all the specified elements will be added ignoring the elements that are already present ( only score is updated ) in the sorted set otherwise a new sorted set will be created before performing the insertion operation. The syntax of redis GEOADD command is as follows :-
redis host:post> GEOADD <keyname> <longitude> <latitude> <name> [longitude latitude name]
- (integer) value, representing the number of elements added to the sorted set, not including elements that were already existed, whose only score was updated. - Error, if key exist and value stored at the key is not a sorted set populated using GEOADD command.
That’s all for how to create and add elements in a geospatial value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.