| 1 |
<?
|
| 2 |
|
| 3 |
class Tile {
|
| 4 |
|
| 5 |
// The point (x,y) for this tile
|
| 6 |
var $p;
|
| 7 |
// The coord (lat,lon) for this tile
|
| 8 |
var $co;
|
| 9 |
// Zoom level for this tile
|
| 10 |
var $z;
|
| 11 |
|
| 12 |
// ...Constants...
|
| 13 |
var $PI = 3.1415926535;
|
| 14 |
var $tileSize = 256;
|
| 15 |
var $pixelsPerLonDegree = Array();
|
| 16 |
var $pixelsPerLonRadian = Array();
|
| 17 |
var $numTiles = Array();
|
| 18 |
var $bitmapOrigo = Array();
|
| 19 |
// Note: These variable names are based on the variables names found in the
|
| 20 |
// Google maps.*.js code.
|
| 21 |
var $c = 256;
|
| 22 |
var $bc;
|
| 23 |
var $Wa;
|
| 24 |
|
| 25 |
// Fill in the constants array
|
| 26 |
function fillinconstants() {
|
| 27 |
$this->bc = 2*$this->PI;
|
| 28 |
$this->Wa = $this->PI/180;
|
| 29 |
|
| 30 |
for($d = 17; $d >= 0; --$d) {
|
| 31 |
$this->pixelsPerLonDegree[$d] = $this->c / 360;
|
| 32 |
$this->pixelsPerLonRadian[$d] = $this->c / $this->bc;
|
| 33 |
$e = $this->c / 2;
|
| 34 |
$this->bitmapOrigo[$d] = new p($e,$e);
|
| 35 |
$this->numTiles[$d] = $this->c / 256;
|
| 36 |
$this->c *= 2;
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
function Tile($latitude, $longitude, $zoomLevel) {
|
| 41 |
$this->fillInConstants();
|
| 42 |
$this->z = $zoomLevel;
|
| 43 |
$this->p = $this->getTileCoordinate($latitude, $longitude, $zoomLevel);
|
| 44 |
$this->co = $this->getLatLong($latitude, $longitude, $zoomLevel);
|
| 45 |
}
|
| 46 |
|
| 47 |
function getTileCoord() {
|
| 48 |
return $this->p;
|
| 49 |
}
|
| 50 |
|
| 51 |
function getTileLatLong() {
|
| 52 |
return $this->co;
|
| 53 |
}
|
| 54 |
|
| 55 |
function getKeyholeString() {
|
| 56 |
$s = "";
|
| 57 |
$myX = $this->p->x;
|
| 58 |
$myY = $this->p->y;
|
| 59 |
|
| 60 |
for($i = 17; $i > $this->z; $i--) {
|
| 61 |
$rx = (fmod($myX, 2));
|
| 62 |
$myX = floor($myX / 2);
|
| 63 |
$ry = (fmod($myY, 2));
|
| 64 |
$myY = floor($myY / 2);
|
| 65 |
$s = $this->getKeyholeDirection($rx, $ry).$s;
|
| 66 |
}
|
| 67 |
return 't'.$s;
|
| 68 |
}
|
| 69 |
|
| 70 |
function getKeyholeDirection($x, $y) {
|
| 71 |
if($x == 1) {
|
| 72 |
if($y == 1) {
|
| 73 |
return 's';
|
| 74 |
} else if($y == 0) {
|
| 75 |
return 'r';
|
| 76 |
}
|
| 77 |
} else if($x == 0) {
|
| 78 |
if($y == 1) {
|
| 79 |
return 't';
|
| 80 |
} else if($y == 0) {
|
| 81 |
return 'q';
|
| 82 |
}
|
| 83 |
}
|
| 84 |
|
| 85 |
return '';
|
| 86 |
}
|
| 87 |
|
| 88 |
function getBitmapCoordinate($a, $b, $c) {
|
| 89 |
$d = new p(0,0);
|
| 90 |
|
| 91 |
$d->x = floor($this->bitmapOrigo[$c]->x + $b * $this->pixelsPerLonDegree[$c]);
|
| 92 |
$e = sin($a * $this->Wa);
|
| 93 |
|
| 94 |
if($e > 0.9999) {
|
| 95 |
$e = 0.9999;
|
| 96 |
}
|
| 97 |
|
| 98 |
if($e < -0.9999) {
|
| 99 |
$e = -0.9999;
|
| 100 |
}
|
| 101 |
|
| 102 |
$d->y = floor($this->bitmapOrigo[$c]->y + 0.5 * log((1 + $e) / (1 - $e)) * -1*($this->pixelsPerLonRadian[$c]));
|
| 103 |
return $d;
|
| 104 |
}
|
| 105 |
|
| 106 |
function getTileCoordinate($a, $b, $c) {
|
| 107 |
$d = $this->getBitmapCoordinate($a, $b, $c);
|
| 108 |
$d->x = floor($d->x / $this->tileSize);
|
| 109 |
$d->y = floor($d->y / $this->tileSize);
|
| 110 |
|
| 111 |
return $d;
|
| 112 |
}
|
| 113 |
|
| 114 |
function getLatLong($a, $b, $c) {
|
| 115 |
$d = new p(0, 0);
|
| 116 |
$e = $this->getBitmapCoordinate($a, $b, $c);
|
| 117 |
$a = $e->x;
|
| 118 |
$b = $e->y;
|
| 119 |
|
| 120 |
$d->x = ($a - $this->bitmapOrigo[$c]->x) / $this->pixelsPerLonDegree[$c];
|
| 121 |
$e = ($b - $this->bitmapOrigo[$c]->y) / (-1*$this->pixelsPerLonRadian[$c]);
|
| 122 |
$d->y = (2 * atan(exp($e)) - $this->PI / 2) / $this->Wa;
|
| 123 |
return $d;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
// A simple PHP class that represents a point
|
| 128 |
class p {
|
| 129 |
var $x;
|
| 130 |
var $y;
|
| 131 |
|
| 132 |
function p($x,$y) {
|
| 133 |
$this->x = $x;
|
| 134 |
$this->y = $y;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
function getLatLongXYZ($x, $y, $zoom) {
|
| 139 |
$lon = -180; // x
|
| 140 |
$lonWidth = 360; // width 360
|
| 141 |
|
| 142 |
$lat = -1;
|
| 143 |
$latHeight = 2;
|
| 144 |
|
| 145 |
$tilesAtThisZoom = 1 << (17 - $zoom);
|
| 146 |
$lonWidth = 360.0 / $tilesAtThisZoom;
|
| 147 |
$lon = -180 + ($x * $lonWidth);
|
| 148 |
$latHeight = 2.0 / $tilesAtThisZoom;
|
| 149 |
$lat = (($tilesAtThisZoom/2 - $y-1) * $latHeight);
|
| 150 |
|
| 151 |
// convert lat and latHeight to degrees in a transverse mercator projection
|
| 152 |
// note that in fact the coordinates go from about -85 to +85 not -90 to 90!
|
| 153 |
$latHeight += $lat;
|
| 154 |
$latHeight = (2 * atan(exp(PI() * $latHeight))) - (PI() / 2);
|
| 155 |
$latHeight *= (180 / PI());
|
| 156 |
|
| 157 |
$lat = (2 * atan(exp(PI() * $lat))) - (PI() / 2);
|
| 158 |
$lat *= (180 / PI());
|
| 159 |
|
| 160 |
$latHeight -= $lat;
|
| 161 |
|
| 162 |
if ($lonWidth < 0) {
|
| 163 |
$lon = $lon + $lonWidth;
|
| 164 |
$lonWidth = -$lonWidth;
|
| 165 |
}
|
| 166 |
|
| 167 |
if ($latHeight < 0) {
|
| 168 |
$lat = $lat + $latHeight;
|
| 169 |
$latHeight = -$latHeight;
|
| 170 |
}
|
| 171 |
|
| 172 |
$rect = array();
|
| 173 |
$rect[] = $lon;
|
| 174 |
$rect[] = $lat;
|
| 175 |
$rect[] = $latHeight;
|
| 176 |
$rect[] = $lonWidth;
|
| 177 |
|
| 178 |
return $rect;
|
| 179 |
}
|
| 180 |
|
| 181 |
function GetUrl($x, $y, $z) {
|
| 182 |
$filename = "${z}/${x}/${y}.png";
|
| 183 |
if (file_exists("./maps/".$filename)) {
|
| 184 |
return "http://tracks.landais.org/maps/".$filename;
|
| 185 |
}else{
|
| 186 |
return "http://tracks.landais.org/maps/transparent.gif";
|
| 187 |
}
|
| 188 |
}
|