Class GeohashUtils


  • public class GeohashUtils
    extends Object
    Utilities for encoding and decoding geohashes.

    This class isn't used by any other part of Spatial4j; it's included largely for convenience of software using Spatial4j. There are other open-source libraries that have more comprehensive geohash utilities but providing this one avoids an additional dependency for what's a small amount of code. If you're using Spatial4j just for this class, consider alternatives.

    This code originally came from Apache Lucene, LUCENE-1512.

    • Method Detail

      • encodeLatLon

        public static String encodeLatLon​(double latitude,
                                          double longitude)
        Encodes the given latitude and longitude into a geohash
        Parameters:
        latitude - Latitude to encode
        longitude - Longitude to encode
        Returns:
        Geohash encoding of the longitude and latitude
      • encodeLatLon

        public static String encodeLatLon​(double latitude,
                                          double longitude,
                                          int precision)
      • decode

        public static Point decode​(String geohash,
                                   SpatialContext ctx)
        Decodes the given geohash into a longitude (X) and latitude (Y)
      • getSubGeohashes

        public static String[] getSubGeohashes​(String baseGeohash)
        Array of geohashes 1 level below the baseGeohash. Sorted.
      • lookupDegreesSizeForHashLen

        public static double[] lookupDegreesSizeForHashLen​(int hashLen)
      • lookupHashLenForWidthHeight

        public static int lookupHashLenForWidthHeight​(double lonErr,
                                                      double latErr)
        Return the shortest geohash length that will have a width & height >= specified arguments.