Class GeohashUtils


  • public class GeohashUtils
    extends java.lang.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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static char[] BASE_32  
      private static int[] BASE_32_IDX  
      private static int[] BITS  
      private static double[] hashLenToLatHeight
      See the table at http://en.wikipedia.org/wiki/Geohash
      private static double[] hashLenToLonWidth
      See the table at http://en.wikipedia.org/wiki/Geohash
      static int MAX_PRECISION  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GeohashUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Point decode​(java.lang.String geohash, SpatialContext ctx)
      Decodes the given geohash into a longitude (X) and latitude (Y)
      static Rectangle decodeBoundary​(java.lang.String geohash, SpatialContext ctx)
      Returns min-max lon (X), min-max lat (Y).
      static java.lang.String encodeLatLon​(double latitude, double longitude)
      Encodes the given latitude and longitude into a geohash
      static java.lang.String encodeLatLon​(double latitude, double longitude, int precision)  
      static java.lang.String[] getSubGeohashes​(java.lang.String baseGeohash)
      Array of geohashes 1 level below the baseGeohash.
      static double[] lookupDegreesSizeForHashLen​(int hashLen)  
      static int lookupHashLenForWidthHeight​(double lonErr, double latErr)
      Return the shortest geohash length that will have a width & height >= specified arguments.
      • Methods inherited from class java.lang.Object

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

      • BASE_32

        private static final char[] BASE_32
      • BASE_32_IDX

        private static final int[] BASE_32_IDX
      • BITS

        private static final int[] BITS
      • hashLenToLatHeight

        private static final double[] hashLenToLatHeight
        See the table at http://en.wikipedia.org/wiki/Geohash
      • hashLenToLonWidth

        private static final double[] hashLenToLonWidth
        See the table at http://en.wikipedia.org/wiki/Geohash
    • Constructor Detail

      • GeohashUtils

        private GeohashUtils()
    • Method Detail

      • encodeLatLon

        public static java.lang.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 java.lang.String encodeLatLon​(double latitude,
                                                    double longitude,
                                                    int precision)
      • decode

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

        public static Rectangle decodeBoundary​(java.lang.String geohash,
                                               SpatialContext ctx)
        Returns min-max lon (X), min-max lat (Y).
      • getSubGeohashes

        public static java.lang.String[] getSubGeohashes​(java.lang.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.