1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Geotools library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\Geotools\Geohash; |
13
|
|
|
|
14
|
|
|
use League\Geotools\Coordinate\CoordinateInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* TenTen class |
18
|
|
|
* |
19
|
|
|
* @see http://blog.jgc.org/2006/07/simple-code-for-entering-latitude-and.html |
20
|
|
|
* @see http://blog.jgc.org/2010/06/1010-code.html |
21
|
|
|
* |
22
|
|
|
* @author Antoine Corcy <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class TenTen |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The alphabet base. |
28
|
|
|
* |
29
|
|
|
* @var integer |
30
|
|
|
*/ |
31
|
|
|
const BASE = 29; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The used alphabet. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $alphabet = 'ABCDEFGHJKMNPQRVWXY0123456789'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Encode the coordinate via the 10:10 algorithm. |
42
|
|
|
* |
43
|
|
|
* @param CoordinateInterface $coordinate The coordinate to encode. |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
1 |
|
public function encode(CoordinateInterface $coordinate) |
47
|
|
|
{ |
48
|
1 |
|
$latitude = floor(($coordinate->getLatitude() + 90.0) * 10000.0); |
49
|
1 |
|
$longitude = floor(($coordinate->getLongitude() + 180.0) * 10000.0); |
50
|
|
|
|
51
|
1 |
|
$position = $latitude * 3600000.0 + $longitude; |
52
|
1 |
|
$ttNumber = $position * self::BASE; |
53
|
1 |
|
$checkDigit = 0; |
54
|
|
|
|
55
|
1 |
|
for ($i = 1; $i < 10; ++$i) { |
56
|
1 |
|
$checkDigit += ($position % self::BASE) * $i; |
57
|
1 |
|
$position = floor($position / self::BASE); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$checkDigit %= self::BASE; |
61
|
|
|
|
62
|
1 |
|
$ttNumber += $checkDigit; |
63
|
1 |
|
$ttNumber = floor($ttNumber); |
64
|
|
|
|
65
|
1 |
|
$tt = ''; |
66
|
1 |
|
for ($i = 0; $i < 10; ++$i) { |
67
|
1 |
|
$digit = $ttNumber % self::BASE; |
68
|
1 |
|
if ($i === 4 || $i === 7) { |
69
|
1 |
|
$tt = ' ' . $tt; |
70
|
|
|
} |
71
|
1 |
|
$tt = $this->alphabet[$digit] . $tt; |
72
|
|
|
|
73
|
1 |
|
$ttNumber = floor($ttNumber / self::BASE); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $tt; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|