English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

GEO Redis

O Redis GEO é usado principalmente para armazenar informações de localização geográfica e operar essas informações, essa funcionalidade está disponível no Redis 32 Novidade na versão.

Métodos de operação GEO do Redis:

  • geoadd: adiciona as coordenadas geográficas de uma posição.

  • geopos: obtém as coordenadas geográficas de uma posição.

  • geodist: calcula a distância entre duas posições.

  • georadius: obtém um conjunto de posições geográficas dentro de um raio específico com base nas coordenadas de latitude e longitude fornecidas pelo usuário.

  • georadiusbymember: obtém um conjunto de posições geográficas dentro de um raio específico a partir de um local armazenado em um conjunto de posições.

  • geohash: retorna o valor geohash de um ou mais objetos de posição.

geoadd

geoadd é usado para armazenar posições geoespaciais específicas, permitindo adicionar um ou mais longitudes (longitude), latitudes (latitude) e nomes de posições (membro) a uma chave específica.

A sintaxe do geoadd é a seguinte:

GEOADD chave longitude latitude membro [longitude latitude membro ...]

Nos exemplos a seguir, a chave é Sicily, e Palermo e Catania são os nomes das posições:

redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania"
(integer) 2
redis> GEODIST Sicily Palermo Catania
"1662741516"
redis> GEORADIUS Sicily 15 37 100 km
1) "Catania"
redis> GEORADIUS Sicily 15 37 200 km
1) "Palermo"
2) "Catania"
redis>

geopos

geopos é usado para retornar todas as posições (longitude e latitude) especificadas pelo nome (membro) de um chave dada, retornando nil para aqueles que não existem.

A sintaxe do geopos é a seguinte:

GEOPOS chave membro [membro ...]
redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania"
(integer) 2
redis> GEOPOS Sicily Palermo Catania NonExisting
1) 1) "1336138933897018433"
   2) "3811555639549629859"
2) 1) "15.08726745843887329"
   2) "3750266842333162032"
3) (nil)
redis>

geodist

geodist é usado para retornar a distância entre duas posições dadas.

The syntax format of geodist is as follows:

GEODIST key member1 member2 [m|km|ft|mi]

member1 member2 For two geographical locations.

Last distance unit parameter description:

  • m: Meter, default unit.

  • km: Kilometer.

  • mi: Mile.

  • ft: Foot.

  • Calculate the distance between Palermo and Catania:

  • redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania"
    (integer) 2
    redis> GEODIST Sicily Palermo Catania
    "1662741516"
    redis> GEODIST Sicily Palermo Catania km
    "1662742"
    redis> GEODIST Sicily Palermo Catania mi
    "1033182"
    redis> GEODIST Sicily Foo Bar
    (nil)
    redis>

    georadius, georadiusbymember

    georadius returns all position elements contained in the key with the given longitude and latitude as the center, and the distance from the center does not exceed the given maximum distance.

    Both georadiusbymember and GEORADIUS commands can find elements within a specified range, but the center point of georadiusbymember is determined by the given position element, not by longitude and latitude to determine the center point.

    The syntax format of georadius and georadiusbymember is as follows:

    GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
    GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]

    Parameter description:

    • m: Meter, default unit.

    • km: Kilometer.

    • mi: Mile.

    • ft: Foot.

    • WITHDIST: Returns the distance between the position element and the center while returning the position element.

    • WITHCOORD: Also returns the longitude and latitude of the position element.

    • WITHHASH: With 52 The form of a signed integer, returns the ordered set score of the position element after the original geohash encoding. This option is mainly used for low-level applications or debugging, and its actual effect is not significant.

    • COUNT limita o número de registros retornados.

    • ASC: Ordena os resultados de acordo com a distância do mais próximo para o mais distante.

    • DESC: Ordena os resultados de acordo com a distância do mais distante para o mais próximo.

    Exemplo do georadius:

    redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania"
    (integer) 2
    redis> GEORADIUS Sicily 15 37 200 km WITHDIST
    1) 1) "Palermo"
       2) "190.4424"
    2) 1) "Catania"
       2) "564413"
    redis> GEORADIUS Sicily 15 37 200 km WITHCOORD
    1) 1) "Palermo"
       2) 1) "1336138933897018433"
          2) "3811555639549629859"
    2) 1) "Catania"
       2) 1) "15.08726745843887329"
          2) "3750266842333162032"
    redis> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD
    1) 1) "Palermo"
       2) "190.4424"
       3) 1) "1336138933897018433"
          2) "3811555639549629859"
    2) 1) "Catania"
       2) "564413"
       3) 1) "15.08726745843887329"
          2) "3750266842333162032"
    redis>

    Exemplo do georadiusbymember:

    redis> GEOADD Sicily 13583333 37316667 "Agrigento"
    (integer) 1
    redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania"
    (integer) 2
    redis> GEORADIUSBYMEMBER Sicily Agrigento 100 km
    1) "Agrigento"
    2) "Palermo"
    redis>

    geohash

    O Redis GEO usa geohash para salvar as coordenadas geográficas.

    O geohash é usado para obter o valor geohash de um ou mais elementos de localização.

    A sintaxe do geohash é a seguinte:

    GEOHASH key member [member ...]

    Exemplo:

    redis> GEOADD Sicily 13361389 38115556 "Palermo" 15.087269 37502669 "Catania"
    (integer) 2
    redis> GEOHASH Sicily Palermo Catania
    1) "sqc8b49rny0"
    2) "sqdtr74hyu0"
    redis>