1<?php 2/* 3 * Copyright (c) 2012-2018 Mark C. Prins <mprins@users.sf.net> 4 * 5 * In part based on staticMapLite 0.03 available at http://staticmaplite.svn.sourceforge.net/viewvc/staticmaplite/ 6 * 7 * Copyright (c) 2009 Gerhard Koch <gerhard.koch AT ymail.com> 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21include_once(realpath(__DIR__) . '/../geophp/geoPHP/geoPHP.inc'); 22 23/** 24 * 25 * @author Mark C. Prins <mprins@users.sf.net> 26 * @author Gerhard Koch <gerhard.koch AT ymail.com> 27 * 28 */ 29class StaticMap { 30 // this should probably not be changed 31 private $doc = ''; 32 33 // the final output 34 private $tileSize = 256; 35 private $tileInfo = array( 36 // OSM sources 37 'openstreetmap' => array( 38 'txt' => '(c) OpenStreetMap CC-BY-SA', 39 'logo' => 'osm_logo.png', 40 'url' => 'https://tile.openstreetmap.org/{Z}/{X}/{Y}.png' 41 ), 42 // OCM sources 43 'cycle' => array( 44 'txt' => 'OpenCycleMap tiles', 45 'logo' => 'cycle_logo.png', 46 'url' => 'https://tile.thunderforest.com/cycle/{Z}/{X}/{Y}.png?apikey=' 47 ), 48 'transport' => array( 49 'txt' => 'OpenCycleMap tiles', 50 'logo' => 'cycle_logo.png', 51 'url' => 'https://tile.thunderforest.com/transport/{Z}/{X}/{Y}.png?apikey=' 52 ), 53 'landscape' => array( 54 'txt' => 'OpenCycleMap tiles', 55 'logo' => 'cycle_logo.png', 56 'url' => 'https://tile.thunderforest.com/landscape/{Z}/{X}/{Y}.png?apikey=' 57 ), 58 'outdoors' => array( 59 'txt' => 'OpenCycleMap tiles', 60 'logo' => 'cycle_logo.png', 61 'url' => 'https://tile.thunderforest.com/outdoors/{Z}/{X}/{Y}.png?apikey=' 62 ), 63 'toner-lite' => array( 64 'txt' => 'Stamen tiles', 65 'logo' => 'stamen.png', 66 'url' => 'http://tile.stamen.com/toner-lite/{Z}/{X}/{Y}.png' 67 ), 68 'terrain' => array( 69 'txt' => 'Stamen tiles', 70 'logo' => 'stamen.png', 71 'url' => 'http://tile.stamen.com/terrain/{Z}/{X}/{Y}.png' 72 ) 73 //, 74 // 'piste'=>array( 75 // 'txt'=>'OpenPisteMap tiles', 76 // 'logo'=>'piste_logo.png', 77 // 'url'=>''), 78 // 'sea'=>array( 79 // 'txt'=>'OpenSeaMap tiles', 80 // 'logo'=>'sea_logo.png', 81 // 'url'=>''), 82 // H&B sources 83 // 'hikeandbike' => array ( 84 // 'txt' => 'Hike & Bike Map', 85 // 'logo' => 'hnb_logo.png', 86 // //'url' => 'http://toolserver.org/tiles/hikebike/{Z}/{X}/{Y}.png' 87 // //moved to: https://www.toolserver.org/tiles/hikebike/12/2105/1388.png 88 // 'url' => 'http://c.tiles.wmflabs.org/hikebike/{Z}/{X}/{Y}.png' 89 // ) 90 ); 91 private $tileDefaultSrc = 'openstreetmap'; 92 93 // set up markers 94 private $markerPrototypes = array( 95 // found at http://www.mapito.net/map-marker-icons.html 96 // these are 17x19 px with a pointer at the bottom left 97 'lightblue' => array( 98 'regex' => '/^lightblue([0-9]+)$/', 99 'extension' => '.png', 100 'shadow' => false, 101 'offsetImage' => '0,-19', 102 'offsetShadow' => false 103 ), 104 // openlayers std markers are 21x25px with shadow 105 'ol-marker' => array( 106 'regex' => '/^marker(|-blue|-gold|-green|-red)+$/', 107 'extension' => '.png', 108 'shadow' => 'marker_shadow.png', 109 'offsetImage' => '-10,-25', 110 'offsetShadow' => '-1,-13' 111 ), 112 // these are 16x16 px 113 'ww_icon' => array( 114 'regex' => '/ww_\S+$/', 115 'extension' => '.png', 116 'shadow' => false, 117 'offsetImage' => '-8,-8', 118 'offsetShadow' => false 119 ), 120 // assume these are 16x16 px 121 'rest' => array( 122 'regex' => '/^(?!lightblue([0-9]+)$)(?!(ww_\S+$))(?!marker(|-blue|-gold|-green|-red)+$)(.*)/', 123 'extension' => '.png', 124 'shadow' => 'marker_shadow.png', 125 'offsetImage' => '-8,-8', 126 'offsetShadow' => '-1,-1' 127 ) 128 ); 129 private $centerX; 130 private $centerY; 131 private $offsetX; 132 private $offsetY; 133 private $image; 134 private $zoom; 135 private $lat; 136 private $lon; 137 private $width; 138 private $height; 139 private $markers; 140 private $maptype; 141 private $kmlFileName; 142 private $gpxFileName; 143 private $geojsonFileName; 144 private $autoZoomExtent; 145 private $apikey; 146 private $tileCacheBaseDir; 147 private $mapCacheBaseDir; 148 private $mediaBaseDir; 149 private $useTileCache = true; 150 private $mapCacheID = ''; 151 private $mapCacheFile = ''; 152 private $mapCacheExtension = 'png'; 153 154 /** 155 * Constructor. 156 * 157 * @param float $lat 158 * Latitude (x) of center of map 159 * @param float $lon 160 * Longitude (y) of center of map 161 * @param int $zoom 162 * Zoomlevel 163 * @param int $width 164 * Width in pixels 165 * @param int $height 166 * Height in pixels 167 * @param string $maptype 168 * Name of the map 169 * @param array $markers 170 * array of markers 171 * @param string $gpx 172 * GPX filename 173 * @param string $kml 174 * KML filename 175 * @param string $mediaDir 176 * Directory to store/cache maps 177 * @param string $tileCacheBaseDir 178 * Directory to cache map tiles 179 * @param bool $autoZoomExtent 180 * Wheter or not to override zoom/lat/lon and zoom to the extent of gpx/kml and markers 181 * @param string apikey 182 * Some service require a key to access 183 */ 184 public function __construct( 185 float $lat, 186 float $lon, 187 int $zoom, 188 int $width, 189 int $height, 190 string $maptype, 191 array $markers, 192 string $gpx, 193 string $kml, 194 string $geojson, 195 string $mediaDir, 196 string $tileCacheBaseDir, 197 bool $autoZoomExtent = true, 198 string $apikey = '' 199 ) { 200 $this->zoom = $zoom; 201 $this->lat = $lat; 202 $this->lon = $lon; 203 $this->width = $width; 204 $this->height = $height; 205 // validate + set maptype 206 $this->maptype = $this->tileDefaultSrc; 207 if(array_key_exists($maptype, $this->tileInfo)) { 208 $this->maptype = $maptype; 209 } 210 $this->markers = $markers; 211 $this->kmlFileName = $kml; 212 $this->gpxFileName = $gpx; 213 $this->geojsonFileName = $geojson; 214 $this->mediaBaseDir = $mediaDir; 215 $this->tileCacheBaseDir = $tileCacheBaseDir . '/olmaptiles'; 216 $this->useTileCache = $this->tileCacheBaseDir !== ''; 217 $this->mapCacheBaseDir = $mediaDir . '/olmapmaps'; 218 $this->autoZoomExtent = $autoZoomExtent; 219 $this->apikey = $apikey; 220 } 221 222 /** 223 * get the map, this may return a reference to a cached copy. 224 * 225 * @return string url relative to media dir 226 */ 227 public function getMap(): string { 228 try { 229 if($this->autoZoomExtent) { 230 $this->autoZoom(); 231 } 232 } catch(Exception $e) { 233 dbglog($e); 234 } 235 236 // use map cache, so check cache for map 237 if(!$this->checkMapCache()) { 238 // map is not in cache, needs to be build 239 $this->makeMap(); 240 $this->mkdirRecursive(dirname($this->mapCacheIDToFilename()), 0777); 241 imagepng($this->image, $this->mapCacheIDToFilename(), 9); 242 } 243 $this->doc = $this->mapCacheIDToFilename(); 244 // make url relative to media dir 245 return str_replace($this->mediaBaseDir, '', $this->doc); 246 } 247 248 /** 249 * Calculate the lat/lon/zoom values to make sure that all of the markers and gpx/kml are on the map. 250 * can throw an error like 251 * "Fatal error: Uncaught Exception: Cannot create a collection with non-geometries in 252 * D:\www\wild-water.nl\www\dokuwiki\lib\plugins\geophp\geoPHP\lib\geometry\Collection.class.php:29" 253 * 254 * @param float $paddingFactor 255 * buffer constant to enlarge (>1.0) the zoom level 256 */ 257 private function autoZoom(float $paddingFactor = 1.0): void { 258 $geoms = array(); 259 $geoms [] = new Point ($this->lon, $this->lat); 260 if(!empty ($this->markers)) { 261 foreach($this->markers as $marker) { 262 $geoms [] = new Point ($marker ['lon'], $marker ['lat']); 263 } 264 } 265 $g = false; 266 if(file_exists($this->kmlFileName)) { 267 $g = geoPHP::load(file_get_contents($this->kmlFileName), 'kml'); 268 if($g !== false) { 269 $geoms [] = $g; 270 } 271 } 272 if(file_exists($this->gpxFileName)) { 273 $g = geoPHP::load(file_get_contents($this->gpxFileName), 'gpx'); 274 if($g !== false) { 275 $geoms [] = $g; 276 } 277 } 278 if(file_exists($this->geojsonFileName)) { 279 $g = geoPHP::load(file_get_contents($this->geojsonFileName), 'geojson'); 280 if($g !== false) { 281 $geoms [] = $g; 282 } 283 } 284 285 if(count($geoms) <= 1) { 286 dbglog($geoms, "StaticMap::autoZoom: Skip setting autozoom options"); 287 return; 288 } 289 290 $geom = new GeometryCollection ($geoms); 291 $centroid = $geom->centroid(); 292 $bbox = $geom->getBBox(); 293 294 // determine vertical resolution, this depends on the distance from the equator 295 // $vy00 = log(tan(M_PI*(0.25 + $centroid->getY()/360))); 296 $vy0 = log(tan(M_PI * (0.25 + $bbox ['miny'] / 360))); 297 $vy1 = log(tan(M_PI * (0.25 + $bbox ['maxy'] / 360))); 298 dbglog("StaticMap::autoZoom: vertical resolution: $vy0, $vy1"); 299 $zoomFactorPowered = ($this->height / 2) / (40.7436654315252 * ($vy1 - $vy0)); 300 $resolutionVertical = 360 / ($zoomFactorPowered * $this->tileSize); 301 // determine horizontal resolution 302 $resolutionHorizontal = ($bbox ['maxx'] - $bbox ['minx']) / $this->width; 303 $resolution = max($resolutionHorizontal, $resolutionVertical) * $paddingFactor; 304 $zoom = log(360 / ($resolution * $this->tileSize), 2); 305 306 if(is_finite($zoom) && $zoom < 15 && $zoom > 2) { 307 $this->zoom = floor($zoom); 308 } 309 $this->lon = $centroid->getX(); 310 $this->lat = $centroid->getY(); 311 dbglog("StaticMap::autoZoom: Set autozoom options to: z: $this->zoom, lon: $this->lon, lat: $this->lat"); 312 } 313 314 public function checkMapCache(): bool { 315 // side effect: set the mapCacheID 316 $this->mapCacheID = md5($this->serializeParams()); 317 $filename = $this->mapCacheIDToFilename(); 318 return file_exists($filename); 319 } 320 321 public function serializeParams() { 322 return join( 323 "&", array( 324 $this->zoom, 325 $this->lat, 326 $this->lon, 327 $this->width, 328 $this->height, 329 serialize($this->markers), 330 $this->maptype, 331 $this->kmlFileName, 332 $this->gpxFileName, 333 $this->geojsonFileName 334 ) 335 ); 336 } 337 338 public function mapCacheIDToFilename(): string { 339 if(!$this->mapCacheFile) { 340 $this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_" 341 . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2) 342 . "/" . substr($this->mapCacheID, 4); 343 } 344 return $this->mapCacheFile . "." . $this->mapCacheExtension; 345 } 346 347 /** 348 * make the map. 349 */ 350 public function makeMap(): void { 351 $this->initCoords(); 352 $this->createBaseMap(); 353 if(!empty ($this->markers)) 354 $this->placeMarkers(); 355 if(file_exists($this->kmlFileName)) 356 $this->drawKML(); 357 if(file_exists($this->gpxFileName)) 358 $this->drawGPX(); 359 if(file_exists($this->geojsonFileName)) 360 $this->drawGeojson(); 361 362 $this->drawCopyright(); 363 } 364 365 /** 366 */ 367 public function initCoords(): void { 368 $this->centerX = $this->lonToTile($this->lon, $this->zoom); 369 $this->centerY = $this->latToTile($this->lat, $this->zoom); 370 $this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize); 371 $this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize); 372 } 373 374 /** 375 * 376 * @param float $long 377 * @param int $zoom 378 * @return float|int 379 */ 380 public function lonToTile(float $long, int $zoom) { 381 return (($long + 180) / 360) * pow(2, $zoom); 382 } 383 384 /** 385 * 386 * @param float $lat 387 * @param int $zoom 388 * @return float|int 389 */ 390 public function latToTile(float $lat, int $zoom) { 391 return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * M_PI / 180)) / M_PI) / 2 * pow(2, $zoom); 392 } 393 394 /** 395 * make basemap image. 396 */ 397 public function createBaseMap(): void { 398 $this->image = imagecreatetruecolor($this->width, $this->height); 399 $startX = floor($this->centerX - ($this->width / $this->tileSize) / 2); 400 $startY = floor($this->centerY - ($this->height / $this->tileSize) / 2); 401 $endX = ceil($this->centerX + ($this->width / $this->tileSize) / 2); 402 $endY = ceil($this->centerY + ($this->height / $this->tileSize) / 2); 403 $this->offsetX = -floor(($this->centerX - floor($this->centerX)) * $this->tileSize); 404 $this->offsetY = -floor(($this->centerY - floor($this->centerY)) * $this->tileSize); 405 $this->offsetX += floor($this->width / 2); 406 $this->offsetY += floor($this->height / 2); 407 $this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize; 408 $this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize; 409 410 for($x = $startX; $x <= $endX; $x++) { 411 for($y = $startY; $y <= $endY; $y++) { 412 $url = str_replace( 413 array( 414 '{Z}', 415 '{X}', 416 '{Y}' 417 ), array( 418 $this->zoom, 419 $x, 420 $y 421 ), $this->tileInfo [$this->maptype] ['url'] 422 ); 423 $url .= $this->apikey; 424 425 $tileData = $this->fetchTile($url); 426 if($tileData) { 427 $tileImage = imagecreatefromstring($tileData); 428 } else { 429 $tileImage = imagecreate($this->tileSize, $this->tileSize); 430 $color = imagecolorallocate($tileImage, 255, 255, 255); 431 @imagestring($tileImage, 1, 127, 127, 'err', $color); 432 } 433 $destX = ($x - $startX) * $this->tileSize + $this->offsetX; 434 $destY = ($y - $startY) * $this->tileSize + $this->offsetY; 435 dbglog($this->tileSize, "imagecopy tile into image: $destX, $destY"); 436 imagecopy( 437 $this->image, $tileImage, $destX, $destY, 0, 0, $this->tileSize, 438 $this->tileSize 439 ); 440 } 441 } 442 } 443 444 /** 445 * Fetch a tile and (if configured) store it in the cache. 446 * 447 * @param string $url 448 */ 449 public function fetchTile($url) { 450 if($this->useTileCache && ($cached = $this->checkTileCache($url))) 451 return $cached; 452 453 $_UA = 'Mozilla/4.0 (compatible; DokuWikiSpatial HTTP Client; ' . PHP_OS . ')'; 454 if(function_exists("curl_init")) { 455 // use cUrl 456 $ch = curl_init(); 457 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 458 curl_setopt($ch, CURLOPT_USERAGENT, $_UA); 459 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 460 curl_setopt($ch, CURLOPT_URL, $url); 461 $tile = curl_exec($ch); 462 curl_close($ch); 463 } else { 464 // use file_get_contents 465 global $conf; 466 $opts = array( 467 'http' => array( 468 'method' => "GET", 469 'header' => "Accept-language: en\r\n" . "User-Agent: $_UA\r\n" . "accept: image/png\r\n", 470 'proxy' => "tcp://" . $conf ['proxy'] ['host'] . ":" . $conf ['proxy'] ['port'], 471 'request_fulluri' => true 472 ) 473 ); 474 $context = stream_context_create($opts); 475 $tile = file_get_contents($url, false, $context); 476 } 477 if($tile && $this->useTileCache) { 478 $this->writeTileToCache($url, $tile); 479 } 480 return $tile; 481 } 482 483 /** 484 * 485 * @param string $url 486 */ 487 public function checkTileCache($url) { 488 $filename = $this->tileUrlToFilename($url); 489 if(file_exists($filename)) { 490 return file_get_contents($filename); 491 } 492 } 493 494 /** 495 * 496 * @param string $url 497 * @return string 498 */ 499 public function tileUrlToFilename(string $url): string { 500 return $this->tileCacheBaseDir . "/" . str_replace( 501 array( 502 'http://' 503 ), '', $url 504 ); 505 } 506 507 /** 508 * Write a tile into the cache. 509 * 510 * @param string $url 511 * @param mixed $data 512 */ 513 public function writeTileToCache($url, $data): void { 514 $filename = $this->tileUrlToFilename($url); 515 $this->mkdirRecursive(dirname($filename), 0777); 516 file_put_contents($filename, $data); 517 } 518 519 /** 520 * Recursively create the directory. 521 * 522 * @param string $pathname 523 * The directory path. 524 * @param int $mode 525 * File access mode. For more information on modes, read the details on the chmod manpage. 526 */ 527 public function mkdirRecursive(string $pathname, int $mode): bool { 528 is_dir(dirname($pathname)) || $this->mkdirRecursive(dirname($pathname), $mode); 529 return is_dir($pathname) || mkdir($pathname, $mode) || is_dir($pathname); 530 } 531 532 /** 533 * Place markers on the map and number them in the same order as they are listed in the html. 534 */ 535 public function placeMarkers(): void { 536 $count = 0; 537 $color = imagecolorallocate($this->image, 0, 0, 0); 538 $bgcolor = imagecolorallocate($this->image, 200, 200, 200); 539 $markerBaseDir = __DIR__ . '/icons'; 540 // loop thru marker array 541 foreach($this->markers as $marker) { 542 // set some local variables 543 $markerLat = $marker ['lat']; 544 $markerLon = $marker ['lon']; 545 $markerType = $marker ['type']; 546 // clear variables from previous loops 547 $markerFilename = ''; 548 $markerShadow = ''; 549 $matches = false; 550 // check for marker type, get settings from markerPrototypes 551 if($markerType) { 552 foreach($this->markerPrototypes as $markerPrototype) { 553 if(preg_match($markerPrototype ['regex'], $markerType, $matches)) { 554 $markerFilename = $matches [0] . $markerPrototype ['extension']; 555 if($markerPrototype ['offsetImage']) { 556 list ($markerImageOffsetX, $markerImageOffsetY) = explode( 557 ",", 558 $markerPrototype ['offsetImage'] 559 ); 560 } 561 $markerShadow = $markerPrototype ['shadow']; 562 if($markerShadow) { 563 list ($markerShadowOffsetX, $markerShadowOffsetY) = explode( 564 ",", 565 $markerPrototype ['offsetShadow'] 566 ); 567 } 568 } 569 } 570 } 571 // create img resource 572 if(file_exists($markerBaseDir . '/' . $markerFilename)) { 573 $markerImg = imagecreatefrompng($markerBaseDir . '/' . $markerFilename); 574 } else { 575 $markerImg = imagecreatefrompng($markerBaseDir . '/marker.png'); 576 } 577 // check for shadow + create shadow recource 578 if($markerShadow && file_exists($markerBaseDir . '/' . $markerShadow)) { 579 $markerShadowImg = imagecreatefrompng($markerBaseDir . '/' . $markerShadow); 580 } 581 // calc position 582 $destX = floor( 583 ($this->width / 2) - 584 $this->tileSize * ($this->centerX - $this->lonToTile($markerLon, $this->zoom)) 585 ); 586 $destY = floor( 587 ($this->height / 2) - 588 $this->tileSize * ($this->centerY - $this->latToTile($markerLat, $this->zoom)) 589 ); 590 // copy shadow on basemap 591 if($markerShadow && $markerShadowImg) { 592 imagecopy( 593 $this->image, 594 $markerShadowImg, 595 $destX + (int) $markerShadowOffsetX, 596 $destY + (int) $markerShadowOffsetY, 597 0, 598 0, 599 imagesx($markerShadowImg), 600 imagesy($markerShadowImg) 601 ); 602 } 603 // copy marker on basemap above shadow 604 imagecopy( 605 $this->image, 606 $markerImg, 607 $destX + (int) $markerImageOffsetX, 608 $destY + (int) $markerImageOffsetY, 609 0, 610 0, 611 imagesx($markerImg), 612 imagesy($markerImg) 613 ); 614 // add label 615 imagestring( 616 $this->image, 617 3, 618 $destX - imagesx($markerImg) + 1, 619 $destY + (int) $markerImageOffsetY + 1, 620 ++$count, 621 $bgcolor 622 ); 623 imagestring( 624 $this->image, 625 3, 626 $destX - imagesx($markerImg), 627 $destY + (int) $markerImageOffsetY, 628 $count, 629 $color 630 ); 631 } 632 } 633 634 /** 635 * Draw kml trace on the map. 636 */ 637 public function drawKML(): void { 638 // TODO get colour from kml node (not currently supported in geoPHP) 639 $col = imagecolorallocatealpha($this->image, 255, 0, 0, .4 * 127); 640 $kmlgeom = geoPHP::load(file_get_contents($this->kmlFileName), 'kml'); 641 $this->drawGeometry($kmlgeom, $col); 642 } 643 644 /** 645 * Draw geometry or geometry collection on the map. 646 * 647 * @param Geometry $geom 648 * @param int $colour 649 * drawing colour 650 */ 651 private function drawGeometry(Geometry $geom, int $colour): void { 652 if(empty($geom)) { 653 return; 654 } 655 656 switch($geom->geometryType()) { 657 case 'GeometryCollection' : 658 // recursively draw part of the collection 659 for($i = 1; $i < $geom->numGeometries() + 1; $i++) { 660 $_geom = $geom->geometryN($i); 661 $this->drawGeometry($_geom, $colour); 662 } 663 break; 664 case 'MultiPolygon' : 665 case 'MultiLineString' : 666 case 'MultiPoint' : 667 // TODO implement / do nothing 668 break; 669 case 'Polygon' : 670 $this->drawPolygon($geom, $colour); 671 break; 672 case 'LineString' : 673 $this->drawLineString($geom, $colour); 674 break; 675 case 'Point' : 676 $this->drawPoint($geom, $colour); 677 break; 678 default : 679 // draw nothing 680 break; 681 } 682 } 683 684 /** 685 * Draw a polygon on the map. 686 * 687 * @param Polygon $polygon 688 * @param int $colour 689 * drawing colour 690 */ 691 private function drawPolygon($polygon, $colour) { 692 // TODO implementation of drawing holes, 693 // maybe draw the polygon to an in-memory image and use imagecopy, draw polygon in col., draw holes in bgcol? 694 695 // print_r('Polygon:<br />'); 696 // print_r($polygon); 697 $extPoints = array(); 698 // extring is a linestring actually.. 699 $extRing = $polygon->exteriorRing(); 700 701 for($i = 1; $i < $extRing->numGeometries(); $i++) { 702 $p1 = $extRing->geometryN($i); 703 $x = floor( 704 ($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($p1->x(), $this->zoom)) 705 ); 706 $y = floor( 707 ($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($p1->y(), $this->zoom)) 708 ); 709 $extPoints [] = $x; 710 $extPoints [] = $y; 711 } 712 // print_r('points:('.($i-1).')<br />'); 713 // print_r($extPoints); 714 // imagepolygon ($this->image, $extPoints, $i-1, $colour ); 715 imagefilledpolygon($this->image, $extPoints, $i - 1, $colour); 716 } 717 718 /** 719 * Draw a line on the map. 720 * 721 * @param LineString $line 722 * @param int $colour 723 * drawing colour 724 */ 725 private function drawLineString($line, $colour) { 726 imagesetthickness($this->image, 2); 727 for($p = 1; $p < $line->numGeometries(); $p++) { 728 // get first pair of points 729 $p1 = $line->geometryN($p); 730 $p2 = $line->geometryN($p + 1); 731 // translate to paper space 732 $x1 = floor( 733 ($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($p1->x(), $this->zoom)) 734 ); 735 $y1 = floor( 736 ($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($p1->y(), $this->zoom)) 737 ); 738 $x2 = floor( 739 ($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($p2->x(), $this->zoom)) 740 ); 741 $y2 = floor( 742 ($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($p2->y(), $this->zoom)) 743 ); 744 // draw to image 745 imageline($this->image, $x1, $y1, $x2, $y2, $colour); 746 } 747 imagesetthickness($this->image, 1); 748 } 749 750 /** 751 * Draw a point on the map. 752 * 753 * @param Point $point 754 * @param int $colour 755 * drawing colour 756 */ 757 private function drawPoint($point, $colour) { 758 imagesetthickness($this->image, 2); 759 // translate to paper space 760 $cx = floor( 761 ($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($point->x(), $this->zoom)) 762 ); 763 $cy = floor( 764 ($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($point->y(), $this->zoom)) 765 ); 766 $r = 5; 767 // draw to image 768 // imageellipse($this->image, $cx, $cy,$r, $r, $colour); 769 imagefilledellipse($this->image, $cx, $cy, $r, $r, $colour); 770 // don't use imageellipse because the imagesetthickness function has 771 // no effect. So the better workaround is to use imagearc. 772 imagearc($this->image, $cx, $cy, $r, $r, 0, 359, $colour); 773 imagesetthickness($this->image, 1); 774 } 775 776 /** 777 * Draw gpx trace on the map. 778 */ 779 public function drawGPX() { 780 $col = imagecolorallocatealpha($this->image, 0, 0, 255, .4 * 127); 781 $gpxgeom = geoPHP::load(file_get_contents($this->gpxFileName), 'gpx'); 782 $this->drawGeometry($gpxgeom, $col); 783 } 784 785 /** 786 * Draw geojson on the map. 787 */ 788 public function drawGeojson() { 789 $col = imagecolorallocatealpha($this->image, 255, 0, 255, .4 * 127); 790 $gpxgeom = geoPHP::load(file_get_contents($this->geojsonFileName), 'json'); 791 $this->drawGeometry($gpxgeom, $col); 792 } 793 794 /** 795 * add copyright and origin notice and icons to the map. 796 */ 797 public function drawCopyright() { 798 $logoBaseDir = dirname(__FILE__) . '/' . 'logo/'; 799 $logoImg = imagecreatefrompng($logoBaseDir . $this->tileInfo ['openstreetmap'] ['logo']); 800 $textcolor = imagecolorallocate($this->image, 0, 0, 0); 801 $bgcolor = imagecolorallocate($this->image, 200, 200, 200); 802 803 imagecopy( 804 $this->image, 805 $logoImg, 806 0, 807 imagesy($this->image) - imagesy($logoImg), 808 0, 809 0, 810 imagesx($logoImg), 811 imagesy($logoImg) 812 ); 813 imagestring( 814 $this->image, 815 1, 816 imagesx($logoImg) + 2, 817 imagesy($this->image) - imagesy($logoImg) + 1, 818 $this->tileInfo ['openstreetmap'] ['txt'], 819 $bgcolor 820 ); 821 imagestring( 822 $this->image, 823 1, 824 imagesx($logoImg) + 1, 825 imagesy($this->image) - imagesy($logoImg), 826 $this->tileInfo ['openstreetmap'] ['txt'], 827 $textcolor 828 ); 829 830 // additional tile source info, ie. who created/hosted the tiles 831 if($this->maptype != 'openstreetmap') { 832 $iconImg = imagecreatefrompng($logoBaseDir . $this->tileInfo [$this->maptype] ['logo']); 833 imagecopy( 834 $this->image, 835 $iconImg, imagesx($logoImg) + 1, 836 imagesy($this->image) - imagesy($iconImg), 837 0, 838 0, 839 imagesx($iconImg), imagesy($iconImg) 840 ); 841 imagestring( 842 $this->image, 843 1, imagesx($logoImg) + imagesx($iconImg) + 4, 844 imagesy($this->image) - ceil(imagesy($logoImg) / 2) + 1, 845 $this->tileInfo [$this->maptype] ['txt'], 846 $bgcolor 847 ); 848 imagestring( 849 $this->image, 850 1, imagesx($logoImg) + imagesx($iconImg) + 3, 851 imagesy($this->image) - ceil(imagesy($logoImg) / 2), 852 $this->tileInfo [$this->maptype] ['txt'], 853 $textcolor 854 ); 855 } 856 } 857} 858