staticmaplite/src/Printer/Printer.php

209 lines
4.9 KiB
PHP
Raw Normal View History

<?php
2018-02-11 18:28:06 +00:00
namespace StaticMapLite\Printer;
2017-05-26 15:24:07 +00:00
use StaticMapLite\Canvas\Canvas;
2017-06-08 21:29:04 +00:00
use StaticMapLite\CanvasTilePainter\CanvasTilePainter;
2018-02-14 18:37:28 +00:00
use StaticMapLite\CopyrightPrinter\CopyrightPrinter;
2017-09-29 14:20:00 +00:00
use StaticMapLite\Element\Marker\AbstractMarker;
2017-05-26 15:28:30 +00:00
use StaticMapLite\Element\Polyline\Polyline;
2017-09-30 17:45:24 +00:00
use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter;
2017-06-08 18:36:08 +00:00
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
2018-02-14 08:44:47 +00:00
use StaticMapLite\MapCache\MapCache;
2018-02-14 17:22:14 +00:00
use StaticMapLite\Output\CacheOutput;
use StaticMapLite\Output\ImageOutput;
2017-05-26 15:04:30 +00:00
use StaticMapLite\TileResolver\CachedTileResolver;
2018-02-13 12:47:12 +00:00
use StaticMapLite\Util;
2017-05-26 14:32:08 +00:00
2018-02-11 18:26:56 +00:00
class Printer extends AbstractPrinter
2017-05-26 09:56:16 +00:00
{
public function __construct()
{
$this->zoom = 0;
2018-02-14 08:44:47 +00:00
$this->latitude = 0;
$this->longitude = 0;
$this->width = 500;
$this->height = 350;
$this->maptype = $this->tileDefaultSrc;
2017-05-26 14:32:08 +00:00
2018-02-14 08:44:47 +00:00
$this->mapCache = new MapCache($this);
2017-05-26 15:04:30 +00:00
$this->tileResolver = new CachedTileResolver();
$this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]);
}
2017-09-29 14:20:00 +00:00
public function addMarker(AbstractMarker $marker): Printer
{
2017-05-26 15:11:00 +00:00
$this->markers[] = $marker;
2017-05-26 14:16:40 +00:00
return $this;
}
2017-05-26 15:28:30 +00:00
public function addPolyline(Polyline $polyline): Printer
{
2017-05-26 15:28:30 +00:00
$this->polylines[] = $polyline;
2017-05-26 14:16:40 +00:00
return $this;
}
public function setCenter(float $latitude, float $longitude): Printer
{
2018-02-14 08:44:47 +00:00
$this->latitude = $latitude;
$this->longitude = $longitude;
2017-05-26 14:16:40 +00:00
return $this;
}
public function setSize(int $width, int $height): Printer
{
$this->width = $width;
$this->height = $height;
if ($this->width > $this->maxWidth) {
$this->width = $this->maxWidth;
}
2017-05-26 14:16:40 +00:00
if ($this->height > $this->maxHeight) {
$this->height = $this->maxHeight;
}
2017-05-26 14:16:40 +00:00
return $this;
}
2018-02-14 08:44:47 +00:00
public function setZoom(int $zoom): PrinterInterface
{
2017-05-26 14:16:40 +00:00
$this->zoom = $zoom;
2017-05-26 14:16:40 +00:00
if ($this->zoom > 18) {
$this->zoom = 18;
}
2017-05-26 14:16:40 +00:00
return $this;
}
2017-05-26 09:56:16 +00:00
2018-02-14 08:44:47 +00:00
public function setMapType(string $mapType): PrinterInterface
2017-05-26 14:16:40 +00:00
{
$this->maptype = $mapType;
$this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]);
2017-05-26 14:16:40 +00:00
return $this;
}
2018-02-14 18:37:28 +00:00
public function initCoords(): Printer
{
2018-02-14 08:44:47 +00:00
$this->centerX = Util::lonToTile($this->longitude, $this->zoom);
$this->centerY = Util::latToTile($this->latitude, $this->zoom);
2017-05-26 14:21:41 +00:00
$this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
2018-02-14 18:37:28 +00:00
return $this;
}
2018-02-14 18:37:28 +00:00
public function createBaseMap(): Printer
{
2017-06-08 18:36:08 +00:00
$this->canvas = new Canvas(
$this->width,
$this->height,
$this->zoom,
$this->centerX,
$this->centerY
);
2017-05-26 15:24:07 +00:00
2017-06-08 21:29:04 +00:00
$ctp = new CanvasTilePainter();
$ctp
->setCanvas($this->canvas)
->setTileResolver($this->tileResolver)
->paint()
;
2018-02-14 18:37:28 +00:00
return $this;
}
2018-02-14 18:37:28 +00:00
public function placeMarkers(): Printer
{
2017-09-30 17:45:24 +00:00
$printer = new ExtraMarkerPrinter();
2017-06-08 18:39:01 +00:00
foreach ($this->markers as $marker) {
$printer
->setMarker($marker)
->paint($this->canvas)
;
}
2018-02-14 18:37:28 +00:00
return $this;
}
2018-02-14 18:37:28 +00:00
public function placePolylines(): Printer
2017-05-26 12:58:47 +00:00
{
2017-06-08 18:36:08 +00:00
$printer = new PolylinePrinter();
2017-05-26 15:28:30 +00:00
/** @var Polyline $polyline */
2017-05-26 12:58:47 +00:00
foreach ($this->polylines as $polyline) {
2017-06-08 18:36:08 +00:00
$printer
->setPolyline($polyline)
->paint($this->canvas)
;
2017-05-26 12:58:47 +00:00
}
2018-02-14 18:37:28 +00:00
return $this;
}
2018-02-14 18:37:28 +00:00
public function makeMap(): Printer
{
$this->initCoords();
2018-02-14 18:37:28 +00:00
$this->createBaseMap();
2017-09-30 16:51:39 +00:00
if (count($this->polylines)) {
$this->placePolylines();
}
if (count($this->markers)) {
$this->placeMarkers();
}
2018-02-14 18:37:28 +00:00
$this->printCopyright();
return $this;
}
protected function printCopyright(): Printer
{
$cp = new CopyrightPrinter();
$cp
->setCanvas($this->canvas)
->printCopyright()
;
return $this;
}
public function showMap()
{
2018-02-14 08:44:47 +00:00
if ($this->mapCache) {
if (!$this->mapCache->checkMapCache()) {
$this->makeMap();
2018-02-14 17:22:14 +00:00
2018-02-14 08:44:47 +00:00
$this->mapCache->cache($this->canvas);
} else {
2018-02-14 17:22:14 +00:00
$output = new CacheOutput();
$output
->setFilename($this->mapCache->getFilename())
->sendHeader()
->sendImage()
;
}
} else {
$this->makeMap();
2018-02-14 17:22:14 +00:00
$output = new ImageOutput();
$output
->setImage($this->image)
->sendHeader()
->sendImage()
;
}
}
}