-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_geohash.php
243 lines (154 loc) · 4.76 KB
/
lib_geohash.php
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
<?php
#
# $Id$
#
# This file has been copied from the Citytracking fork of flamework.
# It has not been forked, or cloned or otherwise jiggery-poked, but
# copied: https://github.com/Citytracking/flamework
#################################################################
# http://en.wikipedia.org/wiki/Geohash
# This is really just a flamework-ified version of:
# http://blog.dixo.net/downloads/geohash-php-class/
#################################################################
$GLOBALS['geohash_alpha'] = "0123456789bcdefghjkmnpqrstuvwxyz";
$GLOBALS['geohash_map'] = array();
# Hey look! Running code !!
for ($i=0; $i<32; $i++){
$k = substr($GLOBALS['geohash_alpha'], $i, 1);
$v = str_pad(decbin($i), 5, "0", STR_PAD_LEFT);
$GLOBALS['geohash_map'][$k] = $v;
}
#################################################################
function geohash_encode($lat, $lon){
$plat = _geohash_precision($lat);
$latbits = 1;
$err = 45;
while($err > $plat){
$latbits++;
$err/=2;
}
$plon = _geohash_precision($lon);
$lonbits = 1;
$err = 90;
while($err > $plon){
$lonbits++;
$err/=2;
}
# bit counts need to be equal
$bits = max($latbits, $lonbits);
# as the hash create bits in groups of 5, lets not
# waste any bits - lets bulk it up to a multiple of 5
# and favour the longitude for any odd bits
$lonbits = $bits;
$latbits = $bits;
$addlon = 1;
while (($lonbits + $latbits) % 5 != 0){
$lonbits += $addlon;
$latbits += !$addlon;
$addlon = !$addlon;
}
# encode each as binary string
$blat = _geohash_binencode($lat, -90, 90, $latbits);
$blon = _geohash_binencode($lon, -180, 180, $lonbits);
# merge lat and long together
$binary = "";
$uselon = 1;
while (strlen($blat) + strlen($blon)){
if ($uselon){
$binary = $binary . substr($blon, 0, 1);
$blon = substr($blon, 1);
}
else {
$binary = $binary . substr($blat, 0, 1);
$blat = substr($blat, 1);
}
$uselon = !$uselon;
}
# convert binary string to hash
$hash="";
for ($i=0; $i<strlen($binary); $i+=5){
$n = bindec(substr($binary,$i,5));
$hash = $hash . $GLOBALS['geohash_alpha'][$n];
}
return $hash;
}
#################################################################
function geohash_decode($hash){
# decode hash into binary string
$binary="";
$hl = strlen($hash);
for($i=0; $i<$hl; $i++){
$binary .= $GLOBALS['geohash_map'][ substr($hash, $i, 1) ];
}
# split the binary into lat and log binary strings
$bl = strlen($binary);
$blat = "";
$blon = "";
for ($i=0; $i<$bl; $i++){
if ($i%2){
$blat = $blat . substr($binary, $i, 1);
}
else {
$blon = $blon . substr($binary, $i, 1);
}
}
# now concert to decimal
$lat = _geohash_bindecode($blat, -90, 90);
$lon = _geohash_bindecode($blon, -180, 180);
# figure out how precise the bit count makes this calculation
$lat_err = _geohash_calc_error(strlen($blat), -90, 90);
$lon_err = _geohash_calc_error(strlen($blon), -180, 180);
# how many decimal places should we use? There's a little art to
# this to ensure I get the same roundings as geohash.org
$lat_places = max(1, -round(log10($lat_err))) - 1;
$lon_places = max(1, -round(log10($lon_err))) - 1;
# round it
$lat = round($lat, $lat_places);
$lon = round($lon, $lon_places);
return array($lat, $lon);
}
#################################################################
function _geohash_precision($number){
$precision = 0;
$pt = strpos($number, '.');
if ($pt !== false){
$precision = -(strlen($number)-$pt-1);
}
return pow(10, $precision) / 2;
}
#################################################################
function _geohash_binencode($number, $min, $max, $bitcount){
if ($bitcount == 0){
return "";
}
# this is our mid point - we will produce a bit to say
# whether $number is above or below this mid point
$mid = ($min + $max) / 2;
if ($number>$mid){
return "1" . _geohash_binencode($number, $mid, $max, $bitcount-1);
}
return "0" . _geohash_binencode($number, $min, $mid, $bitcount-1);
}
#################################################################
function _geohash_bindecode($binary, $min, $max){
$mid = ($min + $max) / 2;
if (strlen($binary) == 0){
return $mid;
}
$bit = substr($binary, 0, 1);
$binary = substr($binary, 1);
if ($bit ==1 ){
return _geohash_bindecode($binary, $mid, $max);
}
return _geohash_bindecode($binary, $min, $mid);
}
#################################################################
function _geohash_calc_error($bits, $min, $max){
$err = ($max-$min) / 2;
while ($bits--){
$err/=2;
}
return $err;
}
#################################################################
?>