-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geohash.java
125 lines (103 loc) · 4.47 KB
/
Geohash.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.ArrayList;
import java.util.HashMap;
public class Geohash {
private static int numbits = 5 * 5;
final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
final static HashMap<Character, Integer> lookup = new HashMap<Character, Integer>();
static {
int i = 0;
for (char c : digits)
lookup.put(c, i++);
}
public static void main(String[] args) {
Geohash e = new Geohash();
String s = e.encode(21.33248519897461,-158.02630615234375);
System.out.println(s);
}
public double[] decode(String geohash) {
StringBuilder buffer = new StringBuilder();
for (char c : geohash.toCharArray()) {
int i = lookup.get(c) + 32;
buffer.append( Integer.toString(i, 2).substring(1) );
}
ArrayList<Boolean> lonset = new ArrayList<Boolean>(64);
ArrayList<Boolean> latset = new ArrayList<Boolean>(64);
for (int i = 0; i < 64; i++) {
lonset.add(false);
latset.add(false);
}
//even bits
int j =0;
for (int i=0; i< numbits*2;i+=2) {
boolean isSet = false;
if ( i < buffer.length() )
isSet = buffer.charAt(i) == '1';
lonset.add(j++, isSet);
}
//odd bits
j=0;
for (int i=1; i< numbits*2;i+=2) {
boolean isSet = false;
if ( i < buffer.length() )
isSet = buffer.charAt(i) == '1';
latset.set(j++, isSet);
}
double lon = decode(lonset, -180, 180);
double lat = decode(latset, -90, 90);
return new double[] {lat, lon};
}
private double decode(ArrayList<Boolean> lonset, double floor, double ceiling) {
double mid = 0;
for (int i=0; i<lonset.size(); i++) {
mid = (floor + ceiling) / 2;
if (lonset.get(i))
floor = mid;
else
ceiling = mid;
}
return mid;
}
public String encode(double lat, double lon) {
ArrayList<Boolean> latbits = getBits(lat, -90, 90);
ArrayList<Boolean> lonbits = getBits(lon, -180, 180);
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < numbits; i++) {
buffer.append( (lonbits.get(i))?'1':'0');
buffer.append( (latbits.get(i))?'1':'0');
}
return base32(Long.parseLong(buffer.toString(), 2));
}
private ArrayList<Boolean> getBits(double lat, double floor, double ceiling) {
ArrayList<Boolean> buffer = new ArrayList<Boolean>(64);
for (int i = 0; i < 64; i++) {
buffer.add(i,false);
}
for (int i = 0; i < numbits; i++) {
double mid = (floor + ceiling) / 2;
if (lat >= mid) {
buffer.set(i,true);
floor = mid;
} else {
ceiling = mid;
}
}
return buffer;
}
public static String base32(long i) {
char[] buf = new char[54];
int charPos = 53;
boolean negative = (i < 0);
if (!negative)
i = -i;
while (i <= -32) {
buf[charPos--] = digits[(int) (-(i % 32))];
i /= 32;
}
buf[charPos] = digits[(int) (-i)];
if (negative)
buf[--charPos] = '-';
return new String(buf, charPos, (54 - charPos));
}
}