Redis Command: GEOPOS

Summary

Command NameGEOPOS
UsageGet position(s) of geo index member(s)
Group geo
ACL Category@read
@geo
@slow
Time ComplexityO(1)
FlagREADONLY
Arity-2

NOTES

  • Time complexity of the operation is O(1) for each member.

Signature

GEOPOS <key> [ <member1> [ <member2> ... ]]

Usage

Get the longitude and latitude of specific members from a geospatial index. Multiple members are allowed in the same command, so we can get the position of multiple members at the same time as an array.

NOTES

  • Returned geo positions are in [longitude, latitude] format. We need to keep this in mind, as this is unusual than what we use generally in other places.

Arguments

ParameterDescriptionNameTypeOptionalMultiple
<key>Name of the key that holds the geo indexkeykey
<member>Name of the member(s) location saved in the geo indexmemberstringTrueTrue

NOTES

  • <member> is optional. If <member> is not provided, then the command returns (empty array).

Return Value

Return valueCase for the return valueType
Array of positionsOn success, it returns the array of the positions(array[longitude, latitude]) values of the membersarray[[number, number]]
(empty array)If the key does not exist and/or members are not provided(empty array)
errorIf the applied to the wrong data typeerror

NOTES

  • (nil) is returned for any non-existing member.
  • If the key does not exist, then it returns (empty array).
  • If <member> is not provided, then it returns (empty array).
  • 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 command-

# Redis GEOPOS command examples

# Add members to a geo index named bigboxcity
127.0.0.1:6379> geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
(integer) 5

# Check members saved in bigboxcity
127.0.0.1:6379> zrange bigboxcity 0 -1
1) "Rome"
2) "Paris"
3) "Bangkok"
4) "Hong Kong"
5) "Tokyo"

# Check geopos of a single member
127.0.0.1:6379> geopos bigboxcity Paris
1) 1) "2.35221952199935913"
   2) "48.85661220395509474"

# Check geopos of multiple members
127.0.0.1:6379> geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
1) 1) "12.49636620283126831"
   2) "41.90278213378983452"
2) 1) "114.10949438810348511"
   2) "22.39642736199028406"
3) 1) "139.69171196222305298"
   2) "35.68948605865241319"
4) 1) "2.35221952199935913"
   2) "48.85661220395509474"
5) 1) "100.50176292657852173"
   2) "13.75633095031508191"

# Check geopos of multiple members
# But pass one non existing member name 
# We get (nil) for the non existing member
127.0.0.1:6379> geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
1) 1) "12.49636620283126831"
   2) "41.90278213378983452"
2) 1) "114.10949438810348511"
   2) "22.39642736199028406"
3) 1) "139.69171196222305298"
   2) "35.68948605865241319"
4) (nil)
5) 1) "100.50176292657852173"
   2) "13.75633095031508191"

# Using the same member multiple times will return the position multiple times
127.0.0.1:6379> geopos bigboxcity Tokyo Tokyo Tokyo
1) 1) "139.69171196222305298"
   2) "35.68948605865241319"
2) 1) "139.69171196222305298"
   2) "35.68948605865241319"
3) 1) "139.69171196222305298"
   2) "35.68948605865241319"

# Check geopos of a non existing members
# (nil) is returned for the non existing members
127.0.0.1:6379> geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
1) (nil)
2) (nil)
3) (nil)

# Check the command without any member
# We get an empty array
127.0.0.1:6379> geopos bigboxcity
(empty array)

# Pass a wrong non existing key
# we get an empty array
127.0.0.1:6379> geopos wrongkey
(empty array)

# Pass wrong key and wrong members
# Returns (nil) for all those members
127.0.0.1:6379> geopos wrongkey membera memberb memberc
1) (nil)
2) (nil)
3) (nil)

# Set string value
127.0.0.1:6379> set bigboxstr "some string here"
OK

# Try to use GEOHASH with some key that is not a geindex
# We get an error, for using key of wrong type
127.0.0.1:6379> geopos bigboxstr abc
(error) WRONGTYPE Operation against a key holding the wrong kind of value

NOTES

  • If we use the same member name multiple times, then the position(longitude, latitude) is returned multiple times.

Code Implementations

Here are the example implementations of the command in Golang, NodeJS, Java, C#, PHP, Python, Ruby-

// Redis GEOPOS 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 members to a geo index named bigboxcity
	*
	* Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
	* Result: (integer) 5
	 */
	getaddResult, err := rdb.GeoAdd(ctx, "bigboxcity",
		&redis.GeoLocation{Longitude: 2.352222, Latitude: 48.856613, Name: "Paris"},
		&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 2.352222 48.856613 Paris 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 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 \"Hong Kong\" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ", getaddResult)

	/**
	* Check members saved in bigboxcity
	*
	* Command: zrange bigboxcity 0 -1
	* Result:
	*      1) "Rome"
	*      2) "Paris"
	*      3) "Bangkok"
	*      4) "Hong Kong"
	*      5) "Tokyo"
	 */
	zrangeResult, err := rdb.ZRange(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)

	/**
	* Check geopos of a single member
	*
	* Command: geopos bigboxcity Paris
	* Result:
	*      1)  1) "2.35221952199935913"
	*          2) "48.85661220395509474"
	 */
	geoposResult, err := rdb.GeoPos(ctx, "bigboxcity", "Paris").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxcity Paris | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxcity Paris | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Check geopos of multiple members
	*
	* Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
	* Result:
	*      1)  1) "12.49636620283126831"
	*          2) "41.90278213378983452"
	*      2)  1) "114.10949438810348511"
	*          2) "22.39642736199028406"
	*      3)  1) "139.69171196222305298"
	*          2) "35.68948605865241319"
	*      4)  1) "2.35221952199935913"
	*          2) "48.85661220395509474"
	*      5)  1) "100.50176292657852173"
	*          2) "13.75633095031508191"
	 */
	geoposResult, err = rdb.GeoPos(ctx, "bigboxcity", "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo Paris Bangkok | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo Paris Bangkok | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Check geopos of multiple members
	* But pass one non existing member name
	* We get (nil) for the non existing member
	*
	* Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
	* Result:
	*      1)  1) "12.49636620283126831"
	*          2) "41.90278213378983452"
	*      2)  1) "114.10949438810348511"
	*          2) "22.39642736199028406"
	*      3)  1) "139.69171196222305298"
	*          2) "35.68948605865241319"
	*      4) (nil)
	*      5)  1) "100.50176292657852173"
	*          2) "13.75633095031508191"
	 */
	geoposResult, err = rdb.GeoPos(ctx, "bigboxcity", "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo WrongMemberValueHere Bangkok | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo WrongMemberValueHere Bangkok | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	 * Using the same member multiple times will return the position multiple times
	 *
	 * Command: geopos bigboxcity Tokyo Tokyo Tokyo
	 * Result:
	 *      1)  1) "139.69171196222305298"
	 *          2) "35.68948605865241319"
	 *      2)  1) "139.69171196222305298"
	 *          2) "35.68948605865241319"
	 *      3)  1) "139.69171196222305298"
	 *          2) "35.68948605865241319"
	 */
	geoposResult, err = rdb.GeoPos(ctx, "bigboxcity", "Tokyo", "Tokyo", "Tokyo").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxcity Tokyo Tokyo Tokyo | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxcity Tokyo Tokyo Tokyo | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Check geopos of a non existing members
	* (nil) is returned for the non existing members
	*
	* Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
	* Result:
	*      1) (nil)
	*      2) (nil)
	*      3) (nil)
	 */
	geoposResult, err = rdb.GeoPos(ctx, "bigboxcity", "wrongmember1", "wrongmember2", "wrongmember3").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Check the command without any member
	* We get an empty array
	*
	* Command: geopos bigboxcity
	* Result: (empty array)
	 */
	geoposResult, err = rdb.GeoPos(ctx, "bigboxcity").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxcity | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxcity | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Pass a wrong non existing key
	* we get an empty array
	*
	* Command: geopos wrongkey
	* Result: (empty array)
	 */
	geoposResult, err = rdb.GeoPos(ctx, "wrongkey").Result()

	if err != nil {
		fmt.Println("Command: geopos wrongkey | Error: " + err.Error())
	}

	fmt.Println("Command: geopos wrongkey | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Pass wrong key and wrong members
	* Returns (nil) for all those members
	*
	* Command: geopos wrongkey membera memberb memberc
	* Result:
	*      1) (nil)
	*      2) (nil)
	*      3) (nil)
	 */
	geoposResult, err = rdb.GeoPos(ctx, "wrongkey", "membera", "memberb", "memberc").Result()

	if err != nil {
		fmt.Println("Command: geopos wrongkey membera memberb memberc | Error: " + err.Error())
	}

	fmt.Println("Command: geopos wrongkey membera memberb memberc | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

	/**
	* Set string value
	*
	* Command: set bigboxstr "some string here"
	* Result: OK
	 */
	setResult, err := rdb.Set(ctx, "bigboxstr", "soem string here", 0).Result()

	if err != nil {
		fmt.Println("Command: set bigboxstr \"some string here\" | Error: " + err.Error())
	}

	fmt.Println("Command: set bigboxstr \"some string here\" | Result: " + setResult)

	/**
	* Try to use GEOHASH with some key that is not a geindex
	* We get an error, for using key of wrong type
	*
	* Command: geopos bigboxstr abc
	* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	 */
	geoposResult, err = rdb.GeoPos(ctx, "bigboxstr", "abc").Result()

	if err != nil {
		fmt.Println("Command: geopos bigboxstr abc | Error: " + err.Error())
	}

	fmt.Println("Command: geopos bigboxstr abc | Result: ")

	for _, item := range geoposResult {
		fmt.Println(item)
	}

}

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result:  5
Command: zrange bigboxcity 0 -1 withscores | Result:  [Rome Paris Bangkok Hong Kong Tokyo]
Command: geopos bigboxcity Paris | Result:
&{2.352219521999359 48.856612203955095}
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result:
&{12.496366202831268 41.902782133789835}
&{114.10949438810349 22.396427361990284}
&{139.69171196222305 35.68948605865241}
&{2.352219521999359 48.856612203955095}
&{100.50176292657852 13.756330950315082}
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result:
&{12.496366202831268 41.902782133789835}
&{114.10949438810349 22.396427361990284}
&{139.69171196222305 35.68948605865241}
<nil>
&{100.50176292657852 13.756330950315082}
Command: geopos bigboxcity Tokyo Tokyo Tokyo | Result:
&{139.69171196222305 35.68948605865241}
&{139.69171196222305 35.68948605865241}
&{139.69171196222305 35.68948605865241}
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result:
<nil>
<nil>
<nil>
Command: geopos bigboxcity | Result:
Command: geopos wrongkey | Result:
Command: geopos wrongkey membera memberb memberc | Result:
<nil>
<nil>
<nil>
Command: set bigboxstr "some string here" | Result: OK
Command: geopos bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: geopos bigboxstr abc | Result:

NOTES

  • Use “GeoPos” method from redis-go module.
  • Signature of the method is-
    • GeoPos(ctx context.Context, key string, members ...string) *GeoPosCmd
// Redis GEOPOS 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 members to a geo index named bigboxcity
 *
 * Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
 * Result: (integer) 5
 */
const locationData = [
  {
    member: "Paris",
    longitude: 2.352222,
    latitude: 48.856613,
  },
  {
    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,
  },
];
let commandResult = await redisClient.geoAdd("bigboxcity", locationData);

console.log(
  'Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: ',
  commandResult
);

/**
 * Check the items in bigboxcity
 * Command: zrange bigboxcity 0 -1
 * Result:
 *      1) "Rome"
 *      2) "Paris"
 *      3) "Bangkok"
 *      4) "Hong Kong"
 *      5) "Tokyo"
 */
commandResult = await redisClient.zRange("bigboxcity", 0, -1);

console.log(
  "Command: zrange bigboxcity 0 -1 withscores | Result: ",
  commandResult
);

/**
 * Check geopos of a single member
 *
 * 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);

/**
 * Check geopos of multiple members
 *
 * Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
 * Result:
 *      1)  1) "12.49636620283126831"
 *          2) "41.90278213378983452"
 *      2)  1) "114.10949438810348511"
 *          2) "22.39642736199028406"
 *      3)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      4)  1) "2.35221952199935913"
 *          2) "48.85661220395509474"
 *      5)  1) "100.50176292657852173"
 *          2) "13.75633095031508191"
 */
commandResult = await redisClient.geoPos("bigboxcity", [
  "Rome",
  "Hong Kong",
  "Tokyo",
  "Paris",
  "Bangkok",
]);

console.log(
  'Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: ',
  commandResult
);

/**
 * Check geopos of multiple members
 * But pass one non existing member name
 * We get (nil) for the non existing member
 *
 * Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
 * Result:
 *      1)  1) "12.49636620283126831"
 *          2) "41.90278213378983452"
 *      2)  1) "114.10949438810348511"
 *          2) "22.39642736199028406"
 *      3)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      4) (nil)
 *      5)  1) "100.50176292657852173"
 *          2) "13.75633095031508191"
 */
commandResult = await redisClient.geoPos("bigboxcity", [
  "Rome",
  "Hong Kong",
  "Tokyo",
  "WrongMemberValueHere",
  "Bangkok",
]);

console.log(
  'Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: ',
  commandResult
);

/**
 * Using the same member multiple times will return the position multiple times
 *
 * Command: geopos bigboxcity Tokyo Tokyo Tokyo
 * Result:
 *      1)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      2)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      3)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 */
commandResult = await redisClient.geoPos("bigboxcity", [
  "Tokyo",
  "Tokyo",
  "Tokyo",
]);

console.log(
  "Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: ",
  commandResult
);

/**
 * Check geopos of a non existing members
 * (nil) is returned for the non existing members
 *
 * Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
 * Result:
 *      1) (nil)
 *      2) (nil)
 *      3) (nil)
 */
commandResult = await redisClient.geoPos("bigboxcity", [
  "wrongmember1",
  "wrongmember2",
  "wrongmember3",
]);

console.log(
  "Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ",
  commandResult
);

/**
 * Check the command without any member
 * We get an empty array
 *
 * Command: geopos bigboxcity
 * Result: (empty array)
 */
commandResult = await redisClient.geoPos("bigboxcity", []);

console.log("Command: geopos bigboxcity | Result: ", commandResult);

/**
 * Pass a wrong non existing key
 * we get an empty array
 *
 * Command: geopos wrongkey
 * Result: (empty array)
 */
commandResult = await redisClient.geoPos("wrongkey", []);

console.log("Command: geopos wrongkey | Result: ", commandResult);

/**
 * Pass wrong key and wrong members
 * Returns (nil) for all those members
 *
 * Command: geopos wrongkey membera memberb memberc
 * Result:
 *      1) (nil)
 *      2) (nil)
 *      3) (nil)
 */
commandResult = await redisClient.geoPos("wrongkey", [
  "membera",
  "memberb",
  "memberc",
]);

console.log(
  "Command: geopos wrongkey membera memberb memberc | Result: ",
  commandResult
);

/**
 * Set string value
 *
 * Command: set bigboxstr "some string here"
 * Result: OK
 */
commandResult = await redisClient.set("bigboxstr", "soem string here");

console.log(
  'Command: set bigboxstr "some string here" | Result: ' + commandResult
);

/**
 * Try to use GEOPOS with some key that is not a geindex
 * We get an error, for using key of wrong type
 *
 * Command: geopos bigboxstr abc
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
  commandResult = await redisClient.geoPos("bigboxstr", "abc");

  console.log("Command: geopos bigboxstr abc | Result: ", commandResult);
} catch (err) {
  console.log("Command: geopos bigboxstr abc | Error: ", err);
}

process.exit(0);

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result:  5
Command: zrange bigboxcity 0 -1 withscores | Result:  [ 'Rome', 'Paris', 'Bangkok', 'Hong Kong', 'Tokyo' ]
Command: geopos bigboxcity Paris | Result:  [
  {
    longitude: '2.35221952199935913',
    latitude: '48.85661220395509474'
  }
]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result:  [
  {
    longitude: '12.49636620283126831',
    latitude: '41.90277959906867977'
  },
  {
    longitude: '114.10949438810348511',
    latitude: '22.39642736199028406'
  },
  {
    longitude: '139.69171196222305298',
    latitude: '35.68948605865241319'
  },
  {
    longitude: '2.35221952199935913',
    latitude: '48.85661220395509474'
  },
  {
    longitude: '100.50176292657852173',
    latitude: '13.75633095031508191'
  }
]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result:  [
  {
    longitude: '12.49636620283126831',
    latitude: '41.90277959906867977'
  },
  {
    longitude: '114.10949438810348511',
    latitude: '22.39642736199028406'
  },
  {
    longitude: '139.69171196222305298',
    latitude: '35.68948605865241319'
  },
  null,
  {
    longitude: '100.50176292657852173',
    latitude: '13.75633095031508191'
  }
]
Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result:  [
  {
    longitude: '139.69171196222305298',
    latitude: '35.68948605865241319'
  },
  {
    longitude: '139.69171196222305298',
    latitude: '35.68948605865241319'
  },
  {
    longitude: '139.69171196222305298',
    latitude: '35.68948605865241319'
  }
]
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result:  [ null, null, null ]
Command: geopos bigboxcity | Result:  []
Command: geopos wrongkey | Result:  []
Command: geopos wrongkey membera memberb memberc | Result:  [ null, null, null ]
Command: set bigboxstr "some string here" | Result: OK
Command: geopos bigboxstr abc | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]

NOTES

  • Use the function “geoPos” of node-redis.
  • Signature of the method-
    • function geoPos(key: RedisCommandArgument, member: RedisCommandArgument | Array<RedisCommandArgument>)
// Redis GEOPOS command example in Java

import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class GeoPos {

    public static void main(String[] args) {
        // Create connection pool
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Add members to a geo index named bigboxcity
             *
             * Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
             * Result: (integer) 5
             */
            Map<String, GeoCoordinate> locationData = new HashMap<>() {{
                put("Paris", new GeoCoordinate(2.352222, 48.856613));
                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));
            }};
            long getaddResult = jedis.geoadd("bigboxcity", locationData);

            System.out.println("Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 \"Hong Kong\" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " + getaddResult);


            /**
             * Check members saved in bigboxcity
             *
             * Command: zrange bigboxcity 0 -1
             * Result:
             *      1) "Rome"
             *      2) "Paris"
             *      3) "Bangkok"
             *      4) "Hong Kong"
             *      5) "Tokyo"
             */
            List<String> zrangeResult = jedis.zrange("bigboxcity", 0, -1);

            System.out.println("Command: zrange bigboxcity 0 -1 | Result: " + zrangeResult.toString());

            /**
             * Check geopos of a single member
             *
             * 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());

            /**
             * Check geopos of multiple members
             *
             * Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
             * Result:
             *      1)  1) "12.49636620283126831"
             *          2) "41.90278213378983452"
             *      2)  1) "114.10949438810348511"
             *          2) "22.39642736199028406"
             *      3)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      4)  1) "2.35221952199935913"
             *          2) "48.85661220395509474"
             *      5)  1) "100.50176292657852173"
             *          2) "13.75633095031508191"
             */
            geoposResult = jedis.geopos("bigboxcity", "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok");

            System.out.println("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo Paris Bangkok | Result: " + geoposResult.toString());

            /**
             * Check geopos of multiple members
             * But pass one non existing member name
             * We get (nil) for the non existing member
             *
             * Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
             * Result:
             *      1)  1) "12.49636620283126831"
             *          2) "41.90278213378983452"
             *      2)  1) "114.10949438810348511"
             *          2) "22.39642736199028406"
             *      3)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      4) (nil)
             *      5)  1) "100.50176292657852173"
             *          2) "13.75633095031508191"
             */
            geoposResult = jedis.geopos("bigboxcity", "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok");

            System.out.println("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo WrongMemberValueHere Bangkok | Result: " + geoposResult.toString());

            /**
             * Using the same member multiple times will return the position multiple times
             *
             * Command: geopos bigboxcity Tokyo Tokyo Tokyo
             * Result:
             *      1)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      2)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      3)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             */
            geoposResult = jedis.geopos("bigboxcity", "Tokyo", "Tokyo", "Tokyo");

            System.out.println("Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: " + geoposResult.toString());

            /**
             * Check geopos of a non existing members
             * (nil) is returned for the non existing members
             *
             * Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
             * Result:
             *      1) (nil)
             *      2) (nil)
             *      3) (nil)
             */
            geoposResult = jedis.geopos("bigboxcity", "wrongmember1", "wrongmember2", "wrongmember3");

            System.out.println("Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: " + geoposResult.toString());

            /**
             * Check the command without any member
             * We get an empty array
             *
             * Command: geopos bigboxcity
             * Result: (empty array)
             */
            geoposResult = jedis.geopos("bigboxcity");

            System.out.println("Command: geopos bigboxcity | Result: " + geoposResult.toString());

            /**
             * Pass a wrong non existing key
             * we get an empty array
             *
             * Command: geopos wrongkey
             * Result: (empty array)
             */
            geoposResult = jedis.geopos("wrongkey");

            System.out.println("Command: geopos wrongkey | Result: " + geoposResult.toString());

            /**
             * Pass wrong key and wrong members
             * Returns (nil) for all those members
             *
             * Command: geopos wrongkey membera memberb memberc
             * Result:
             *      1) (nil)
             *      2) (nil)
             *      3) (nil)
             */
            geoposResult = jedis.geopos("wrongkey", "membera", "memberb", "memberc");

            System.out.println("Command: geopos wrongkey membera memberb memberc | Result: " + geoposResult.toString());

            /**
             * Set string value
             *
             * Command: set bigboxstr "some string here"
             * Result: OK
             */
            String setResult = jedis.set("bigboxstr", "soem string here");

            System.out.println("Command: set bigboxstr \"some string here\" | Result: " + setResult);

            /**
             * Try to use GEOPOS with some key that is not a geindex
             * We get an error, for using key of wrong type
             *
             * Command: geopos bigboxstr abc
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                geoposResult = jedis.geopos("bigboxstr", "abc");

                System.out.println("Command: geopos bigboxstr abc | Result: " + geoposResult.toString());
            } catch (Exception e) {
                System.out.println("Command: geopos bigboxstr abc | Error: " + e.getMessage());
            }

        }

        jedisPool.close();

    }
}

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 | Result: [Rome, Paris, Bangkok, Hong Kong, Tokyo]
Command: geopos bigboxcity Paris | Result: [(2.352219521999359,48.856612203955095)]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: [(12.496366202831268,41.90277959906868), (114.10949438810349,22.396427361990284), (139.69171196222305,35.68948605865241), (2.352219521999359,48.856612203955095), (100.50176292657852,13.756330950315082)]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: [(12.496366202831268,41.90277959906868), (114.10949438810349,22.396427361990284), (139.69171196222305,35.68948605865241), null, (100.50176292657852,13.756330950315082)]
Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: [(139.69171196222305,35.68948605865241), (139.69171196222305,35.68948605865241), (139.69171196222305,35.68948605865241)]
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [null, null, null]
Command: geopos bigboxcity | Result: []
Command: geopos wrongkey | Result: []
Command: geopos wrongkey membera memberb memberc | Result: [null, null, null]
Command: set bigboxstr "some string here" | Result: OK
Command: geopos bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value

NOTES

  • Use method “geopos” from Jedis package.
  • The signatures of the method are-
    • public List<GeoCoordinate> geopos(final String key, String... members)
// Redis GEOPOS command examples in C#

using StackExchange.Redis;

namespace GeoPos
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase rdb = redis.GetDatabase();

            /**
             * Add city longitude and latitude to geoindex named bigboxcity
             * Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
             * Result: (integer) 5
             */
            GeoEntry[] locationData = new GeoEntry[]{
                new GeoEntry(2.352222, 48.856613, "Paris"),
                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 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 \"Hong Kong\" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: " + getaddResults);

            /**
             * Check the items in bigboxcity
             * Command: zrange bigboxcity 0 -1
             * Result:
             *      1) "Rome"
             *      2) "Paris"
             *      3) "Bangkok"
             *      4) "Hong Kong"
             *      5) "Tokyo"
             */
            RedisValue[] zrangeResult = rdb.SortedSetRangeByScore("bigboxcity", 0, -1);

            Console.WriteLine("Command: zrange bigboxcity 0 -1 withscores | Result: " + String.Join(",", zrangeResult));

            /**
             * Check geopos of a single member
             *
             * Command: geopos bigboxcity Paris
             * Result:
             *      1)  1) "2.35221952199935913"
             *          2) "48.85661220395509474"
             */
            GeoPosition? geoposResult = rdb.GeoPosition("bigboxcity", "Paris");

            Console.WriteLine("Command: geopos bigboxcity Paris | Result: " + geoposResult.ToString());

            /**
             * Check geopos of multiple members
             *
             * Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
             * Result:
             *      1)  1) "12.49636620283126831"
             *          2) "41.90278213378983452"
             *      2)  1) "114.10949438810348511"
             *          2) "22.39642736199028406"
             *      3)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      4)  1) "2.35221952199935913"
             *          2) "48.85661220395509474"
             *      5)  1) "100.50176292657852173"
             *          2) "13.75633095031508191"
             */
            GeoPosition?[] geoposResults = rdb.GeoPosition("bigboxcity", new RedisValue[] { "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok" });

            Console.WriteLine("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo Paris Bangkok | Result: " + String.Join(", ", geoposResults));

            /**
             * Check geopos of multiple members
             * But pass one non existing member name
             * We get (nil) for the non existing member
             *
             * Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
             * Result:
             *      1)  1) "12.49636620283126831"
             *          2) "41.90278213378983452"
             *      2)  1) "114.10949438810348511"
             *          2) "22.39642736199028406"
             *      3)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      4) (nil)
             *      5)  1) "100.50176292657852173"
             *          2) "13.75633095031508191"
             */
            geoposResults = rdb.GeoPosition("bigboxcity", new RedisValue[] { "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok" });

            Console.WriteLine("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo WrongMemberValueHere Bangkok | Result: " + String.Join(", ", geoposResults));

            /**
             * Using the same member multiple times will return the position multiple times
             *
             * Command: geopos bigboxcity Tokyo Tokyo Tokyo
             * Result:
             *      1)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      2)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             *      3)  1) "139.69171196222305298"
             *          2) "35.68948605865241319"
             */
            geoposResults = rdb.GeoPosition("bigboxcity", new RedisValue[] { "Tokyo", "Tokyo", "Tokyo" });

            Console.WriteLine("Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: " + String.Join(", ", geoposResults));

            /**
             * Check geopos of a non existing members
             * (nil) is returned for the non existing members
             *
             * Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
             * Result:
             *      1) (nil)
             *      2) (nil)
             *      3) (nil)
             */
            geoposResults = rdb.GeoPosition("bigboxcity", new RedisValue[] { "wrongmember1", "wrongmember2", "wrongmember3" });

            Console.WriteLine("Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: " + String.Join(", ", geoposResults));

            /**
             * Check the command without any member
             * We get an empty array
             *
             * Command: geopos bigboxcity
             * Result: (empty array)
             */
            geoposResults = rdb.GeoPosition("bigboxcity", new RedisValue[] { });

            Console.WriteLine("Command: geopos bigboxcity | Result: " + String.Join(", ", geoposResults));

            /**
             * Pass a wrong non existing key
             * we get an empty array
             *
             * Command: geopos wrongkey
             * Result: (empty array)
             */
            geoposResults = rdb.GeoPosition("wrongkey", new RedisValue[] { });

            Console.WriteLine("Command: geopos wrongkey | Result: " + String.Join(", ", geoposResults));

            /**
             * Pass wrong key and wrong members
             * Returns (nil) for all those members
             *
             * Command: geopos wrongkey membera memberb memberc
             * Result:
             *      1) (nil)
             *      2) (nil)
             *      3) (nil)
             */
            geoposResults = rdb.GeoPosition("wrongkey", new RedisValue[] { "membera", "memberb", "memberc" });

            Console.WriteLine("Command: geopos wrongkey membera memberb memberc | Result: " + String.Join(", ", geoposResults));

            /**
             * Set string value
             *
             * Command: set bigboxstr "some string here"
             * Result: OK
             */
            bool setResult = rdb.StringSet("bigboxstr", "soem string here");

            Console.WriteLine("Command: set bigboxstr \"some string here\" | Result: " + setResult);

            /**
             * Try to use GEOPOS with some key that is not a geindex
             * We get an error, for using key of wrong type
             *
             * Command: geopos bigboxstr abc
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                geoposResult = rdb.GeoPosition("bigboxstr", "abc");

                Console.WriteLine("Command: geopos bigboxstr abc | Result: " + geoposResult.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: geopos bigboxstr abc | Error: " + e.Message);
            }
        }
    }
}

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result:
Command: geopos bigboxcity Paris | Result: 2.352219521999359 48.856612203955095
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: 12.496366202831268 41.90277959906868, 114.10949438810349 22.396427361990284, 139.69171196222305 35.68948605865241, 2.352219521999359 48.856612203955095, 100.50176292657852 13.756330950315082
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: 12.496366202831268 41.90277959906868, 114.10949438810349 22.396427361990284, 139.69171196222305 35.68948605865241, 0 0, 100.50176292657852 13.756330950315082
Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: 139.69171196222305 35.68948605865241, 139.69171196222305 35.68948605865241, 139.69171196222305 35.68948605865241
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: 0 0, 0 0, 0 0
Command: geopos bigboxcity | Result:
Command: geopos wrongkey | Result:
Command: geopos wrongkey membera memberb memberc | Result: 0 0, 0 0, 0 0
Command: set bigboxstr "some string here" | Result: True
Command: geopos bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value

NOTES

  • Use the method “GeoPos” from StackExchange.Redis.
  • Method signatures-
    • GeoPosition? GeoPosition(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
    • GeoPosition?[] GeoPosition(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
<?php
// Redis GEOPOS command example in PHP

require 'vendor/autoload.php';

// Connect to Redis
$redisClient = new Predis\Client([
    'scheme' => 'tcp',
    'host' => 'localhost',
    'port' => 6379,
]);


/**
 * Add city longitude and latitude to geoindex named bigboxcity
 * Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
 * Result: (integer) 5
 */
$commandResult = $redisClient->geoadd(
    "bigboxcity",
    2.352222,
    48.856613,
    "Paris",
    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 2.352222 48.856613 Paris 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 the items in bigboxcity
 * Command: zrange bigboxcity 0 -1
 * Result:
 *      1) "Rome"
 *      2) "Paris"
 *      3) "Bangkok"
 *      4) "Hong Kong"
 *      5) "Tokyo"
 */
$commandResult = $redisClient->zrange("bigboxcity", 0, -1);

echo "Command: zrange bigboxcity 0 -1 withscores | Result: ";
print_r($commandResult);

/**
 * Check geopos of a single member
 *
 * Command: geopos bigboxcity Paris
 * Result:
 *      1)  1) "2.35221952199935913"
 *          2) "48.85661220395509474"
 */
$commandResult = $redisClient->geopos("bigboxcity", ["Paris"]);

echo "Command: geopos bigboxcity Paris | Result: ";
print_r($commandResult);

/**
 * Check geopos of multiple members
 *
 * Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
 * Result:
 *      1)  1) "12.49636620283126831"
 *          2) "41.90278213378983452"
 *      2)  1) "114.10949438810348511"
 *          2) "22.39642736199028406"
 *      3)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      4)  1) "2.35221952199935913"
 *          2) "48.85661220395509474"
 *      5)  1) "100.50176292657852173"
 *          2) "13.75633095031508191"
 */
$commandResult = $redisClient->geopos("bigboxcity", ["Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok"]);

echo "Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo Paris Bangkok | Result: ";
print_r($commandResult);

/**
 * Check geopos of multiple members
 * But pass one non existing member name
 * We get (nil) for the non existing member
 *
 * Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
 * Result:
 *      1)  1) "12.49636620283126831"
 *          2) "41.90278213378983452"
 *      2)  1) "114.10949438810348511"
 *          2) "22.39642736199028406"
 *      3)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      4) (nil)
 *      5)  1) "100.50176292657852173"
 *          2) "13.75633095031508191"
 */
$commandResult = $redisClient->geopos("bigboxcity", ["Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok"]);

echo "Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo WrongMemberValueHere Bangkok | Result: ";
print_r($commandResult);

/**
 * Using the same member multiple times will return the position multiple times
 *
 * Command: geopos bigboxcity Tokyo Tokyo Tokyo
 * Result:
 *      1)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      2)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 *      3)  1) "139.69171196222305298"
 *          2) "35.68948605865241319"
 */
$commandResult = $redisClient->geopos("bigboxcity", ["Tokyo", "Tokyo", "Tokyo"]);

echo "Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: ";
print_r($commandResult);

/**
 * Check geopos of a non existing members
 * (nil) is returned for the non existing members
 *
 * Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
 * Result:
 *      1) (nil)
 *      2) (nil)
 *      3) (nil)
 */
$commandResult = $redisClient->geopos("bigboxcity", ["wrongmember1", "wrongmember2", "wrongmember3"]);

echo "Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ";
print_r($commandResult);

/**
 * Check the command without any member
 * We get an empty array
 *
 * Command: geopos bigboxcity
 * Result: (empty array)
 */
$commandResult = $redisClient->geopos("bigboxcity", []);

echo "Command: geopos bigboxcity | Result: ";
print_r($commandResult);

/**
 * Pass a wrong non existing key
 * we get an empty array
 *
 * Command: geopos wrongkey
 * Result: (empty array)
 */
$commandResult = $redisClient->geopos("wrongkey", []);

echo "Command: geopos wrongkey | Result: ";
print_r($commandResult);

/**
 * Pass wrong key and wrong members
 * Returns (nil) for all those members
 *
 * Command: geopos wrongkey membera memberb memberc
 * Result:
 *      1) (nil)
 *      2) (nil)
 *      3) (nil)
 */
$commandResult = $redisClient->geopos("wrongkey", ["membera", "memberb", "memberc"]);

echo "Command: geopos wrongkey membera memberb memberc | Result: ";
print_r($commandResult);

/**
 * Set string value
 *
 * Command: set bigboxstr "some string here"
 * Result: OK
 */
$commandResult = $redisClient->set("bigboxstr", "soem string here");

echo "Command: set bigboxstr \"some string here\" | Result: " . $commandResult . "\n";

/**
 * Try to use GEOPOS with some key that is not a geindex
 * We get an error, for using key of wrong type
 *
 * Command: geopos bigboxstr abc
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->geopos("bigboxstr", ["abc"]);

    echo "Command: geopos bigboxstr abc | Result: ";
    print_r($commandResult);
} catch (\Exception $e) {
    echo "Command: geopos bigboxstr abc | Error: " . $e->getMessage() . "\n";
}

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: Array
(
    [0] => Rome
    [1] => Paris
    [2] => Bangkok
    [3] => Hong Kong
    [4] => Tokyo
)
Command: geopos bigboxcity Paris | Result: Array
(
    [0] => Array
        (
            [0] => 2.35221952199935913
            [1] => 48.85661220395509474
        )

)
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: Array
(
    [0] => Array
        (
            [0] => 12.49636620283126831
            [1] => 41.90278213378983452
        )

    [1] => Array
        (
            [0] => 114.10949438810348511
            [1] => 22.39642736199028406
        )

    [2] => Array
        (
            [0] => 139.69171196222305298
            [1] => 35.68948605865241319
        )

    [3] => Array
        (
            [0] => 2.35221952199935913
            [1] => 48.85661220395509474
        )

    [4] => Array
        (
            [0] => 100.50176292657852173
            [1] => 13.75633095031508191
        )

)
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: Array
(
    [0] => Array
        (
            [0] => 12.49636620283126831
            [1] => 41.90278213378983452
        )

    [1] => Array
        (
            [0] => 114.10949438810348511
            [1] => 22.39642736199028406
        )

    [2] => Array
        (
            [0] => 139.69171196222305298
            [1] => 35.68948605865241319
        )

    [3] =>
    [4] => Array
        (
            [0] => 100.50176292657852173
            [1] => 13.75633095031508191
        )

)
Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: Array
(
    [0] => Array
        (
            [0] => 139.69171196222305298
            [1] => 35.68948605865241319
        )

    [1] => Array
        (
            [0] => 139.69171196222305298
            [1] => 35.68948605865241319
        )

    [2] => Array
        (
            [0] => 139.69171196222305298
            [1] => 35.68948605865241319
        )

)
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: Array
(
    [0] =>
    [1] =>
    [2] =>
)
Command: geopos bigboxcity | Result: Array
(
)
Command: geopos wrongkey | Result: Array
(
)
Command: geopos wrongkey membera memberb memberc | Result: Array
(
    [0] =>
    [1] =>
    [2] =>
)
Command: set bigboxstr "some string here" | Result: OK
Command: geopos bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value

NOTES

  • Use the method “geopos” of predis.
  • Signature of the method is-
    • geopos(string $key, array $members): array
# Redis GEOPOS 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 city longitude and latitude to geoindex named bigboxcity
# Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
# Result: (integer) 5
commandResult = redisClient.geoadd(
    "bigboxcity",
    (2.352222, 48.856613, "Paris")
    + (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 2.352222 48.856613 Paris 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 the items in bigboxcity
# Command: zrange bigboxcity 0 -1
# Result:
#      1) "Rome"
#      2) "Paris"
#      3) "Bangkok"
#      4) "Hong Kong"
#      5) "Tokyo"
commandResult = redisClient.zrange("bigboxcity", 0, -1)

print("Command: zrange bigboxcity 0 -1 withscores | Result: {}".format(commandResult))

# Check geopos of a single member
# 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))

# Check geopos of multiple members
# Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
# Result:
#      1)  1) "12.49636620283126831"
#          2) "41.90278213378983452"
#      2)  1) "114.10949438810348511"
#          2) "22.39642736199028406"
#      3)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      4)  1) "2.35221952199935913"
#          2) "48.85661220395509474"
#      5)  1) "100.50176292657852173"
#          2) "13.75633095031508191"
commandResult = redisClient.geopos(
    "bigboxcity", "Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok"
)

print(
    'Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: {}'.format(
        commandResult
    )
)

# Check geopos of multiple members
# But pass one non existing member name
# We get (nil) for the non existing member
# Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
# Result:
#      1)  1) "12.49636620283126831"
#          2) "41.90278213378983452"
#      2)  1) "114.10949438810348511"
#          2) "22.39642736199028406"
#      3)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      4) (nil)
#      5)  1) "100.50176292657852173"
#          2) "13.75633095031508191"
commandResult = redisClient.geopos(
    "bigboxcity", "Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok"
)

print(
    'Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: {}'.format(
        commandResult
    )
)

# Using the same member multiple times will return the position multiple times
# Command: geopos bigboxcity Tokyo Tokyo Tokyo
# Result:
#      1)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      2)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      3)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
commandResult = redisClient.geopos("bigboxcity", "Tokyo", "Tokyo", "Tokyo")

print(
    "Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: {}".format(
        commandResult
    )
)

# Check geopos of a non existing members
# (nil) is returned for the non existing members
# Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
# Result:
#      1) (nil)
#      2) (nil)
#      3) (nil)
commandResult = redisClient.geopos(
    "bigboxcity", "wrongmember1", "wrongmember2", "wrongmember3"
)

print(
    "Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: {}".format(
        commandResult
    )
)

# Check the command without any member
# We get an empty array
#
# Command: geopos bigboxcity
# Result: (empty array)
commandResult = redisClient.geopos("bigboxcity")

print("Command: geopos bigboxcity | Result: {}".format(commandResult))

# Pass a wrong non existing key
# we get an empty array
# Command: geopos wrongkey
# Result: (empty array)
commandResult = redisClient.geopos("wrongkey")

print("Command: geopos wrongkey | Result: {}".format(commandResult))

# Pass wrong key and wrong members
# Returns (nil) for all those members
# Command: geopos wrongkey membera memberb memberc
# Result:
#      1) (nil)
#      2) (nil)
#      3) (nil)
commandResult = redisClient.geopos("wrongkey", "membera", "memberb", "memberc")

print(
    "Command: geopos wrongkey membera memberb memberc | Result: {}".format(
        commandResult
    )
)

# Set string value
# Command: set bigboxstr "some string here"
# Result: OK
commandResult = redisClient.set("bigboxstr", "soem string here")

print('Command: set bigboxstr "some string here" | Result: {}'.format(commandResult))

# Try to use GEOPOS with some key that is not a geindex
# We get an error, for using key of wrong type
# Command: geopos bigboxstr abc
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.geopos("bigboxstr", "abc")

    print("Command: geopos bigboxstr abc | Result: {}".format(commandResult))
except Exception as error:
    print("Command: geopos bigboxstr abc | Error: ", error)

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: ['Rome', 'Paris', 'Bangkok', 'Hong Kong', 'Tokyo']
Command: geopos bigboxcity Paris | Result: [(2.352219521999359, 48.856612203955095)]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: [(12.496366202831268, 41.902782133789835), (114.10949438810349, 22.396427361990284), (139.69171196222305, 35.68948605865241), (2.352219521999359, 48.856612203955095), (100.50176292657852, 13.756330950315082)]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: [(12.496366202831268, 41.902782133789835), (114.10949438810349, 22.396427361990284), (139.69171196222305, 35.68948605865241), None, (100.50176292657852, 13.756330950315082)]  
Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: [(139.69171196222305, 35.68948605865241), (139.69171196222305, 35.68948605865241), (139.69171196222305, 35.68948605865241)]
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [None, None, None]
Command: geopos bigboxcity | Result: []
Command: geopos wrongkey | Result: []
Command: geopos wrongkey membera memberb memberc | Result: [None, None, None]
Command: set bigboxstr "some string here" | Result: True
Command: geopos bigboxstr abc | Error:  WRONGTYPE Operation against a key holding the wrong kind of value

NOTES

  • Use method “geopos” from redis-py.
  • Signature of the method is –
    • def geopos(self, name: KeyT, *values: FieldT) -> ResponseT
# Redis GEOPOS command example in Ruby

require 'redis'

redis = Redis.new(host: "localhost", port: 6379)


# Add city longitude and latitude to geoindex named bigboxcity
# Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome
# Result: (integer) 5
commandResult = redis.geoadd(
    "bigboxcity",
    2.352222, 48.856613, "Paris",
    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 2.352222 48.856613 Paris 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 the items in bigboxcity
# Command: zrange bigboxcity 0 -1
# Result:
#      1) "Rome"
#      2) "Paris"
#      3) "Bangkok"
#      4) "Hong Kong"
#      5) "Tokyo"
commandResult = redis.zrange("bigboxcity", 0, -1)

print("Command: zrange bigboxcity 0 -1 withscores | Result: ", commandResult, "\n")

# Check geopos of a single member
# Command: geopos bigboxcity Paris
# Result:
#      1)  1) "2.35221952199935913"
#          2) "48.85661220395509474"
commandResult = redis.geopos("bigboxcity", "Paris")

print("Command: geopos bigboxcity Paris | Result: ", commandResult, "\n")

# Check geopos of multiple members
# Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok
# Result:
#      1)  1) "12.49636620283126831"
#          2) "41.90278213378983452"
#      2)  1) "114.10949438810348511"
#          2) "22.39642736199028406"
#      3)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      4)  1) "2.35221952199935913"
#          2) "48.85661220395509474"
#      5)  1) "100.50176292657852173"
#          2) "13.75633095031508191"
commandResult = redis.geopos(
    "bigboxcity", ["Rome", "Hong Kong", "Tokyo", "Paris", "Bangkok"]
)

print("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo Paris Bangkok | Result: ", commandResult, "\n")

# Check geopos of multiple members
# But pass one non existing member name
# We get (nil) for the non existing member
# Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok
# Result:
#      1)  1) "12.49636620283126831"
#          2) "41.90278213378983452"
#      2)  1) "114.10949438810348511"
#          2) "22.39642736199028406"
#      3)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      4) (nil)
#      5)  1) "100.50176292657852173"
#          2) "13.75633095031508191"
commandResult = redis.geopos(
    "bigboxcity", ["Rome", "Hong Kong", "Tokyo", "WrongMemberValueHere", "Bangkok"]
)

print("Command: geopos bigboxcity Rome \"Hong Kong\" Tokyo WrongMemberValueHere Bangkok | Result: ", commandResult, "\n")

# Using the same member multiple times will return the position multiple times
# Command: geopos bigboxcity Tokyo Tokyo Tokyo
# Result:
#      1)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      2)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
#      3)  1) "139.69171196222305298"
#          2) "35.68948605865241319"
commandResult = redis.geopos("bigboxcity", ["Tokyo", "Tokyo", "Tokyo"])

print("Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: ", commandResult, "\n")

# Check geopos of a non existing members
# (nil) is returned for the non existing members
# Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3
# Result:
#      1) (nil)
#      2) (nil)
#      3) (nil)
commandResult = redis.geopos("bigboxcity", ["wrongmember1", "wrongmember2", "wrongmember3"])

print("Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: ", commandResult, "\n")

# Check the command without any member
# We get an empty array
#
# Command: geopos bigboxcity
# Result: (empty array)
commandResult = redis.geopos("bigboxcity", [])

print("Command: geopos bigboxcity | Result: ", commandResult, "\n")

# Pass a wrong non existing key
# we get an empty array
# Command: geopos wrongkey
# Result: (empty array)
commandResult = redis.geopos("wrongkey", [])

print("Command: geopos wrongkey | Result: ", commandResult, "\n")

# Pass wrong key and wrong members
# Returns (nil) for all those members
# Command: geopos wrongkey membera memberb memberc
# Result:
#      1) (nil)
#      2) (nil)
#      3) (nil)
commandResult = redis.geopos("wrongkey", ["membera", "memberb", "memberc"])

print("Command: geopos wrongkey membera memberb memberc | Result: ", commandResult, "\n")

# Set string value
# Command: set bigboxstr "some string here"
# Result: OK
commandResult = redis.set("bigboxstr", "soem string here")

print("Command: set bigboxstr \"some string here\" | Result: ", commandResult, "\n")

# Try to use GEOPOS with some key that is not a geindex
# We get an error, for using key of wrong type
# Command: geopos bigboxstr abc
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.geopos("bigboxstr", "abc")

    print("Command: geopos bigboxstr abc | Result: ", commandResult, "\n")
rescue => e
    print("Command: geopos bigboxstr abc | Error: ", e, "\n")
end

Output:

Command: geoadd bigboxcity 2.352222 48.856613 Paris 100.501762 13.756331 Bangkok 114.109497 22.396427 "Hong Kong" 139.691711 35.689487 Tokyo 12.496365 41.902782 Rome | Result: 5
Command: zrange bigboxcity 0 -1 withscores | Result: ["Rome", "Paris", "Bangkok", "Hong Kong", "Tokyo"]
Command: geopos bigboxcity Paris | Result: [["2.35221952199935913", "48.85661220395509474"]]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo Paris Bangkok | Result: [["12.49636620283126831", "41.90278213378983452"], ["114.10949438810348511", "22.39642736199028406"], ["139.69171196222305298", "35.68948605865241319"], ["2.35221952199935913", "48.85661220395509474"], ["100.50176292657852173", "13.75633095031508191"]]
Command: geopos bigboxcity Rome "Hong Kong" Tokyo WrongMemberValueHere Bangkok | Result: [["12.49636620283126831", "41.90278213378983452"], ["114.10949438810348511", "22.39642736199028406"], ["139.69171196222305298", "35.68948605865241319"], nil, ["100.50176292657852173", "13.75633095031508191"]]
Command: geopos bigboxcity geopos bigboxcity Tokyo Tokyo Tokyo | Result: [["139.69171196222305298", "35.68948605865241319"], ["139.69171196222305298", "35.68948605865241319"], ["139.69171196222305298", "35.68948605865241319"]]
Command: geopos bigboxcity wrongmember1 wrongmember2 wrongmember3 | Result: [nil, nil, nil]
Command: geopos bigboxcity | Result: []
Command: geopos wrongkey | Result: []
Command: geopos wrongkey membera memberb memberc | Result: [nil, nil, nil]
Command: set bigboxstr "some string here" | Result: OK
Command: geopos bigboxstr abc | Error: WRONGTYPE Operation against a key holding the wrong kind of value

NOTES

  • Use method “geopos” from the redis-rb.
  • Signature of the method is-
    • # @param [String] key
      # @param [String, Array<String>] member one member or array of members
      # @return [Array<Array<String>, nil>] returns array of elements, where each
      #   element is either array of longitude and latitude or nil

      def geopos(key, member)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
GEOADD Command Details
GEOHASH Command Details
ZRANGE Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.