Much WIP.

This commit is contained in:
Malte Hübner 2017-06-08 20:36:08 +02:00
parent 45ee43895f
commit bd9e686cf0
4 changed files with 237 additions and 119 deletions

View File

@ -5,14 +5,25 @@ namespace StaticMapLite\Canvas;
class Canvas class Canvas
{ {
protected $image = null; protected $image = null;
protected $width = 0; protected $width = 0;
protected $height = 0; protected $height = 0;
public function __construct(int $width, int $height) protected $centerX = 0;
protected $centerY = 0;
protected $zoom = 0;
public function __construct(int $width, int $height, int $zoom, int $centerX, int $centerY)
{ {
$this->width = $width; $this->width = $width;
$this->height = $height; $this->height = $height;
$this->zoom = $zoom;
$this->centerX = $centerX;
$this->centerY = $centerY;
$this->image = imagecreatetruecolor($this->width, $this->height); $this->image = imagecreatetruecolor($this->width, $this->height);
} }
@ -20,4 +31,29 @@ class Canvas
{ {
return $this->image; return $this->image;
} }
public function getWidth(): int
{
return $this->width;
}
public function getHeight(): int
{
return $this->height;
}
public function getCenterX(): int
{
return $this->centerX;
}
public function getCenterY(): int
{
return $this->centerY;
}
public function getZoom(): int
{
return $this->zoom;
}
} }

View File

@ -0,0 +1,118 @@
<?php
namespace StaticMapLite\ElementPrinter\Marker;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\Element\Marker\Marker;
use StaticMapLite\Util;
class MarkerPrinter
{
/** @var Marker $marker */
protected $marker = null;
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]+)$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '0,-19',
'offsetShadow' => false,
),
// openlayers std markers
'ol-marker' => array('regex' => '/^ol-marker(|-blue|-gold|-green)+$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-25',
'offsetShadow' => '-1,-13',
),
// taken from http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
'ylw' => array('regex' => '/^(pink|purple|red|ltblu|ylw)-pushpin$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-32',
'offsetShadow' => '-1,-13',
),
// http://svn.openstreetmap.org/sites/other/StaticMap/symbols/0.png
'ojw' => array('regex' => '/^bullseye$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '-20,-20',
'offsetShadow' => false,
),
);
public function __construct()
{
}
public function setMarker(Marker $marker): MarkerPrinter
{
$this->marker = $marker;
return $this;
}
public function paint(Canvas $canvas): MarkerPrinter
{
$markerFilename = '';
$markerShadow = '';
$matches = false;
// check for marker type, get settings from markerPrototypes
if ($this->marker->getMarkerType()) {
foreach ($this->markerPrototypes as $markerPrototype) {
if (preg_match($markerPrototype['regex'], $this->marker->getMarkerType(), $matches)) {
$markerFilename = $matches[0] . $markerPrototype['extension'];
if ($markerPrototype['offsetImage']) {
list($markerImageOffsetX, $markerImageOffsetY) = explode(",", $markerPrototype['offsetImage']);
}
$markerShadow = $markerPrototype['shadow'];
if ($markerShadow) {
list($markerShadowOffsetX, $markerShadowOffsetY) = explode(",", $markerPrototype['offsetShadow']);
}
}
}
}
// check required files or set default
if ($markerFilename == '' || !file_exists($this->markerBaseDir . '/' . $markerFilename)) {
$markerIndex++;
$markerFilename = 'lightblue' . $markerIndex . '.png';
$markerImageOffsetX = 0;
$markerImageOffsetY = -19;
}
// create img resource
if (file_exists($this->markerBaseDir . '/' . $markerFilename)) {
$markerImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerFilename);
} else {
$markerImg = imagecreatefrompng($this->markerBaseDir . '/lightblue1.png');
}
// check for shadow + create shadow recource
if ($markerShadow && file_exists($this->markerBaseDir . '/' . $markerShadow)) {
$markerShadowImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerShadow);
}
// calc position
$destX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::lonToTile($this->marker->getLongitude(), $this->zoom)));
$destY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($this->marker->getLatitude(), $this->zoom)));
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {
imagecopy($canvas->getImage(), $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
}
// copy marker on basemap above shadow
imagecopy($canvas->getImage(), $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg));
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace StaticMapLite\ElementPrinter\Polyline;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\Util;
class PolylinePrinter
{
/** @var Polyline $polyline */
protected $polyline = null;
protected $tileSize = 256;
public function __construct()
{
}
public function setPolyline(Polyline $polyline): PolylinePrinter
{
$this->polyline = $polyline;
return $this;
}
public function paint(Canvas $canvas): PolylinePrinter
{
$polylineList = \Polyline::decode($this->polyline->getPolyline());
$sourceLatitude = null;
$sourceLongitude = null;
$destinationLatitude = null;
$destinationLongitude = null;
$color = imagecolorallocate($canvas->getImage(), $this->polyline->getColorRed(), $this->polyline->getColorGreen(), $this->polyline->getColorBlue());
imagesetthickness($canvas->getImage(), 5);
//imageantialias($this->image, true);
while (!empty($polylineList)) {
if (!$sourceLatitude) {
$sourceLatitude = array_shift($polylineList);
}
if (!$sourceLongitude) {
$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())));
$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())));
imageline($canvas->getImage() , $sourceX, $sourceY , $destinationX, $destinationY, $color);
$sourceLatitude = $destinationLatitude;
$sourceLongitude = $destinationLongitude;
}
return $this;
}
}

View File

@ -5,6 +5,7 @@ namespace StaticMapLite;
use StaticMapLite\Canvas\Canvas; use StaticMapLite\Canvas\Canvas;
use StaticMapLite\Element\Marker\Marker; use StaticMapLite\Element\Marker\Marker;
use StaticMapLite\Element\Polyline\Polyline; use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
use StaticMapLite\TileResolver\CachedTileResolver; use StaticMapLite\TileResolver\CachedTileResolver;
class Printer class Printer
@ -26,39 +27,9 @@ class Printer
]; ];
protected $tileDefaultSrc = 'mapnik'; protected $tileDefaultSrc = 'mapnik';
protected $markerBaseDir = '../images/markers';
protected $osmLogo = '../images/osm_logo.png'; protected $osmLogo = '../images/osm_logo.png';
protected $markerPrototypes = array(
// found at http://www.mapito.net/map-marker-icons.html
'lighblue' => array('regex' => '/^lightblue([0-9]+)$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '0,-19',
'offsetShadow' => false,
),
// openlayers std markers
'ol-marker' => array('regex' => '/^ol-marker(|-blue|-gold|-green)+$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-25',
'offsetShadow' => '-1,-13',
),
// taken from http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
'ylw' => array('regex' => '/^(pink|purple|red|ltblu|ylw)-pushpin$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-32',
'offsetShadow' => '-1,-13',
),
// http://svn.openstreetmap.org/sites/other/StaticMap/symbols/0.png
'ojw' => array('regex' => '/^bullseye$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '-20,-20',
'offsetShadow' => false,
),
);
protected $useTileCache = true; protected $useTileCache = true;
@ -157,7 +128,13 @@ class Printer
public function createBaseMap() public function createBaseMap()
{ {
$this->canvas = new Canvas($this->width, $this->height); $this->canvas = new Canvas(
$this->width,
$this->height,
$this->zoom,
$this->centerX,
$this->centerY
);
$startX = floor($this->centerX - ($this->width / $this->tileSize) / 2); $startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
$startY = floor($this->centerY - ($this->height / $this->tileSize) / 2); $startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
@ -192,101 +169,21 @@ class Printer
public function placeMarkers() public function placeMarkers()
{ {
foreach ($this->markers as $marker) { foreach ($this->markers as $marker) {
$markerFilename = '';
$markerShadow = '';
$matches = false;
// check for marker type, get settings from markerPrototypes
if ($marker->getMarkerType()) {
foreach ($this->markerPrototypes as $markerPrototype) {
if (preg_match($markerPrototype['regex'], $marker->getMarkerType(), $matches)) {
$markerFilename = $matches[0] . $markerPrototype['extension'];
if ($markerPrototype['offsetImage']) {
list($markerImageOffsetX, $markerImageOffsetY) = explode(",", $markerPrototype['offsetImage']);
}
$markerShadow = $markerPrototype['shadow'];
if ($markerShadow) {
list($markerShadowOffsetX, $markerShadowOffsetY) = explode(",", $markerPrototype['offsetShadow']);
}
}
}
}
// check required files or set default
if ($markerFilename == '' || !file_exists($this->markerBaseDir . '/' . $markerFilename)) {
$markerIndex++;
$markerFilename = 'lightblue' . $markerIndex . '.png';
$markerImageOffsetX = 0;
$markerImageOffsetY = -19;
}
// create img resource
if (file_exists($this->markerBaseDir . '/' . $markerFilename)) {
$markerImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerFilename);
} else {
$markerImg = imagecreatefrompng($this->markerBaseDir . '/lightblue1.png');
}
// check for shadow + create shadow recource
if ($markerShadow && file_exists($this->markerBaseDir . '/' . $markerShadow)) {
$markerShadowImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerShadow);
}
// calc position
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($marker->getLongitude(), $this->zoom)));
$destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($marker->getLatitude(), $this->zoom)));
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {
imagecopy($this->canvas->getImage(), $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
}
// copy marker on basemap above shadow
imagecopy($this->canvas->getImage(), $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg));
}; };
} }
public function placePolylines() public function placePolylines()
{ {
$printer = new PolylinePrinter();
/** @var Polyline $polyline */ /** @var Polyline $polyline */
foreach ($this->polylines as $polyline) { foreach ($this->polylines as $polyline) {
$polylineList = \Polyline::decode($polyline->getPolyline()); $printer
->setPolyline($polyline)
$sourceLatitude = null; ->paint($this->canvas)
$sourceLongitude = null; ;
$destinationLatitude = null;
$destinationLongitude = null;
$color = imagecolorallocate($this->canvas->getImage(), $polyline->getColorRed(), $polyline->getColorGreen(), $polyline->getColorBlue());
imagesetthickness($this->image, 5);
//imageantialias($this->image, true);
while (!empty($polylineList)) {
if (!$sourceLatitude) {
$sourceLatitude = array_shift($polylineList);
}
if (!$sourceLongitude) {
$sourceLongitude = array_shift($polylineList);
}
$sourceX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($sourceLongitude, $this->zoom)));
$sourceY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($sourceLatitude, $this->zoom)));
$destinationLatitude = array_shift($polylineList);
$destinationLongitude = array_shift($polylineList);
$destinationX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($destinationLongitude, $this->zoom)));
$destinationY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($destinationLatitude, $this->zoom)));
imageline($this->canvas->getImage() , $sourceX, $sourceY , $destinationX, $destinationY, $color);
$sourceLatitude = $destinationLatitude;
$sourceLongitude = $destinationLongitude;
}
} }
} }