'Server:getHtml', '/test' => 'Server:getInfo', '/html' => 'Server:getHtml', '/:string.json' => 'Json:getJson', '/:string.jsonp' => 'Json:getJsonp', '/:string/:number/:number/:number.grid.json' => 'Json:getUTFGrid', '/wmts' => 'Wmts:get', '/wmts/1.0.0/WMTSCapabilities.xml' => 'Wmts:getCapabilities', '/:string/:number/:number/:number.:string' => 'Wmts:getTile', '/tms' => 'Tms:getCapabilities', '/tms/:string' => 'Tms:getLayerCapabilities', )); /** * Server base */ class Server { /** * Configuration of TileServer [baseUrls, serverTitle, host] * @var array */ public $config; /** * Datasets stored in file structure * @var array */ public $fileLayer = array(); /** * Datasets stored in database * @var array */ public $dbLayer = array(); /** * PDO database connection * @var object */ private $db; /** sercer.com/ts.php * Set config */ public function __construct() { $this->config = $GLOBALS['config']; } /** * Looks for datasets */ public function setDatasets() { $mjs = glob('*/metadata.json'); $mbts = glob('*.mbtiles'); if ($mjs) { foreach ($mjs as $mj) { $layer = $this->metadataFromMetadataJson($mj); array_push($this->fileLayer, $layer); } } else { $e = 1; } if ($mbts) { foreach ($mbts as $mbt) { $this->dbLayer[] = $this->metadataFromMbtiles($mbt); } } else { $e = 1; } // if (isset($e)) { // echo 'Server: No JSON or MBtiles file with metadata'; // die; // } } /** * Processing params from router ////.ext * @param array $params */ public function setParams($params) { if (isset($params[1])) { $this->layer = $params[1]; } if (isset($params[2])) { $this->z = $params[2]; $this->x = $params[3]; $this->y = $params[4]; } if (isset($params[5])) { $this->ext = $params[5]; } } /** * Get variable don't independent on sensitivity * @param string $key * @return boolean */ public function getGlobal($isKey) { $get = $_GET; foreach ($get as $key => $value) { if(strtolower($isKey) == strtolower($key)){ return $value; } } return FALSE; } /** * Testing if is a database layer * @param string $layer * @return boolean */ public function isDBLayer($layer) { if(is_file($layer.'.mbtiles')){ return TRUE; } else { return FALSE; } } /** * Testing if is a file layer * @param string $layer * @return boolean */ public function isFileLayer($layer) { if(is_dir($layer)){ return TRUE; } else { return FALSE; } } /** * * @param string $jsonFileName * @return array */ public function metadataFromMetadataJson($jsonFileName) { $metadata = json_decode(file_get_contents($jsonFileName), true); $metadata = $this->metadataValidation($metadata); $metadata['basename'] = str_replace('/metadata.json', '', $jsonFileName); return $metadata; } /** * Loads metadata from MBtiles * @param string $mbt * @return object */ public function metadataFromMbtiles($mbt) { $metadata = array(); $this->DBconnect($mbt); $result = $this->db->query('select * from metadata'); $resultdata = $result->fetchAll(); foreach ($resultdata as $r) { $metadata[$r['name']] = $r['value']; } $metadata = $this->metadataValidation($metadata); $mbt = explode('.', $mbt); $metadata['basename'] = $mbt[0]; return $metadata; } /** * Valids metaJSON * @param object $metadata * @return object */ public function metadataValidation($metadata) { if (array_key_exists('bounds', $metadata)) { // TODO: Calculate bounds from tiles if bounds is missing - with GlobalMercator $metadata['bounds'] = array_map('floatval', explode(',', $metadata['bounds'])); } else { $metadata['bounds'] = array(-180, -85.051128779807, 180, 85.051128779807); } if (!array_key_exists('profile', $metadata)) { $metadata['profile'] = 'mercator'; } // TODO: detect format, minzoom, maxzoom, thumb // scandir() for directory / SQL for mbtiles if (array_key_exists('minzoom', $metadata)) $metadata['minzoom'] = intval($metadata['minzoom']); else $metadata['minzoom'] = 0; if (array_key_exists('maxzoom', $metadata)) $metadata['maxzoom'] = intval($metadata['maxzoom']); else $metadata['maxzoom'] = 18; if (!array_key_exists('format', $metadata)) { $metadata['format'] = 'png'; } /* if (!array_key_exists('thumb', $metadata )) { $metadata['profile'] = 'mercator'; } */ return $metadata; } /** * SQLite connection * @param string $tileset */ public function DBconnect($tileset) { try { $this->db = new PDO('sqlite:' . $tileset, '', '', array(PDO::ATTR_PERSISTENT => true)); } catch (Exception $exc) { echo $exc->getTraceAsString(); die; } if (!isset($this->db)) { header('Content-type: text/plain'); echo 'Incorrect tileset name: ' . $tileset; exit; } } /** * Check if file is modified and set Etag headers * @param string $filename * @return boolean */ public function isModified($filename) { $filename = $filename . '.mbtiles'; $lastModifiedTime = filemtime($filename); $eTag = md5_file($filename); header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastModifiedTime) . " GMT"); header("Etag:" . $eTag); if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModifiedTime || trim($_SERVER['HTTP_IF_NONE_MATCH']) == $eTag) { return TRUE; } else { return FALSE; } } /** * Returns tile of dataset * @param string $tileset * @param integer $z * @param integer $y * @param integer $x * @param string $ext */ public function getTile($tileset, $z, $y, $x, $ext) { if ($this->isDBLayer($tileset)) { if ($this->isModified($tileset) == TRUE) { header('HTTP/1.1 304 Not Modified'); } $this->DBconnect($tileset . '.mbtiles'); $z = floatval($z); $y = floatval($y); $x = floatval($x); $flip = true; if ($flip) { $y = pow(2, $z) - 1 - $y; } $result = $this->db->query('select tile_data as t from tiles where zoom_level=' . $z . ' and tile_column=' . $x . ' and tile_row=' . $y); $data = $result->fetchColumn(); if (!isset($data) || $data === FALSE) { $this->getCleanTile(); } else { $result = $this->db->query('select value from metadata where name="format"'); $resultdata = $result->fetchColumn(); $format = isset($resultdata) && $resultdata !== FALSE ? $resultdata : 'png'; if ($format == 'jpg') { $format = 'jpeg'; } header('Content-type: image/' . $format); echo $data; } } elseif ($this->isFileLayer($tileset)) { $name = './' . $tileset . '/' . $z . '/' . $y . '/' . $x . '.' . $ext; if ($fp = @fopen($name, 'rb')) { header('Content-Type: image/' . $ext); header('Content-Length: ' . filesize($name)); fpassthru($fp); die; } else { $this->getCleanTile(); } } else { echo 'Server: Unknown or not specified dataset'; die; } } /** * Returns clean tile */ public function getCleanTile() { $png = imagecreatetruecolor(256, 256); imagesavealpha($png, true); $trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127); imagefill($png, 0, 0, $trans_colour); header('Content-type: image/png'); imagepng($png); die; } /** * Returns tile's UTFGrid * @param string $tileset * @param integer $z * @param integer $y * @param integer $x */ public function getUTFGrid($tileset, $z, $y, $x) { if ($this->isDBLayer($tileset)) { if ($this->isModified($tileset) == TRUE) { header('HTTP/1.1 304 Not Modified'); } $this->DBconnect($tileset . '.mbtiles'); try { $result = $this->db->query('SELECT grid FROM grids WHERE tile_column = ' . $x . ' AND tile_row = ' . $y . ' AND zoom_level = ' . $z); $data = $result->fetchColumn(); if (!isset($data) || $data === FALSE) { header('Access-Control-Allow-Origin: *'); echo 'grid({});'; die; } else { $grid = gzuncompress($data); $grid = substr(trim($grid), 0, -1); //adds legend (data) to output $grid .= ',"data":{'; $result = $this->db->query('SELECT key_name as key, key_json as json FROM grid_data WHERE zoom_level=' . $z . ' and tile_column=' . $x . ' and tile_row=' . $y); while ($r = $result->fetch(PDO::FETCH_ASSOC)) { $grid .= '"' . $r['key'] . '":' . $r['json'] . ','; } $grid = rtrim($grid, ',') . '}}'; header('Access-Control-Allow-Origin: *'); if (isset($_GET['callback'])) { header("Content-Type:text/javascript charset=utf-8"); echo $_GET['callback'] . '(' . $grid . ');'; } else { header("Content-Type:text/javascript; charset=utf-8"); echo 'grid(' . $grid . ');'; } } } catch (PDOException $e) { header('Content-type: text/plain'); print 'Error querying the database: ' . $e->getMessage(); } } else { echo 'Server: no MBTiles tileset'; die; } } /** * Returns server info */ public function getInfo() { $this->setDatasets(); $maps = array_merge($this->fileLayer, $this->dbLayer); header('Content-Type: text/html;charset=UTF-8'); echo '' . $this->config['serverTitle'] . ''; foreach ($maps as $map) { $extend = '['; foreach ($map['bounds'] as $ext) { $extend = $extend . ' ' . $ext; } $extend = $extend . ' ]'; if (strpos($map['basename'], 'mbtiles') !== false) { echo '

Available MBtiles tileset: ' . $map['basename'] . '
'; } else { echo '

Available file tileset: ' . $map['basename'] . '
'; } echo 'Bounds: ' . $extend . '

'; } echo ''; } /** * Returns html viewer */ public function getHtml() { $this->setDatasets(); $maps = array_merge($this->fileLayer, $this->dbLayer); header('Content-Type: text/html;charset=UTF-8'); echo '' . $this->config['serverTitle'] . ''; echo '

Welcome to ' . $this->config['serverTitle'] . '

This server distributes maps to desktop, web, and mobile applications.

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.

'; if (!isset($maps)) { echo '

No maps available yet

Ready to go - just upload some maps into directory:' . getcwd() . '/ on this server.

Note: The maps can be a directory with tiles in XYZ format with metadata.json file.
You can easily convert existing geodata (GeoTIFF, ECW, MrSID, etc) to this tile structure with MapTiler Cluster or open-source projects such as GDAL2Tiles or MapTiler or simply upload any maps in MBTiles format made by TileMill. Helpful is also the mbutil tool. Serving directly from .mbtiles files is supported, but with decreased performance.

'; } else { echo '
    '; foreach ($maps as $map) { echo "
  • " . $map['name'] . '
  • '; } echo '
'; } echo ''; } } /** * JSON service */ class Json extends Server { /** * Callback for JSONP default grid * @var string */ private $callback = 'grid'; /** * @param array $params */ public $layer = 'index'; /** * @var integer */ public $z; /** * @var integer */ public $y; /** * @var integer */ public $x; /** * @var string */ public $ext; /** * * @param array $params */ public function __construct($params) { parent::__construct(); parent::setParams($params); if (isset($_GET['callback']) && !empty($_GET['callback'])) { $this->callback = $_GET['callback']; } } /** * Adds metadata about layer * @param array $metadata * @return array */ public function metadataTileJson($metadata) { $metadata['tilejson'] = '2.0.0'; $metadata['scheme'] = 'xyz'; $tiles = array(); foreach ($this->config['baseUrls'] as $url) { $tiles[] = 'http://' . $url . '/' . $metadata['basename'] . '/{z}/{x}/{y}.' . $metadata['format']; } $metadata['tiles'] = $tiles; return $metadata; } /** * Creates JSON from array * @param string $basename * @return string */ private function createJson($basename) { $maps = array_merge($this->fileLayer, $this->dbLayer); if ($basename == 'index') { $output = '['; foreach ($maps as $map) { $output = $output . json_encode($this->metadataTileJson($map)) . ','; } $output = substr_replace($output, ']', -1); } else { foreach ($maps as $map) { if (strpos($map['basename'], $basename) !== false) { $output = json_encode($this->metadataTileJson($map)); break; } } } if (!isset($output)) { echo 'TileServer: unknown map ' . $basename; die; } return $output; } /** * Returns JSON with callback */ public function getJson() { parent::setDatasets(); header('Access-Control-Allow-Origin: *'); header("Content-Type:application/javascript charset=utf-8"); if ($this->callback !== 'grid') { echo $this->callback . '(' . $this->createJson($this->layer) . ');'; } else { echo $this->createJson($this->layer); } } /** * Returns JSONP with callback */ public function getJsonp() { parent::setDatasets(); header('Access-Control-Allow-Origin: *'); header("Content-Type:text/javascript charset=utf-8"); echo $this->callback . '(' . $this->createJson($this->layer) . ');'; } /** * Returns UTFGrid in JSON format */ public function getUTFGrid() { parent::getUTFGrid($this->layer, $this->z, $this->y, $this->x); } } /** * Web map tile service */ class Wmts extends Server { /** * @param array $params */ public $layer; /** * @var integer */ public $z; /** * @var integer */ public $y; /** * @var integer */ public $x; /** * @var string */ public $ext; /** * * @param array $params */ public function __construct($params) { parent::__construct(); if (isset($params)) { parent::setParams($params); } } /** * Tests request from url and call method */ public function get(){ $request = $this->getGlobal('Request'); if($request !== FALSE && $request == 'gettile'){ $this->getTile(); }else{ parent::setDatasets(); $this->getCapabilities(); } } /** * Returns tilesets getCapabilities */ public function getCapabilities() { header("Content-type: application/xml"); echo ''; echo ' ' . $this->config['serverTitle'] . ' OGC WMTS 1.0.0 RESTful KVP RESTful KVP '; $maps = array_merge($this->fileLayer, $this->dbLayer); $mercator = new GlobalMercator(); foreach ($maps as $m) { if (strpos($m['basename'], '.') !== false) { $basename = explode('.', $m['basename']); } else { $basename = $m['basename']; } $title = (array_key_exists('name', $m)) ? $m['name'] : $basename; $profile = $m['profile']; $bounds = $m['bounds']; $format = $m['format']; $mime = ($format == 'jpg') ? 'image/jpeg' : 'image/png'; if ($profile == 'geodetic') { $tileMatrixSet = "WGS84"; } else { $tileMatrixSet = "GoogleMapsCompatible"; list( $minx, $miny ) = $mercator->LatLonToMeters($bounds[1], $bounds[0]); list( $maxx, $maxy ) = $mercator->LatLonToMeters($bounds[3], $bounds[2]); $bounds3857 = array($minx, $miny, $maxx, $maxy); } echo' ' . $title . ' ' . $basename . ''; echo ' ' . $bounds[0] . ' ' . $bounds[1] . ' ' . $bounds[2] . ' ' . $bounds[3] . ' ' . $mime . ' ' . $tileMatrixSet . ' '; } echo ' GoogleMapsCompatible the wellknown "GoogleMapsCompatible" tile matrix set defined by OGC WMTS specification GoogleMapsCompatible urn:ogc:def:crs:EPSG:6.18:3:3857 urn:ogc:def:wkss:OGC:1.0:GoogleMapsCompatible 0 559082264.0287178 -20037508.34278925 20037508.34278925 256 256 1 1 1 279541132.0143589 -20037508.34278925 20037508.34278925 256 256 2 2 2 139770566.0071794 -20037508.34278925 20037508.34278925 256 256 4 4 3 69885283.00358972 -20037508.34278925 20037508.34278925 256 256 8 8 4 34942641.50179486 -20037508.34278925 20037508.34278925 256 256 16 16 5 17471320.75089743 -20037508.34278925 20037508.34278925 256 256 32 32 6 8735660.375448715 -20037508.34278925 20037508.34278925 256 256 64 64 7 4367830.187724357 -20037508.34278925 20037508.34278925 256 256 128 128 8 2183915.093862179 -20037508.34278925 20037508.34278925 256 256 256 256 9 1091957.546931089 -20037508.34278925 20037508.34278925 256 256 512 512 10 545978.7734655447 -20037508.34278925 20037508.34278925 256 256 1024 1024 11 272989.3867327723 -20037508.34278925 20037508.34278925 256 256 2048 2048 12 136494.6933663862 -20037508.34278925 20037508.34278925 256 256 4096 4096 13 68247.34668319309 -20037508.34278925 20037508.34278925 256 256 8192 8192 14 34123.67334159654 -20037508.34278925 20037508.34278925 256 256 16384 16384 15 17061.83667079827 -20037508.34278925 20037508.34278925 256 256 32768 32768 16 8530.918335399136 -20037508.34278925 20037508.34278925 256 256 65536 65536 17 4265.459167699568 -20037508.34278925 20037508.34278925 256 256 131072 131072 18 2132.729583849784 -20037508.34278925 20037508.34278925 256 256 262144 262144 WGS84 GoogleCRS84Quad urn:ogc:def:crs:EPSG:6.3:4326 -180.000000 -90.000000 180.000000 90.000000 urn:ogc:def:wkss:OGC:1.0:GoogleCRS84Quad 0 279541132.01435887813568115234 90.000000 -180.000000 256 256 2 1 1 139770566.00717943906784057617 90.000000 -180.000000 256 256 4 2 2 69885283.00358971953392028809 90.000000 -180.000000 256 256 8 4 3 34942641.50179485976696014404 90.000000 -180.000000 256 256 16 8 4 17471320.75089742988348007202 90.000000 -180.000000 256 256 32 16 5 8735660.37544871494174003601 90.000000 -180.000000 256 256 64 32 6 4367830.18772435747087001801 90.000000 -180.000000 256 256 128 64 7 2183915.09386217873543500900 90.000000 -180.000000 256 256 256 128 8 1091957.54693108936771750450 90.000000 -180.000000 256 256 512 256 9 545978.77346554468385875225 90.000000 -180.000000 256 256 1024 512 10 272989.38673277234192937613 90.000000 -180.000000 256 256 2048 1024 11 136494.69336638617096468806 90.000000 -180.000000 256 256 4096 2048 12 68247.34668319308548234403 90.000000 -180.000000 256 256 8192 4096 13 34123.67334159654274117202 90.000000 -180.000000 256 256 16384 8192 14 17061.83667079825318069197 90.000000 -180.000000 256 256 32768 16384 15 8530.91833539912659034599 90.000000 -180.000000 256 256 65536 32768 16 4265.45916769956329517299 90.000000 -180.000000 256 256 131072 65536 17 2132.72958384978574031265 90.000000 -180.000000 256 256 262144 131072 '; } /** * Returns tile via WMTS specification */ public function getTile() { $request = $this->getGlobal('Request'); if ($request) { if (strpos('/', $_GET['Format']) !== FALSE) { $format = explode('/', $_GET['Format']); $format = $format[1]; } else { $format = $this->getGlobal('Format'); } parent::getTile($this->getGlobal('Layer'), $this->getGlobal('TileMatrix'), $this->getGlobal('TileRow'), $this->getGlobal('TileCol'), $format); } else { parent::getTile($this->layer, $this->z, $this->y, $this->x, $this->ext); } } } /** * Tile map service */ class Tms extends Server { /** * @param array $params */ public $layer; /** * @var integer */ public $z; /** * @var integer */ public $y; /** * @var integer */ public $x; /** * @var string */ public $ext; /** * * @param array $params */ public function __construct($params) { parent::__construct(); parent::setParams($params); } /** * Returns getCapabilities metadata request */ public function getCapabilities() { parent::setDatasets(); $maps = array_merge($this->fileLayer, $this->dbLayer); header("Content-type: application/xml"); echo''; foreach ($maps as $m) { $basename = $m['basename']; $title = (array_key_exists('name', $m) ) ? $m['name'] : $basename; $profile = $m['profile']; if ($profile == 'geodetic') { $srs = "EPSG:4326"; } else { $srs = "EPSG:3857"; echo ''; } } echo ''; } /** * Prints metadata about layer */ public function getLayerCapabilities() { parent::setDatasets(); $maps = array_merge($this->fileLayer, $this->dbLayer); foreach ($maps as $map) { if (strpos($map['basename'], $this->layer) !== false) { $m = $map; break; } } $title = (array_key_exists('name', $m)) ? $m['name'] : $m['basename']; $description = (array_key_exists('description', $m)) ? $m['description'] : ""; $bounds = $m['bounds']; if ($m['profile'] == 'geodetic') { $srs = "EPSG:4326"; $originx = -180.0; $originy = -90.0; $initialResolution = 0.703125; } else { $srs = "EPSG:3857"; $originx = -20037508.342789; $originy = -20037508.342789; $mercator = new GlobalMercator(); list( $minx, $miny ) = $mercator->LatLonToMeters($bounds[1], $bounds[0]); list( $maxx, $maxy ) = $mercator->LatLonToMeters($bounds[3], $bounds[2]); $bounds = array($minx, $miny, $maxx, $maxy); $initialResolution = 156543.03392804062; } $mime = ($m['format'] == 'jpg') ? 'image/jpeg' : 'image/png'; header("Content-type: application/xml"); echo ' ' . htmlspecialchars($title) . ' ' . htmlspecialchars($description) . ' ' . $srs . ' '; for ($zoom = $m['minzoom']; $zoom < $m['maxzoom'] + 1; $zoom++) { echo ''; } echo''; } /** * Process getTile request */ public function getTile() { parent::getTile($this->layer, $this->z, $this->y, $this->x, $this->ext); } } /* GlobalMapTiles - part of Aggregate Map Tools Version 1.0 Copyright (c) 2009 The Bivings Group All rights reserved. Author: John Bafford http://www.bivings.com/ http://bafford.com/softare/aggregate-map-tools/ Based on GDAL2Tiles / globalmaptiles.py Original python version Copyright (c) 2008 Klokan Petr Pridal. All rights reserved. http://www.klokan.cz/projects/gdal2tiles/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublic ense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The abov e copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ class GlobalMercator { var $tileSize; var $initialResolution; var $originShift; //Initialize the TMS Global Mercator pyramid function __construct($tileSize = 256) { $this->tileSize = $tileSize; $this->initialResolution = 2 * M_PI * 6378137 / $this->tileSize; # 156543.03392804062 for tileSize 256 Pixels $this->originShift = 2 * M_PI * 6378137 / 2.0; # 20037508.342789244 } //Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913 function LatLonToMeters($lat, $lon) { $mx = $lon * $this->originShift / 180.0; $my = log(tan((90 + $lat) * M_PI / 360.0)) / (M_PI / 180.0); $my *= $this->originShift / 180.0; return array($mx, $my); } //Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum function MetersToLatLon($mx, $my) { $lon = ($mx / $this->originShift) * 180.0; $lat = ($my / $this->originShift) * 180.0; $lat = 180 / M_PI * (2 * atan(exp($lat * M_PI / 180.0)) - M_PI / 2.0); return array($lat, $lon); } //Converts pixel coordinates in given zoom level of pyramid to EPSG:900913 function PixelsToMeters($px, $py, $zoom) { $res = $this->Resolution($zoom); $mx = $px * $res - $this->originShift; $my = $py * $res - $this->originShift; return array($mx, $my); } //Converts EPSG:900913 to pyramid pixel coordinates in given zoom level function MetersToPixels($mx, $my, $zoom) { $res = $this->Resolution($zoom); $px = ($mx + $this->originShift) / $res; $py = ($my + $this->originShift) / $res; return array($px, $py); } //Returns a tile covering region in given pixel coordinates function PixelsToTile($px, $py) { $tx = ceil($px / $this->tileSize) - 1; $ty = ceil($py / $this->tileSize) - 1; return array($tx, $ty); } //Returns tile for given mercator coordinates function MetersToTile($mx, $my, $zoom) { list($px, $py) = $this->MetersToPixels($mx, $my, $zoom); return $this->PixelsToTile($px, $py); } //Returns bounds of the given tile in EPSG:900913 coordinates function TileBounds($tx, $ty, $zoom) { list($minx, $miny) = $this->PixelsToMeters($tx * $this->tileSize, $ty * $this->tileSize, $zoom); list($maxx, $maxy) = $this->PixelsToMeters(($tx + 1) * $this->tileSize, ($ty + 1) * $this->tileSize, $zoom); return array($minx, $miny, $maxx, $maxy); } //Returns bounds of the given tile in latutude/longitude using WGS84 datum function TileLatLonBounds($tx, $ty, $zoom) { $bounds = $this->TileBounds($tx, $ty, $zoom); list($minLat, $minLon) = $this->MetersToLatLon($bounds[0], $bounds[1]); list($maxLat, $maxLon) = $this->MetersToLatLon($bounds[2], $bounds[3]); return array($minLat, $minLon, $maxLat, $maxLon); } //Resolution (meters/pixel) for given zoom level (measured at Equator) function Resolution($zoom) { return $this->initialResolution / (1 < $zoom); } } /** * Simple router */ class Router { /** * @param array $routes */ public static function serve($routes) { $request_method = strtolower($_SERVER['REQUEST_METHOD']); $path_info = '/'; if (!empty($_SERVER['PATH_INFO'])) { $path_info = $_SERVER['PATH_INFO']; } else if (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/tileserver.php') { $path_info = $_SERVER['ORIG_PATH_INFO']; } else { if (!empty($_SERVER['REQUEST_URI'])) { $path_info = (strpos($_SERVER['REQUEST_URI'], '?') > 0) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI']; } } $discovered_handler = null; $regex_matches = array(); if ($routes) { $tokens = array( ':string' => '([a-zA-Z]+)', ':number' => '([0-9]+)', ':alpha' => '([a-zA-Z0-9-_]+)' ); global $config; foreach ($routes as $pattern => $handler_name) { $pattern = strtr($pattern, $tokens); if (preg_match('#/?' . $pattern . '/?$#', $path_info, $matches)) { if(!isset($config['baseUrls'])){ $config['baseUrls'][0] = $_SERVER['HTTP_HOST'].preg_replace('#/?'.$pattern.'/?$#', '', $path_info); } $discovered_handler = $handler_name; $regex_matches = $matches; break; } } } $handler_instance = null; if ($discovered_handler) { if (is_string($discovered_handler)) { if (strpos($discovered_handler, ':') !== false) { $discoverered_class = explode(':', $discovered_handler); $discoverered_method = explode(':', $discovered_handler); $handler_instance = new $discoverered_class[0]($regex_matches); call_user_func(array($handler_instance, $discoverered_method[1])); } else { $handler_instance = new $discovered_handler($regex_matches); } } elseif (is_callable($discovered_handler)) { $handler_instance = $discovered_handler(); } } else { echo 'Router: No route'; die; //$handler_instance = new Server; //$handler_instance->getHtml(); } } }