Add WMTS manager
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
/.buildpath
|
/.buildpath
|
||||||
/.project
|
/.project
|
||||||
/log.html
|
/log.html
|
||||||
|
/cache/
|
||||||
|
/globalsettings.php
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class ClassManagement extends PhpObject
|
|||||||
const INC_FOLDER = 'inc/';
|
const INC_FOLDER = 'inc/';
|
||||||
const INC_EXT = '.php';
|
const INC_EXT = '.php';
|
||||||
const SETTINGS_FILE = 'settings.php';
|
const SETTINGS_FILE = 'settings.php';
|
||||||
|
const GLOBAL_SETTINGS_FILE = 'globalsettings.php';
|
||||||
const TOOLBOX_CLASS = 'toolbox';
|
const TOOLBOX_CLASS = 'toolbox';
|
||||||
const MAIN_CLASS_ABS = 'main';
|
const MAIN_CLASS_ABS = 'main';
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ class ClassManagement extends PhpObject
|
|||||||
$this->asIncFiles = array();
|
$this->asIncFiles = array();
|
||||||
|
|
||||||
//try to include default files
|
//try to include default files
|
||||||
|
$this->incFile(self::OBJECT_FOLDER.self::GLOBAL_SETTINGS_FILE);
|
||||||
$this->incFile(self::SETTINGS_FILE);
|
$this->incFile(self::SETTINGS_FILE);
|
||||||
$this->incClass(self::TOOLBOX_CLASS);
|
$this->incClass(self::TOOLBOX_CLASS);
|
||||||
|
|
||||||
|
|||||||
9
globalsettings-sample.php
Normal file
9
globalsettings-sample.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class GlobalSettings
|
||||||
|
{
|
||||||
|
const MAPBOX_KEY = '';
|
||||||
|
const IGN_FR_KEY = '';
|
||||||
|
const LINZ_KEY = '';
|
||||||
|
const LOC_API_KEY = '';
|
||||||
|
}
|
||||||
162
inc/wmts.php
Normal file
162
inc/wmts.php
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WMTS Class
|
||||||
|
* Provides API to get WMTS tiles from various sources
|
||||||
|
* @author Franzz
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Wmts extends PhpObject
|
||||||
|
{
|
||||||
|
const CACHE_FOLDER = ClassManagement::OBJECT_FOLDER.'cache/tiles/';
|
||||||
|
const DATA_RETENTION = 60 * 60 * 24 * 90; //3 months
|
||||||
|
|
||||||
|
//Params
|
||||||
|
private $sId;
|
||||||
|
private $sPattern;
|
||||||
|
private $iX;
|
||||||
|
private $iY;
|
||||||
|
private $iZ;
|
||||||
|
private $sToken;
|
||||||
|
private $asDomains;
|
||||||
|
private $sReferer;
|
||||||
|
|
||||||
|
public function __construct($sId, $asCustomParams=array())
|
||||||
|
{
|
||||||
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||||
|
|
||||||
|
$asParams = $this->getPreset($sId);
|
||||||
|
foreach($asCustomParams as $sParam => $sValue) $asParams[$sParam] = $sValue;
|
||||||
|
|
||||||
|
$this->setPattern($asParams['pattern']);
|
||||||
|
$this->setId($sId);
|
||||||
|
$this->setGeoPos(0, 0, 0);
|
||||||
|
$this->setToken($asParams['token']);
|
||||||
|
$this->setDomains($asParams['domains']);
|
||||||
|
$this->setReferer($asParams['referer']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setId($sId) {
|
||||||
|
$this->sId = $sId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPattern($sPattern) {
|
||||||
|
$this->sPattern = $sPattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setToken($sToken) {
|
||||||
|
$this->sToken = $sToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDomains($asDomains) {
|
||||||
|
$this->asDomains = $asDomains;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setReferer($sReferer) {
|
||||||
|
$this->sReferer = $sReferer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setGeoPos($iX, $iY, $iZ) {
|
||||||
|
$this->iX = $iX;
|
||||||
|
$this->iY = $iY;
|
||||||
|
$this->iZ = $iZ;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTile($iX, $iY, $iZ) {
|
||||||
|
$this->setGeoPos($iX, $iY, $iZ);
|
||||||
|
$sTilePath = $this->getFilePath();
|
||||||
|
if(!$this->isFileAvailable($sTilePath)) {
|
||||||
|
$oCurl = curl_init();
|
||||||
|
curl_setopt($oCurl, CURLOPT_URL, $this->getUrl());
|
||||||
|
curl_setopt($oCurl, CURLOPT_HEADER, false);
|
||||||
|
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
|
||||||
|
curl_setopt($oCurl, CURLOPT_REFERER, $this->sReferer);
|
||||||
|
$sContent = curl_exec($oCurl);
|
||||||
|
curl_close($oCurl);
|
||||||
|
|
||||||
|
file_put_contents($sTilePath, $sContent);
|
||||||
|
}
|
||||||
|
else $sContent = file_get_contents($sTilePath);
|
||||||
|
|
||||||
|
return array('mime' => mime_content_type($sTilePath), 'content' => $sContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function pushTile($iX, $iY, $iZ) {
|
||||||
|
$asTile = $this->getTile($iX, $iY, $iZ);
|
||||||
|
header('Content-type: '.$asTile['mime']);
|
||||||
|
return $asTile['content'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUrl() {
|
||||||
|
$asParams = $this->getUrlParams();
|
||||||
|
$sUrl = $this->sPattern;
|
||||||
|
foreach($asParams as $sParam=>$sValue) {
|
||||||
|
$sUrl = str_replace('{'.$sParam.'}', $sValue, $sUrl);
|
||||||
|
}
|
||||||
|
return $sUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isFileAvailable($sTilePath) {
|
||||||
|
if(!file_exists($sTilePath)) return false;
|
||||||
|
else return (time() - filemtime($sTilePath) < self::DATA_RETENTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getFilePath() {
|
||||||
|
$asParams = $this->getUrlParams();
|
||||||
|
return self::CACHE_FOLDER.$asParams['id'].'_'.$asParams['x'].'_'.$asParams['y'].'_'.$asParams['z'].'.tile';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getUrlParams() {
|
||||||
|
return array(
|
||||||
|
'id' => $this->sId,
|
||||||
|
'x' => $this->iX,
|
||||||
|
'y' => $this->iY,
|
||||||
|
'z' => $this->iZ,
|
||||||
|
'token' => $this->sToken,
|
||||||
|
's' => empty($this->asDomains)?'':$this->asDomains[array_rand($this->asDomains)]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPreset($sMapId) {
|
||||||
|
$sPattern = '';
|
||||||
|
$sToken = '';
|
||||||
|
$asDomains = array();
|
||||||
|
$sReferer = '';
|
||||||
|
switch($sMapId) {
|
||||||
|
case 'mapbox.satellite':
|
||||||
|
case 'mapbox.streets':
|
||||||
|
$sPattern = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}@2x.png?access_token={token}';
|
||||||
|
$sToken = GlobalSettings::MAPBOX_KEY;
|
||||||
|
break;
|
||||||
|
case 'linz':
|
||||||
|
$sPattern = 'http://tiles-{s}.data-cdn.linz.govt.nz/services;key={token}/tiles/v4/layer=50767/EPSG:3857/{z}/{x}/{y}.png';
|
||||||
|
$sToken = GlobalSettings::LINZ_KEY;
|
||||||
|
$asDomains = array('a', 'b', 'c', 'd');
|
||||||
|
break;
|
||||||
|
case 'ign.es':
|
||||||
|
$sPattern = 'http://www.ign.es/wmts/mapa-raster?request=getTile&format=image/png&layer=MTN&TileMatrixSet=GoogleMapsCompatible&TileMatrix={z}&TileCol={x}&TileRow={y}';
|
||||||
|
break;
|
||||||
|
case 'ign.fr':
|
||||||
|
$sPattern = 'https://wxs.ign.fr/{token}/geoportail/wmts?LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS&EXCEPTIONS=text/xml&FORMAT=image/jpeg&SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&STYLE=normal&TILEMATRIXSET=PM&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}';
|
||||||
|
$sToken = GlobalSettings::IGN_FR_KEY;
|
||||||
|
$sReferer = 'https://www.visugpx.com/yfJDwfuTlf';
|
||||||
|
break;
|
||||||
|
case 'opentopomap':
|
||||||
|
$sPattern = 'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png';
|
||||||
|
$asDomains = array('a', 'b', 'c');
|
||||||
|
break;
|
||||||
|
case 'static':
|
||||||
|
$sPattern = 'https://api.mapbox.com/v4/mapbox.satellite/url-'.urlencode(Settings::HOST_SERVER).'%2Fimages%2Ffootprint_mapbox.png({x},{y})/{x},{y},{z}/400x300.png?access_token={token}';
|
||||||
|
$sToken = GlobalSettings::MAPBOX_KEY;
|
||||||
|
break;
|
||||||
|
case 'swisstopo':
|
||||||
|
$sPattern = 'https://wmts{s}.geo.admin.ch/1.0.0/ch.swisstopo.pixelkarte-farbe/default/current/3857/{z}/{x}/{y}.jpeg';
|
||||||
|
$sReferer = 'https://map.schweizmobil.ch/?lang=fr&bgLayer=pk&logo=yes&season=winter&resolution=20&E=2506240&N=1148440&layers=Winterhiking';
|
||||||
|
$asDomains = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array('id'=>$sMapId, 'pattern'=>$sPattern, 'token'=>$sToken, 'domains'=>$asDomains, 'referer'=>$sReferer);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user