| [36be960] | 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * TileServer.php project
|
|---|
| 5 | * ======================
|
|---|
| 6 | * https://github.com/klokantech/tileserver-php/
|
|---|
| 7 | * Copyright (C) 2016 - Klokan Technologies GmbH
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | global $config;
|
|---|
| 11 | $config['serverTitle'] = 'GNUBoat Tileserver';
|
|---|
| 12 | $config['availableFormats'] = array('png', 'jpg', 'jpeg', 'gif', 'webp', 'pbf', 'hybrid');
|
|---|
| 13 | //$config['template'] = 'template.php';
|
|---|
| 14 | $config['baseUrls'] = array('localhost/~joachim/gnuboat/webservice');
|
|---|
| 15 |
|
|---|
| 16 | Router::serve(array(
|
|---|
| 17 | '/' => 'Server:getHtml',
|
|---|
| 18 | '/maps' => 'Server:getInfo',
|
|---|
| 19 | '/html' => 'Server:getHtml',
|
|---|
| 20 | '/:alpha/:number/:number/:number.grid.json' => 'Json:getUTFGrid',
|
|---|
| 21 | '/:alpha.json' => 'Json:getJson',
|
|---|
| 22 | '/:alpha.jsonp' => 'Json:getJsonp',
|
|---|
| 23 | '/wmts' => 'Wmts:get',
|
|---|
| 24 | '/wmts/1.0.0/WMTSCapabilities.xml' => 'Wmts:get',
|
|---|
| 25 | '/wmts/:alpha/:number/:number/:alpha' => 'Wmts:getTile',
|
|---|
| 26 | '/wmts/:alpha/:alpha/:number/:number/:alpha' => 'Wmts:getTile',
|
|---|
| 27 | '/wmts/:alpha/:alpha/:alpha/:number/:number/:alpha' => 'Wmts:getTile',
|
|---|
| 28 | '/:alpha/:number/:number/:alpha' => 'Wmts:getTile',
|
|---|
| 29 | '/tms' => 'Tms:getCapabilities',
|
|---|
| 30 | '/tms/:alpha' => 'Tms:getLayerCapabilities',
|
|---|
| 31 | ));
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Server base
|
|---|
| 35 | */
|
|---|
| 36 | class Server {
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Configuration of TileServer [baseUrls, serverTitle]
|
|---|
| 40 | * @var array
|
|---|
| 41 | */
|
|---|
| 42 | public $config;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Datasets stored in file structure
|
|---|
| 46 | * @var array
|
|---|
| 47 | */
|
|---|
| 48 | public $fileLayer = array();
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Datasets stored in database
|
|---|
| 52 | * @var array
|
|---|
| 53 | */
|
|---|
| 54 | public $dbLayer = array();
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * PDO database connection
|
|---|
| 58 | * @var object
|
|---|
| 59 | */
|
|---|
| 60 | public $db;
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Set config
|
|---|
| 64 | */
|
|---|
| 65 | public function __construct() {
|
|---|
| 66 | $this->config = $GLOBALS['config'];
|
|---|
| 67 |
|
|---|
| 68 | //Get config from enviroment
|
|---|
| 69 | $envServerTitle = getenv('serverTitle');
|
|---|
| 70 | if($envServerTitle !== FALSE){
|
|---|
| 71 | $this->config['serverTitle'] = $envServerTitle;
|
|---|
| 72 | }
|
|---|
| 73 | $envBaseUrls = getenv('baseUrls');
|
|---|
| 74 | if($envBaseUrls !== FALSE){
|
|---|
| 75 | $this->config['baseUrls'] = is_array($envBaseUrls) ?
|
|---|
| 76 | $envBaseUrls : explode(',', $envBaseUrls);
|
|---|
| 77 | }
|
|---|
| 78 | $envTemplate = getenv('template');
|
|---|
| 79 | if($envBaseUrls !== FALSE){
|
|---|
| 80 | $this->config['template'] = $envTemplate;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Looks for datasets
|
|---|
| 86 | */
|
|---|
| 87 | public function setDatasets() {
|
|---|
| 88 | $mjs = glob('*/metadata.json');
|
|---|
| 89 | $mbts = glob('*.mbtiles');
|
|---|
| 90 | if ($mjs) {
|
|---|
| 91 | foreach (array_filter($mjs, 'is_readable') as $mj) {
|
|---|
| 92 | $layer = $this->metadataFromMetadataJson($mj);
|
|---|
| 93 | array_push($this->fileLayer, $layer);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | if ($mbts) {
|
|---|
| 97 | foreach (array_filter($mbts, 'is_readable') as $mbt) {
|
|---|
| 98 | $this->dbLayer[] = $this->metadataFromMbtiles($mbt);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Processing params from router <server>/<layer>/<z>/<x>/<y>.ext
|
|---|
| 105 | * @param array $params
|
|---|
| 106 | */
|
|---|
| 107 | public function setParams($params) {
|
|---|
| 108 | if (isset($params[1])) {
|
|---|
| 109 | $this->layer = $params[1];
|
|---|
| 110 | }
|
|---|
| 111 | $params = array_reverse($params);
|
|---|
| 112 | if (isset($params[2])) {
|
|---|
| 113 | $this->z = $params[2];
|
|---|
| 114 | $this->x = $params[1];
|
|---|
| 115 | $file = explode('.', $params[0]);
|
|---|
| 116 | $this->y = $file[0];
|
|---|
| 117 | $this->ext = isset($file[1]) ? $file[1] : NULL;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * Get variable don't independent on sensitivity
|
|---|
| 123 | * @param string $key
|
|---|
| 124 | * @return boolean
|
|---|
| 125 | */
|
|---|
| 126 | public function getGlobal($isKey) {
|
|---|
| 127 | $get = $_GET;
|
|---|
| 128 | foreach ($get as $key => $value) {
|
|---|
| 129 | if (strtolower($isKey) == strtolower($key)) {
|
|---|
| 130 | return $value;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | return FALSE;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Testing if is a database layer
|
|---|
| 138 | * @param string $layer
|
|---|
| 139 | * @return boolean
|
|---|
| 140 | */
|
|---|
| 141 | public function isDBLayer($layer) {
|
|---|
| 142 | if (is_file($layer . '.mbtiles')) {
|
|---|
| 143 | return TRUE;
|
|---|
| 144 | } else {
|
|---|
| 145 | return FALSE;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Testing if is a file layer
|
|---|
| 151 | * @param string $layer
|
|---|
| 152 | * @return boolean
|
|---|
| 153 | */
|
|---|
| 154 | public function isFileLayer($layer) {
|
|---|
| 155 | if (is_dir($layer)) {
|
|---|
| 156 | return TRUE;
|
|---|
| 157 | } else {
|
|---|
| 158 | return FALSE;
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Get metadata from metadataJson
|
|---|
| 164 | * @param string $jsonFileName
|
|---|
| 165 | * @return array
|
|---|
| 166 | */
|
|---|
| 167 | public function metadataFromMetadataJson($jsonFileName) {
|
|---|
| 168 | $metadata = json_decode(file_get_contents($jsonFileName), true);
|
|---|
| 169 | $metadata['basename'] = str_replace('/metadata.json', '', $jsonFileName);
|
|---|
| 170 | return $this->metadataValidation($metadata);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Loads metadata from MBtiles
|
|---|
| 175 | * @param string $mbt
|
|---|
| 176 | * @return object
|
|---|
| 177 | */
|
|---|
| 178 | public function metadataFromMbtiles($mbt) {
|
|---|
| 179 | $metadata = array();
|
|---|
| 180 | $this->DBconnect($mbt);
|
|---|
| 181 | $result = $this->db->query('select * from metadata');
|
|---|
| 182 |
|
|---|
| 183 | $resultdata = $result->fetchAll();
|
|---|
| 184 | foreach ($resultdata as $r) {
|
|---|
| 185 | $value = preg_replace('/(\\n)+/', '', $r['value']);
|
|---|
| 186 | $metadata[$r['name']] = addslashes($value);
|
|---|
| 187 | }
|
|---|
| 188 | if (!array_key_exists('minzoom', $metadata)
|
|---|
| 189 | || !array_key_exists('maxzoom', $metadata)
|
|---|
| 190 | ) {
|
|---|
| 191 | // autodetect minzoom and maxzoom
|
|---|
| 192 | $result = $this->db->query('select min(zoom_level) as min, max(zoom_level) as max from tiles');
|
|---|
| 193 | $resultdata = $result->fetchAll();
|
|---|
| 194 | if (!array_key_exists('minzoom', $metadata)){
|
|---|
| 195 | $metadata['minzoom'] = $resultdata[0]['min'];
|
|---|
| 196 | }
|
|---|
| 197 | if (!array_key_exists('maxzoom', $metadata)){
|
|---|
| 198 | $metadata['maxzoom'] = $resultdata[0]['max'];
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 | // autodetect format using JPEG magic number FFD8
|
|---|
| 202 | if (!array_key_exists('format', $metadata)) {
|
|---|
| 203 | $result = $this->db->query('select hex(substr(tile_data,1,2)) as magic from tiles limit 1');
|
|---|
| 204 | $resultdata = $result->fetchAll();
|
|---|
| 205 | $metadata['format'] = ($resultdata[0]['magic'] == 'FFD8')
|
|---|
| 206 | ? 'jpg'
|
|---|
| 207 | : 'png';
|
|---|
| 208 | }
|
|---|
| 209 | // autodetect bounds
|
|---|
| 210 | if (!array_key_exists('bounds', $metadata)) {
|
|---|
| 211 | $result = $this->db->query('select min(tile_column) as w, max(tile_column) as e, min(tile_row) as s, max(tile_row) as n from tiles where zoom_level='.$metadata['maxzoom']);
|
|---|
| 212 | $resultdata = $result->fetchAll();
|
|---|
| 213 | $w = -180 + 360 * ($resultdata[0]['w'] / pow(2, $metadata['maxzoom']));
|
|---|
| 214 | $e = -180 + 360 * ((1 + $resultdata[0]['e']) / pow(2, $metadata['maxzoom']));
|
|---|
| 215 | $n = $this->row2lat($resultdata[0]['n'], $metadata['maxzoom']);
|
|---|
| 216 | $s = $this->row2lat($resultdata[0]['s'] - 1, $metadata['maxzoom']);
|
|---|
| 217 | $metadata['bounds'] = implode(',', array($w, $s, $e, $n));
|
|---|
| 218 | }
|
|---|
| 219 | $mbt = explode('.', $mbt);
|
|---|
| 220 | $metadata['basename'] = $mbt[0];
|
|---|
| 221 | $metadata = $this->metadataValidation($metadata);
|
|---|
| 222 | return $metadata;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | * Convert row number to latitude of the top of the row
|
|---|
| 227 | * @param integer $r
|
|---|
| 228 | * @param integer $zoom
|
|---|
| 229 | * @return integer
|
|---|
| 230 | */
|
|---|
| 231 | public function row2lat($r, $zoom) {
|
|---|
| 232 | $y = $r / pow(2, $zoom - 1 ) - 1;
|
|---|
| 233 | return rad2deg(2.0 * atan(exp(3.191459196 * $y)) - 1.57079632679489661922);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * Valids metaJSON
|
|---|
| 238 | * @param object $metadata
|
|---|
| 239 | * @return object
|
|---|
| 240 | */
|
|---|
| 241 | public function metadataValidation($metadata) {
|
|---|
| 242 | if (!array_key_exists('bounds', $metadata)) {
|
|---|
| 243 | $metadata['bounds'] = array(-180, -85.06, 180, 85.06);
|
|---|
| 244 | } elseif (!is_array($metadata['bounds'])) {
|
|---|
| 245 | $metadata['bounds'] = array_map('floatval', explode(',', $metadata['bounds']));
|
|---|
| 246 | }
|
|---|
| 247 | if (!array_key_exists('profile', $metadata)) {
|
|---|
| 248 | $metadata['profile'] = 'mercator';
|
|---|
| 249 | }
|
|---|
| 250 | if (array_key_exists('minzoom', $metadata)){
|
|---|
| 251 | $metadata['minzoom'] = intval($metadata['minzoom']);
|
|---|
| 252 | }else{
|
|---|
| 253 | $metadata['minzoom'] = 0;
|
|---|
| 254 | }
|
|---|
| 255 | if (array_key_exists('maxzoom', $metadata)){
|
|---|
| 256 | $metadata['maxzoom'] = intval($metadata['maxzoom']);
|
|---|
| 257 | }else{
|
|---|
| 258 | $metadata['maxzoom'] = 18;
|
|---|
| 259 | }
|
|---|
| 260 | if (!array_key_exists('format', $metadata)) {
|
|---|
| 261 | if(array_key_exists('tiles', $metadata)){
|
|---|
| 262 | $pos = strrpos($metadata['tiles'][0], '.');
|
|---|
| 263 | $metadata['format'] = trim(substr($metadata['tiles'][0], $pos + 1));
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 | $formats = $this->config['availableFormats'];
|
|---|
| 267 | if(!in_array(strtolower($metadata['format']), $formats)){
|
|---|
| 268 | $metadata['format'] = 'png';
|
|---|
| 269 | }
|
|---|
| 270 | if (!array_key_exists('scale', $metadata)) {
|
|---|
| 271 | $metadata['scale'] = 1;
|
|---|
| 272 | }
|
|---|
| 273 | if(!array_key_exists('tiles', $metadata)){
|
|---|
| 274 | $tiles = array();
|
|---|
| 275 | foreach ($this->config['baseUrls'] as $url) {
|
|---|
| 276 | $url = '' . $this->config['protocol'] . '://' . $url . '/' .
|
|---|
| 277 | $metadata['basename'] . '/{z}/{x}/{y}';
|
|---|
| 278 | if(strlen($metadata['format']) <= 4){
|
|---|
| 279 | $url .= '.' . $metadata['format'];
|
|---|
| 280 | }
|
|---|
| 281 | $tiles[] = $url;
|
|---|
| 282 | }
|
|---|
| 283 | $metadata['tiles'] = $tiles;
|
|---|
| 284 | }
|
|---|
| 285 | return $metadata;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /**
|
|---|
| 289 | * SQLite connection
|
|---|
| 290 | * @param string $tileset
|
|---|
| 291 | */
|
|---|
| 292 | public function DBconnect($tileset) {
|
|---|
| 293 | try {
|
|---|
| 294 | $this->db = new PDO('sqlite:' . $tileset, '', '', array(PDO::ATTR_PERSISTENT => true));
|
|---|
| 295 | } catch (Exception $exc) {
|
|---|
| 296 | echo $exc->getTraceAsString();
|
|---|
| 297 | die;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | if (!isset($this->db)) {
|
|---|
| 301 | header('Content-type: text/plain');
|
|---|
| 302 | echo 'Incorrect tileset name: ' . $tileset;
|
|---|
| 303 | die;
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /**
|
|---|
| 308 | * Check if file is modified and set Etag headers
|
|---|
| 309 | * @param string $filename
|
|---|
| 310 | * @return boolean
|
|---|
| 311 | */
|
|---|
| 312 | public function isModified($filename) {
|
|---|
| 313 | $filename = $filename . '.mbtiles';
|
|---|
| 314 | $lastModifiedTime = filemtime($filename);
|
|---|
| 315 | $eTag = md5($lastModifiedTime);
|
|---|
| 316 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastModifiedTime) . ' GMT');
|
|---|
| 317 | header('Etag:' . $eTag);
|
|---|
| 318 | if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModifiedTime ||
|
|---|
| 319 | @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $eTag) {
|
|---|
| 320 | return TRUE;
|
|---|
| 321 | } else {
|
|---|
| 322 | return FALSE;
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * Returns tile of dataset
|
|---|
| 328 | * @param string $tileset
|
|---|
| 329 | * @param integer $z
|
|---|
| 330 | * @param integer $y
|
|---|
| 331 | * @param integer $x
|
|---|
| 332 | * @param string $ext
|
|---|
| 333 | */
|
|---|
| 334 | public function renderTile($tileset, $z, $y, $x, $ext) {
|
|---|
| 335 | if ($this->isDBLayer($tileset)) {
|
|---|
| 336 | if ($this->isModified($tileset) == TRUE) {
|
|---|
| 337 | header('Access-Control-Allow-Origin: *');
|
|---|
| 338 | header('HTTP/1.1 304 Not Modified');
|
|---|
| 339 | die;
|
|---|
| 340 | }
|
|---|
| 341 | $this->DBconnect($tileset . '.mbtiles');
|
|---|
| 342 | $z = floatval($z);
|
|---|
| 343 | $y = floatval($y);
|
|---|
| 344 | $x = floatval($x);
|
|---|
| 345 | $flip = true;
|
|---|
| 346 | if ($flip) {
|
|---|
| 347 | $y = pow(2, $z) - 1 - $y;
|
|---|
| 348 | }
|
|---|
| 349 | $result = $this->db->query('select tile_data as t from tiles where zoom_level=' . $z . ' and tile_column=' . $x . ' and tile_row=' . $y);
|
|---|
| 350 | $data = $result->fetchColumn();
|
|---|
| 351 | if (!isset($data) || $data === FALSE) {
|
|---|
| 352 | //if tile doesn't exist
|
|---|
| 353 | //select scale of tile (for retina tiles)
|
|---|
| 354 | $result = $this->db->query('select value from metadata where name="scale"');
|
|---|
| 355 | $resultdata = $result->fetchColumn();
|
|---|
| 356 | $scale = isset($resultdata) && $resultdata !== FALSE ? $resultdata : 1;
|
|---|
| 357 | $this->getCleanTile($scale, $ext);
|
|---|
| 358 | } else {
|
|---|
| 359 | $result = $this->db->query('select value from metadata where name="format"');
|
|---|
| 360 | $resultdata = $result->fetchColumn();
|
|---|
| 361 | $format = isset($resultdata) && $resultdata !== FALSE ? $resultdata : 'png';
|
|---|
| 362 | if ($format == 'jpg') {
|
|---|
| 363 | $format = 'jpeg';
|
|---|
| 364 | }
|
|---|
| 365 | if ($format == 'pbf') {
|
|---|
| 366 | header('Content-type: application/x-protobuf');
|
|---|
| 367 | header('Content-Encoding:gzip');
|
|---|
| 368 | } else {
|
|---|
| 369 | header('Content-type: image/' . $format);
|
|---|
| 370 | }
|
|---|
| 371 | header('Access-Control-Allow-Origin: *');
|
|---|
| 372 | echo $data;
|
|---|
| 373 | }
|
|---|
| 374 | } elseif ($this->isFileLayer($tileset)) {
|
|---|
| 375 | $name = './' . $tileset . '/' . $z . '/' . $x . '/' . $y;
|
|---|
| 376 | $mime = 'image/';
|
|---|
| 377 | if($ext != NULL){
|
|---|
| 378 | $name .= '.' . $ext;
|
|---|
| 379 | }
|
|---|
| 380 | if ($fp = @fopen($name, 'rb')) {
|
|---|
| 381 | if($ext != NULL){
|
|---|
| 382 | $mime .= $ext;
|
|---|
| 383 | }else{
|
|---|
| 384 | //detect image type from file
|
|---|
| 385 | $mimetypes = array('gif', 'jpeg', 'png');
|
|---|
| 386 | $mime .= $mimetypes[exif_imagetype($name) - 1];
|
|---|
| 387 | }
|
|---|
| 388 | header('Access-Control-Allow-Origin: *');
|
|---|
| 389 | header('Content-Type: ' . $mime);
|
|---|
| 390 | header('Content-Length: ' . filesize($name));
|
|---|
| 391 | fpassthru($fp);
|
|---|
| 392 | die;
|
|---|
| 393 | } else {
|
|---|
| 394 | //scale of tile (for retina tiles)
|
|---|
| 395 | $meta = json_decode(file_get_contents($tileset . '/metadata.json'));
|
|---|
| 396 | if(!isset($meta->scale)){
|
|---|
| 397 | $meta->scale = 1;
|
|---|
| 398 | }
|
|---|
| 399 | }
|
|---|
| 400 | $this->getCleanTile($meta->scale, $ext);
|
|---|
| 401 | } else {
|
|---|
| 402 | header('HTTP/1.1 404 Not Found');
|
|---|
| 403 | echo 'Server: Unknown or not specified dataset "' . $tileset . '"';
|
|---|
| 404 | die;
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /**
|
|---|
| 409 | * Returns clean tile
|
|---|
| 410 | * @param integer $scale Default 1
|
|---|
| 411 | */
|
|---|
| 412 | public function getCleanTile($scale = 1, $format = 'png') {
|
|---|
| 413 | switch ($format) {
|
|---|
| 414 | case 'pbf':
|
|---|
| 415 | header('HTTP/1.1 404 Not Found');
|
|---|
| 416 | header('Content-Type: application/json; charset=utf-8');
|
|---|
| 417 | echo '{"message":"Tile does not exist"}';
|
|---|
| 418 | break;
|
|---|
| 419 | case 'webp':
|
|---|
| 420 | header('Access-Control-Allow-Origin: *');
|
|---|
| 421 | header('Content-type: image/webp');
|
|---|
| 422 | echo base64_decode('UklGRhIAAABXRUJQVlA4TAYAAAAvQWxvAGs=');
|
|---|
| 423 | break;
|
|---|
| 424 | case 'jpg':
|
|---|
| 425 | header('Access-Control-Allow-Origin: *');
|
|---|
| 426 | header('Content-type: image/jpg');
|
|---|
| 427 | echo base64_decode('/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=');
|
|---|
| 428 | break;
|
|---|
| 429 | case 'png':
|
|---|
| 430 | default:
|
|---|
| 431 | header('Access-Control-Allow-Origin: *');
|
|---|
| 432 | header('Content-type: image/png');
|
|---|
| 433 | // 256x256 transparent optimised png tile
|
|---|
| 434 | echo unpack('H', '89504e470d0a1a0a0000000d494844520000010000000100010300000066bc3a2500000003504c5445000000a77a3dda0000000174524e530040e6d8660000001f494441541819edc1010d000000c220fba77e0e37600000000000000000e70221000001f5a2bd040000000049454e44ae426082');
|
|---|
| 435 | break;
|
|---|
| 436 | }
|
|---|
| 437 | die;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /**
|
|---|
| 441 | * Returns tile's UTFGrid
|
|---|
| 442 | * @param string $tileset
|
|---|
| 443 | * @param integer $z
|
|---|
| 444 | * @param integer $y
|
|---|
| 445 | * @param integer $x
|
|---|
| 446 | */
|
|---|
| 447 | public function renderUTFGrid($tileset, $z, $y, $x, $flip = TRUE) {
|
|---|
| 448 | if ($this->isDBLayer($tileset)) {
|
|---|
| 449 | if ($this->isModified($tileset) == TRUE) {
|
|---|
| 450 | header('HTTP/1.1 304 Not Modified');
|
|---|
| 451 | }
|
|---|
| 452 | if ($flip) {
|
|---|
| 453 | $y = pow(2, $z) - 1 - $y;
|
|---|
| 454 | }
|
|---|
| 455 | try {
|
|---|
| 456 | $this->DBconnect($tileset . '.mbtiles');
|
|---|
| 457 |
|
|---|
| 458 | $query = 'SELECT grid FROM grids WHERE tile_column = ' . $x . ' AND '
|
|---|
| 459 | . 'tile_row = ' . $y . ' AND zoom_level = ' . $z;
|
|---|
| 460 | $result = $this->db->query($query);
|
|---|
| 461 | $data = $result->fetch(PDO::FETCH_ASSOC);
|
|---|
| 462 |
|
|---|
| 463 | if ($data !== FALSE) {
|
|---|
| 464 | $grid = gzuncompress($data['grid']);
|
|---|
| 465 | $grid = substr(trim($grid), 0, -1);
|
|---|
| 466 |
|
|---|
| 467 | //adds legend (data) to output
|
|---|
| 468 | $grid .= ',"data":{';
|
|---|
| 469 | $kquery = 'SELECT key_name as key, key_json as json FROM grid_data '
|
|---|
| 470 | . 'WHERE zoom_level=' . $z . ' and '
|
|---|
| 471 | . 'tile_column=' . $x . ' and tile_row=' . $y;
|
|---|
| 472 | $result = $this->db->query($kquery);
|
|---|
| 473 | while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
|
|---|
| 474 | $grid .= '"' . $r['key'] . '":' . $r['json'] . ',';
|
|---|
| 475 | }
|
|---|
| 476 | $grid = rtrim($grid, ',') . '}}';
|
|---|
| 477 | header('Access-Control-Allow-Origin: *');
|
|---|
| 478 |
|
|---|
| 479 | if (isset($_GET['callback']) && !empty($_GET['callback'])) {
|
|---|
| 480 | header('Content-Type:text/javascript charset=utf-8');
|
|---|
| 481 | echo $_GET['callback'] . '(' . $grid . ');';
|
|---|
| 482 | } else {
|
|---|
| 483 | header('Content-Type:text/javascript; charset=utf-8');
|
|---|
| 484 | echo $grid;
|
|---|
| 485 | }
|
|---|
| 486 | } else {
|
|---|
| 487 | header('Access-Control-Allow-Origin: *');
|
|---|
| 488 | echo '{}';
|
|---|
| 489 | die;
|
|---|
| 490 | }
|
|---|
| 491 | } catch (Exception $e) {
|
|---|
| 492 | header('Content-type: text/plain');
|
|---|
| 493 | print 'Error querying the database: ' . $e->getMessage();
|
|---|
| 494 | }
|
|---|
| 495 | } else {
|
|---|
| 496 | echo 'Server: no MBTiles tileset';
|
|---|
| 497 | die;
|
|---|
| 498 | }
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | /**
|
|---|
| 502 | * Returns server info
|
|---|
| 503 | */
|
|---|
| 504 | public function getInfo() {
|
|---|
| 505 | $this->setDatasets();
|
|---|
| 506 | $maps = array_merge($this->fileLayer, $this->dbLayer);
|
|---|
| 507 | header('Content-Type: text/html;charset=UTF-8');
|
|---|
| 508 | echo '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>' . $this->config['serverTitle'] . '</title></head><body>' .
|
|---|
| 509 | '<h1>' . $this->config['serverTitle'] . '</h1>' .
|
|---|
| 510 | 'TileJSON service: <a href="//' . $this->config['baseUrls'][0] . '/index.json">' . $this->config['baseUrls'][0] . '/index.json</a><br>' .
|
|---|
| 511 | 'WMTS service: <a href="//' . $this->config['baseUrls'][0] . '/wmts">' . $this->config['baseUrls'][0] . '/wmts</a><br>' .
|
|---|
| 512 | 'TMS service: <a href="//' . $this->config['baseUrls'][0] . '/tms">' . $this->config['baseUrls'][0] . '/tms</a>';
|
|---|
| 513 | foreach ($maps as $map) {
|
|---|
| 514 | $extend = '[' . implode($map['bounds'], ', ') . ']';
|
|---|
| 515 | echo '<p>Tileset: <b>' . $map['basename'] . '</b><br>' .
|
|---|
| 516 | 'Metadata: <a href="//' . $this->config['baseUrls'][0] . '/' . $map['basename'] . '.json">' .
|
|---|
| 517 | $this->config['baseUrls'][0] . '/' . $map['basename'] . '.json</a><br>' .
|
|---|
| 518 | 'Bounds: ' . $extend ;
|
|---|
| 519 | if(isset($map['crs'])){echo '<br>CRS: ' . $map['crs'];}
|
|---|
| 520 | echo '</p>';
|
|---|
| 521 | }
|
|---|
| 522 | echo '<p>Copyright (C) 2016 - Klokan Technologies GmbH</p>';
|
|---|
| 523 | echo '</body></html>';
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | /**
|
|---|
| 527 | * Returns html viewer
|
|---|
| 528 | */
|
|---|
| 529 | public function getHtml() {
|
|---|
| 530 | $this->setDatasets();
|
|---|
| 531 | $maps = array_merge($this->fileLayer, $this->dbLayer);
|
|---|
| 532 | if (isset($this->config['template']) && file_exists($this->config['template'])) {
|
|---|
| 533 | $baseUrls = $this->config['baseUrls'];
|
|---|
| 534 | $serverTitle = $this->config['serverTitle'];
|
|---|
| 535 | include_once $this->config['template'];
|
|---|
| 536 | } else {
|
|---|
| 537 | header('Content-Type: text/html;charset=UTF-8');
|
|---|
| 538 | echo '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>' . $this->config['serverTitle'] . '</title>';
|
|---|
| 539 | echo '<link rel="stylesheet" type="text/css" href="//cdn.klokantech.com/tileviewer/v1/index.css" />
|
|---|
| 540 | <script src="//cdn.klokantech.com/tileviewer/v1/index.js"></script><body>
|
|---|
| 541 | <script>tileserver({index:"' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/index.json", tilejson:"' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/%n.json", tms:"' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/tms", wmts:"' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/wmts"});</script>
|
|---|
| 542 | <h1>Welcome to ' . $this->config['serverTitle'] . '</h1>
|
|---|
| 543 | <p>This server distributes maps to desktop, web, and mobile applications.</p>
|
|---|
| 544 | <p>The mapping data are available as OpenGIS Web Map Tiling Service (OGC WMTS), OSGEO Tile Map Service (TMS), and popular XYZ urls described with TileJSON metadata.</p>';
|
|---|
| 545 | if (!isset($maps)) {
|
|---|
| 546 | echo '<h3 style="color:darkred;">No maps available yet</h3>
|
|---|
| 547 | <p style="color:darkred; font-style: italic;">
|
|---|
| 548 | Ready to go - just upload some maps into directory:' . getcwd() . '/ on this server.</p>
|
|---|
| 549 | <p>Note: The maps can be a directory with tiles in XYZ format with metadata.json file.<br/>
|
|---|
| 550 | You can easily convert existing geodata (GeoTIFF, ECW, MrSID, etc) to this tile structure with <a href="http://www.maptiler.com">MapTiler Cluster</a> or open-source projects such as <a href="http://www.klokan.cz/projects/gdal2tiles/">GDAL2Tiles</a> or <a href="http://www.maptiler.org/">MapTiler</a> or simply upload any maps in MBTiles format made by <a href="http://www.tilemill.com/">TileMill</a>. Helpful is also the <a href="https://github.com/mapbox/mbutil">mbutil</a> tool. Serving directly from .mbtiles files is supported, but with decreased performance.</p>';
|
|---|
| 551 | } else {
|
|---|
| 552 | echo '<ul>';
|
|---|
| 553 | foreach ($maps as $map) {
|
|---|
| 554 | echo "<li>" . $map['name'] . '</li>';
|
|---|
| 555 | }
|
|---|
| 556 | echo '</ul>';
|
|---|
| 557 | }
|
|---|
| 558 | echo '</body></html>';
|
|---|
| 559 | }
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | /**
|
|---|
| 565 | * JSON service
|
|---|
| 566 | */
|
|---|
| 567 | class Json extends Server {
|
|---|
| 568 |
|
|---|
| 569 | /**
|
|---|
| 570 | * Callback for JSONP default grid
|
|---|
| 571 | * @var string
|
|---|
| 572 | */
|
|---|
| 573 | private $callback = 'grid';
|
|---|
| 574 |
|
|---|
| 575 | /**
|
|---|
| 576 | * @param array $params
|
|---|
| 577 | */
|
|---|
| 578 | public $layer = 'index';
|
|---|
| 579 |
|
|---|
| 580 | /**
|
|---|
| 581 | * @var integer
|
|---|
| 582 | */
|
|---|
| 583 | public $z;
|
|---|
| 584 |
|
|---|
| 585 | /**
|
|---|
| 586 | * @var integer
|
|---|
| 587 | */
|
|---|
| 588 | public $y;
|
|---|
| 589 |
|
|---|
| 590 | /**
|
|---|
| 591 | * @var integer
|
|---|
| 592 | */
|
|---|
| 593 | public $x;
|
|---|
| 594 |
|
|---|
| 595 | /**
|
|---|
| 596 | * @var string
|
|---|
| 597 | */
|
|---|
| 598 | public $ext;
|
|---|
| 599 |
|
|---|
| 600 | /**
|
|---|
| 601 | *
|
|---|
| 602 | * @param array $params
|
|---|
| 603 | */
|
|---|
| 604 | public function __construct($params) {
|
|---|
| 605 | parent::__construct();
|
|---|
| 606 | parent::setParams($params);
|
|---|
| 607 | if (isset($_GET['callback']) && !empty($_GET['callback'])) {
|
|---|
| 608 | $this->callback = $_GET['callback'];
|
|---|
| 609 | }
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | /**
|
|---|
| 613 | * Adds metadata about layer
|
|---|
| 614 | * @param array $metadata
|
|---|
| 615 | * @return array
|
|---|
| 616 | */
|
|---|
| 617 | public function metadataTileJson($metadata) {
|
|---|
| 618 | $metadata['tilejson'] = '2.0.0';
|
|---|
| 619 | $metadata['scheme'] = 'xyz';
|
|---|
| 620 | if ($this->isDBLayer($metadata['basename'])) {
|
|---|
| 621 | $this->DBconnect($metadata['basename'] . '.mbtiles');
|
|---|
| 622 | $res = $this->db->query('SELECT name FROM sqlite_master WHERE name="grids";');
|
|---|
| 623 | if ($res) {
|
|---|
| 624 | foreach ($this->config['baseUrls'] as $url) {
|
|---|
| 625 | $grids[] = '' . $this->config['protocol'] . '://' . $url . '/' . $metadata['basename'] . '/{z}/{x}/{y}.grid.json';
|
|---|
| 626 | }
|
|---|
| 627 | $metadata['grids'] = $grids;
|
|---|
| 628 | }
|
|---|
| 629 | }
|
|---|
| 630 | if (array_key_exists('json', $metadata)) {
|
|---|
| 631 | $mjson = json_decode(stripslashes($metadata['json']));
|
|---|
| 632 | foreach ($mjson as $key => $value) {
|
|---|
| 633 | $metadata[$key] = $value;
|
|---|
| 634 | }
|
|---|
| 635 | unset($metadata['json']);
|
|---|
| 636 | }
|
|---|
| 637 | return $metadata;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | /**
|
|---|
| 641 | * Creates JSON from array
|
|---|
| 642 | * @param string $basename
|
|---|
| 643 | * @return string
|
|---|
| 644 | */
|
|---|
| 645 | private function createJson($basename) {
|
|---|
| 646 | $maps = array_merge($this->fileLayer, $this->dbLayer);
|
|---|
| 647 | if ($basename == 'index') {
|
|---|
| 648 | $output = '[';
|
|---|
| 649 | foreach ($maps as $map) {
|
|---|
| 650 | $output = $output . json_encode($this->metadataTileJson($map)) . ',';
|
|---|
| 651 | }
|
|---|
| 652 | if (strlen($output) > 1) {
|
|---|
| 653 | $output = substr_replace($output, ']', -1);
|
|---|
| 654 | } else {
|
|---|
| 655 | $output = $output . ']';
|
|---|
| 656 | }
|
|---|
| 657 | } else {
|
|---|
| 658 | foreach ($maps as $map) {
|
|---|
| 659 | if (strpos($map['basename'], $basename) !== false) {
|
|---|
| 660 | $output = json_encode($this->metadataTileJson($map));
|
|---|
| 661 | break;
|
|---|
| 662 | }
|
|---|
| 663 | }
|
|---|
| 664 | }
|
|---|
| 665 | if (!isset($output)) {
|
|---|
| 666 | echo 'TileServer: unknown map ' . $basename;
|
|---|
| 667 | die;
|
|---|
| 668 | }
|
|---|
| 669 | return stripslashes($output);
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | /**
|
|---|
| 673 | * Returns JSON with callback
|
|---|
| 674 | */
|
|---|
| 675 | public function getJson() {
|
|---|
| 676 | parent::setDatasets();
|
|---|
| 677 | header('Access-Control-Allow-Origin: *');
|
|---|
| 678 | header('Content-Type: application/json; charset=utf-8');
|
|---|
| 679 | if ($this->callback !== 'grid') {
|
|---|
| 680 | echo $this->callback . '(' . $this->createJson($this->layer) . ');'; die;
|
|---|
| 681 | } else {
|
|---|
| 682 | echo $this->createJson($this->layer); die;
|
|---|
| 683 | }
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | /**
|
|---|
| 687 | * Returns JSONP with callback
|
|---|
| 688 | */
|
|---|
| 689 | public function getJsonp() {
|
|---|
| 690 | parent::setDatasets();
|
|---|
| 691 | header('Access-Control-Allow-Origin: *');
|
|---|
| 692 | header('Content-Type: application/javascript; charset=utf-8');
|
|---|
| 693 | echo $this->callback . '(' . $this->createJson($this->layer) . ');';
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | /**
|
|---|
| 697 | * Returns UTFGrid in JSON format
|
|---|
| 698 | */
|
|---|
| 699 | public function getUTFGrid() {
|
|---|
| 700 | parent::renderUTFGrid($this->layer, $this->z, $this->y, $this->x);
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | /**
|
|---|
| 706 | * Web map tile service
|
|---|
| 707 | */
|
|---|
| 708 | class Wmts extends Server {
|
|---|
| 709 |
|
|---|
| 710 | /**
|
|---|
| 711 | * @param array $params
|
|---|
| 712 | */
|
|---|
| 713 | public $layer;
|
|---|
| 714 |
|
|---|
| 715 | /**
|
|---|
| 716 | * @var integer
|
|---|
| 717 | */
|
|---|
| 718 | public $z;
|
|---|
| 719 |
|
|---|
| 720 | /**
|
|---|
| 721 | * @var integer
|
|---|
| 722 | */
|
|---|
| 723 | public $y;
|
|---|
| 724 |
|
|---|
| 725 | /**
|
|---|
| 726 | * @var integer
|
|---|
| 727 | */
|
|---|
| 728 | public $x;
|
|---|
| 729 |
|
|---|
| 730 | /**
|
|---|
| 731 | * @var string
|
|---|
| 732 | */
|
|---|
| 733 | public $ext;
|
|---|
| 734 |
|
|---|
| 735 | /**
|
|---|
| 736 | *
|
|---|
| 737 | * @param array $params
|
|---|
| 738 | */
|
|---|
| 739 | public function __construct($params) {
|
|---|
| 740 | parent::__construct();
|
|---|
| 741 | if (isset($params)) {
|
|---|
| 742 | parent::setParams($params);
|
|---|
| 743 | }
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | /**
|
|---|
| 747 | * Tests request from url and call method
|
|---|
| 748 | */
|
|---|
| 749 | public function get() {
|
|---|
| 750 | $request = $this->getGlobal('Request');
|
|---|
| 751 | if ($request !== FALSE && $request == 'gettile') {
|
|---|
| 752 | $this->getTile();
|
|---|
| 753 | } else {
|
|---|
| 754 | parent::setDatasets();
|
|---|
| 755 | $this->getCapabilities();
|
|---|
| 756 | }
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | /**
|
|---|
| 760 | * Validates tilematrixset, calculates missing params
|
|---|
| 761 | * @param Object $tileMatrix
|
|---|
| 762 | * @return Object
|
|---|
| 763 | */
|
|---|
| 764 | public function parseTileMatrix($layer, $tileMatrix){
|
|---|
| 765 |
|
|---|
| 766 | //process projection
|
|---|
| 767 | if(isset($layer['proj4'])){
|
|---|
| 768 | preg_match_all("/([^+= ]+)=([^= ]+)/", $layer['proj4'], $res);
|
|---|
| 769 | $proj4 = array_combine($res[1], $res[2]);
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | for($i = 0; $i < count($tileMatrix); $i++){
|
|---|
| 773 |
|
|---|
| 774 | if(!isset($tileMatrix[$i]['id'])){
|
|---|
| 775 | $tileMatrix[$i]['id'] = (string) $i;
|
|---|
| 776 | }
|
|---|
| 777 | if (!isset($tileMatrix[$i]['extent']) && isset($layer['extent'])) {
|
|---|
| 778 | $tileMatrix[$i]['extent'] = $layer['extent'];
|
|---|
| 779 | }
|
|---|
| 780 | if (!isset($tileMatrix[$i]['matrix_size'])) {
|
|---|
| 781 | $tileExtent = $this->tilesOfExtent(
|
|---|
| 782 | $tileMatrix[$i]['extent'],
|
|---|
| 783 | $tileMatrix[$i]['origin'],
|
|---|
| 784 | $tileMatrix[$i]['pixel_size'],
|
|---|
| 785 | $tileMatrix[$i]['tile_size']
|
|---|
| 786 | );
|
|---|
| 787 | $tileMatrix[$i]['matrix_size'] = array(
|
|---|
| 788 | $tileExtent[2] + 1,
|
|---|
| 789 | $tileExtent[1] + 1
|
|---|
| 790 | );
|
|---|
| 791 | }
|
|---|
| 792 | if(!isset($tileMatrix[$i]['origin']) && isset($tileMatrix[$i]['extent'])){
|
|---|
| 793 | $tileMatrix[$i]['origin'] = array(
|
|---|
| 794 | $tileMatrix[$i]['extent'][0], $tileMatrix[$i]['extent'][3]
|
|---|
| 795 | );
|
|---|
| 796 | }
|
|---|
| 797 | // Origins of geographic coordinate systems are setting in opposite order
|
|---|
| 798 | if (isset($proj4) && $proj4['proj'] === 'longlat') {
|
|---|
| 799 | $tileMatrix[$i]['origin'] = array_reverse($tileMatrix[$i]['origin']);
|
|---|
| 800 | }
|
|---|
| 801 | if(!isset($tileMatrix[$i]['scale_denominator'])){
|
|---|
| 802 | $tileMatrix[$i]['scale_denominator'] = count($tileMatrix) - $i;
|
|---|
| 803 | }
|
|---|
| 804 | if(!isset($tileMatrix[$i]['tile_size'])){
|
|---|
| 805 | $tileSize = 256 * (int) $layer['scale'];
|
|---|
| 806 | $tileMatrix[$i]['tile_size'] = array($tileSize, $tileSize);
|
|---|
| 807 | }
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | return $tileMatrix;
|
|---|
| 811 | }
|
|---|
| 812 |
|
|---|
| 813 | /**
|
|---|
| 814 | * Calculates corners of tilematrix
|
|---|
| 815 | * @param array $extent
|
|---|
| 816 | * @param array $origin
|
|---|
| 817 | * @param array $pixel_size
|
|---|
| 818 | * @param array $tile_size
|
|---|
| 819 | * @return array
|
|---|
| 820 | */
|
|---|
| 821 | public function tilesOfExtent($extent, $origin, $pixel_size, $tile_size) {
|
|---|
| 822 | $tiles = array(
|
|---|
| 823 | $this->minsample($extent[0] - $origin[0], $pixel_size[0] * $tile_size[0]),
|
|---|
| 824 | $this->minsample($extent[1] - $origin[1], $pixel_size[1] * $tile_size[1]),
|
|---|
| 825 | $this->maxsample($extent[2] - $origin[0], $pixel_size[0] * $tile_size[0]),
|
|---|
| 826 | $this->maxsample($extent[3] - $origin[1], $pixel_size[1] * $tile_size[1]),
|
|---|
| 827 | );
|
|---|
| 828 | return $tiles;
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | private function minsample($x, $f){
|
|---|
| 832 | return $f > 0 ? floor($x / $f) : ceil(($x / $f) - 1);
|
|---|
| 833 | }
|
|---|
| 834 |
|
|---|
| 835 | private function maxsample($x, $f){
|
|---|
| 836 | return $f < 0 ? floor($x / $f) : ceil(($x / $f) - 1);
|
|---|
| 837 | }
|
|---|
| 838 |
|
|---|
| 839 | /**
|
|---|
| 840 | * Default TileMetrixSet for Pseudo Mercator projection 3857
|
|---|
| 841 | * @param ?number $maxZoom
|
|---|
| 842 | * @return string TileMatrixSet xml
|
|---|
| 843 | */
|
|---|
| 844 | public function getMercatorTileMatrixSet($maxZoom = 18){
|
|---|
| 845 | $denominatorBase = 559082264.0287178;
|
|---|
| 846 | $extent = array(-20037508.34,-20037508.34,20037508.34,20037508.34);
|
|---|
| 847 | $tileMatrixSet = array();
|
|---|
| 848 |
|
|---|
| 849 | for($i = 0; $i <= $maxZoom; $i++){
|
|---|
| 850 | $matrixSize = pow(2, $i);
|
|---|
| 851 | $tileMatrixSet[] = array(
|
|---|
| 852 | 'extent' => $extent,
|
|---|
| 853 | 'id' => (string) $i,
|
|---|
| 854 | 'matrix_size' => array($matrixSize, $matrixSize),
|
|---|
| 855 | 'origin' => array($extent[0], $extent[3]),
|
|---|
| 856 | 'scale_denominator' => $denominatorBase / pow(2, $i),
|
|---|
| 857 | 'tile_size' => array(256, 256)
|
|---|
| 858 | );
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | return $this->getTileMatrixSet('GoogleMapsCompatible', $tileMatrixSet, 'EPSG:3857');
|
|---|
| 862 | }
|
|---|
| 863 |
|
|---|
| 864 | /**
|
|---|
| 865 | * Default TileMetrixSet for WGS84 projection 4326
|
|---|
| 866 | * @return string Xml
|
|---|
| 867 | */
|
|---|
| 868 | public function getWGS84TileMatrixSet(){
|
|---|
| 869 | $extent = array(-180.000000, -90.000000, 180.000000, 90.000000);
|
|---|
| 870 | $scaleDenominators = array(279541132.01435887813568115234, 139770566.00717943906784057617,
|
|---|
| 871 | 69885283.00358971953392028809, 34942641.50179485976696014404, 17471320.75089742988348007202,
|
|---|
| 872 | 8735660.37544871494174003601, 4367830.18772435747087001801, 2183915.09386217873543500900,
|
|---|
| 873 | 1091957.54693108936771750450, 545978.77346554468385875225, 272989.38673277234192937613,
|
|---|
| 874 | 136494.69336638617096468806, 68247.34668319308548234403, 34123.67334159654274117202,
|
|---|
| 875 | 17061.83667079825318069197, 8530.91833539912659034599, 4265.45916769956329517299,
|
|---|
| 876 | 2132.72958384978574031265);
|
|---|
| 877 | $tileMatrixSet = array();
|
|---|
| 878 |
|
|---|
| 879 | for($i = 0; $i <= 17; $i++){
|
|---|
| 880 | $matrixSize = pow(2, $i);
|
|---|
| 881 | $tileMatrixSet[] = array(
|
|---|
| 882 | 'extent' => $extent,
|
|---|
| 883 | 'id' => (string) $i,
|
|---|
| 884 | 'matrix_size' => array($matrixSize * 2, $matrixSize),
|
|---|
| 885 | 'origin' => array($extent[3], $extent[0]),
|
|---|
| 886 | 'scale_denominator' => $scaleDenominators[$i],
|
|---|
| 887 | 'tile_size' => array(256, 256)
|
|---|
| 888 | );
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | return $this->getTileMatrixSet('WGS84', $tileMatrixSet, 'EPSG:4326');
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | /**
|
|---|
| 895 | * Prints WMTS TileMatrixSet
|
|---|
| 896 | * @param string $name
|
|---|
| 897 | * @param array $tileMatrixSet Array of levels
|
|---|
| 898 | * @param string $crs Code of crs eg: EPSG:3857
|
|---|
| 899 | * @return string TileMatrixSet xml
|
|---|
| 900 | */
|
|---|
| 901 | public function getTileMatrixSet($name, $tileMatrixSet, $crs = 'EPSG:3857'){
|
|---|
| 902 | $srs = explode(':', $crs);
|
|---|
| 903 | $TileMatrixSet = '<TileMatrixSet>
|
|---|
| 904 | <ows:Title>' . $name . '</ows:Title>
|
|---|
| 905 | <ows:Abstract>' . $name . ' '. $crs .'</ows:Abstract>
|
|---|
| 906 | <ows:Identifier>' . $name . '</ows:Identifier>
|
|---|
| 907 | <ows:SupportedCRS>urn:ogc:def:crs:'.$srs[0].'::'.$srs[1].'</ows:SupportedCRS>';
|
|---|
| 908 | // <WellKnownScaleSet>urn:ogc:def:wkss:OGC:1.0:GoogleMapsCompatible</WellKnownScaleSet>;
|
|---|
| 909 | foreach($tileMatrixSet as $level){
|
|---|
| 910 | $TileMatrixSet .= '
|
|---|
| 911 | <TileMatrix>
|
|---|
| 912 | <ows:Identifier>' . $level['id'] . '</ows:Identifier>
|
|---|
| 913 | <ScaleDenominator>' . $level['scale_denominator'] . '</ScaleDenominator>
|
|---|
| 914 | <TopLeftCorner>'. $level['origin'][0] . ' ' . $level['origin'][1] .'</TopLeftCorner>
|
|---|
| 915 | <TileWidth>' . $level['tile_size'][0] . '</TileWidth>
|
|---|
| 916 | <TileHeight>' . $level['tile_size'][1] . '</TileHeight>
|
|---|
| 917 | <MatrixWidth>' . $level['matrix_size'][0] . '</MatrixWidth>
|
|---|
| 918 | <MatrixHeight>' . $level['matrix_size'][1] . '</MatrixHeight>
|
|---|
| 919 | </TileMatrix>';
|
|---|
| 920 | }
|
|---|
| 921 | $TileMatrixSet .= '</TileMatrixSet>';
|
|---|
| 922 |
|
|---|
| 923 | return $TileMatrixSet;
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 | /**
|
|---|
| 927 | * Returns tilesets getCapabilities
|
|---|
| 928 | */
|
|---|
| 929 | public function getCapabilities() {
|
|---|
| 930 |
|
|---|
| 931 | $layers = array_merge($this->fileLayer, $this->dbLayer);
|
|---|
| 932 |
|
|---|
| 933 | //if TileMatrixSet is provided validate it
|
|---|
| 934 | for($i = 0; $i < count($layers); $i++){
|
|---|
| 935 | if($layers[$i]['profile'] == 'custom'){
|
|---|
| 936 | $layers[$i]['tile_matrix'] = $this->parseTileMatrix(
|
|---|
| 937 | $layers[$i],
|
|---|
| 938 | $layers[$i]['tile_matrix']
|
|---|
| 939 | );
|
|---|
| 940 | }
|
|---|
| 941 | }
|
|---|
| 942 |
|
|---|
| 943 | header('Content-type: application/xml');
|
|---|
| 944 | echo '<?xml version="1.0" encoding="UTF-8" ?>
|
|---|
| 945 | <Capabilities xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd" version="1.0.0">
|
|---|
| 946 | <!-- Service Identification -->
|
|---|
| 947 | <ows:ServiceIdentification>
|
|---|
| 948 | <ows:Title>tileserverphp</ows:Title>
|
|---|
| 949 | <ows:ServiceType>OGC WMTS</ows:ServiceType>
|
|---|
| 950 | <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
|
|---|
| 951 | </ows:ServiceIdentification>
|
|---|
| 952 | <!-- Operations Metadata -->
|
|---|
| 953 | <ows:OperationsMetadata>
|
|---|
| 954 | <ows:Operation name="GetCapabilities">
|
|---|
| 955 | <ows:DCP>
|
|---|
| 956 | <ows:HTTP>
|
|---|
| 957 | <ows:Get xlink:href="' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/wmts/1.0.0/WMTSCapabilities.xml">
|
|---|
| 958 | <ows:Constraint name="GetEncoding">
|
|---|
| 959 | <ows:AllowedValues>
|
|---|
| 960 | <ows:Value>RESTful</ows:Value>
|
|---|
| 961 | </ows:AllowedValues>
|
|---|
| 962 | </ows:Constraint>
|
|---|
| 963 | </ows:Get>
|
|---|
| 964 | <!-- add KVP binding in 10.1 -->
|
|---|
| 965 | <ows:Get xlink:href="' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/wmts?">
|
|---|
| 966 | <ows:Constraint name="GetEncoding">
|
|---|
| 967 | <ows:AllowedValues>
|
|---|
| 968 | <ows:Value>KVP</ows:Value>
|
|---|
| 969 | </ows:AllowedValues>
|
|---|
| 970 | </ows:Constraint>
|
|---|
| 971 | </ows:Get>
|
|---|
| 972 | </ows:HTTP>
|
|---|
| 973 | </ows:DCP>
|
|---|
| 974 | </ows:Operation>
|
|---|
| 975 | <ows:Operation name="GetTile">
|
|---|
| 976 | <ows:DCP>
|
|---|
| 977 | <ows:HTTP>
|
|---|
| 978 | <ows:Get xlink:href="' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/wmts/">
|
|---|
| 979 | <ows:Constraint name="GetEncoding">
|
|---|
| 980 | <ows:AllowedValues>
|
|---|
| 981 | <ows:Value>RESTful</ows:Value>
|
|---|
| 982 | </ows:AllowedValues>
|
|---|
| 983 | </ows:Constraint>
|
|---|
| 984 | </ows:Get>
|
|---|
| 985 | <ows:Get xlink:href="' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/wmts?">
|
|---|
| 986 | <ows:Constraint name="GetEncoding">
|
|---|
| 987 | <ows:AllowedValues>
|
|---|
| 988 | <ows:Value>KVP</ows:Value>
|
|---|
| 989 | </ows:AllowedValues>
|
|---|
| 990 | </ows:Constraint>
|
|---|
| 991 | </ows:Get>
|
|---|
| 992 | </ows:HTTP>
|
|---|
| 993 | </ows:DCP>
|
|---|
| 994 | </ows:Operation>
|
|---|
| 995 | </ows:OperationsMetadata>
|
|---|
| 996 | <Contents>';
|
|---|
| 997 |
|
|---|
| 998 | $customtileMatrixSets = '';
|
|---|
| 999 | $maxMercatorZoom = 18;
|
|---|
| 1000 |
|
|---|
| 1001 | //layers
|
|---|
| 1002 | foreach ($layers as $m) {
|
|---|
| 1003 |
|
|---|
| 1004 | $basename = $m['basename'];
|
|---|
| 1005 | $title = (array_key_exists('name', $m)) ? $m['name'] : $basename;
|
|---|
| 1006 | $profile = $m['profile'];
|
|---|
| 1007 | $bounds = $m['bounds'];
|
|---|
| 1008 | $format = $m['format'] == 'hybrid' ? 'jpgpng' : $m['format'];
|
|---|
| 1009 | $mime = ($format == 'jpg') ? 'image/jpeg' : 'image/' . $format;
|
|---|
| 1010 |
|
|---|
| 1011 | if ($profile == 'geodetic') {
|
|---|
| 1012 | $tileMatrixSet = 'WGS84';
|
|---|
| 1013 | }elseif ($m['profile'] == 'custom') {
|
|---|
| 1014 | $crs = explode(':', $m['crs']);
|
|---|
| 1015 | $tileMatrixSet = 'custom' . $crs[1] . $m['basename'];
|
|---|
| 1016 | $customtileMatrixSets .= $this->getTileMatrixSet(
|
|---|
| 1017 | $tileMatrixSet,
|
|---|
| 1018 | $m['tile_matrix'],
|
|---|
| 1019 | $m['crs']
|
|---|
| 1020 | );
|
|---|
| 1021 | } else {
|
|---|
| 1022 | $tileMatrixSet = 'GoogleMapsCompatible';
|
|---|
| 1023 | $maxMercatorZoom = max($maxMercatorZoom, $m['maxzoom']);
|
|---|
| 1024 | }
|
|---|
| 1025 |
|
|---|
| 1026 | $wmtsHost = substr($m['tiles'][0], 0, strpos($m['tiles'][0], $m['basename']));
|
|---|
| 1027 | $resourceUrlTemplate = $wmtsHost . $basename
|
|---|
| 1028 | . '/{TileMatrix}/{TileCol}/{TileRow}';
|
|---|
| 1029 | if(strlen($format) <= 4){
|
|---|
| 1030 | $resourceUrlTemplate .= '.' . $format;
|
|---|
| 1031 | }
|
|---|
| 1032 |
|
|---|
| 1033 | echo'
|
|---|
| 1034 | <Layer>
|
|---|
| 1035 | <ows:Title>' . $title . '</ows:Title>
|
|---|
| 1036 | <ows:Identifier>' . $basename . '</ows:Identifier>
|
|---|
| 1037 | <ows:WGS84BoundingBox crs="urn:ogc:def:crs:OGC:2:84">
|
|---|
| 1038 | <ows:LowerCorner>' . $bounds[0] . ' ' . $bounds[1] . '</ows:LowerCorner>
|
|---|
| 1039 | <ows:UpperCorner>' . $bounds[2] . ' ' . $bounds[3] . '</ows:UpperCorner>
|
|---|
| 1040 | </ows:WGS84BoundingBox>
|
|---|
| 1041 | <Style isDefault="true">
|
|---|
| 1042 | <ows:Identifier>default</ows:Identifier>
|
|---|
| 1043 | </Style>
|
|---|
| 1044 | <Format>' . $mime . '</Format>
|
|---|
| 1045 | <TileMatrixSetLink>
|
|---|
| 1046 | <TileMatrixSet>' . $tileMatrixSet . '</TileMatrixSet>
|
|---|
| 1047 | </TileMatrixSetLink>
|
|---|
| 1048 | <ResourceURL format="' . $mime . '" resourceType="tile" template="' . $resourceUrlTemplate . '"/>
|
|---|
| 1049 | </Layer>';
|
|---|
| 1050 | }
|
|---|
| 1051 |
|
|---|
| 1052 | // Print custom TileMatrixSets
|
|---|
| 1053 | if (strlen($customtileMatrixSets) > 0) {
|
|---|
| 1054 | echo $customtileMatrixSets;
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | // Print PseudoMercator TileMatrixSet
|
|---|
| 1058 | echo $this->getMercatorTileMatrixSet($maxMercatorZoom);
|
|---|
| 1059 |
|
|---|
| 1060 | // Print WGS84 TileMatrixSet
|
|---|
| 1061 | echo $this->getWGS84TileMatrixSet();
|
|---|
| 1062 |
|
|---|
| 1063 | echo '</Contents>
|
|---|
| 1064 | <ServiceMetadataURL xlink:href="' . $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/wmts/1.0.0/WMTSCapabilities.xml"/>
|
|---|
| 1065 | </Capabilities>';
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | /**
|
|---|
| 1069 | * Returns tile via WMTS specification
|
|---|
| 1070 | */
|
|---|
| 1071 | public function getTile() {
|
|---|
| 1072 | $request = $this->getGlobal('Request');
|
|---|
| 1073 | if ($request) {
|
|---|
| 1074 | if (strpos('/', $_GET['Format']) !== FALSE) {
|
|---|
| 1075 | $format = explode('/', $_GET['Format']);
|
|---|
| 1076 | $format = $format[1];
|
|---|
| 1077 | } else {
|
|---|
| 1078 | $format = $this->getGlobal('Format');
|
|---|
| 1079 | }
|
|---|
| 1080 | parent::renderTile(
|
|---|
| 1081 | $this->getGlobal('Layer'),
|
|---|
| 1082 | $this->getGlobal('TileMatrix'),
|
|---|
| 1083 | $this->getGlobal('TileRow'),
|
|---|
| 1084 | $this->getGlobal('TileCol'),
|
|---|
| 1085 | $format
|
|---|
| 1086 | );
|
|---|
| 1087 | } else {
|
|---|
| 1088 | parent::renderTile($this->layer, $this->z, $this->y, $this->x, $this->ext);
|
|---|
| 1089 | }
|
|---|
| 1090 | }
|
|---|
| 1091 |
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| 1094 | /**
|
|---|
| 1095 | * Tile map service
|
|---|
| 1096 | */
|
|---|
| 1097 | class Tms extends Server {
|
|---|
| 1098 |
|
|---|
| 1099 | /**
|
|---|
| 1100 | * @param array $params
|
|---|
| 1101 | */
|
|---|
| 1102 | public $layer;
|
|---|
| 1103 |
|
|---|
| 1104 | /**
|
|---|
| 1105 | * @var integer
|
|---|
| 1106 | */
|
|---|
| 1107 | public $z;
|
|---|
| 1108 |
|
|---|
| 1109 | /**
|
|---|
| 1110 | * @var integer
|
|---|
| 1111 | */
|
|---|
| 1112 | public $y;
|
|---|
| 1113 |
|
|---|
| 1114 | /**
|
|---|
| 1115 | * @var integer
|
|---|
| 1116 | */
|
|---|
| 1117 | public $x;
|
|---|
| 1118 |
|
|---|
| 1119 | /**
|
|---|
| 1120 | * @var string
|
|---|
| 1121 | */
|
|---|
| 1122 | public $ext;
|
|---|
| 1123 |
|
|---|
| 1124 | /**
|
|---|
| 1125 | *
|
|---|
| 1126 | * @param array $params
|
|---|
| 1127 | */
|
|---|
| 1128 | public function __construct($params) {
|
|---|
| 1129 | parent::__construct();
|
|---|
| 1130 | parent::setParams($params);
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | /**
|
|---|
| 1134 | * Returns getCapabilities metadata request
|
|---|
| 1135 | */
|
|---|
| 1136 | public function getCapabilities() {
|
|---|
| 1137 | parent::setDatasets();
|
|---|
| 1138 | $maps = array_merge($this->fileLayer, $this->dbLayer);
|
|---|
| 1139 | header('Content-type: application/xml');
|
|---|
| 1140 | echo'<TileMapService version="1.0.0"><TileMaps>';
|
|---|
| 1141 | foreach ($maps as $m) {
|
|---|
| 1142 | $basename = $m['basename'];
|
|---|
| 1143 | $title = (array_key_exists('name', $m) ) ? $m['name'] : $basename;
|
|---|
| 1144 | $profile = $m['profile'];
|
|---|
| 1145 | if ($profile == 'geodetic') {
|
|---|
| 1146 | $srs = 'EPSG:4326';
|
|---|
| 1147 | } else {
|
|---|
| 1148 | $srs = 'EPSG:3857';
|
|---|
| 1149 | }
|
|---|
| 1150 | $url = $this->config['protocol'] . '://' . $this->config['baseUrls'][0]
|
|---|
| 1151 | . '/tms/' . $basename;
|
|---|
| 1152 | echo '<TileMap title="' . $title . '" srs="' . $srs
|
|---|
| 1153 | . '" type="InvertedTMS" ' . 'profile="global-' . $profile
|
|---|
| 1154 | . '" href="' . $url . '" />';
|
|---|
| 1155 | }
|
|---|
| 1156 | echo '</TileMaps></TileMapService>';
|
|---|
| 1157 | }
|
|---|
| 1158 |
|
|---|
| 1159 | /**
|
|---|
| 1160 | * Prints metadata about layer
|
|---|
| 1161 | */
|
|---|
| 1162 | public function getLayerCapabilities() {
|
|---|
| 1163 | parent::setDatasets();
|
|---|
| 1164 | $maps = array_merge($this->fileLayer, $this->dbLayer);
|
|---|
| 1165 | foreach ($maps as $map) {
|
|---|
| 1166 | if (strpos($map['basename'], $this->layer) !== false) {
|
|---|
| 1167 | $m = $map;
|
|---|
| 1168 | break;
|
|---|
| 1169 | }
|
|---|
| 1170 | }
|
|---|
| 1171 | $title = (array_key_exists('name', $m)) ? $m['name'] : $m['basename'];
|
|---|
| 1172 | $description = (array_key_exists('description', $m)) ? $m['description'] : "";
|
|---|
| 1173 | $bounds = $m['bounds'];
|
|---|
| 1174 | if ($m['profile'] == 'geodetic') {
|
|---|
| 1175 | $srs = 'EPSG:4326';
|
|---|
| 1176 | $initRes = 0.703125;
|
|---|
| 1177 | } elseif ($m['profile'] == 'custom') {
|
|---|
| 1178 | $srs = $m['crs'];
|
|---|
| 1179 | $bounds = $m['extent'];
|
|---|
| 1180 | if(isset($m['tile_matrix'][0]['pixel_size'][0])){
|
|---|
| 1181 | $initRes = $m['tile_matrix'][0]['pixel_size'][0];
|
|---|
| 1182 | }else{
|
|---|
| 1183 | $initRes = 1;
|
|---|
| 1184 | }
|
|---|
| 1185 | } else {
|
|---|
| 1186 | $srs = 'EPSG:3857';
|
|---|
| 1187 | $bounds = array(-20037508.34,-20037508.34,20037508.34,20037508.34);
|
|---|
| 1188 | $initRes = 156543.03392804062;
|
|---|
| 1189 | }
|
|---|
| 1190 | $mime = ($m['format'] == 'jpg') ? 'image/jpeg' : 'image/png';
|
|---|
| 1191 | header("Content-type: application/xml");
|
|---|
| 1192 | $serviceUrl = $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/' . $m['basename'];
|
|---|
| 1193 | echo '<TileMap version="1.0.0" tilemapservice="' . $serviceUrl . '" type="InvertedTMS">
|
|---|
| 1194 | <Title>' . htmlspecialchars($title) . '</Title>
|
|---|
| 1195 | <Abstract>' . htmlspecialchars($description) . '</Abstract>
|
|---|
| 1196 | <SRS>' . $srs . '</SRS>
|
|---|
| 1197 | <BoundingBox minx="' . $bounds[0] . '" miny="' . $bounds[1] . '" maxx="' . $bounds[2] . '" maxy="' . $bounds[3] . '" />
|
|---|
| 1198 | <Origin x="' . $bounds[0] . '" y="' . $bounds[1] . '"/>
|
|---|
| 1199 | <TileFormat width="256" height="256" mime-type="' . $mime . '" extension="' . $m['format'] . '"/>
|
|---|
| 1200 | <TileSets profile="global-' . $m['profile'] . '">';
|
|---|
| 1201 | for ($zoom = $m['minzoom']; $zoom < $m['maxzoom'] + 1; $zoom++) {
|
|---|
| 1202 | $res = $initRes / pow(2, $zoom);
|
|---|
| 1203 | $url = $this->config['protocol'] . '://' . $this->config['baseUrls'][0] . '/' . $m['basename'] . '/' . $zoom;
|
|---|
| 1204 | echo '<TileSet href="' . $url . '" units-per-pixel="' . $res . '" order="' . $zoom . '" />';
|
|---|
| 1205 | }
|
|---|
| 1206 | echo'</TileSets></TileMap>';
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | /**
|
|---|
| 1210 | * Process getTile request
|
|---|
| 1211 | */
|
|---|
| 1212 | public function getTile() {
|
|---|
| 1213 | parent::renderTile($this->layer, $this->z, $this->y, $this->x, $this->ext);
|
|---|
| 1214 | }
|
|---|
| 1215 | }
|
|---|
| 1216 |
|
|---|
| 1217 | /**
|
|---|
| 1218 | * Simple router
|
|---|
| 1219 | */
|
|---|
| 1220 | class Router {
|
|---|
| 1221 |
|
|---|
| 1222 | /**
|
|---|
| 1223 | * @param array $routes
|
|---|
| 1224 | */
|
|---|
| 1225 | public static function serve($routes) {
|
|---|
| 1226 | $request_method = strtolower($_SERVER['REQUEST_METHOD']);
|
|---|
| 1227 | $path_info = '/';
|
|---|
| 1228 | global $config;
|
|---|
| 1229 | $config['protocol'] = ( isset($_SERVER["HTTPS"]) or $_SERVER['SERVER_PORT'] == '443') ? "https" : "http";
|
|---|
| 1230 | if (!empty($_SERVER['PATH_INFO'])) {
|
|---|
| 1231 | $path_info = $_SERVER['PATH_INFO'];
|
|---|
| 1232 | } else if (!empty($_SERVER['ORIG_PATH_INFO']) && strpos($_SERVER['ORIG_PATH_INFO'], 'tileserver.php') === false) {
|
|---|
| 1233 | $path_info = $_SERVER['ORIG_PATH_INFO'];
|
|---|
| 1234 | } else if (!empty($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/tileserver.php') !== false) {
|
|---|
| 1235 | $path_info = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|---|
| 1236 | $config['baseUrls'][0] = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '?';
|
|---|
| 1237 | } else {
|
|---|
| 1238 | if (!empty($_SERVER['REQUEST_URI'])) {
|
|---|
| 1239 | $path_info = (strpos($_SERVER['REQUEST_URI'], '?') > 0) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'];
|
|---|
| 1240 | }
|
|---|
| 1241 | }
|
|---|
| 1242 | $discovered_handler = null;
|
|---|
| 1243 | $regex_matches = array();
|
|---|
| 1244 |
|
|---|
| 1245 | if ($routes) {
|
|---|
| 1246 | $tokens = array(
|
|---|
| 1247 | ':string' => '([a-zA-Z]+)',
|
|---|
| 1248 | ':number' => '([0-9]+)',
|
|---|
| 1249 | ':alpha' => '([a-zA-Z0-9-_@\.]+)'
|
|---|
| 1250 | );
|
|---|
| 1251 | //global $config;
|
|---|
| 1252 | foreach ($routes as $pattern => $handler_name) {
|
|---|
| 1253 | $pattern = strtr($pattern, $tokens);
|
|---|
| 1254 | if (preg_match('#/?' . $pattern . '/?$#', $path_info, $matches)) {
|
|---|
| 1255 | if (!isset($config['baseUrls'])) {
|
|---|
| 1256 | $config['baseUrls'][0] = $_SERVER['HTTP_HOST'] . preg_replace('#/?' . $pattern . '/?$#', '', $path_info);
|
|---|
| 1257 | }
|
|---|
| 1258 | $discovered_handler = $handler_name;
|
|---|
| 1259 | $regex_matches = $matches;
|
|---|
| 1260 | break;
|
|---|
| 1261 | }
|
|---|
| 1262 | }
|
|---|
| 1263 | }
|
|---|
| 1264 | $handler_instance = null;
|
|---|
| 1265 | if ($discovered_handler) {
|
|---|
| 1266 | if (is_string($discovered_handler)) {
|
|---|
| 1267 | if (strpos($discovered_handler, ':') !== false) {
|
|---|
| 1268 | $discoverered_class = explode(':', $discovered_handler);
|
|---|
| 1269 | $discoverered_method = explode(':', $discovered_handler);
|
|---|
| 1270 | $handler_instance = new $discoverered_class[0]($regex_matches);
|
|---|
| 1271 | call_user_func(array($handler_instance, $discoverered_method[1]));
|
|---|
| 1272 | } else {
|
|---|
| 1273 | $handler_instance = new $discovered_handler($regex_matches);
|
|---|
| 1274 | }
|
|---|
| 1275 | } elseif (is_callable($discovered_handler)) {
|
|---|
| 1276 | $handler_instance = $discovered_handler();
|
|---|
| 1277 | }
|
|---|
| 1278 | } else {
|
|---|
| 1279 | if (!isset($config['baseUrls'][0])) {
|
|---|
| 1280 | $config['baseUrls'][0] = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
|---|
| 1281 | }
|
|---|
| 1282 | if (strpos($_SERVER['REQUEST_URI'], '=') != FALSE) {
|
|---|
| 1283 | $kvp = explode('=', $_SERVER['REQUEST_URI']);
|
|---|
| 1284 | $_GET['callback'] = $kvp[1];
|
|---|
| 1285 | $params[0] = 'index';
|
|---|
| 1286 | $handler_instance = new Json($params);
|
|---|
| 1287 | $handler_instance->getJson();
|
|---|
| 1288 | }
|
|---|
| 1289 | $handler_instance = new Server;
|
|---|
| 1290 | $handler_instance->getHtml();
|
|---|
| 1291 | }
|
|---|
| 1292 | }
|
|---|
| 1293 |
|
|---|
| 1294 | }
|
|---|