-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGeoHash.php
223 lines (194 loc) · 6.47 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
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
<?php
class GeoHash
{
const LEVEL_MAX = 26; // 26位已能达到 0.6m 的精度
const MERCATOR_LENGTH = 40075452.74; // 墨卡托投影下地球的赤道周长
private static $LNG_RANGE = ['min' => -180, 'max' => 180];
private static $LAT_RANGE = ['min' => -90, 'max' => 90];
public function geoRadius()
{
/**
* 数据准备:将地理点通过 getHashInt($lng, $lat) 获取其 hashInt 值,存入redis 的 sorted set
* 查询:
* 1. 通过 getIntLimit($center_lng, $center_lat, $radius); 获取到九个方格的 hashInt 值 和 其 range
* 2. 用 ZRANGEBYSCORE key hashInt hashInt+range 获取九个方格内的地址点
* 3. 遍历各个点(九个方格点范围略大),计算距离,返回严格符合要求的点
*/
}
/**
* 获取某点geohash对应的Int值
*
* @param $lng
* @param $lat
*
* @return number
*/
public function getHashInt($lng, $lat)
{
$bits_lng = $this->getBits($lng, self::$LNG_RANGE);
$bits_lat = $this->getBits($lat, self::$LAT_RANGE);
$bits = $this->assembleBits($bits_lng, $bits_lat);
return $this->getFormalInt($bits);
}
/**
* 测试某一目的点是否在中心点N米范围内
*
* @param $center_lng
* @param $center_lat
* @param $radius
* @param $aim_lng
* @param $aim_lat
*/
public function testInRadius($center_lng, $center_lat, $radius, $aim_lng, $aim_lat)
{
$limit = $this->getIntLimit($center_lng, $center_lat, $radius);
$int_aim = $this->getHashInt($aim_lng, $aim_lat);
$res = 'not_in_radius';
foreach ($limit['cells'] as $int_cell) {
if ($int_aim > $int_cell && $int_aim < $int_cell + $limit['range']) {
$res = 'in_radius';
break;
}
}
echo $res . PHP_EOL;
}
/**
* 通过二进制哈希串获取到规范化为52位的正整数
*
* @param $bits
*
* @return number
*/
private function getFormalInt($bits)
{
$bits_padded = str_pad($bits, self::LEVEL_MAX * 2, '0');
return bindec($bits_padded);
}
/**
* 获取int值的限制
*
* @param $lng
* @param $lat
* @param $radius
*
* @return array
*/
private function getIntLimit($lng, $lat, $radius)
{
$level = $this->getLevel($radius);
$bits_lng = $this->getBits($lng, self::$LNG_RANGE, $level);
$bits_lat = $this->getBits($lat, self::$LAT_RANGE, $level);
$cells = $this->getRoundCells($bits_lng, $bits_lat);
$cells['mid'] = $this->getFormalInt($this->assembleBits($bits_lng, $bits_lat));
$range = $this->getLevelRange($level);
$limit = [
'cells' => $cells,
'range' => $range,
];
return $limit;
}
/**
* 通过经度/纬度 和其范围值获取到其二进制哈希串
*
* @param $loc
* @param $range
* @param int $level
*
* @return string
*/
private function getBits($loc, $range, $level = self::LEVEL_MAX)
{
$bits = '';
for ($i = 0; $i < $level; $i++) {
$mid = ($range['min'] + $range['max']) / 2;
if ($loc < $mid) {
$bits .= '0';
$range = ['min' => $range['min'], 'max' => $mid];
} else {
$bits .= '1';
$range = ['min' => $mid, 'max' => $range['max']];
}
}
return $bits;
}
/**
* 组合经度和纬度的二进制串
*
* @param $lng
* @param $lat
*
* @return string
*/
private function assembleBits($lng, $lat)
{
$bits_assembled = '';
$arr_lng = str_split($lng);
$arr_lat = str_split($lat);
for ($i = 0, $c = count($arr_lng); $i < $c; $i++) {
$bits_assembled .= $arr_lat[$i] . $arr_lng[$i];
}
return $bits_assembled;
}
/**
* 获取中心格子四周的八个格子
*
* @param $bits_lng
* @param $bits_lat
*
* @return mixed
*/
private function getRoundCells($bits_lng, $bits_lat)
{
$lng_incr = decbin(bindec($bits_lng) + 1);
$lng_decr = decbin(bindec($bits_lng) - 1);
$lat_incr = decbin(bindec($bits_lat) + 1);
$lat_decr = decbin(bindec($bits_lat) - 1);
$cells['mid'] = $this->getFormalInt($this->assembleBits($bits_lng, $bits_lat));
$cells['up'] = $this->getFormalInt($this->assembleBits($bits_lng, $lat_incr));
$cells['down'] = $this->getFormalInt($this->assembleBits($bits_lng, $lat_decr));
$cells['left'] = $this->getFormalInt($this->assembleBits($lng_decr, $bits_lat));
$cells['right'] = $this->getFormalInt($this->assembleBits($lng_incr, $bits_lat));
$cells['left_up'] = $this->getFormalInt($this->assembleBits($lng_decr, $lat_incr));
$cells['right_up'] = $this->getFormalInt($this->assembleBits($lng_incr, $lat_incr));
$cells['left_down'] = $this->getFormalInt($this->assembleBits($lng_incr, $lat_decr));
$cells['right_down'] = $this->getFormalInt($this->assembleBits($lng_incr, $lat_decr));
return $cells;
}
/**
* 通过范围值获取geohash层级
*
* @param $range_meter
*
* @return int
*/
private function getLevel($range_meter)
{
$level = 0;
$global = self::MERCATOR_LENGTH;
while ($global > $range_meter) {
$global /= 2;
$level++;
}
return $level;
}
/**
* 通过哈希层级获取每一格子的范围
*
* @param $level
*
* @return number
*/
private function getLevelRange($level)
{
// 最小的格子边上是 0.6m,级别值越大格子的边长就越大;
$range = pow(2, 2 * (self::LEVEL_MAX - $level)) * 0.6;
return $range;
}
}
$geohash = new GeoHash();
// 中心地址:新浪总部大厦
$geohash->testInRadius(116.276231, 40.041143, 3000, 116.276301, 40.041532); // 新浪餐厅 in
$geohash->testInRadius(116.276317, 40.04168, 3000, 116.27236, 40.04214); // 百度科技园 in
$geohash->testInRadius(116.276317, 40.04168, 3000, 116.274826521, 40.0321647826); // 兰园小区 in
$geohash->testInRadius(116.276317, 40.04168, 3000, 116.298505, 40.023749); // 上地医院 in
$geohash->testInRadius(116.276317, 40.04168, 3000, 116.31763, 40.01522); // 圆明园 not in