本文实例介绍了JAVA实现将GeoHash转化为对应的经纬度坐标的详细代码,分享给大家供大家参考,具体内容如下
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
package com.lulei.geo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import com.lulei.geo.bean.LocationBean;
import com.lulei.util.JsonUtil;
public class GeoHash {
private LocationBean location;
/**
* 1 2500km;2 630km;3 78km;4 30km
* 5 2.4km; 6 610m; 7 76m; 8 19m
*/
private int hashLength = 8 ; //经纬度转化为geohash长度
private int latLength = 20 ; //纬度转化为二进制长度
private int lngLength = 20 ; //经度转化为二进制长度
private double minLat; //每格纬度的单位大小
private double minLng; //每个经度的单位大小
private static final char [] CHARS = { '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' };
private static HashMap<Character, Integer> CHARSMAP;
static {
CHARSMAP = new HashMap<Character, Integer>();
for ( int i = 0 ; i < CHARS.length; i++) {
CHARSMAP.put(CHARS[i], i);
}
}
public GeoHash( double lat, double lng) {
location = new LocationBean(lat, lng);
setMinLatLng();
}
public int gethashLength() {
return hashLength;
}
/**
* @Author:lulei
* @Description: 设置经纬度的最小单位
*/
private void setMinLatLng() {
minLat = LocationBean.MAXLAT - LocationBean.MINLAT;
for ( int i = 0 ; i < latLength; i++) {
minLat /= 2.0 ;
}
minLng = LocationBean.MAXLNG - LocationBean.MINLNG;
for ( int i = 0 ; i < lngLength; i++) {
minLng /= 2.0 ;
}
}
/**
* @return
* @Author:lulei
* @Description: 求所在坐标点及周围点组成的九个
*/
public List<String> getGeoHashBase32For9() {
double leftLat = location.getLat() - minLat;
double rightLat = location.getLat() + minLat;
double upLng = location.getLng() - minLng;
double downLng = location.getLng() + minLng;
List<String> base32For9 = new ArrayList<String>();
//左侧从上到下 3个
String leftUp = getGeoHashBase32(leftLat, upLng);
if (!(leftUp == null || "" .equals(leftUp))) {
base32For9.add(leftUp);
}
String leftMid = getGeoHashBase32(leftLat, location.getLng());
if (!(leftMid == null || "" .equals(leftMid))) {
base32For9.add(leftMid);
}
String leftDown = getGeoHashBase32(leftLat, downLng);
if (!(leftDown == null || "" .equals(leftDown))) {
base32For9.add(leftDown);
}
//中间从上到下 3个
String midUp = getGeoHashBase32(location.getLat(), upLng);
if (!(midUp == null || "" .equals(midUp))) {
base32For9.add(midUp);
}
String midMid = getGeoHashBase32(location.getLat(), location.getLng());
if (!(midMid == null || "" .equals(midMid))) {
base32For9.add(midMid);
}
String midDown = getGeoHashBase32(location.getLat(), downLng);
if (!(midDown == null || "" .equals(midDown))) {
base32For9.add(midDown);
}
//右侧从上到下 3个
String rightUp = getGeoHashBase32(rightLat, upLng);
if (!(rightUp == null || "" .equals(rightUp))) {
base32For9.add(rightUp);
}
String rightMid = getGeoHashBase32(rightLat, location.getLng());
if (!(rightMid == null || "" .equals(rightMid))) {
base32For9.add(rightMid);
}
String rightDown = getGeoHashBase32(rightLat, downLng);
if (!(rightDown == null || "" .equals(rightDown))) {
base32For9.add(rightDown);
}
return base32For9;
}
/**
* @param length
* @return
* @Author:lulei
* @Description: 设置经纬度转化为geohash长度
*/
public boolean sethashLength( int length) {
if (length < 1 ) {
return false ;
}
hashLength = length;
latLength = (length * 5 ) / 2 ;
if (length % 2 == 0 ) {
lngLength = latLength;
} else {
lngLength = latLength + 1 ;
}
setMinLatLng();
return true ;
}
/**
* @return
* @Author:lulei
* @Description: 获取经纬度的base32字符串
*/
public String getGeoHashBase32() {
return getGeoHashBase32(location.getLat(), location.getLng());
}
/**
* @param lat
* @param lng
* @return
* @Author:lulei
* @Description: 获取经纬度的base32字符串
*/
private String getGeoHashBase32( double lat, double lng) {
boolean [] bools = getGeoBinary(lat, lng);
if (bools == null ) {
return null ;
}
StringBuffer sb = new StringBuffer();
for ( int i = 0 ; i < bools.length; i = i + 5 ) {
boolean [] base32 = new boolean [ 5 ];
for ( int j = 0 ; j < 5 ; j++) {
base32[j] = bools[i + j];
}
char cha = getBase32Char(base32);
if ( ' ' == cha) {
return null ;
}
sb.append(cha);
}
return sb.toString();
}
/**
* @param base32
* @return
* @Author:lulei
* @Description: 将五位二进制转化为base32
*/
private char getBase32Char( boolean [] base32) {
if (base32 == null || base32.length != 5 ) {
return ' ' ;
}
int num = 0 ;
for ( boolean bool : base32) {
num <<= 1 ;
if (bool) {
num += 1 ;
}
}
return CHARS[num % CHARS.length];
}
/**
* @param i
* @return
* @Author:lulei
* @Description: 将数字转化为二进制字符串
*/
private String getBase32BinaryString( int i) {
if (i < 0 || i > 31 ) {
return null ;
}
String str = Integer.toBinaryString(i + 32 );
return str.substring( 1 );
}
/**
* @param geoHash
* @return
* @Author:lulei
* @Description: 将geoHash转化为二进制字符串
*/
private String getGeoHashBinaryString(String geoHash) {
if (geoHash == null || "" .equals(geoHash)) {
return null ;
}
StringBuffer sb = new StringBuffer();
for ( int i = 0 ; i < geoHash.length(); i++) {
char c = geoHash.charAt(i);
if (CHARSMAP.containsKey(c)) {
String cStr = getBase32BinaryString(CHARSMAP.get(c));
if (cStr != null ) {
sb.append(cStr);
}
}
}
return sb.toString();
}
/**
* @param geoHash
* @return
* @Author:lulei
* @Description: 返回geoHash 对应的坐标
*/
public LocationBean getLocation(String geoHash) {
String geoHashBinaryStr = getGeoHashBinaryString(geoHash);
if (geoHashBinaryStr == null ) {
return null ;
}
StringBuffer lat = new StringBuffer();
StringBuffer lng = new StringBuffer();
for ( int i = 0 ; i < geoHashBinaryStr.length(); i++) {
if (i % 2 != 0 ) {
lat.append(geoHashBinaryStr.charAt(i));
} else {
lng.append(geoHashBinaryStr.charAt(i));
}
}
double latValue = getGeoHashMid(lat.toString(), LocationBean.MINLAT, LocationBean.MAXLAT);
double lngValue = getGeoHashMid(lng.toString(), LocationBean.MINLNG, LocationBean.MAXLNG);
LocationBean location = new LocationBean(latValue, lngValue);
location.setGeoHash(geoHash);
return location;
}
/**
* @param binaryStr
* @param min
* @param max
* @return
* @Author:lulei
* @Description: 返回二进制对应的中间值
*/
private double getGeoHashMid(String binaryStr, double min, double max) {
if (binaryStr == null || binaryStr.length() < 1 ) {
return (min + max) / 2.0 ;
}
if (binaryStr.charAt( 0 ) == '1' ) {
return getGeoHashMid(binaryStr.substring( 1 ), (min + max) / 2.0 , max);
} else {
return getGeoHashMid(binaryStr.substring( 1 ), min, (min + max) / 2.0 );
}
}
/**
* @param lat
* @param lng
* @return
* @Author:lulei
* @Description: 获取坐标的geo二进制字符串
*/
private boolean [] getGeoBinary( double lat, double lng) {
boolean [] latArray = getHashArray(lat, LocationBean.MINLAT, LocationBean.MAXLAT, latLength);
boolean [] lngArray = getHashArray(lng, LocationBean.MINLNG, LocationBean.MAXLNG, lngLength);
return merge(latArray, lngArray);
}
/**
* @param latArray
* @param lngArray
* @return
* @Author:lulei
* @Description: 合并经纬度二进制
*/
private boolean [] merge( boolean [] latArray, boolean [] lngArray) {
if (latArray == null || lngArray == null ) {
return null ;
}
boolean [] result = new boolean [lngArray.length + latArray.length];
Arrays.fill(result, false );
for ( int i = 0 ; i < lngArray.length; i++) {
result[ 2 * i] = lngArray[i];
}
for ( int i = 0 ; i < latArray.length; i++) {
result[ 2 * i + 1 ] = latArray[i];
}
return result;
}
/**
* @param value
* @param min
* @param max
* @return
* @Author:lulei
* @Description: 将数字转化为geohash二进制字符串
*/
private boolean [] getHashArray( double value, double min, double max, int length) {
if (value < min || value > max) {
return null ;
}
if (length < 1 ) {
return null ;
}
boolean [] result = new boolean [length];
for ( int i = 0 ; i < length; i++) {
double mid = (min + max) / 2.0 ;
if (value > mid) {
result[i] = true ;
min = mid;
} else {
result[i] = false ;
max = mid;
}
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GeoHash g = new GeoHash( 40.221227 , 116.24875 );
String geoHash = g.getGeoHashBase32();
System.out.println(geoHash);
LocationBean bean = g.getLocation(geoHash);
System.out.println(JsonUtil.parseJson(bean));
System.out.println( new GeoHash(bean.getLat(), bean.getLng()).getGeoHashBase32());
System.out.println(DistanceUtil.getDistance(bean.getLat(), bean.getLng(), bean.getLat() - g.minLat, bean.getLng() - g.minLng));
}
}
|
以上就是本文的详细内容,希望对大家的学习有所帮助。