1<?php 2 3/* 4 * Copyright (c) 2008-2022 Mark C. Prins <mprins@users.sf.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps 19 */ 20use dokuwiki\Extension\Plugin; 21use dokuwiki\plugin\openlayersmap\StaticMap; 22 23/** 24 * DokuWiki Plugin openlayersmap (staticmap Helper Component). 25 * This provides the interface to generate a static map based on predefined OSM layers. 26 * 27 * @author Mark Prins 28 */ 29class helper_plugin_openlayersmap_staticmap extends Plugin 30{ 31 /** maximum width of the resulting image. */ 32 private $maxWidth = 1024; 33 /** maximum heigth of the resulting image. */ 34 private $maxHeight = 1024; 35 36 /** 37 * Provide metadata of the public methods of this class. 38 * 39 * @return array Information to all provided methods. 40 */ 41 public function getMethods(): array 42 { 43 return [['name' => 'getMap', 'desc' => 'returns url to the image', 'params' => ['lat' => 'float', 'lon' => 'float', 'zoom' => 'integer', 'size' => 'string', 'maptype' => 'string', 'markers' => 'string', 'gpx' => 'string', 'kml' => 'string', 'geojson' => 'string', 'apikey' => 'string'], 'return' => ['image' => 'string']]]; 44 } 45 46 /** 47 * Create the map. 48 * 49 * @param float $lat the latitude of the map's center, eg. 40.714728 50 * @param float $lon the longitude of the map's center, eg -73.998672 51 * @param int $zoom the zoom level in the tile cache, eg. 14 52 * @param string $size the size in WxH px, eg. 512x512 53 * @param string $maptype the maptype, eg. cycle 54 * @param array $markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), 55 * eg. array('lat'=>40.702147,'lon'=>-74.015794,'type'=>lightblue1); 56 * @param string $gpx media link 57 * @param string $kml media link 58 * @param string $geojson media link 59 * @param string $apikey optional API key eg. for Thunderforest maps 60 */ 61 public function getMap( 62 float $lat, 63 float $lon, 64 int $zoom, 65 string $size, 66 string $maptype, 67 array $markers, 68 string $gpx, 69 string $kml, 70 string $geojson, 71 string $apikey = '' 72 ): string { 73 global $conf; 74 // dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :'); 75 76 // normalize zoom 77 $zoom = $zoom ?: 0; 78 if ($zoom > 18) { 79 $zoom = 18; 80 } 81 // normalize WxH 82 [$width, $height] = explode('x', $size); 83 $width = (int) $width; 84 if ($width > $this->maxWidth) { 85 $width = $this->maxWidth; 86 } 87 $height = (int) $height; 88 if ($height > $this->maxHeight) { 89 $height = $this->maxHeight; 90 } 91 92 // cleanup/validate gpx/kml 93 $kml = $this->mediaIdToPath($kml); 94 // dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:'); 95 $gpx = $this->mediaIdToPath($gpx); 96 // dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:'); 97 $geojson = $this->mediaIdToPath($geojson); 98 99 // create map 100 $map = new StaticMap( 101 $lat, 102 $lon, 103 $zoom, 104 $width, 105 $height, 106 $maptype, 107 $markers, 108 $gpx, 109 $kml, 110 $geojson, 111 $conf['mediadir'], 112 $conf['cachedir'], 113 $this->getConf('autoZoomMap'), 114 $apikey 115 ); 116 117 // return the media id url 118 // $mediaId = str_replace('/', ':', $map->getMap()); 119 // if($this->startsWith($mediaId,':')) { 120 // $mediaId = substr($mediaId, 1); 121 // } 122 // return $mediaId; 123 return str_replace('/', ':', $map->getMap()); 124 } 125 126 /** 127 * Constructs the path to a file. 128 * @param string $id the DW media id 129 * @return string the path to the file 130 */ 131 private function mediaIdToPath(string $id): string 132 { 133 global $conf; 134 if (empty($id)) { 135 return ""; 136 } 137 $id = str_replace(["[[", "]]"], "", $id); 138 if ((strpos($id, ':') === 0)) { 139 $id = substr($id, 1); 140 } 141 $id = str_replace(":", "/", $id); 142 return $conf['mediadir'] . '/' . $id; 143 } 144} 145