-
Notifications
You must be signed in to change notification settings - Fork 7
/
geohash.php
187 lines (171 loc) · 4.63 KB
/
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
<?php
class GeoHash {
private $hash;
private $latitude;
private $longitude;
private $precision;
/**
* Returns the hash
* @return string
*/
public function getHash() {
if(!$this->hash) {
if(empty($this->latitude)) throw new Exception("Latitude is required");
if(empty($this->longitude)) throw new Exception("Longitude is required");
$this->hash = $this->createHash();
}
return $this->hash;
}
/**
* Set a hash, this will clear any latitude/longitude or precision set
* @return GeoHash
*/
public function setHash($hash) {
$this->hash = $hash;
$this->parseHash();
return $this;
}
/**
* Get the latitude
* @return float
*/
public function getLatitude() {
return $this->latitude;
}
/**
* Set a latitude, this will clear any hash
* @return GeoHash
*/
public function setLatitude($latitude) {
$this->hash = null;
$this->latitude = $latitude;
return $this;
}
/**
* Get the longitude
* @return float
*/
public function getLongitude() {
return $this->longitude;
}
/**
* Set a latitude, this will clear any hash
* @return GeoHash
*/
public function setLongitude($longitude) {
$this->hash = null;
$this->longitude = $longitude;
return $this;
}
/**
* Gets the precision
* @return float
*/
public function getPrecision() {
return $this->precision;
}
/**
* Set a precision, clears any hash
* @return GeoHash
*/
public function setPrecision($precision) {
$this->hash = null;
$this->precision = $precision;
return $this;
}
/**
* Return the hash, obviously, to print out
* @return string
*/
public function __toString() {
return $this->getHash();
}
private function clearCoords() {
$this->latitude = null;
$this->longitude = null;
$this->precision = null;
}
private function createHash() {
$table = "0123456789bcdefghjkmnpqrstuvwxyz";
$lng = $this->longitude;
$lat = $this->latitude;
if(isset($this->precision)) {
$p = $this->precision;
} else {
$lap = strlen($lat)-strpos($lat,".");
$lop = strlen($lng)-strpos($lng,".");
$p = $this->precision = pow(10,-max($lap-1,$lop-1,0))/2;
}
$minlat = -90;
$maxlat = 90;
$minlng = -180;
$maxlng = 180;
$latE = 90;
$lngE = 180;
$i=0;
$hash = "";
$error = 180;
while($error>=$p) {
$chr = 0;
for($b=4;$b>=0;--$b) {
if((1&$b) == (1&$i)) { // even char, even bit OR odd char, odd bit...a lng
$next = ($minlng+$maxlng)/2;
if($lng>$next) {
$chr |= pow(2,$b);
$minlng = $next;
} else {
$maxlng = $next;
}
$lngE /= 2;
} else { // odd char, even bit OR even char, odd bit...a lat
$next = ($minlat+$maxlat)/2;
if($lat>$next) {
$chr |= pow(2,$b);
$minlat = $next;
} else {
$maxlat = $next;
}
$latE /= 2;
}
}
$hash .= $table[$chr];
$i++;
$error = min($latE,$lngE);
}
return $hash;
}
private function parseHash() {
$table = "0123456789bcdefghjkmnpqrstuvwxyz";
$hash = $this->hash;
$this->clearCoords();
$minlat = -90;
$maxlat = 90;
$minlng = -180;
$maxlng = 180;
$latE = 90;
$lngE = 180;
for($i=0,$c=strlen($hash);$i<$c;$i++) {
$v = strpos($table,$hash[$i]);
if(1&$i) {
if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
if(8&$v) $minlng = ($minlng+$maxlng)/2; else $maxlng = ($minlng+$maxlng)/2;
if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
if(2&$v) $minlng = ($minlng+$maxlng)/2; else $maxlng = ($minlng+$maxlng)/2;
if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
$latE /= 8;
$lngE /= 4;
} else {
if(16&$v)$minlng = ($minlng+$maxlng)/2; else $maxlng = ($minlng+$maxlng)/2;
if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
if(4&$v) $minlng = ($minlng+$maxlng)/2; else $maxlng = ($minlng+$maxlng)/2;
if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
if(1&$v) $minlng = ($minlng+$maxlng)/2; else $maxlng = ($minlng+$maxlng)/2;
$latE /= 4;
$lngE /= 8;
}
}
$this->latitude = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1);
$this->longitude = round(($minlng+$maxlng)/2, max(1, -round(log10($lngE)))-1);
$this->precision = max($latE,$lngE);
}
}