-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencode_decode.php
More file actions
135 lines (132 loc) · 4.13 KB
/
encode_decode.php
File metadata and controls
135 lines (132 loc) · 4.13 KB
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
<?php
class Polyline
{
/**
* @var int $precision
*/
//protected static $precision = 5;
protected static $precision = 6;
/**
*
* @param array $points List of points to encode. Can be a list of tuples,
* or a flat, one-dimensional array.
* @return string encoded string
*/
final public static function encode()
{
/**
* If points in string format
* $points = '77.2681117 28.5495164,77.2675753 28.5515426,77.2674680 28.5539929,77.2665989 28.5551331,77.2633159 28.5577529,77.2506559 28.5625023,77.2525871 28.5667802';
* $i =0;
* $points = explode(",", $points);
* foreach($points as $sub){
* $points[$i] = array_reverse(explode(' ', $sub));
* $i++;
* }
* $points = self::flatten($points);
*/
$points = self::flatten($points = array(
array(41.89084,-87.62386),
array(41.89086,-87.62279),
array(41.89028,-87.62277),
array(41.89028,-87.62385),
array(41.89084,-87.62386)
));
$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;
}
/**
* @param string $string Encoded string to extract points from.
*
* @return array points
*/
final public static function decode()
{
$result = array();
$result = array();
$value = filter_var($path, FILTER_SANITIZE_STRING); //decoded string goes here
$index = 0;
$points = array();
$lat = 0;
$lng = 0;
while ($index < strlen($value)) {
$b;
$shift = 0;
$result = 0;
do {
$b = ord(substr($value, $index++, 1)) - 63;
$result |= ($b & 0x1f) << $shift;
$shift += 5;
} while ($b > 31);
$dlat = (($result & 1) ? ~($result >> 1) : ($result >> 1));
$lat += $dlat;
$shift = 0;
$result = 0;
do {
$b = ord(substr($value, $index++, 1)) - 63;
$result |= ($b & 0x1f) << $shift;
$shift += 5;
} while ($b > 31);
$dlng = (($result & 1) ? ~($result >> 1) : ($result >> 1));
$lng += $dlng;
$points[] = array($lat/pow(10, static::$precision), $lng/pow(10, static::$precision));
}
}
/*
* Returning a string like(lng lat, lng lat......)
* foreach (array_chunk($points, 2) as $sub) {
* $tmpArr[] = implode(' ', array_reverse($sub));
* }
* $result = implode(',', $tmpArr);
* return $result;
*/
return $points;
}
/**
* Reduce multi-dimensional to single list
*
* @param array $array Subject array to flatten.
*
* @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 One-dimensional array to segment into list of tuples.
*
* @return array pairs
*/
final public static function pair( $list )
{
return is_array($list) ? array_chunk($list, 2) : array();
}
}