Class GeoHashUtil

java.lang.Object
org.infinispan.server.resp.commands.geo.GeoHashUtil

public final class GeoHashUtil extends Object
Geohash encoding/decoding utilities for Redis GEO commands.

Redis uses a 52-bit geohash (26 bits for longitude, 26 bits for latitude) stored as a sorted set score (double). This class provides the encoding/decoding algorithms to convert between (longitude, latitude) coordinates and geohash values.

Since:
16.2
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Number of bits used for each coordinate (longitude and latitude).
    static final double
    Maximum valid latitude (Web Mercator limit).
    static final double
    Minimum valid latitude (Web Mercator limit).
    static final double
    Maximum valid longitude.
    static final double
    Minimum valid longitude.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    decode(long geohash)
    Decode a 52-bit geohash to longitude and latitude.
    static long
    encode(double longitude, double latitude)
    Encode longitude and latitude to a 52-bit geohash.
    static double
    geohashToScore(long geohash)
    Convert a geohash (long) to a sorted set score (double).
    static boolean
    isValidLatitude(double latitude)
    Check if the given latitude is valid.
    static boolean
    isValidLongitude(double longitude)
    Check if the given longitude is valid.
    static long
    scoreToGeohash(double score)
    Convert a sorted set score (double) to a geohash (long).
    static String
    toBase32(long geohash)
    Convert a 52-bit geohash to an 11-character base32 string.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • GEO_STEP

      public static final int GEO_STEP
      Number of bits used for each coordinate (longitude and latitude). Redis uses 26 bits each for a total of 52 bits.
      See Also:
    • LON_MIN

      public static final double LON_MIN
      Minimum valid longitude.
      See Also:
    • LON_MAX

      public static final double LON_MAX
      Maximum valid longitude.
      See Also:
    • LAT_MIN

      public static final double LAT_MIN
      Minimum valid latitude (Web Mercator limit).
      See Also:
    • LAT_MAX

      public static final double LAT_MAX
      Maximum valid latitude (Web Mercator limit).
      See Also:
  • Method Details

    • encode

      public static long encode(double longitude, double latitude)
      Encode longitude and latitude to a 52-bit geohash.
      Parameters:
      longitude - longitude in degrees (-180 to 180)
      latitude - latitude in degrees (-85.05112878 to 85.05112878)
      Returns:
      52-bit geohash value
    • decode

      public static double[] decode(long geohash)
      Decode a 52-bit geohash to longitude and latitude.
      Parameters:
      geohash - 52-bit geohash value
      Returns:
      array of [longitude, latitude]
    • scoreToGeohash

      public static long scoreToGeohash(double score)
      Convert a sorted set score (double) to a geohash (long).
      Parameters:
      score - the sorted set score
      Returns:
      the geohash value
    • geohashToScore

      public static double geohashToScore(long geohash)
      Convert a geohash (long) to a sorted set score (double).
      Parameters:
      geohash - the geohash value
      Returns:
      the sorted set score
    • toBase32

      public static String toBase32(long geohash)
      Convert a 52-bit geohash to an 11-character base32 string. This is used by the GEOHASH command.
      Parameters:
      geohash - 52-bit geohash value
      Returns:
      11-character base32 string
    • isValidLongitude

      public static boolean isValidLongitude(double longitude)
      Check if the given longitude is valid.
      Parameters:
      longitude - longitude to check
      Returns:
      true if valid
    • isValidLatitude

      public static boolean isValidLatitude(double latitude)
      Check if the given latitude is valid.
      Parameters:
      latitude - latitude to check
      Returns:
      true if valid