From 62f6f8ef84c4c2bf5597fdb1e1ba0dbaa005fc35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Hu=CC=88bner?= Date: Sun, 11 Feb 2018 19:26:56 +0100 Subject: [PATCH] WIP for cache. --- src/AbstractPrinter.php | 570 ++++++++++++++++++++++++++++++++++++++ src/MapCache/MapCache.php | 46 +++ src/Printer.php | 42 +-- src/PrinterInterface.php | 8 + 4 files changed, 625 insertions(+), 41 deletions(-) create mode 100644 src/AbstractPrinter.php create mode 100644 src/MapCache/MapCache.php create mode 100644 src/PrinterInterface.php diff --git a/src/AbstractPrinter.php b/src/AbstractPrinter.php new file mode 100644 index 0000000..87367b5 --- /dev/null +++ b/src/AbstractPrinter.php @@ -0,0 +1,570 @@ + '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', + ]; + + /** @var string $tileDefaultSrc */ + protected $tileDefaultSrc = 'mapnik'; + + /** @var string $osmLogo */ + protected $osmLogo = '../images/osm_logo.png'; + + /** @var bool $useTileCache */ + protected $useTileCache = true; + + /** @var bool $useMapCache */ + protected $useMapCache = false; + + /** @var string $mapCacheBaseDir */ + protected $mapCacheBaseDir = '../cache/maps'; + + /** @var string $mapCacheID */ + protected $mapCacheID = ''; + + /** @var string $mapCacheFile */ + protected $mapCacheFile = ''; + + /** @var string $mapCacheExtension */ + protected $mapCacheExtension = 'png'; + + protected $zoom; + + protected $lat; + + protected $lon; + + protected $width; + + protected $height; + + protected $image; + + protected $maptype; + + protected $centerX; + + protected $centerY; + + protected $offsetX; + + protected $offsetY; + + /** @var array $markers */ + protected $markers = []; + + /** @var array $polylines */ + protected $polylines = []; + + /** + * @return int + */ + public function getMaxWidth(): int + { + return $this->maxWidth; + } + + /** + * @param int $maxWidth + * @return AbstractPrinter + */ + public function setMaxWidth(int $maxWidth): AbstractPrinter + { + $this->maxWidth = $maxWidth; + return $this; + } + + /** + * @return int + */ + public function getMaxHeight(): int + { + return $this->maxHeight; + } + + /** + * @param int $maxHeight + * @return AbstractPrinter + */ + public function setMaxHeight(int $maxHeight): AbstractPrinter + { + $this->maxHeight = $maxHeight; + return $this; + } + + /** + * @return TileResolverInterface + */ + public function getTileResolver(): TileResolverInterface + { + return $this->tileResolver; + } + + /** + * @param TileResolverInterface $tileResolver + * @return AbstractPrinter + */ + public function setTileResolver(TileResolverInterface $tileResolver): AbstractPrinter + { + $this->tileResolver = $tileResolver; + return $this; + } + + /** + * @return CanvasInterface + */ + public function getCanvas(): CanvasInterface + { + return $this->canvas; + } + + /** + * @param CanvasInterface $canvas + * @return AbstractPrinter + */ + public function setCanvas(CanvasInterface $canvas): AbstractPrinter + { + $this->canvas = $canvas; + return $this; + } + + /** + * @return int + */ + public function getTileSize(): int + { + return $this->tileSize; + } + + /** + * @param int $tileSize + * @return AbstractPrinter + */ + public function setTileSize(int $tileSize): AbstractPrinter + { + $this->tileSize = $tileSize; + return $this; + } + + /** + * @return array + */ + public function getTileSrcUrl(): array + { + return $this->tileSrcUrl; + } + + /** + * @param array $tileSrcUrl + * @return AbstractPrinter + */ + public function setTileSrcUrl(array $tileSrcUrl): AbstractPrinter + { + $this->tileSrcUrl = $tileSrcUrl; + return $this; + } + + /** + * @return string + */ + public function getTileDefaultSrc(): string + { + return $this->tileDefaultSrc; + } + + /** + * @param string $tileDefaultSrc + * @return AbstractPrinter + */ + public function setTileDefaultSrc(string $tileDefaultSrc): AbstractPrinter + { + $this->tileDefaultSrc = $tileDefaultSrc; + return $this; + } + + /** + * @return string + */ + public function getOsmLogo(): string + { + return $this->osmLogo; + } + + /** + * @param string $osmLogo + * @return AbstractPrinter + */ + public function setOsmLogo(string $osmLogo): AbstractPrinter + { + $this->osmLogo = $osmLogo; + return $this; + } + + /** + * @return bool + */ + public function isUseTileCache(): bool + { + return $this->useTileCache; + } + + /** + * @param bool $useTileCache + * @return AbstractPrinter + */ + public function setUseTileCache(bool $useTileCache): AbstractPrinter + { + $this->useTileCache = $useTileCache; + return $this; + } + + /** + * @return bool + */ + public function isUseMapCache(): bool + { + return $this->useMapCache; + } + + /** + * @param bool $useMapCache + * @return AbstractPrinter + */ + public function setUseMapCache(bool $useMapCache): AbstractPrinter + { + $this->useMapCache = $useMapCache; + return $this; + } + + /** + * @return string + */ + public function getMapCacheBaseDir(): string + { + return $this->mapCacheBaseDir; + } + + /** + * @param string $mapCacheBaseDir + * @return AbstractPrinter + */ + public function setMapCacheBaseDir(string $mapCacheBaseDir): AbstractPrinter + { + $this->mapCacheBaseDir = $mapCacheBaseDir; + return $this; + } + + /** + * @return string + */ + public function getMapCacheID(): string + { + return $this->mapCacheID; + } + + /** + * @param string $mapCacheID + * @return AbstractPrinter + */ + public function setMapCacheID(string $mapCacheID): AbstractPrinter + { + $this->mapCacheID = $mapCacheID; + return $this; + } + + /** + * @return string + */ + public function getMapCacheFile(): string + { + return $this->mapCacheFile; + } + + /** + * @param string $mapCacheFile + * @return AbstractPrinter + */ + public function setMapCacheFile(string $mapCacheFile): AbstractPrinter + { + $this->mapCacheFile = $mapCacheFile; + return $this; + } + + /** + * @return string + */ + public function getMapCacheExtension(): string + { + return $this->mapCacheExtension; + } + + /** + * @param string $mapCacheExtension + * @return AbstractPrinter + */ + public function setMapCacheExtension(string $mapCacheExtension): AbstractPrinter + { + $this->mapCacheExtension = $mapCacheExtension; + return $this; + } + + /** + * @return mixed + */ + public function getZoom() + { + return $this->zoom; + } + + /** + * @param mixed $zoom + * @return AbstractPrinter + */ + public function setZoom($zoom) + { + $this->zoom = $zoom; + return $this; + } + + /** + * @return mixed + */ + public function getLat() + { + return $this->lat; + } + + /** + * @param mixed $lat + * @return AbstractPrinter + */ + public function setLat($lat) + { + $this->lat = $lat; + return $this; + } + + /** + * @return mixed + */ + public function getLon() + { + return $this->lon; + } + + /** + * @param mixed $lon + * @return AbstractPrinter + */ + public function setLon($lon) + { + $this->lon = $lon; + return $this; + } + + /** + * @return mixed + */ + public function getWidth() + { + return $this->width; + } + + /** + * @param mixed $width + * @return AbstractPrinter + */ + public function setWidth($width) + { + $this->width = $width; + return $this; + } + + /** + * @return mixed + */ + public function getHeight() + { + return $this->height; + } + + /** + * @param mixed $height + * @return AbstractPrinter + */ + public function setHeight($height) + { + $this->height = $height; + return $this; + } + + /** + * @return mixed + */ + public function getImage() + { + return $this->image; + } + + /** + * @param mixed $image + * @return AbstractPrinter + */ + public function setImage($image) + { + $this->image = $image; + return $this; + } + + /** + * @return mixed + */ + public function getMaptype() + { + return $this->maptype; + } + + /** + * @param mixed $maptype + * @return AbstractPrinter + */ + public function setMaptype($maptype) + { + $this->maptype = $maptype; + return $this; + } + + /** + * @return mixed + */ + public function getCenterX() + { + return $this->centerX; + } + + /** + * @param mixed $centerX + * @return AbstractPrinter + */ + public function setCenterX($centerX) + { + $this->centerX = $centerX; + return $this; + } + + /** + * @return mixed + */ + public function getCenterY() + { + return $this->centerY; + } + + /** + * @param mixed $centerY + * @return AbstractPrinter + */ + public function setCenterY($centerY) + { + $this->centerY = $centerY; + return $this; + } + + /** + * @return mixed + */ + public function getOffsetX() + { + return $this->offsetX; + } + + /** + * @param mixed $offsetX + * @return AbstractPrinter + */ + public function setOffsetX($offsetX) + { + $this->offsetX = $offsetX; + return $this; + } + + /** + * @return mixed + */ + public function getOffsetY() + { + return $this->offsetY; + } + + /** + * @param mixed $offsetY + * @return AbstractPrinter + */ + public function setOffsetY($offsetY) + { + $this->offsetY = $offsetY; + return $this; + } + + /** + * @return array + */ + public function getMarkers(): array + { + return $this->markers; + } + + /** + * @param array $markers + * @return AbstractPrinter + */ + public function setMarkers(array $markers): AbstractPrinter + { + $this->markers = $markers; + return $this; + } + + /** + * @return array + */ + public function getPolylines(): array + { + return $this->polylines; + } + + /** + * @param array $polylines + * @return AbstractPrinter + */ + public function setPolylines(array $polylines): AbstractPrinter + { + $this->polylines = $polylines; + return $this; + } +} diff --git a/src/MapCache/MapCache.php b/src/MapCache/MapCache.php new file mode 100644 index 0000000..c73c033 --- /dev/null +++ b/src/MapCache/MapCache.php @@ -0,0 +1,46 @@ +mapCacheID = md5($this->serializeParams()); + $filename = $this->mapCacheIDToFilename(); + + return file_exists($filename); + } + + public function serializeParams(): string + { + return join('&', [ + $this->printer->getZoom(), + $this->printer->getCenterLatitude(), + $this->printer->getCenterLongitude(), + $this->printer->getWidth(), + $this->printer->getHeight(), + serialize($this->printer->getMarkers()), + serialize($this->printer->getPolylines()), + $this->printer->getMapType() + ]); + } + + public function mapCacheIDToFilename() + { + if (!$this->mapCacheFile) { + $this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_" . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2) . "/" . substr($this->mapCacheID, 4); + } + return $this->mapCacheFile . "." . $this->mapCacheExtension; + } +} diff --git a/src/Printer.php b/src/Printer.php index 04d6a5e..e0a300a 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -10,48 +10,8 @@ use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter; use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter; use StaticMapLite\TileResolver\CachedTileResolver; -class Printer +class Printer extends AbstractPrinter { - /** @var int $maxWidth */ - protected $maxWidth = 1024; - - /** @var int $maxHeight */ - protected $maxHeight = 1024; - - /** @var */ - protected $tileResolver = null; - - /** @var Canvas $canvas */ - protected $canvas = null; - - protected $tileSize = 256; - 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 $osmLogo = '../images/osm_logo.png'; - - - - - protected $useTileCache = true; - - protected $useMapCache = false; - protected $mapCacheBaseDir = '../cache/maps'; - protected $mapCacheID = ''; - protected $mapCacheFile = ''; - protected $mapCacheExtension = 'png'; - - protected $zoom, $lat, $lon, $width, $height, $image, $maptype; - protected $centerX, $centerY, $offsetX, $offsetY; - - protected $markers = []; - protected $polylines = []; - public function __construct() { $this->zoom = 0; diff --git a/src/PrinterInterface.php b/src/PrinterInterface.php new file mode 100644 index 0000000..343e832 --- /dev/null +++ b/src/PrinterInterface.php @@ -0,0 +1,8 @@ +