1628e43ccSMark Prins<?php 2628e43ccSMark Prins/* 3*c81a89cdSMark Prins * Copyright (c) 2008-2016 Mark C. Prins <mprins@users.sf.net> 4628e43ccSMark Prins * 5628e43ccSMark Prins * Permission to use, copy, modify, and distribute this software for any 6628e43ccSMark Prins * purpose with or without fee is hereby granted, provided that the above 7628e43ccSMark Prins * copyright notice and this permission notice appear in all copies. 8628e43ccSMark Prins * 9628e43ccSMark Prins * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10628e43ccSMark Prins * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11628e43ccSMark Prins * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12628e43ccSMark Prins * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13628e43ccSMark Prins * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14628e43ccSMark Prins * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15628e43ccSMark Prins * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16628e43ccSMark Prins */ 17628e43ccSMark Prinsif (!defined('DOKU_INC')) die(); 18628e43ccSMark Prinsif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 19628e43ccSMark Prinsrequire_once DOKU_PLUGIN.'openlayersmap/StaticMap.php'; 20628e43ccSMark Prins/** 21cc74a83cSMark Prins * DokuWiki Plugin openlayersmap (staticmap Helper Component). 22cc74a83cSMark Prins * This provides the interface to generate a static map based on predefined OSM layers. 23628e43ccSMark Prins * 24628e43ccSMark Prins * @author Mark Prins 25628e43ccSMark Prins */ 26628e43ccSMark Prinsclass helper_plugin_openlayersmap_staticmap extends DokuWiki_Plugin { 276eb3157eSMark Prins /** maximum width of the resulting image. */ 28628e43ccSMark Prins private $maxWidth = 1024; 296eb3157eSMark Prins /** maximum heigth of the resulting image. */ 30628e43ccSMark Prins private $maxHeight = 1024; 31cc74a83cSMark Prins 326eb3157eSMark Prins /** 336eb3157eSMark Prins * Provide metadata of the public methods of this class. 346eb3157eSMark Prins * 356eb3157eSMark Prins * @return array Information to all provided methods. 366eb3157eSMark Prins */ 37628e43ccSMark Prins function getMethods(){ 38628e43ccSMark Prins $result = array(); 39628e43ccSMark Prins $result[] = array( 40628e43ccSMark Prins 'name' => 'getMap', 41628e43ccSMark Prins 'desc' => 'returns url to the image', 42628e43ccSMark Prins 'params' => array( 43628e43ccSMark Prins 'center' => 'string', 44628e43ccSMark Prins 'zoom' => 'integer', 45628e43ccSMark Prins 'size' => 'string', 46628e43ccSMark Prins 'maptype' => 'string', 47628e43ccSMark Prins 'markers' => 'string', 48628e43ccSMark Prins 'gpx' => 'string', 496914b920SMark Prins 'kml' => 'string', 506914b920SMark Prins 'geojson' => 'string'), 51628e43ccSMark Prins 'return' => array('image' => 'string'), 52628e43ccSMark Prins ); 53628e43ccSMark Prins return $result; 54628e43ccSMark Prins } 55628e43ccSMark Prins 56628e43ccSMark Prins /** 57628e43ccSMark Prins * Create the map. 58628e43ccSMark Prins * 59628e43ccSMark Prins * @param number lat the latitude of the map's center, eg. 40.714728 60628e43ccSMark Prins * @param number lon the longitude of the map's center, eg -73.998672 61628e43ccSMark Prins * @param number zoom the zoom level in the tile cache, eg. 14 62628e43ccSMark Prins * @param mixed size the size in WxH px, eg. 512x512 63628e43ccSMark Prins * @param string maptype the maptype, eg. cycle 64628e43ccSMark Prins * @param mixed markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), eg. array( 'lat'=>40.702147, 'lon'=>-74.015794, 'type'=>lightblue1); 65628e43ccSMark Prins * @param string gpx media link 66628e43ccSMark Prins * @param string kml media link 676914b920SMark Prins * @param string geojson media link 68628e43ccSMark Prins * 69628e43ccSMark Prins * @return the media id url 70628e43ccSMark Prins */ 716914b920SMark Prins public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml, $geojson){ 72628e43ccSMark Prins global $conf; 736eb3157eSMark Prins // dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :'); 74628e43ccSMark Prins 75628e43ccSMark Prins // normalize zoom 76628e43ccSMark Prins $zoom = $zoom?intval($zoom):0; 77628e43ccSMark Prins if($zoom > 18) $zoom = 18; 78628e43ccSMark Prins // normalize WxH 79*c81a89cdSMark Prins list($width, $height) = explode('x',$size); 80628e43ccSMark Prins $width = intval($width); 81628e43ccSMark Prins if($width > $this->maxWidth) $width = $this->maxWidth; 82628e43ccSMark Prins $height = intval($height); 83628e43ccSMark Prins if($height > $this->maxHeight) $height = $this->maxHeight; 846eb3157eSMark Prins 85e61425c7SMark Prins // cleanup/validate gpx/kml 86e61425c7SMark Prins $kml = $this->mediaIdToPath($kml); 876eb3157eSMark Prins // dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:'); 88e61425c7SMark Prins $gpx = $this->mediaIdToPath($gpx); 896eb3157eSMark Prins // dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:'); 906914b920SMark Prins $geojson = $this->mediaIdToPath($geojson); 916c6bb022SMark Prins 92628e43ccSMark Prins // create map 9353bfe4a3SMark Prins $map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype, 946914b920SMark Prins $markers, $gpx, $kml, $geojson, $conf['mediadir'], $conf['cachedir'], 952d11d700SMark Prins $this->getConf('autoZoomMap') 96628e43ccSMark Prins ); 972d11d700SMark Prins 98628e43ccSMark Prins // return the media id url 99628e43ccSMark Prins $mediaId = str_replace('/', ':', $map->getMap()); 100fc16f3cdSMark Prins // if($this->startsWith($mediaId,':')) { 101fc16f3cdSMark Prins // $mediaId = substr($mediaId, 1); 102fc16f3cdSMark Prins // } 103628e43ccSMark Prins return $mediaId; 104628e43ccSMark Prins } 105e61425c7SMark Prins 106e61425c7SMark Prins private function startsWith($haystack, $needle) { 107e61425c7SMark Prins $length = strlen($needle); 108e61425c7SMark Prins return (substr($haystack, 0, $length) === $needle); 109e61425c7SMark Prins } 110e61425c7SMark Prins 111cc74a83cSMark Prins /** 112cc74a83cSMark Prins * Constructs the path to a file. 113cc74a83cSMark Prins * @param string $id the DW media id 114cc74a83cSMark Prins * @return the path to the file 115cc74a83cSMark Prins */ 116e61425c7SMark Prins private function mediaIdToPath($id){ 117e61425c7SMark Prins global $conf; 118e61425c7SMark Prins if(empty($id)) { 119e61425c7SMark Prins return ""; 120e61425c7SMark Prins } 121e61425c7SMark Prins $id=str_replace("[[","",$id); 122e61425c7SMark Prins $id=str_replace("]]","",$id); 123e61425c7SMark Prins if($this->startsWith($id,':')) { 124e61425c7SMark Prins $id = substr($id, 1); 125e61425c7SMark Prins } 126e61425c7SMark Prins $id=str_replace(":","/",$id); 127e61425c7SMark Prins return $conf['mediadir'].'/'.$id; 128e61425c7SMark Prins } 129628e43ccSMark Prins} 130