| | <?
define('ZTILE_PATH', '/var/www/cache/ztile/');
define("ZTILE_NAV_DEPTH", 240);
class ZTile
{
static public $consar = Array(-85051128, -83979259, -82676284, -81093213, -79171334, -76840816, -74019543, -70612614, -66513260, -61606396, -55776579, -48922499, -40979898, -31952162, -21943045, -11178401, 0, 11178401, 21943045, 31952162, 40979898, 48922499, 55776579, 61606396, 66513260, 70612614, 74019543, 76840816, 79171334, 81093213, 82676284, 83979259, 85051128);
public static function extendcode ($code)
{
return substr($code, 0, 16) . substr('000000000000000000000000000', 0, max(0, 16 - strlen($code)));
}
private static function bin2code ($code)
{
//$code=intval($code);
$code = floatval($code); //bindec(decbin($code));
$len = 16;
$n = 1;
$digi = 0;
$ret = '';
while ($n <= $len)
{
$z = 16 - $n + 1;
$z1 = (($z) * 2) - 2;
$digi = ($code >> $z1) & 0x3;
$ret .= $digi;
$n++;
}
return $ret;
}
private static function bincode ($code, &$upperlimit = NULL)
{
$len = strlen($code);
$n = 1;
$digi = 0;
while ($n <= $len)
{
$c = intval($code[$n - 1]);
$z = 16 - $n + 1;
$z1 = (($z) * 2) - 2;
$digi |= $c << $z1;
$n++;
}
$digi1 = 0;
$to = (16 - ($len)) * 2;
for ($n = 0; $n < $to; $n++)
$digi1 += 1 << $n;
$digi1 = $digi | $digi1;
$digi1 = bindec(decbin($digi1));
if (isset($upperlimit) && $upperlimit !== NULL)
$upperlimit = $digi1;
$digi = bindec(decbin($digi));
return $digi;
}
public static function decode ($RQ)
{
$x1 = -180000000;
$x2 = 180000000;
$y1 = -85051128;
$y2 = 85051128;
$y1cons = 0;
$y2cons = 32;
$yconsdel = 0;
$z = 1;
$RQ = str_replace('/', '', $RQ);
$L = strlen($RQ);
$ZM = $L;
for ($i = 0; $i < $L; ++$i)
{
$test = intval($RQ[$i]);
$xdel = round(($x1 + $x2) / 2);
if ($i < 4) {
$yconsdel = ($y1cons + $y2cons) / 2;
$ydel = self::$consar[$yconsdel];
}
else {
$ydel = round(($y1 + $y2) / 2);
}
$p1 = $p2 = 0;
switch ($test) {
case 0:
$p1 = 0;
$p2 = 1;
break;
case 1:
$p1 = 1;
$p2 = 1;
break;
case 2:
$p1 = 0;
$p2 = 0;
break;
case 3:
$p1 = 1;
$p2 = 0;
break;
}
//echo "$test / $xdel - $ydel |$p1 $p2\n";
if ($p1) {
$x1 = $xdel + 1;
}
else {
$x2 = $xdel;
}
if ($p2) {
$y2 = $ydel;
$y2cons = $yconsdel;
}
else {
$y1 = $ydel + 1;
$y1cons = $yconsdel;
}
}
return array($y1, $x1, $y2, $x2, $RQ);
}
/**
$y=lat*1000000.
$x=lng*1000000
**/
public static function encode ($y, $x)
{
$x = round($x);
$y = round($y);
$x1 = -180000000;
$x2 = 180000000;
$y1 = -85051128;
$y2 = 85051128;
$y1cons = 0;
$y2cons = 32;
$yconsdel = 0;
$z = 1;
$result = '';
for ($i = 0; $i < 16; ++$i)
{
$xdel = round(($x1 + $x2) / 2);
if ($i < 4) {
$yconsdel = ($y1cons + $y2cons) / 2;
$ydel = self::$consar[$yconsdel];
}
else {
$ydel = round(($y1 + $y2) / 2);
}
$p1 = $p2 = $test = 0;
if ($x > $xdel) $p1 = 1;
if ($x < $xdel) $p1 = 0;
if ($y > $ydel) $p2 = 0;
if ($y < $ydel) $p2 = 1;
if (!$p1 && !$p2) $test = 2;
elseif (!$p1 && $p2) $test = 0;
elseif ($p1 && !$p2) $test = 3;
elseif ($p1 && $p2) $test = 1;
$result .= $test;
if ($p1) {
$x1 = $xdel + 1;
}
else {
$x2 = $xdel;
}
if ($p2) {
$y2 = $ydel;
$y2cons = $yconsdel;
}
else {
$y1 = $ydel + 1;
$y1cons = $yconsdel;
}
}
return array($result, $y, $x, $y1, $x1);
}
static function formatRQ ($RQ)
{
$RQ = "0" . str_replace('/', '', $RQ);
$L = strlen($RQ);
$ret = '';
for ($i = 0; $i < $L; $i += 3)
{
if ($ret) $ret .= '/';
$ret .= substr($RQ, $i, 3);
}
return $ret;
}
}
/* *
$dl=ZTile::decode("30/231/032/033/0");
print_r($dl);
$lists=ZTile::encode($dl[0],$dl[1]);
print_r($lists[0]);
exit;
/* */
?>
|
|