2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.spatial.geohash;
20 import java.util.HashMap;
24 * Utilities for encoding and decoding geohashes. Based on
25 * <a href="http://en.wikipedia.org/wiki/Geohash">http://en.wikipedia.org/wiki/Geohash</a>.
27 public class GeoHashUtils {
29 private static final char[] BASE_32 = {'0', '1', '2', '3', '4', '5', '6',
30 '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n',
31 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
33 private final static Map<Character,Integer> DECODE_MAP = new HashMap<Character,Integer>();
35 private static final int PRECISION = 12;
36 private static final int[] BITS = {16, 8, 4, 2, 1};
39 for (int i = 0; i < BASE_32.length; i++) {
40 DECODE_MAP.put(Character.valueOf(BASE_32[i]), Integer.valueOf(i));
44 private GeoHashUtils() {
48 * Encodes the given latitude and longitude into a geohash
50 * @param latitude Latitude to encode
51 * @param longitude Longitude to encode
52 * @return Geohash encoding of the longitude and latitude
54 public static String encode(double latitude, double longitude) {
55 double[] latInterval = {-90.0, 90.0};
56 double[] lngInterval = {-180.0, 180.0};
58 final StringBuilder geohash = new StringBuilder();
59 boolean isEven = true;
64 while (geohash.length() < PRECISION) {
67 mid = (lngInterval[0] + lngInterval[1]) / 2D;
68 if (longitude > mid) {
75 mid = (latInterval[0] + latInterval[1]) / 2D;
89 geohash.append(BASE_32[ch]);
95 return geohash.toString();
99 * Decodes the given geohash into a latitude and longitude
101 * @param geohash Geohash to deocde
102 * @return Array with the latitude at index 0, and longitude at index 1
104 public static double[] decode(String geohash) {
105 final double[] latInterval = {-90.0, 90.0};
106 final double[] lngInterval = {-180.0, 180.0};
108 boolean isEven = true;
112 for (int i = 0; i < geohash.length(); i++) {
113 final int cd = DECODE_MAP.get(Character.valueOf(
114 geohash.charAt(i))).intValue();
116 for (int mask : BITS) {
118 if ((cd & mask) != 0) {
119 lngInterval[0] = (lngInterval[0] + lngInterval[1]) / 2D;
121 lngInterval[1] = (lngInterval[0] + lngInterval[1]) / 2D;
124 if ((cd & mask) != 0) {
125 latInterval[0] = (latInterval[0] + latInterval[1]) / 2D;
127 latInterval[1] = (latInterval[0] + latInterval[1]) / 2D;
134 latitude = (latInterval[0] + latInterval[1]) / 2D;
135 longitude = (lngInterval[0] + lngInterval[1]) / 2D;
137 return new double[] {latitude, longitude};