2525import java .util .ArrayList ;
2626import java .util .Collection ;
2727
28+ import static org .apache .lucene .geo .GeoUtils .MAX_LAT_INCL ;
29+
2830/**
2931 * Utilities for converting to/from the GeoHash standard
3032 *
@@ -48,6 +50,8 @@ public class GeoHashUtils {
4850 private static final double LAT_SCALE = (0x1L <<BITS )/180.0D ;
4951 private static final double LON_SCALE = (0x1L <<BITS )/360.0D ;
5052 private static final short MORTON_OFFSET = (BITS <<1 ) - (PRECISION *5 );
53+ /** Bit encoded representation of the latitude of north pole */
54+ private static final long MAX_LAT_BITS = (0x1L << (PRECISION * 5 / 2 )) - 1 ;
5155
5256 // No instance:
5357 private GeoHashUtils () {
@@ -218,12 +222,19 @@ public static Rectangle bbox(final String geohash) {
218222 long ghLong = longEncode (geohash , len );
219223 // shift away the level
220224 ghLong >>>= 4 ;
221- // deinterleave and add 1 to lat and lon to get topRight
222- long lat = BitUtil .deinterleave (ghLong >>> 1 ) + 1 ;
223- long lon = BitUtil .deinterleave (ghLong ) + 1 ;
224- GeoPoint topRight = GeoPoint .fromGeohash (BitUtil .interleave ((int )lon , (int )lat ) << 4 | len );
225-
226- return new Rectangle (bottomLeft .lat (), topRight .lat (), bottomLeft .lon (), topRight .lon ());
225+ // deinterleave
226+ long lon = BitUtil .deinterleave (ghLong >>> 1 );
227+ long lat = BitUtil .deinterleave (ghLong );
228+ if (lat < MAX_LAT_BITS ) {
229+ // add 1 to lat and lon to get topRight
230+ GeoPoint topRight = GeoPoint .fromGeohash (BitUtil .interleave ((int )(lat + 1 ), (int )(lon + 1 )) << 4 | len );
231+ return new Rectangle (bottomLeft .lat (), topRight .lat (), bottomLeft .lon (), topRight .lon ());
232+ } else {
233+ // We cannot go north of north pole, so just using 90 degrees instead of calculating it using
234+ // add 1 to lon to get lon of topRight, we are going to use 90 for lat
235+ GeoPoint topRight = GeoPoint .fromGeohash (BitUtil .interleave ((int )lat , (int )(lon + 1 )) << 4 | len );
236+ return new Rectangle (bottomLeft .lat (), MAX_LAT_INCL , bottomLeft .lon (), topRight .lon ());
237+ }
227238 }
228239
229240 /**
0 commit comments