GeohashBinningStrategy implements AggregationBinningStrategy<T> { protected static String encodeToGeohash(Geometry geo, int precision) { final String GEOHASH_BASE_32 = "0123456789bcdefghjkmnpqrstuvwxyz"; Point centroid = geo.getCentroid(); double latitude = centroid.getX(); double longitude = centroid.getY(); int idx = 0; int bit = 0; boolean evenBit = true; double latitudeMin = -90; double latitudeMax = 90; double longitudeMin = -180; double longitudeMax = 180; StringBuilder geohash = new StringBuilder(precision); while (geohash.length() < precision) { if (evenBit) { final double currentLongitudeMid = (longitudeMin + longitudeMax) / 2; if (longitude >= currentLongitudeMid) { idx = idx * 2 + 1; longitudeMin = currentLongitudeMid; } else { idx *= 2; longitudeMax = currentLongitudeMid; } } else { final double currentLatitudeMid = (latitudeMin + latitudeMax) / 2; if (latitude >= currentLatitudeMid) { idx = idx * 2 + 1; latitudeMin = currentLatitudeMid; } else { idx *= 2; latitudeMax = currentLatitudeMid; } } evenBit = !evenBit; if (++bit == 5) { geohash.append(GEOHASH_BASE_32.charAt(idx)); idx = 0; bit = 0; } } return geohash.toString(); } GeohashBinningStrategy(int precision); int getPrecision(); @Override String[] binEntry(T entry); @Override byte[] toBinary(); @Override void fromBinary(byte[] bytes);  }
@Test public void testEncodeToGeohash() { Coordinate coord = new Coordinate(49.619, -5.821); Point point = geoFactory.createPoint(coord); String hash = "gbgf78c78u5b"; for (int i = 1; i < hash.length(); i++) { assertThat(hash.substring(0, i), is(GeohashBinningStrategy.encodeToGeohash(point, i))); } }