Even more work at TileResolver and caching.

This commit is contained in:
Malte Hübner 2017-05-26 17:18:49 +02:00
parent 4142756155
commit 8c39ac68e2
3 changed files with 31 additions and 16 deletions

View File

@ -13,11 +13,12 @@ class Printer
protected $tileResolver = null;
protected $tileSize = 256;
protected $tileSrcUrl = array('mapnik' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png',
'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png',
'cycle' => 'http://a.tile.opencyclemap.org/cycle/{Z}/{X}/{Y}.png',
'wikimedia-intl' => 'https://maps.wikimedia.org/osm-intl/{Z}/{X}/{Y}.png',
);
protected $tileSrcUrl = [
'mapnik' => 'http://tile.openstreetmap.org/{z}/{x}/{y}.png',
'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
'cycle' => 'http://a.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png',
'wikimedia-intl' => 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png',
];
protected $tileDefaultSrc = 'mapnik';
protected $markerBaseDir = '../images/markers';
@ -80,6 +81,7 @@ class Printer
$this->maptype = $this->tileDefaultSrc;
$this->tileResolver = new CachedTileResolver();
$this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]);
}
public function addMarker(Marker $marker): Printer
@ -137,6 +139,8 @@ class Printer
{
$this->maptype = $mapType;
$this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]);
return $this;
}
@ -165,8 +169,8 @@ class Printer
for ($x = $startX; $x <= $endX; $x++) {
for ($y = $startY; $y <= $endY; $y++) {
$url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]);
$tileData = $this->fetchTile($url);
$tileData = $this->tileResolver->fetch($this->zoom, $x, $y);
if ($tileData) {
$tileImage = imagecreatefromstring($tileData);
} else {
@ -309,11 +313,6 @@ class Printer
return $this->mapCacheFile . "." . $this->mapCacheExtension;
}
public function fetchTile($url)
{
return $this->tileResolver->fetch($url);
}
public function copyrightNotice()
{
$logoImg = imagecreatefrompng($this->osmLogo);

View File

@ -6,15 +6,17 @@ class CachedTileResolver extends TileResolver
{
protected $tileCacheBaseDir = '../cache/tiles';
public function fetch(string $url): string
public function fetch(int $zoom, int $x, int $y): string
{
$url = $this->resolve($zoom, $x, $y);
$cachedTile = $this->checkTileCache($url);
if ($cachedTile) {
return $cachedTile;
}
$tile = parent::fetch($url);
$tile = parent::fetch($zoom, $x, $y);
if ($tile) {
$this->writeTileToCache($url, $tile);

View File

@ -6,7 +6,7 @@ use Curl\Curl;
class TileResolver
{
protected $tileLayer = null;
protected $tileLayerUrl = null;
protected $curl = null;
@ -15,8 +15,22 @@ class TileResolver
$this->curl = new Curl();
}
public function fetch(string $url): string
public function setTileLayerUrl(string $tileLayerUrl): TileResolver
{
$this->tileLayerUrl = $tileLayerUrl;
return $this;
}
public function resolve(int $zoom, int $x, int $y): string
{
return str_replace(['{z}', '{x}', '{y}'], [$zoom, $x, $y], $this->tileLayerUrl);
}
public function fetch(int $zoom, int $x, int $y): string
{
$url = $this->resolve($zoom, $x, $y);
$this->curl->get($url);
return $this->curl->response;