forked from tinuzz/wp-plugin-trackserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolyline.php
278 lines (261 loc) · 8.3 KB
/
Polyline.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
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
<?php
/**
* Polyline
*
* A simple class to handle polyline-encoding for Google Maps
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Polyline
* @version @VERSION@
* @copyright @DATE@ emcconville
* @license GNU Lesser General Public License <http://www.gnu.org/licenses/lgpl.html>
* @link https://github.com/emcconville/google-map-polyline-encoding-tool
* @author E. McConville <[email protected]>
*/
//@NAMESPACE@ namespace emcconville {
class Polyline
{
/**
* @var array $polylines
* @deprecated
* @ignore
*/
private $polylines = array();
/**
* Default precision level of 1e-5.
*
* Overwrite this property in extended class to adjust precision of numbers.
* !!!CAUTION!!!
* 1) Adjusting this value will not guarantee that third party
* libraries will understand the change.
* 2) Float point arithmetic IS NOT real number arithmetic. PHP's internal
* float precision may contribute to undesired rounding.
*
* @var int $precision
*/
protected static $precision = 5;
/**
* @var Polyline $instance
* @deprecated
* @ignore
*/
private static $instance;
public function __construct()
{
// Overloading bug #11
}
/**
* Static instance method
*
* @return Polyline
* @deprecated
* @codeCoverageIgnore
* @ignore
*/
public static function Singleton()
{
trigger_error('Polyline::Singleton deprecated.', E_USER_DEPRECATED);
return self::$instance instanceof self ? self::$instance : self::$instance = new self;
}
/**
* Magic method for supporting wildcard getters
*
* @let {Node} be the name of the polyline
* @method get{Node}Points( ) //=> array of points for polyline "Node"
* @method get{Node}Encoded( ) //=> encoded string for polyline "Node"
* @method getPoints( "{Node}") //=> array of points for polyline "Node"
* @method getEncoded("{Node}") //=> encoded string for polyline "Node"
* @deprecated
* @codeCoverageIgnore
* @ignore
*/
public function __call($method,$arguments)
{
trigger_error('Polyline::__call('.$method.') deprecated.', E_USER_DEPRECATED);
$return = null;
if (preg_match('/^get(.+?)(points|encoded)$/i', $method, $matches)) {
list($all,$node,$type) = $matches;
return $this->getPolyline(strtolower($node), strtolower($type));
} elseif (preg_match('/^get(points|encoded)$/i', $method, $matches)) {
list($all,$type) = $matches;
$node = array_shift($arguments);
return $this->getPolyline(strtolower($node), strtolower($type));
} else {
throw new BadMethodCallException();
}
return $return;
}
/**
* Polyline getter
* @param string $node
* @param string $type
* @return mixed
* @deprecated
* @codeCoverageIgnore
* @ignore
*/
public function getPolyline($node, $type)
{
trigger_error('Polyline::getPolyline deprecated.', E_USER_DEPRECATED);
$node = strtolower($node);
$type = in_array($type, array('points','encoded')) ? $type : 'encoded';
return isset($this->polylines[$node])
? $this->polylines[$node][$type]
: ($type =='points' ? array() : null);
}
/**
* General purpose data method
*
* @param string polyline name
* @param mixed [ string | array ] optional
* @return array
* @deprecated
* @codeCoverageIgnore
* @ignore
*/
public function polyline()
{
trigger_error('Polyline::polyline deprecated.', E_USER_DEPRECATED);
$arguments = func_get_args();
$return = null;
switch (count($arguments)) {
case 2:
list($node,$value) = $arguments;
$isArray = is_array($value);
$return = $this->polylines[strtolower($node)] = array(
'points' => $isArray ? self::Flatten($value) : self::Decode($value),
'encoded' => $isArray ? self::Encode($value) : $value
);
$return = $return[$isArray ? 'encoded' : 'points' ];
break;
case 1:
$node = strtolower((string)array_shift($arguments));
$return = isset($this->polylines[$node])
? $this->polylines[$node]
: array( 'points' => null, 'encoded' => null );
break;
}
return $return;
}
/**
* Retrieve list of polyline within singleton
*
* @return array polylines
* @deprecated
* @codeCoverageIgnore
* @ignore
*/
public function listPolylines()
{
trigger_error('Polyline::listPolylines deprecated.', E_USER_DEPRECATED);
return $return = array_keys($this->polylines);
}
/**
* Apply Google Polyline algorithm to list of points
*
* @param array $points
* @param integer $precision optional
* @return string encoded string
*/
final public static function Encode($points)
{
$points = self::Flatten($points);
$encodedString = '';
$index = 0;
$previous = array(0,0);
foreach ($points as $number) {
$number = (float)($number);
$number = (int)round($number * pow(10, static::$precision));
$diff = $number - $previous[$index % 2];
$previous[$index % 2] = $number;
$number = $diff;
$index++;
$number = ($number < 0) ? ~($number << 1) : ($number << 1);
$chunk = '';
while ($number >= 0x20) {
$chunk .= chr((0x20 | ($number & 0x1f)) + 63);
$number >>= 5;
}
$chunk .= chr($number + 63);
$encodedString .= $chunk;
}
return $encodedString;
}
/**
* Reverse Google Polyline algorithm on encoded string
*
* @param string $string
* @param integer $precision optional
* @return array points
*/
final public static function Decode($string)
{
$points = array();
$index = $i = 0;
$previous = array(0,0);
while ($i < strlen($string)) {
$shift = $result = 0x00;
do {
$bit = ord(substr($string, $i++)) - 63;
$result |= ($bit & 0x1f) << $shift;
$shift += 5;
} while ($bit >= 0x20);
$diff = ($result & 1) ? ~($result >> 1) : ($result >> 1);
$number = $previous[$index % 2] + $diff;
$previous[$index % 2] = $number;
$index++;
$points[] = $number * 1 / pow(10, static::$precision);
}
return $points;
}
/**
* Reduce multi-dimensional to single list
*
* @param array $array
* @return array flattened
*/
final public static function Flatten($array)
{
$flatten = array();
array_walk_recursive(
$array, // @codeCoverageIgnore
function ($current) use (&$flatten) {
$flatten[] = $current;
}
);
return $flatten;
}
/**
* Concat list into pairs of points
*
* @param array $list
* @return array pairs
*/
final public static function Pair($list)
{
$pairs = array();
if (!is_array($list)) {
return $pairs;
}
do {
$pairs[] = array(
array_shift($list),
array_shift($list)
);
} while (!empty($list));
return $pairs;
}
}
//@NAMESPACE@ }