|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP Geometry GeoHash encoder/decoder. |
|
4
|
|
|
* |
|
5
|
|
|
* @author prinsmc |
|
6
|
|
|
* @see http://en.wikipedia.org/wiki/Geohash |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class GeoHash extends GeoAdapter{ |
|
10
|
|
|
private $table = "0123456789bcdefghjkmnpqrstuvwxyz"; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Convert the geohash to a Point. The point is 2-dimensional. |
|
14
|
|
|
* @return Point the converted geohash |
|
15
|
|
|
* @param string $hash a geohash |
|
16
|
|
|
* @see GeoAdapter::read() |
|
17
|
|
|
*/ |
|
18
|
|
|
public function read($hash, $as_grid = FALSE) { |
|
19
|
|
|
$ll = $this->decode($hash); |
|
20
|
|
|
if (!$as_grid) { |
|
21
|
|
|
return new Point($ll['medlon'], $ll['medlat']); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
else { |
|
24
|
|
|
return new Polygon(array( |
|
|
|
|
|
|
25
|
|
|
new LineString(array( |
|
26
|
|
|
new Point($ll['minlon'], $ll['maxlat']), |
|
27
|
|
|
new Point($ll['maxlon'], $ll['maxlat']), |
|
28
|
|
|
new Point($ll['maxlon'], $ll['minlat']), |
|
29
|
|
|
new Point($ll['minlon'], $ll['minlat']), |
|
30
|
|
|
new Point($ll['minlon'], $ll['maxlat']), |
|
31
|
|
|
)) |
|
32
|
|
|
)); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Convert the geometry to geohash. |
|
38
|
|
|
* @return string the geohash or null when the $geometry is not a Point |
|
39
|
|
|
* @param Point $geometry |
|
40
|
|
|
* @see GeoAdapter::write() |
|
41
|
|
|
*/ |
|
42
|
|
|
public function write(Geometry $geometry, $precision = NULL){ |
|
43
|
|
|
if ($geometry->isEmpty()) return ''; |
|
44
|
|
|
|
|
45
|
|
|
if($geometry->geometryType() === 'Point'){ |
|
46
|
|
|
return $this->encodePoint($geometry, $precision); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
else { |
|
49
|
|
|
// The geohash is the hash grid ID that fits the envelope |
|
50
|
|
|
$envelope = $geometry->envelope(); |
|
51
|
|
|
$geohashes = array(); |
|
52
|
|
|
$geohash = ''; |
|
53
|
|
|
foreach ($envelope->getPoints() as $point) { |
|
54
|
|
|
$geohashes[] = $this->encodePoint($point, 0.0000001); |
|
55
|
|
|
} |
|
56
|
|
|
$i = 0; |
|
57
|
|
|
while ($i < strlen($geohashes[0])) { |
|
58
|
|
|
$char = $geohashes[0][$i]; |
|
59
|
|
|
foreach ($geohashes as $hash) { |
|
60
|
|
|
if ($hash[$i] != $char) { |
|
61
|
|
|
return $geohash; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
$geohash .= $char; |
|
65
|
|
|
$i++; |
|
66
|
|
|
} |
|
67
|
|
|
return $geohash; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string geohash |
|
73
|
|
|
* @param Point $point |
|
74
|
|
|
* @author algorithm based on code by Alexander Songe <[email protected]> |
|
75
|
|
|
* @see https://github.com/asonge/php-geohash/issues/1 |
|
76
|
|
|
*/ |
|
77
|
|
|
private function encodePoint($point, $precision = NULL){ |
|
78
|
|
|
if ($precision === NULL) { |
|
79
|
|
|
$lap = strlen($point->y())-strpos($point->y(),"."); |
|
80
|
|
|
$lop = strlen($point->x())-strpos($point->x(),"."); |
|
81
|
|
|
$precision = pow(10,-max($lap-1,$lop-1,0))/2; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$minlat = -90; |
|
85
|
|
|
$maxlat = 90; |
|
86
|
|
|
$minlon = -180; |
|
87
|
|
|
$maxlon = 180; |
|
88
|
|
|
$latE = 90; |
|
89
|
|
|
$lonE = 180; |
|
90
|
|
|
$i = 0; |
|
91
|
|
|
$error = 180; |
|
92
|
|
|
$hash=''; |
|
93
|
|
|
while($error>=$precision) { |
|
94
|
|
|
$chr = 0; |
|
95
|
|
|
for($b=4;$b>=0;--$b) { |
|
96
|
|
|
if((1&$b) == (1&$i)) { |
|
97
|
|
|
// even char, even bit OR odd char, odd bit...a lon |
|
98
|
|
|
$next = ($minlon+$maxlon)/2; |
|
99
|
|
|
if($point->x()>$next) { |
|
100
|
|
|
$chr |= pow(2,$b); |
|
101
|
|
|
$minlon = $next; |
|
102
|
|
|
} else { |
|
103
|
|
|
$maxlon = $next; |
|
104
|
|
|
} |
|
105
|
|
|
$lonE /= 2; |
|
106
|
|
|
} else { |
|
107
|
|
|
// odd char, even bit OR even char, odd bit...a lat |
|
108
|
|
|
$next = ($minlat+$maxlat)/2; |
|
109
|
|
|
if($point->y()>$next) { |
|
110
|
|
|
$chr |= pow(2,$b); |
|
111
|
|
|
$minlat = $next; |
|
112
|
|
|
} else { |
|
113
|
|
|
$maxlat = $next; |
|
114
|
|
|
} |
|
115
|
|
|
$latE /= 2; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
$hash .= $this->table[$chr]; |
|
119
|
|
|
$i++; |
|
120
|
|
|
$error = min($latE,$lonE); |
|
121
|
|
|
} |
|
122
|
|
|
return $hash; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $hash a geohash |
|
127
|
|
|
* @author algorithm based on code by Alexander Songe <[email protected]> |
|
128
|
|
|
* @see https://github.com/asonge/php-geohash/issues/1 |
|
129
|
|
|
*/ |
|
130
|
|
|
private function decode($hash){ |
|
131
|
|
|
$ll = array(); |
|
132
|
|
|
$minlat = -90; |
|
133
|
|
|
$maxlat = 90; |
|
134
|
|
|
$minlon = -180; |
|
135
|
|
|
$maxlon = 180; |
|
136
|
|
|
$latE = 90; |
|
137
|
|
|
$lonE = 180; |
|
138
|
|
|
for($i=0,$c=strlen($hash);$i<$c;$i++) { |
|
139
|
|
|
$v = strpos($this->table,$hash[$i]); |
|
140
|
|
|
if(1&$i) { |
|
141
|
|
|
if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
142
|
|
|
if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
143
|
|
|
if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
144
|
|
|
if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
145
|
|
|
if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
146
|
|
|
$latE /= 8; |
|
147
|
|
|
$lonE /= 4; |
|
148
|
|
|
} else { |
|
149
|
|
|
if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
150
|
|
|
if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
151
|
|
|
if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
152
|
|
|
if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2; |
|
153
|
|
|
if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2; |
|
154
|
|
|
$latE /= 4; |
|
155
|
|
|
$lonE /= 8; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
$ll['minlat'] = $minlat; |
|
159
|
|
|
$ll['minlon'] = $minlon; |
|
160
|
|
|
$ll['maxlat'] = $maxlat; |
|
161
|
|
|
$ll['maxlon'] = $maxlon; |
|
162
|
|
|
$ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1); |
|
163
|
|
|
$ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1); |
|
164
|
|
|
return $ll; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: