Extracted CanvasTilePrinter.

This commit is contained in:
Malte Hübner 2017-06-08 23:29:04 +02:00
parent d834667d8a
commit 9a262047dd
5 changed files with 90 additions and 38 deletions

View File

@ -6,6 +6,8 @@ class Canvas
{
protected $image = null;
protected $tileSize = 256;
protected $width = 0;
protected $height = 0;
@ -56,4 +58,9 @@ class Canvas
{
return $this->zoom;
}
public function getTileSize(): int
{
return $this->tileSize;
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace StaticMapLite\CanvasTilePainter;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\TileResolver\TileResolver;
class CanvasTilePainter
{
/** @var Canvas $canvas */
protected $canvas;
/** @var TileResolver $tileResolver */
protected $tileResolver;
public function __construct()
{
}
public function setCanvas(Canvas $canvas): CanvasTilePainter
{
$this->canvas = $canvas;
return $this;
}
public function setTileResolver(TileResolver $tileResolver): CanvasTilePainter
{
$this->tileResolver = $tileResolver;
return $this;
}
public function paint(): CanvasTilePainter
{
$startX = floor($this->canvas->getCenterX() - ($this->canvas->getWidth() / $this->canvas->getTileSize()) / 2);
$startY = floor($this->canvas->getCenterY() - ($this->canvas->getHeight() / $this->canvas->getTileSize()) / 2);
$endX = ceil($this->canvas->getCenterX() + ($this->canvas->getWidth() / $this->canvas->getTileSize()) / 2);
$endY = ceil($this->canvas->getCenterY() + ($this->canvas->getHeight() / $this->canvas->getTileSize()) / 2);
$offsetX = -floor(($this->canvas->getCenterX() - floor($this->canvas->getCenterX())) * $this->canvas->getTileSize());
$offsetY = -floor(($this->canvas->getCenterY() - floor($this->canvas->getCenterY())) * $this->canvas->getTileSize());
$offsetX += floor($this->canvas->getWidth() / 2);
$offsetY += floor($this->canvas->getHeight() / 2);
$offsetX += floor($startX - floor($this->canvas->getCenterX())) * $this->canvas->getTileSize();
$offsetY += floor($startY - floor($this->canvas->getCenterY())) * $this->canvas->getTileSize();
for ($x = $startX; $x <= $endX; $x++) {
for ($y = $startY; $y <= $endY; $y++) {
$tileData = $this->tileResolver->fetch($this->canvas->getZoom(), $x, $y);
if ($tileData) {
$tileImage = imagecreatefromstring($tileData);
} else {
$tileImage = imagecreate($this->canvas->getTileSize(), $this->canvas->getTileSize());
$color = imagecolorallocate($tileImage, 255, 255, 255);
@imagestring($tileImage, 1, 127, 127, 'err', $color);
}
$destX = ($x - $startX) * $this->canvas->getTileSize() + $offsetX;
$destY = ($y - $startY) * $this->canvas->getTileSize() + $offsetY;
imagecopy($this->canvas->getImage(), $tileImage, $destX, $destY, 0, 0, $this->canvas->getTileSize(), $this->canvas->getTileSize());
}
}
return $this;
}
}

View File

@ -13,8 +13,6 @@ class MarkerPrinter
protected $markerBaseDir = '../images/markers';
protected $tileSize = 256;
protected $markerPrototypes = array(
// found at http://www.mapito.net/map-marker-icons.html
'lighblue' => array('regex' => '/^lightblue([0-9]+)$/',
@ -103,8 +101,8 @@ class MarkerPrinter
}
// calc position
$destX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterX() - Util::lonToTile($this->marker->getLongitude(), $canvas->getZoom())));
$destY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($this->marker->getLatitude(), $canvas->getZoom())));
$destX = floor(($canvas->getWidth() / 2) - $canvas->getTileSize() * ($canvas->getCenterX() - Util::lonToTile($this->marker->getLongitude(), $canvas->getZoom())));
$destY = floor(($canvas->getHeight() / 2) - $canvas->getTileSize() * ($canvas->getCenterY() - Util::latToTile($this->marker->getLatitude(), $canvas->getZoom())));
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {

View File

@ -11,8 +11,6 @@ class PolylinePrinter
/** @var Polyline $polyline */
protected $polyline = null;
protected $tileSize = 256;
public function __construct()
{
@ -47,14 +45,14 @@ class PolylinePrinter
$sourceLongitude = array_shift($polylineList);
}
$sourceX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterX() - Util::lonToTile($sourceLongitude, $canvas->getZoom())));
$sourceY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($sourceLatitude, $canvas->getZoom())));
$sourceX = floor(($canvas->getWidth() / 2) - $canvas->getTileSize() * ($canvas->getCenterX() - Util::lonToTile($sourceLongitude, $canvas->getZoom())));
$sourceY = floor(($canvas->getHeight() / 2) - $canvas->getTileSize() * ($canvas->getCenterY() - Util::latToTile($sourceLatitude, $canvas->getZoom())));
$destinationLatitude = array_shift($polylineList);
$destinationLongitude = array_shift($polylineList);
$destinationX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterX() - Util::lonToTile($destinationLongitude, $canvas->getZoom())));
$destinationY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($destinationLatitude, $canvas->getZoom())));
$destinationX = floor(($canvas->getWidth() / 2) - $canvas->getTileSize() * ($canvas->getCenterX() - Util::lonToTile($destinationLongitude, $canvas->getZoom())));
$destinationY = floor(($canvas->getHeight() / 2) - $canvas->getTileSize() * ($canvas->getCenterY() - Util::latToTile($destinationLatitude, $canvas->getZoom())));
imageline($canvas->getImage() , $sourceX, $sourceY , $destinationX, $destinationY, $color);

View File

@ -3,6 +3,7 @@
namespace StaticMapLite;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\CanvasTilePainter\CanvasTilePainter;
use StaticMapLite\Element\Marker\Marker;
use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\ElementPrinter\Marker\MarkerPrinter;
@ -137,36 +138,14 @@ class Printer
$this->centerY
);
$startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
$startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
$endX = ceil($this->centerX + ($this->width / $this->tileSize) / 2);
$endY = ceil($this->centerY + ($this->height / $this->tileSize) / 2);
$this->offsetX = -floor(($this->centerX - floor($this->centerX)) * $this->tileSize);
$this->offsetY = -floor(($this->centerY - floor($this->centerY)) * $this->tileSize);
$this->offsetX += floor($this->width / 2);
$this->offsetY += floor($this->height / 2);
$this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
$this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;
for ($x = $startX; $x <= $endX; $x++) {
for ($y = $startY; $y <= $endY; $y++) {
$tileData = $this->tileResolver->fetch($this->zoom, $x, $y);
if ($tileData) {
$tileImage = imagecreatefromstring($tileData);
} else {
$tileImage = imagecreate($this->tileSize, $this->tileSize);
$color = imagecolorallocate($tileImage, 255, 255, 255);
@imagestring($tileImage, 1, 127, 127, 'err', $color);
}
$destX = ($x - $startX) * $this->tileSize + $this->offsetX;
$destY = ($y - $startY) * $this->tileSize + $this->offsetY;
imagecopy($this->canvas->getImage(), $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
}
}
$ctp = new CanvasTilePainter();
$ctp
->setCanvas($this->canvas)
->setTileResolver($this->tileResolver)
->paint()
;
}
public function placeMarkers()
{
$printer = new MarkerPrinter();