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