Summary
Command Name | GEOADD |
Usage | Add geospatial item(s) |
Group | geo |
ACL Category | @write @geo @slow |
Time Complexity | O(log(N)) |
Flag | WRITE DENYOOM |
Arity | -5 |
NOTES
- Time complexity of this command is O(log(N)), where N is the number of elements in the key, to which the geo data is being added.
Signature
GEOADD <key>
[NX|XX]
[CH]
<longitude>
<latitude>
<member>
[
… ]<longitude>
<latitude>
<member>
Usage
Add one or more geospatial items/members to a geospatial index.
NOTES
- If the <key> does not exist, it is created first, and then the data is inserted.
- Longitude range is from -180 to 180 degrees.
- Latitude range is from -85.05112878 to 85.05112878 degrees.
- The geospatial data is saved in a key as a sorted set.
- Though we provided longitude and latitude, but Redis internally saves the location information as geohash.
Arguments
Parameter | Group | Description | Name | Type | Multiple | Optional |
---|---|---|---|---|---|---|
<key> | Name of the key that holds the (longitude, latitude, member identifier) combinations. Consider this as a namespace for storing location data. | key | key | |||
NX | condition | Add only if the entry does not exist already. Do not update any existing item. | nx | pure-token | True | |
XX | condition | Only update already existing items. Do not add any item. | xx | pure-token | True | |
CH | Return the number of total changed(added or updated) items. In general(without CH) options, the command returns the number of items added. We add the “CH” option to get the total changed items. | change | pure-token | True | ||
<longitude> | data | Longitude value | longitude | double | True | |
<latitude> | data | Latitude value | latitude | double | True | |
<member> | data | Name or identifier of the geolocation | member | string | True |
NOTES
- <member> name is case sensitive. So be careful while setting and querying it.
- Only one of the options between “NX” and “XX” is allowed.
Return Value
Return value | Case for the return value | Type |
---|---|---|
Number of items | On success, it returns one of the following- 1. in case of items added, it returns the number of items added 2. in case option CH is passed, it returns the number of the items changed. | integer |
error | If the value or longitude and/or latitude are out of range or applied to the wrong data type. | error |
NOTES
- Make sure to pass the “CH” option if you want to get the total number of items that are changed(added and/or updated).
Without the “CH” options this command will return only the number of added items. So without “CH” it will return Zero(0) if items are only updated. - If the longitude or latitude value is out of range then the following error is returned-
(error) ERR invalid longitude,latitude pair 200.000000,80.000000
- If the command is applied to a key that is not a hash, then the following error is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Examples
Here are a few examples of the usage examples of the Redis GEOADD command-
# Redis GEOADD command examples
# Add single location
127.0.0.1:6379> geoadd bigboxcity 2.352222 48.856613 Paris
(integer) 1
# Add multiple location data
127.0.0.1:6379> geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
(integer) 4
# Check geospatial data using sorted set command
127.0.0.1:6379> zrange bigboxcity 0 -1 withscores
1) "Rome"
2) "3480343273965391"
3) "Paris"
4) "3663832779125283"
5) "Bangkok"
6) "3962257436268857"
7) "Hong Kong"
8) "4046429669534462"
9) "Tokyo"
10) "4171231230197033"
# Try to add only if the record does not exit
127.0.0.1:6379> geoadd bigboxcity NX 2.352222 48.856613 Paris
(integer) 0
# Check the location data
127.0.0.1:6379> geopos bigboxcity Paris
1) 1) "2.35221952199935913"
2) "48.85661220395509474"
# Add/change only if it exits
127.0.0.1:6379> geoadd bigboxcity XX 2.352222 48.856615 Paris
(integer) 0
# Check location data
# It is changed by the previous GEOADD command
127.0.0.1:6379> geopos bigboxcity Paris
1) 1) "2.35221952199935913"
2) "48.85661473867625659"
# Add/change location data
# and return the total number of items changed(added or update)
127.0.0.1:6379> geoadd bigboxcity CH 2.352222 48.856612 Paris
(integer) 1
# Check location. it is changed by the preivous command
127.0.0.1:6379> geopos bigboxcity Paris
1) 1) "2.35221952199935913"
2) "48.85661220395509474"
# Add a member with empty string
127.0.0.1:6379> geoadd bigboxcity -87.629799 41.878113 ""
(integer) 1
# Check members
127.0.0.1:6379> zrange bigboxcity 0 -1
1) ""
2) "Rome"
3) "Paris"
4) "Bangkok"
5) "Hong Kong"
6) "Tokyo"
# Try to use value that is out of range
# We get an error, which indicates vlaue is out of range
127.0.0.1:6379> geoadd bigboxcity 200 80 "Out of range"
(error) ERR invalid longitude,latitude pair 200.000000,80.000000
# Set a string value
127.0.0.1:6379> set bigboxstr "my string for testing"
OK
# Try to use the string key for GETADD command
# We get an error, which indicates the type of key is wrong
127.0.0.1:6379> geoadd bigboxstr 37.617298 55.755825 Moscow
(error) WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Member name in the geoindex can be an empty string(“”).
Code Implementations
Redis GEOADD command example implementation in Golang, NodeJS, Java, C#, PHP, Python, Ruby-
// Redis GEOADD command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
/**
* Add single location
* Command: geoadd bigboxcity 2.352222 48.856613 Paris
* Result: (integer) 1
*/
getaddResult, err := rdb.GeoAdd(ctx, "bigboxcity",
&redis.GeoLocation{Longitude: 2.352222, Latitude: 48.85661, Name: "Paris"},
).Result()
if err != nil {
fmt.Println("Command: geoadd bigboxcity 2.352222 48.856613 Paris | Error: " + err.Error())
}
fmt.Println("Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: ", getaddResult)
/**
* Add multiple location data
* Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 4
*/
getaddResult, err = rdb.GeoAdd(ctx, "bigboxcity",
&redis.GeoLocation{Longitude: 100.501762, Latitude: 13.756331, Name: "Bangkok"},
&redis.GeoLocation{Longitude: 114.109497, Latitude: 22.396427, Name: "Hong Kong"},
&redis.GeoLocation{Longitude: 139.691711, Latitude: 35.689487, Name: "Tokyo"},
&redis.GeoLocation{Longitude: 12.496365, Latitude: 41.902782, Name: "Rome"},
).Result()
if err != nil {
fmt.Println("Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Error: " + err.Error())
}
fmt.Println("Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ", getaddResult)
/**
* Check geospatial data using sorted set command
* Command: zrange bigboxcity 0 -1 withscores
* Result:
* 1) "Rome"
* 2) "3480343273965391"
* 3) "Paris"
* 4) "3663832779125283"
* 5) "Bangkok"
* 6) "3962257436268857"
* 7) "Hong Kong"
* 8) "4046429669534462"
* 9) "Tokyo"
* 10) "4171231230197033"
*/
zrangeResult, err := rdb.ZRangeWithScores(ctx, "bigboxcity", 0, -1).Result()
if err != nil {
fmt.Println("Command: zrange bigboxcity 0 -1 withscores | Error: " + err.Error())
}
fmt.Println("Command: zrange bigboxcity 0 -1 withscores | Result: ", zrangeResult)
/**
* Try to use value that is out of range
* We get an error, which indicates vlaue is out of range
* Command: geoadd bigboxcity 200 80 "Out of range"
* Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
*/
getaddResult, err = rdb.GeoAdd(ctx, "bigboxcity",
&redis.GeoLocation{Longitude: 200, Latitude: 80, Name: "Out of range"},
).Result()
if err != nil {
fmt.Println("Command: geoadd bigboxcity 200 80 "Out of range" | Error: " + err.Error())
}
fmt.Println("Command: geoadd bigboxcity 200 80 "Out of range" | Result: ", getaddResult)
/**
* Set a string value
* Command: set bigboxstr "my string for testing"
* Result: OK
*/
setResult, err := rdb.Set(ctx, "bigboxstr", "my string for testing", 0).Result()
if err != nil {
fmt.Println("Command: set bigboxstr "my string for testing" | Error: " + err.Error())
}
fmt.Println("Command: set bigboxstr "my string for testing" | Result: " + setResult)
/**
* Try to use the string key for GETADD command
* We get an error, which indicates the type of key is wrong
* Command: geoadd bigboxstr 37.617298 55.755825 Moscow
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
getaddResult, err = rdb.GeoAdd(ctx, "bigboxstr",
&redis.GeoLocation{Longitude: 37.617298, Latitude: 55.755825, Name: "Moscow"},
).Result()
if err != nil {
fmt.Println("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Error: " + err.Error())
}
fmt.Println("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: ", getaddResult)
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: 1
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: [{3.480343273965391e+15 Rome} {3.663832779125282e+15 Paris} {3.962257436268857e+15 Bangkok} {4.046429669534462e+15 Hong Kong} {4.171231230197033e+15 Tokyo}]
Command: geoadd bigboxcity 200 80 "Out of range" | Error: ERR invalid longitude,latitude pair 200.000000,80.000000
Command: geoadd bigboxcity 200 80 "Out of range" | Result: 0
Command: set bigboxstr "my string for testing" | Result: OK
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: 0
NOTES
- Use “
GeoAdd
” method from redis-go module. - Signature of the method is-
GeoAdd(ctx context.Context, key string, geoLocation ...*GeoLocation) *IntCmd
- Definition of
Geolocation
is as follows-type GeoLocation struct {
Name string
Longitude, Latitude, Dist float64
GeoHash int64
}
// Redis GEOADD command example in JavaScript(NodeJS)
import { createClient } from "redis";
// Create redis client
const redisClient = createClient({
url: "redis://default:@localhost:6379",
});
redisClient.on("error", (err) =>
console.log("Error while connecting to Redis", err)
);
// Connect Redis client
await redisClient.connect();
/**
* Add single location
* Command: geoadd bigboxcity 2.352222 48.856613 Paris
* Result: (integer) 1
*/
let commandResult = await redisClient.geoAdd("bigboxcity", {
member: "Paris",
longitude: 2.352222,
latitude: 48.85661,
});
console.log(
"Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: ",
commandResult
);
/**
* Add multiple location data
* Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 4
*/
const locationData = [
{
member: "Bangkok",
longitude: 100.501762,
latitude: 13.756331,
},
{
member: "Hong Kong",
longitude: 114.109497,
latitude: 22.396427,
},
{
member: "Tokyo",
longitude: 139.691711,
latitude: 35.689487,
},
{
member: "Rome",
longitude: 12.496365,
latitude: 41.90278,
},
];
commandResult = await redisClient.geoAdd("bigboxcity", locationData);
console.log(
'Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ',
commandResult
);
/**
* Check geospatial data using sorted set command
* Command: zrange bigboxcity 0 -1 withscores
* Result:
* 1) "Rome"
* 2) "3480343273965391"
* 3) "Paris"
* 4) "3663832779125283"
* 5) "Bangkok"
* 6) "3962257436268857"
* 7) "Hong Kong"
* 8) "4046429669534462"
* 9) "Tokyo"
* 10) "4171231230197033"
*/
commandResult = await redisClient.zRangeWithScores("bigboxcity", 0, -1);
console.log(
"Command: zrange bigboxcity 0 -1 withscores | Result: ",
commandResult
);
/**
* Try to add only if the record does not exit
* Command: geoadd bigboxcity NX 2.352222 48.856613 Paris
* Result: (integer) 0
*/
commandResult = await redisClient.geoAdd(
"bigboxcity",
{
member: "Paris",
longitude: 2.352222,
latitude: 48.856613,
},
{ NX: true }
);
console.log(
"Command: geoadd bigboxcity NX 2.352222 48.856613 Paris | Result: ",
commandResult
);
/**
* Check the location data
* Command: geopos bigboxcity Paris
* Result:
* 1) 1) "2.35221952199935913"
* 2) "48.85661220395509474"
*/
commandResult = await redisClient.geoPos("bigboxcity", "Paris");
console.log("Command: geopos bigboxcity Paris | Result: ", commandResult);
/**
* Add/change only if it exits
* Command: geoadd bigboxcity XX 2.352222 48.856615 Paris
* Result: (integer) 0
*/
commandResult = await redisClient.geoAdd(
"bigboxcity",
{
member: "Paris",
longitude: 2.352222,
latitude: 48.856615,
},
{ XX: true }
);
console.log(
"Command: geoadd bigboxcity XX 2.352222 48.856615 Paris | Result: ",
commandResult
);
/**
* Check location data
* It is changed by the previous GEOADD command
* Command: geopos bigboxcity Paris
* Result:
* 1) 1) "2.35221952199935913"
* 2) "48.85661473867625659"
*/
commandResult = await redisClient.geoPos("bigboxcity", "Paris");
console.log("Command: geopos bigboxcity Paris | Result: ", commandResult);
/**
* Add/change location data
* and return the total number of items changed(added or update)
* Command: geoadd bigboxcity CH 2.352222 48.856612 Paris
* Result: (integer) 1
*/
commandResult = await redisClient.geoAdd(
"bigboxcity",
{
member: "Paris",
longitude: 2.352222,
latitude: 48.856612,
},
{ CH: true }
);
console.log(
"Command: geoadd bigboxcity CH 2.352222 48.856612 Paris | Result: ",
commandResult
);
/**
* Check location. it is changed by the preivous command
* Command: geopos bigboxcity Paris
* Result:
* 1) 1) "2.35221952199935913"
* 2) "48.85661220395509474"
*/
commandResult = await redisClient.geoPos("bigboxcity", "Paris");
console.log("Command: geopos bigboxcity Paris | Result: ", commandResult);
/**
* Try to use value that is out of range
* We get an error, which indicates vlaue is out of range
* Command: geoadd bigboxcity 200 80 "Out of range"
* Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
*/
try {
commandResult = await redisClient.geoAdd("bigboxcity", {
member: "Out of range",
longitude: 200,
latitude: 80,
});
console.log(
'Command: geoadd bigboxcity 200 80 "Out of range" | Result: ',
commandResult
);
} catch (err) {
console.log(
'Command: geoadd bigboxcity 200 80 "Out of range" | Result: ',
err
);
}
/**
* Set a string value
* Command: set bigboxstr "my string for testing"
* Result: OK
*/
commandResult = await redisClient.set("bigboxstr", "my string for testing");
console.log(
'Command: set bigboxstr "my string for testing" | Result: ' + commandResult
);
/**
* Try to use the string key for GETADD command
* We get an error, which indicates the type of key is wrong
* Command: geoadd bigboxstr 37.617298 55.755825 Moscow
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.geoAdd("bigboxstr", {
member: "Moscow",
longitude: 37.617298,
latitude: 55.755825,
});
console.log(
"Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " +
commandResult
);
} catch (err) {
console.log(
"Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: ",
err
);
}
process.exit(0);
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: 1
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: [
{ value: 'Rome', score: 3480343273965390 },
{ value: 'Paris', score: 3663832779125282 },
{ value: 'Bangkok', score: 3962257436268857 },
{ value: 'Hong Kong', score: 4046429669534462 },
{ value: 'Tokyo', score: 4171231230197033 }
]
Command: geoadd bigboxcity NX 2.352222 48.856613 Paris | Result: 0
Command: geopos bigboxcity Paris | Result: [
{
longitude: '2.35221952199935913',
latitude: '48.85660966923393289'
}
]
Command: geoadd bigboxcity XX 2.352222 48.856615 Paris | Result: 0
Command: geopos bigboxcity Paris | Result: [
{
longitude: '2.35221952199935913',
latitude: '48.85661473867625659'
}
]
Command: geoadd bigboxcity CH 2.352222 48.856612 Paris | Result: 1
Command: geopos bigboxcity Paris | Result: [
{
longitude: '2.35221952199935913',
latitude: '48.85661220395509474'
}
]
Command: geoadd bigboxcity 200 80 "Out of range" | Result: [ErrorReply: ERR invalid longitude,latitude pair 200.000000,80.000000]
Command: set bigboxstr "my string for testing" | Result: OK
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
NOTES
- Use the function “
geoAdd
” of node-redis. - Signature of the method-
function geoAdd(key: RedisCommandArgument, toAdd: GeoMember | Array<GeoMember>, options?: GeoAddOptions)
- Example of the
GeoMember
is-{
member: "Moscow",
longitude: 37.617298,
latitude: 55.755825,
} - Example of
GeoAddOptions
is –{
NX: true,
XX: true,
CH: true,
}
this is just an example to show all the options, in real usage, one of the NX and XX should be used.
// Redis GEOADD command example in Java
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.GeoAddParams;
import redis.clients.jedis.resps.Tuple;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GeoAdd {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Add single location
* Command: geoadd bigboxcity 2.352222 48.856613 Paris
* Result: (integer) 1
*/
long getaddResult = jedis.geoadd("bigboxcity", 2.352222, 48.85661, "Paris");
System.out.println("Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: " + getaddResult);
/**
* Add multiple location data
* Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 4
*/
Map<String, GeoCoordinate> locationData = new HashMap<>() {{
put("Bangkok", new GeoCoordinate(100.501762, 13.756331));
put("Hong Kong", new GeoCoordinate(114.109497, 22.396427));
put("Tokyo", new GeoCoordinate(139.691711, 35.689487));
put("Rome", new GeoCoordinate(12.496365, 41.90278));
}};
getaddResult = jedis.geoadd("bigboxcity", locationData);
System.out.println("Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " + getaddResult);
/**
* Check geospatial data using sorted set command
* Command: zrange bigboxcity 0 -1 withscores
* Result:
* 1) "Rome"
* 2) "3480343273965391"
* 3) "Paris"
* 4) "3663832779125283"
* 5) "Bangkok"
* 6) "3962257436268857"
* 7) "Hong Kong"
* 8) "4046429669534462"
* 9) "Tokyo"
* 10) "4171231230197033"
*/
List<Tuple> zrangeResult = jedis.zrangeWithScores("bigboxcity", 0, -1);
System.out.println("Command: zrange bigboxcity 0 -1 withscores | Result: " + zrangeResult.toString());
/**
* Try to add only if the record does not exit
* Command: geoadd bigboxcity NX 2.352222 48.856613 Paris
* Result: (integer) 0
*/
locationData = new HashMap<>() {{
put("Paris", new GeoCoordinate(2.352222, 48.856613));
}};
getaddResult = jedis.geoadd("bigboxcity", GeoAddParams.geoAddParams().nx(), locationData);
System.out.println("Command: geoadd bigboxcity NX 2.352222 48.856613 Paris | Result: " + getaddResult);
/**
* Check the location data
* Command: geopos bigboxcity Paris
* Result:
* 1) 1) "2.35221952199935913"
* 2) "48.85661220395509474"
*/
List<GeoCoordinate> geoposResult = jedis.geopos("bigboxcity", "Paris");
System.out.println("Command: geopos bigboxcity Paris | Result: " + geoposResult.toString());
/**
* Add/change only if it exits
* Command: geoadd bigboxcity XX 2.352222 48.856615 Paris
* Result: (integer) 0
*/
locationData = new HashMap<>() {{
put("Paris", new GeoCoordinate(2.352222, 48.856615));
}};
getaddResult = jedis.geoadd("bigboxcity", GeoAddParams.geoAddParams().xx(), locationData);
System.out.println("Command: geoadd bigboxcity XX 2.352222 48.856615 Paris | Result: " + getaddResult);
/**
* Check location data
* It is changed by the previous GEOADD command
* Command: geopos bigboxcity Paris
* Result:
* 1) 1) "2.35221952199935913"
* 2) "48.85661473867625659"
*/
geoposResult = jedis.geopos("bigboxcity", "Paris");
System.out.println("Command: geopos bigboxcity Paris | Result: " + geoposResult.toString());
/**
* Add/change location data
* and return the total number of items changed(added or update)
* Command: geoadd bigboxcity CH 2.352222 48.856612 Paris
* Result: (integer) 1
*/
locationData = new HashMap<>() {{
put("Paris", new GeoCoordinate(2.352222, 48.856612));
}};
getaddResult = jedis.geoadd("bigboxcity", GeoAddParams.geoAddParams().ch(), locationData);
System.out.println("Command: geoadd bigboxcity CH 2.352222 48.856612 Paris | Result: " + getaddResult);
/**
* Check location. it is changed by the preivous command
* Command: geopos bigboxcity Paris
* Result:
* 1) 1) "2.35221952199935913"
* 2) "48.85661220395509474"
*/
geoposResult = jedis.geopos("bigboxcity", "Paris");
System.out.println("Command: geopos bigboxcity Paris | Result: " + geoposResult.toString());
/**
* Try to use value that is out of range
* We get an error, which indicates vlaue is out of range
* Command: geoadd bigboxcity 200 80 "Out of range"
* Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
*/
try {
getaddResult = jedis.geoadd("bigboxcity", 200, 80, "Out of range");
System.out.println("Command: geoadd bigboxcity 200 80 "Out of range" | Result: " + getaddResult);
} catch (Exception e) {
System.out.println("Command: geoadd bigboxcity 200 80 "Out of range" | Result: " + e.getMessage());
}
/**
* Set a string value
* Command: set bigboxstr "my string for testing"
* Result: OK
*/
String setResult = jedis.set("bigboxstr", "my string for testing");
System.out.println("Command: set bigboxstr "my string for testing" | Result: " + setResult);
/**
* Try to use the string key for GETADD command
* We get an error, which indicates the type of key is wrong
* Command: geoadd bigboxstr 37.617298 55.755825 Moscow
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
getaddResult = jedis.geoadd("bigboxstr", 37.617298, 55.755825, "Moscow");
System.out.println("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " + getaddResult);
} catch (Exception e) {
System.out.println("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: 1
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: [[Rome,3.48034327396539E15], [Paris,3.663832779125282E15], [Bangkok,3.962257436268857E15], [Hong Kong,4.046429669534462E15], [Tokyo,4.171231230197033E15]]
Command: geoadd bigboxcity NX 2.352222 48.856613 Paris | Result: 0
Command: geopos bigboxcity Paris | Result: [(2.352219521999359,48.85660966923393)]
Command: geoadd bigboxcity XX 2.352222 48.856615 Paris | Result: 0
Command: geopos bigboxcity Paris | Result: [(2.352219521999359,48.85661473867626)]
Command: geoadd bigboxcity CH 2.352222 48.856612 Paris | Result: 1
Command: geopos bigboxcity Paris | Result: [(2.352219521999359,48.856612203955095)]
Command: geoadd bigboxcity 200 80 "Out of range" | Result: ERR invalid longitude,latitude pair 200.000000,80.000000
Command: set bigboxstr "my string for testing" | Result: OK
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use method “
geoadd
” from Jedis package. - The signatures of the method are-
long geoadd(String key, double longitude, double latitude, String member)
long geoadd(String key, Map<String, GeoCoordinate> memberCoordinateMap)
long geoadd(String key, GeoAddParams params, Map<String, GeoCoordinate> memberCoordinateMap)
// Redis GEOADD command examples in C#
using StackExchange.Redis;
namespace GeoAdd
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Add single location
* Command: geoadd bigboxcity 2.352222 48.856613 Paris
* Result: (integer) 1
*/
bool getaddResult = rdb.GeoAdd("bigboxcity", 2.352222, 48.85661, "Paris");
Console.WriteLine("Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: " + getaddResult);
/**
* Add multiple location data
* Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 4
*/
GeoEntry[] locationData = new GeoEntry[]{
new GeoEntry(100.501762, 13.756331, "Bangkok"),
new GeoEntry(114.109497, 22.396427, "Hong Kong"),
new GeoEntry(139.691711, 35.689487, "Tokyo"),
new GeoEntry(12.496365, 41.90278, "Rome"),
};
long getaddResults = rdb.GeoAdd("bigboxcity", locationData);
Console.WriteLine("Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " + getaddResults);
/**
* Check geospatial data using sorted set command
* Command: zrange bigboxcity 0 -1 withscores
* Result:
* 1) "Rome"
* 2) "3480343273965391"
* 3) "Paris"
* 4) "3663832779125283"
* 5) "Bangkok"
* 6) "3962257436268857"
* 7) "Hong Kong"
* 8) "4046429669534462"
* 9) "Tokyo"
* 10) "4171231230197033"
*/
SortedSetEntry[] zrangeResult = rdb.SortedSetRangeByRankWithScores("bigboxcity", 0, -1);
Console.WriteLine("Command: zrange bigboxcity 0 -1 withscores | Result: " + String.Join(",", zrangeResult));
/**
* Try to use value that is out of range
* We get an error, which indicates vlaue is out of range
* Command: geoadd bigboxcity 200 80 "Out of range"
* Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
*/
try
{
getaddResult = rdb.GeoAdd("bigboxcity", 200, 80, "Out of range");
Console.WriteLine("Command: geoadd bigboxcity 200 80 "Out of range" | Result: " + getaddResult);
}
catch (Exception e)
{
Console.WriteLine("Command: geoadd bigboxcity 200 80 "Out of range" | Result: " + e.Message);
}
/**
* Set a string value
* Command: set bigboxstr "my string for testing"
* Result: OK
*/
bool setResult = rdb.StringSet("bigboxstr", "my string for testing");
Console.WriteLine("Command: set bigboxstr "my string for testing" | Result: " + setResult);
/**
* Try to use the string key for GETADD command
* We get an error, which indicates the type of key is wrong
* Command: geoadd bigboxstr 37.617298 55.755825 Moscow
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
getaddResult = rdb.GeoAdd("bigboxstr", 37.617298, 55.755825, "Moscow");
Console.WriteLine("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " + getaddResult);
}
catch (Exception e)
{
Console.WriteLine("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " + e.Message);
}
}
}
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: True
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: Rome: 3480343273965390,Paris: 3663832779125282,Bangkok: 3962257436268857,Hong Kong: 4046429669534462,Tokyo: 4171231230197033
Command: geoadd bigboxcity 200 80 "Out of range" | Result: ERR invalid longitude,latitude pair 200.000000,80.000000
Command: set bigboxstr "my string for testing" | Result: True
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use the method “
” from StackExchange.Redis.GeoAdd
- Method signatures-
bool GeoAdd(RedisKey key, double longitude, double latitude, RedisValue member, CommandFlags flags = CommandFlags.None)
bool GeoAdd(RedisKey key, GeoEntry value, CommandFlags flags = CommandFlags.None)
long GeoAdd(RedisKey key, GeoEntry[] values, CommandFlags flags = CommandFlags.None)
<?php
// Redis GEOADD command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Add single location
* Command: geoadd bigboxcity 2.352222 48.856613 Paris
* Result: (integer) 1
*/
$commandResult = $redisClient->geoadd("bigboxcity", 2.352222, 48.85661, "Paris");
echo "Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: " . $commandResult . "n";
/**
* Add multiple location data
* Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
* Result: (integer) 4
*/
$commandResult = $redisClient->geoadd("bigboxcity",
100.501762, 13.756331, "Bangkok",
114.109497, 22.396427, "Hong Kong",
139.691711, 35.689487, "Tokyo",
12.496365, 41.902782, "Rome"
);
echo "Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " . $commandResult . "n";
/**
* Check geospatial data using sorted set command
* Command: zrange bigboxcity 0 -1 withscores
* Result:
* 1) "Rome"
* 2) "3480343273965391"
* 3) "Paris"
* 4) "3663832779125283"
* 5) "Bangkok"
* 6) "3962257436268857"
* 7) "Hong Kong"
* 8) "4046429669534462"
* 9) "Tokyo"
* 10) "4171231230197033"
*/
$commandResult = $redisClient->zrange("bigboxcity", 0, -1, ['withscores' => true]);
echo "Command: zrange bigboxcity 0 -1 withscores | Result: ";
print_r($commandResult);
/**
* Try to use value that is out of range
* We get an error, which indicates vlaue is out of range
* Command: geoadd bigboxcity 200 80 "Out of range"
* Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
*/
try {
$commandResult = $redisClient->geoadd("bigboxcity", 200, 80, "Out of range");
echo "Command: geoadd bigboxcity 200 80 "Out of range" | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: geoadd bigboxcity 200 80 "Out of range" | Result: " . $e->getMessage() . "n";
}
/**
* Set a string value
* Command: set bigboxstr "my string for testing"
* Result: OK
*/
$commandResult = $redisClient->set("bigboxstr", "my string for testing");
echo "Command: set bigboxstr "my string for testing" | Result: " . $commandResult . "n";
/**
* Try to use the string key for GETADD command
* We get an error, which indicates the type of key is wrong
* Command: geoadd bigboxstr 37.617298 55.755825 Moscow
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->geoadd("bigboxstr", 37.617298, 55.755825, "Moscow");
echo "Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: " . $e->getMessage() . "n";
}
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: 1
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: Array
(
[Rome] => 3480343273965391
[Paris] => 3663832779125282
[Bangkok] => 3962257436268857
[Hong Kong] => 4046429669534462
[Tokyo] => 4171231230197033
)
Command: geoadd bigboxcity 200 80 "Out of range" | Result: ERR invalid longitude,latitude pair 200.000000,80.000000
Command: set bigboxstr "my string for testing" | Result: OK
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use the method “
geoadd
” of predis. - Signature of the method is-
geoadd(string $key, $longitude, $latitude, $member): int
# Redis GEOADD command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(
host="localhost", port=6379, username="default", password="", decode_responses=True
)
# Add single location
# Command: geoadd bigboxcity 2.352222 48.856613 Paris
# Result: (integer) 1
commandResult = redisClient.geoadd("bigboxcity", (2.352222, 48.85661, "Paris"))
print(
"Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: {}".format(
commandResult
)
)
# Add multiple location data
# Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
# Result: (integer) 4
commandResult = redisClient.geoadd(
"bigboxcity",
(100.501762, 13.756331, "Bangkok")
+ (114.109497, 22.396427, "Hong Kong")
+ (139.691711, 35.689487, "Tokyo")
+ (12.496365, 41.902782, "Rome"),
)
print(
'Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: {}'.format(
commandResult
)
)
# Check geospatial data using sorted set command
# Command: zrange bigboxcity 0 -1 withscores
# Result:
# 1) "Rome"
# 2) "3480343273965391"
# 3) "Paris"
# 4) "3663832779125283"
# 5) "Bangkok"
# 6) "3962257436268857"
# 7) "Hong Kong"
# 8) "4046429669534462"
# 9) "Tokyo"
# 10) "4171231230197033"
commandResult = redisClient.zrange("bigboxcity", 0, -1, withscores=True)
print("Command: zrange bigboxcity 0 -1 withscores | Result: {}".format(commandResult))
# Try to add only if the record does not exit
# Command: geoadd bigboxcity NX 2.352222 48.856613 Paris
# Result: (integer) 0
commandResult = redisClient.geoadd(
"bigboxcity", (2.352222, 48.856613, "Paris"), nx=True
)
print(
"Command: geoadd bigboxcity NX 2.352222 48.856613 Paris | Result: {}".format(
commandResult
)
)
# Check the location data
# Command: geopos bigboxcity Paris
# Result:
# 1) 1) "2.35221952199935913"
# 2) "48.85661220395509474"
commandResult = redisClient.geopos("bigboxcity", "Paris")
print("Command: geopos bigboxcity Paris | Result: {}".format(commandResult))
# Add/change only if it exits
# Command: geoadd bigboxcity XX 2.352222 48.856615 Paris
# Result: (integer) 0
commandResult = redisClient.geoadd(
"bigboxcity", (2.352222, 48.856615, "Paris"), xx=True
)
print(
"Command: geoadd bigboxcity XX 2.352222 48.856615 Paris | Result: {}".format(
commandResult
)
)
# Check location data
# It is changed by the previous GEOADD command
# Command: geopos bigboxcity Paris
# Result:
# 1) 1) "2.35221952199935913"
# 2) "48.85661473867625659"
commandResult = redisClient.geopos("bigboxcity", "Paris")
print("Command: geopos bigboxcity Paris | Result: {}".format(commandResult))
# Add/change location data
# and return the total number of items changed(added or update)
# Command: geoadd bigboxcity CH 2.352222 48.856612 Paris
# Result: (integer) 1
commandResult = redisClient.geoadd(
"bigboxcity", (2.352222, 48.856612, "Paris"), ch=True
)
print(
"Command: geoadd bigboxcity CH 2.352222 48.856612 Paris | Result: {}".format(
commandResult
)
)
# Check location. it is changed by the preivous command
# Command: geopos bigboxcity Paris
# Result:
# 1) 1) "2.35221952199935913"
# 2) "48.85661220395509474"
commandResult = redisClient.geopos("bigboxcity", "Paris")
print("Command: geopos bigboxcity Paris | Result: {}".format(commandResult))
# Try to use value that is out of range
# We get an error, which indicates vlaue is out of range
# Command: geoadd bigboxcity 200 80 "Out of range"
# Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
try:
commandResult = redisClient.geoadd("bigboxcity", (200, 80, "Out of range"))
print(
'Command: geoadd bigboxcity 200 80 "Out of range" | Result: {}'.format(
commandResult
)
)
except Exception as error:
print('Command: geoadd bigboxcity 200 80 "Out of range" | Result: ', error)
# Set a string value
# Command: set bigboxstr "my string for testing"
# Result: OK
commandResult = redisClient.set("bigboxstr", "my string for testing")
print(
'Command: set bigboxstr "my string for testing" | Result: {}'.format(commandResult)
)
# Try to use the string key for GETADD command
# We get an error, which indicates the type of key is wrong
# Command: geoadd bigboxstr 37.617298 55.755825 Moscow
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.geoadd("bigboxstr", (37.617298, 55.755825, "Moscow"))
print(
"Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: {}".format(
commandResult
)
)
except Exception as error:
print("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: ", error)
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: 1
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: [('Rome', 3480343273965391.0), ('Paris', 3663832779125282.0), ('Bangkok', 3962257436268857.0), ('Hong Kong', 4046429669534462.0), ('Tokyo', 4171231230197033.0)]
Command: geoadd bigboxcity NX 2.352222 48.856613 Paris | Result: 0
Command: geopos bigboxcity Paris | Result: [(2.352219521999359, 48.85660966923393)]
Command: geoadd bigboxcity XX 2.352222 48.856615 Paris | Result: 0
Command: geopos bigboxcity Paris | Result: [(2.352219521999359, 48.85661473867626)]
Command: geoadd bigboxcity CH 2.352222 48.856612 Paris | Result: 1
Command: geopos bigboxcity Paris | Result: [(2.352219521999359, 48.856612203955095)]
Command: geoadd bigboxcity 200 80 "Out of range" | Result: invalid longitude,latitude pair 200.000000,80.000000
Command: set bigboxstr "my string for testing" | Result: True
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use method “
geoadd
” from redis-py. - Signature of the method is –
def geoadd(self, name: KeyT, values: Sequence[EncodableT], nx: bool = False, xx: bool = False, ch: bool = False,) -> ResponseT
# Redis GEOADD command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Add single location
# Command: geoadd bigboxcity 2.352222 48.856613 Paris
# Result: (integer) 1
commandResult = redis.geoadd("bigboxcity", 2.352222, 48.85661, "Paris")
print("Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: ", commandResult, "n")
# Add multiple location data
# Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
# Result: (integer) 4
commandResult = redis.geoadd(
"bigboxcity",
100.501762, 13.756331, "Bangkok",
114.109497, 22.396427, "Hong Kong",
139.691711, 35.689487, "Tokyo",
12.496365, 41.902782, "Rome",
)
print("Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ", commandResult, "n")
# Check geospatial data using sorted set command
# Command: zrange bigboxcity 0 -1 withscores
# Result:
# 1) "Rome"
# 2) "3480343273965391"
# 3) "Paris"
# 4) "3663832779125283"
# 5) "Bangkok"
# 6) "3962257436268857"
# 7) "Hong Kong"
# 8) "4046429669534462"
# 9) "Tokyo"
# 10) "4171231230197033"
commandResult = redis.zrange("bigboxcity", 0, -1, :withscores => true)
print("Command: zrange bigboxcity 0 -1 withscores | Result: ", commandResult, "n")
# Try to use value that is out of range
# We get an error, which indicates vlaue is out of range
# Command: geoadd bigboxcity 200 80 "Out of range"
# Result: (error) ERR invalid longitude,latitude pair 200.000000,80.000000
begin
commandResult = redis.geoadd("bigboxcity", 200, 80, "Out of range")
print("Command: geoadd bigboxcity 200 80 "Out of range" | Result: ", commandResult, "n")
rescue => e
print("Command: geoadd bigboxcity 200 80 "Out of range" | Result: ", e, "n")
end
# Set a string value
# Command: set bigboxstr "my string for testing"
# Result: OK
commandResult = redis.set("bigboxstr", "my string for testing")
print("Command: set bigboxstr "my string for testing" | Result: ", commandResult, "n")
# Try to use the string key for GETADD command
# We get an error, which indicates the type of key is wrong
# Command: geoadd bigboxstr 37.617298 55.755825 Moscow
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.geoadd("bigboxstr", 37.617298, 55.755825, "Moscow")
print("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: ", commandResult, "n")
rescue => e
print("Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: ", e, "n")
end
Output:
Command: geoadd bigboxcity 2.352222 48.856613 Paris | Result: 1
Command: geoadd bigboxcity 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 4
Command: zrange bigboxcity 0 -1 withscores | Result: [["Rome", 3.480343273965391e+15], ["Paris", 3.663832779125282e+15], ["Bangkok", 3.962257436268857e+15], ["Hong Kong", 4.046429669534462e+15], ["Tokyo", 4.171231230197033e+15]]
Command: geoadd bigboxcity 200 80 "Out of range" | Result: ERR invalid longitude,latitude pair 200.000000,80.000000
Command: set bigboxstr "my string for testing" | Result: OK
Command: geoadd bigboxstr 37.617298 55.755825 Moscow | Result: WRONGTYPE Operation against a key holding the wrong kind of value
NOTES
- Use method “
geoadd
” from the redis-rb. - Signature of the method is-
# @param [String] key
# @param [Array] member arguemnts for member or members: longitude, latitude, name
# @return [Integer] number of elements added to the sorted set
def geoadd(key, *member)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
SADD | Command Details |
SMEMBERS | Command Details |