GeoHashExtensions { public static double[] decode(final String geohash) { if ((geohash == null) || geohash.isEmpty()) { throw new IllegalArgumentException("Argument geohash should not be null."); } boolean even = true; double latitudeError = 90.0; double longitudeError = 180.0; double latitude; double longitude; final double[] latitudeInterval = { -90.0, 90.0 }; final double[] longitudeInterval = { -180.0, 180.0 }; for (int i = 0; i < geohash.length(); i++) { final int cd = DECODE_MAP.get(geohash.charAt(i)); for (int j = 0; j < bits.length; j++) { final int mask = bits[j]; if (even) { longitudeError /= 2; if ((cd & mask) != 0) { longitudeInterval[0] = (longitudeInterval[0] + longitudeInterval[1]) / 2D; } else { longitudeInterval[1] = (longitudeInterval[0] + longitudeInterval[1]) / 2D; } } else { latitudeError /= 2; if ((cd & mask) != 0) { latitudeInterval[0] = (latitudeInterval[0] + latitudeInterval[1]) / 2D; } else { latitudeInterval[1] = (latitudeInterval[0] + latitudeInterval[1]) / 2D; } } even = !even; } } latitude = (latitudeInterval[0] + latitudeInterval[1]) / 2D; longitude = (longitudeInterval[0] + longitudeInterval[1]) / 2D; return new double[] { latitude, longitude, latitudeError, longitudeError }; }  static double[] decode(final String geohash); static double[] decodeAndRound(final String geohash); static String encode(final double latitude, final double longitude); static String getAdjacent(final String geohash, final String direction); static String getAdjacent(final String geohash, final String... directions); static List<String> getAllAdjacentAreasList(final String geohash); static Map<String, String> getAllAdjacentAreasMap(final String geohash); static FirstAndSecondRingRegion getFirstAndSecondRingRegion(final String geohash); static FirstRingRegion getFirstRingRegion(final String geohash); static double getLatitude(final String geohash); static double getLongitude(final String geohash); static Map<String, String> getTwentyFiveAreasMap(final String geohash);  }
@Test public void testDecode() { double[] actual; double[] expected; String geohash; expected = new double[2]; geohash = GeoHashExtensions.encode(53.5526394, 10.0067103); expected[0] = 53.55263943783939d; expected[1] = 10.006710458546877d; actual = GeoHashExtensions.decode(geohash); assertEquals(expected[0], actual[0]); assertEquals(expected[1], actual[1]); geohash = GeoHashExtensions.encode(30.0, -90.0); expected[0] = 29.999999972060323d; expected[1] = -90.00000016763806d; actual = GeoHashExtensions.decode(geohash); assertEquals(expected[0], actual[0]); assertEquals(expected[1], actual[1]); geohash = GeoHashExtensions.encode(51.4797, -0.0124); expected[0] = 51.47970006801188d; expected[1] = -0.01240001991391182d; actual = GeoHashExtensions.decode(geohash); assertEquals(expected[0], actual[0]); assertEquals(expected[1], actual[1]); }
@Test(expectedExceptions = IllegalArgumentException.class) public final void testDecodeWithEmptyValue() { GeoHashExtensions.decode(""); }
@Test(expectedExceptions = IllegalArgumentException.class) public final void testDecodeWithNullValue() { GeoHashExtensions.decode(null); }