Skip to content

Commit 04d0edc

Browse files
authored
Fix incorrect geohash for lat 90, lon 180 (#29256)
Due to special treatment for the 0xFFFFFF... value in GeoHashUtils' encodeLatLon method, the hashcode for lat 90, lon 180 is incorrectly encoded as `"000000000000"` instead of "zzzzzzzzzzzz". This commit removes the special treatment and fixes the issue. Closes #22163
1 parent b6568d0 commit 04d0edc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

server/src/main/java/org/elasticsearch/common/geo/GeoHashUtils.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ private GeoHashUtils() {
5757
* 31 bit encoding utils *
5858
*************************/
5959
public static long encodeLatLon(final double lat, final double lon) {
60-
long result = MortonEncoder.encode(lat, lon);
61-
if (result == 0xFFFFFFFFFFFFFFFFL) {
62-
return result & 0xC000000000000000L;
63-
}
64-
return result >>> 2;
60+
return MortonEncoder.encode(lat, lon) >>> 2;
6561
}
6662

6763
/**

server/src/test/java/org/elasticsearch/common/geo/GeoHashTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Tests for {@link org.elasticsearch.common.geo.GeoHashUtils}
2626
*/
2727
public class GeoHashTests extends ESTestCase {
28-
public void testGeohashAsLongRoutines() {
28+
public void testGeohashAsLongRoutines() {
2929
final GeoPoint expected = new GeoPoint();
3030
final GeoPoint actual = new GeoPoint();
3131
//Ensure that for all points at all supported levels of precision
@@ -70,4 +70,16 @@ public void testBboxFromHash() {
7070
assertEquals(expectedLatDiff, bbox.maxLat - bbox.minLat, 0.00001);
7171
assertEquals(hash, GeoHashUtils.stringEncode(bbox.minLon, bbox.minLat, level));
7272
}
73+
74+
public void testGeohashExtremes() {
75+
assertEquals("000000000000", GeoHashUtils.stringEncode(-180, -90));
76+
assertEquals("800000000000", GeoHashUtils.stringEncode(-180, 0));
77+
assertEquals("bpbpbpbpbpbp", GeoHashUtils.stringEncode(-180, 90));
78+
assertEquals("h00000000000", GeoHashUtils.stringEncode(0, -90));
79+
assertEquals("s00000000000", GeoHashUtils.stringEncode(0, 0));
80+
assertEquals("upbpbpbpbpbp", GeoHashUtils.stringEncode(0, 90));
81+
assertEquals("pbpbpbpbpbpb", GeoHashUtils.stringEncode(180, -90));
82+
assertEquals("xbpbpbpbpbpb", GeoHashUtils.stringEncode(180, 0));
83+
assertEquals("zzzzzzzzzzzz", GeoHashUtils.stringEncode(180, 90));
84+
}
7385
}

0 commit comments

Comments
 (0)