xref: /plugin/openlayersmap/syntax/olmap.php (revision cc74a83ce15a73db93d0109fec7ef672a03e2e99)
1<?php
2/*
3 * Copyright (c) 2008-2012 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'))define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
18if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
19require_once (DOKU_PLUGIN . 'syntax.php');
20
21/**
22 * DokuWiki Plugin openlayersmap (Syntax Component).
23 * Provides for display of an OpenLayers based map in a wiki page.
24 *
25 * @author Mark Prins
26 */
27class syntax_plugin_openlayersmap_olmap extends DokuWiki_Syntax_Plugin {
28
29	/** defaults of the known attributes of the olmap tag. */
30	private $dflt = array (
31			'id'		=> 'olmap',
32			'width'		=> '550px',
33			'height'	=> '450px',
34			'lat'		=> 50.0,
35			'lon'		=> 5.1,
36			'zoom'		=> 12,
37			'toolbar'	=> true,
38			'statusbar'	=> true,
39			'controls'	=> true,
40			'poihoverstyle'	=> false,
41			'baselyr'	=> 'OpenStreetMap',
42			'gpxfile'	=> '',
43			'kmlfile'	=> '',
44			'summary'	=> ''
45	);
46
47	private $usingLocalStaticMap = false;
48
49	/**
50	 * (non-PHPdoc)
51	 * @see DokuWiki_Syntax_Plugin::getType()
52	 */
53	function getType() {
54		return 'substition';
55	}
56
57	/**
58	 * (non-PHPdoc)
59	 * @see DokuWiki_Syntax_Plugin::getPType()
60	 */
61	function getPType() {
62		return 'block';
63	}
64
65	/**
66	 * (non-PHPdoc)
67	 * @see Doku_Parser_Mode::getSort()
68	 */
69	function getSort() {
70		return 901;
71	}
72	/**
73	 * (non-PHPdoc)
74	 * @see Doku_Parser_Mode::connectTo()
75	 */
76	function connectTo($mode) {
77		$this->Lexer->addSpecialPattern('<olmap ?[^>\n]*>.*?</olmap>', $mode, 'plugin_openlayersmap_olmap');
78	}
79	/**
80	 * (non-PHPdoc)
81	 * @see DokuWiki_Syntax_Plugin::handle()
82	 */
83	function handle($match, $state, $pos, &$handler) {
84		// break matched cdata into its components
85		list ($str_params, $str_points) = explode('>', substr($match, 7, -9), 2);
86		// get the lat/lon for adding them to the metadata (used by geotag)
87		preg_match('(lat[:|=]\"-?\d*\.\d*\")',$match,$mainLat);
88		preg_match('(lon[:|=]\"-?\d*\.\d*\")',$match,$mainLon);
89		$mainLat=substr($mainLat[0],5,-1);
90		$mainLon=substr($mainLon[0],5,-1);
91
92		$gmap = $this->_extract_params($str_params);
93		$overlay = $this->_extract_points($str_points);
94
95		// choose maptype based on tag
96		$imgUrl = "{{";
97		if (stripos($gmap['baselyr'],'google') !== false){
98			// use google
99			$imgUrl .= $this->_getGoogle($gmap, $overlay);
100			$imgUrl .="&.png";
101		} elseif (stripos($gmap['baselyr'],'ve') !== false){
102			// use bing
103			$imgUrl .= $this->_getBing($gmap, $overlay);
104			$imgUrl .="&.png";
105		} elseif (stripos($gmap['baselyr'],'bing') !== false){
106			// use bing
107			$imgUrl .= $this->_getBing($gmap, $overlay);
108			$imgUrl .="&.png";
109		} elseif (stripos($gmap['baselyr'],'mapquest') !== false){
110			if($this->getConf('optionStaticMapGenerator')=='remote'){
111				// use mapquest remote
112				$imgUrl .=$this->_getMapQuest($gmap,$overlay);
113				$imgUrl .="&.png";
114			} else{
115				$imgUrl .=$this->_getStaticOSM($gmap,$overlay);
116			}
117		} else {
118			$imgUrl .=$this->_getStaticOSM($gmap,$overlay);
119		}
120
121		// append dw p_render specific params and render
122		$imgUrl .="?".str_replace("px", "",$gmap['width'])."x".str_replace("px", "",$gmap['height']);
123		$imgUrl .= "&nolink";
124		$imgUrl .= " |".$gmap['summary']." }}";
125		$imgUrl=p_render("xhtml", p_get_instructions($imgUrl), $info);
126
127		$mapid = $gmap['id'];
128		// create a javascript parameter string for the map
129		$param = '';
130		foreach ($gmap as $key => $val) {
131			$param .= is_numeric($val) ? "$key: $val, " : "$key: '" . hsc($val) . "', ";
132		}
133		if (!empty ($param)) {
134			$param = substr($param, 0, -2);
135		}
136		unset ($gmap['id']);
137
138		// create a javascript serialisation of the point data
139		$poi = '';
140		$poitable='';
141		$rowId=0;
142		if (!empty ($overlay)) {
143			foreach ($overlay as $data) {
144				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
145				$rowId++;
146				$poi .= ", {lat: $lat, lon: $lon, txt: '$text', angle: $angle, opacity: $opacity, img: '$img', rowId: $rowId}";
147				$poitable .='
148				<tr>
149				<td class="rowId">'.$rowId.'</td>
150				<td class="icon"><img src="'.DOKU_BASE.'lib/plugins/openlayersmap/icons/'.$img.'" alt="icon" /></td>
151				<td class="lat" title="'.$this->getLang('olmapPOIlatTitle').'">'.$lat.'</td>
152				<td class="lon" title="'.$this->getLang('olmapPOIlonTitle').'">'.$lon.'</td>
153				<td class="txt">'.$text.'</td>
154				</tr>';
155			}
156			$poi = substr($poi, 2);
157		}
158		if (!empty ($gmap['kmlfile'])) {
159			$poitable .='
160			<tr>
161			<td class="rowId"><img src="'.DOKU_BASE.'lib/plugins/openlayersmap/toolbar/kml_file.png" alt="KML icon" /></td>
162			<td class="icon"><img src="'.DOKU_BASE.'lib/plugins/openlayersmap/toolbar/kml_line.png" alt="icon" /></td>
163			<td class="txt" colspan="3">KML track: '.$this->getFileName($gmap['kmlfile']).'</td>
164			</tr>';
165		}
166		if (!empty ($gmap['gpxfile'])) {
167			$poitable .='
168			<tr>
169			<td class="rowId"><img src="'.DOKU_BASE.'lib/plugins/openlayersmap/toolbar/gpx_file.png" alt="GPX icon" /></td>
170			<td class="icon"><img src="'.DOKU_BASE.'lib/plugins/openlayersmap/toolbar/gpx_line.png" alt="icon" /></td>
171			<td class="txt" colspan="3">GPX track: '.$this->getFileName($gmap['gpxfile']).'</td>
172			</tr>';
173		}
174
175		$js .= "{mapOpts:{" . $param . " },poi:[$poi]};";
176		// unescape the json
177		$poitable = stripslashes($poitable);
178
179		return array($mapid,$js,$mainLat,$mainLon,$poitable,$gmap['summary'],$imgUrl);
180	}
181
182	/**
183	 * (non-PHPdoc)
184	 * @see DokuWiki_Syntax_Plugin::render()
185	 */
186	function render($mode, &$renderer, $data) {
187		static $initialised = false; // set to true after script initialisation
188		static $mapnumber = 0; // incremented for each map tag in the page source
189		list ($mapid, $param, $mainLat, $mainLon, $poitable, $poitabledesc, $staticImgUrl) = $data;
190
191		if ($mode == 'xhtml') {
192			$olscript = '';
193			$olEnable = false;
194			$gscript = '';
195			$gEnable = $this->getConf('enableGoogle');
196			$mqEnable = $this->getConf('enableMapQuest');
197			$osmEnable = $this->getConf('enableOSM');
198			$enableBing = $this->getConf('enableBing');
199
200			$scriptEnable = '';
201
202			if (!$initialised) {
203				$initialised = true;
204				// render necessary script tags
205				if($gEnable){
206					$gscript ='<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&amp;sensor=false"></script>';
207				}
208				$olscript = $this->getConf('olScriptUrl');
209				$olscript = $olscript ? '<script type="text/javascript" src="' . $olscript . '"></script>' : "";
210				$olscript = str_replace('DOKU_BASE/', DOKU_BASE, $olscript);
211
212				$scriptEnable = '<script type="text/javascript"><!--//--><![CDATA[//><!--' . "\n";
213				$scriptEnable .= $olscript ? 'olEnable = true;' : 'olEnable = false;';
214				$scriptEnable .= 'gEnable = '.($gEnable ? 'true' : 'false').';';
215				$scriptEnable .= 'osmEnable = '.($osmEnable ? 'true' : 'false').';';
216				$scriptEnable .= 'mqEnable = '.($mqEnable ? 'true' : 'false').';';
217				$scriptEnable .= 'bEnable = '.($enableBing ? 'true' : 'false').';';
218				$scriptEnable .= 'bApiKey="'.$this->getConf('bingAPIKey').'";';
219				$scriptEnable .= 'OpenLayers.ImgPath = "'.DOKU_BASE.'lib/plugins/openlayersmap/lib/'.$this->getConf('olMapStyle').'/";';
220				$scriptEnable .= "\n" . '//--><!]]></script>';
221			}
222			$renderer->doc .= "
223			$gscript
224			$olscript
225			$scriptEnable";
226			if ($this->getConf('enableA11y')){
227				$renderer->doc .= '<div id="'.$mapid.'-static" class="olStaticMap">'.$staticImgUrl.'</div>';
228			}
229			$renderer->doc .= '<div id="'.$mapid.'-clearer" class="clearer"><p>&nbsp;</p></div>';
230			if ($this->getConf('enableA11y')){
231				// render a (initially hidden) table of the POI for the print and a11y presentation
232				$renderer->doc .= '<div class="olPOItableSpan" id="'.$mapid.'-table-span"><table class="olPOItable" id="'.$mapid.'-table" summary="'.$poitabledesc.'" title="'.$this->getLang('olmapPOItitle').'">
233				<caption class="olPOITblCaption">'.$this->getLang('olmapPOItitle').'</caption>
234				<thead class="olPOITblHeader">
235				<tr>
236				<th class="rowId" scope="col">id</th>
237				<th class="icon" scope="col">'.$this->getLang('olmapPOIicon').'</th>
238				<th class="lat" scope="col" title="'.$this->getLang('olmapPOIlatTitle').'">'.$this->getLang('olmapPOIlat').'</th>
239				<th class="lon" scope="col" title="'.$this->getLang('olmapPOIlonTitle').'">'.$this->getLang('olmapPOIlon').'</th>
240				<th class="txt" scope="col">'.$this->getLang('olmapPOItxt').'</th>
241				</tr>
242				</thead>';
243				if ($poitabledesc !=''){
244					$renderer->doc .='<tfoot class="olPOITblFooter"><tr><td colspan="5">'.$poitabledesc.'</td></tr></tfoot>';
245				}
246				$renderer->doc .='<tbody class="olPOITblBody">'.$poitable.'</tbody>
247				</table></div>';
248			}
249			// render inline mapscript
250			$renderer->doc .="\n		<script type='text/javascript'><!--//--><![CDATA[//><!--\n";
251			$renderer->doc .="		olMapData[$mapnumber] = $param
252			//--><!]]></script>";
253			$mapnumber++;
254			return true;
255		} elseif ($mode == 'metadata') {
256			if (!(($this->dflt['lat']==$mainLat)||($thisdflt['lon']==$mainLon))){
257				// render metadata if available, unless they are the default
258				$renderer->meta['geo']['lat'] = $mainLat;
259				$renderer->meta['geo']['lon'] = $mainLon;
260			}
261			return true;
262		}
263		return false;
264	}
265
266	/**
267	 * extract parameters for the map from the parameter string
268	 *
269	 * @param   string    $str_params   string of key="value" pairs
270	 * @return  array                   associative array of parameters key=>value
271	 */
272	private function _extract_params($str_params) {
273		$param = array ();
274		preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);
275		// parse match for instructions, break into key value pairs
276		$gmap = $this->dflt;
277		foreach ($param as $kvpair) {
278			list ($match, $key, $val) = $kvpair;
279			$key = strtolower($key);
280			if (isset ($gmap[$key])){
281				if ($key == 'summary'){
282					// preserve case for summary field
283					$gmap[$key] = $val;
284				}else {
285					$gmap[$key] = strtolower($val);
286				}
287			}
288		}
289		return $gmap;
290	}
291
292	/**
293	 * extract overlay points for the map from the wiki syntax data
294	 *
295	 * @param   string    $str_points   multi-line string of lat,lon,text triplets
296	 * @return  array                   multi-dimensional array of lat,lon,text triplets
297	 */
298	private function _extract_points($str_points) {
299		$point = array ();
300		//preg_match_all('/^([+-]?[0-9].*?),\s*([+-]?[0-9].*?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER);
301		/*
302		group 1: ([+-]?[0-9]+(?:\.[0-9]*)?)
303		group 2: ([+-]?[0-9]+(?:\.[0-9]*)?)
304		group 3: (.*?)
305		group 4: (.*?)
306		group 5: (.*?)
307		group 6: (.*)
308		*/
309		preg_match_all('/^([+-]?[0-9]+(?:\.[0-9]*)?),\s*([+-]?[0-9]+(?:\.[0-9]*)?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER);
310		// create poi array
311		$overlay = array ();
312		foreach ($point as $pt) {
313			list ($match, $lat, $lon, $angle, $opacity, $img, $text) = $pt;
314			$lat = is_numeric($lat) ? $lat : 0;
315			$lon = is_numeric($lon) ? $lon : 0;
316			$angle = is_numeric($angle) ? $angle : 0;
317			$opacity = is_numeric($opacity) ? $opacity : 0.8;
318			$img = trim($img);
319			// TODO validate using exist & set up default img?
320			$text = addslashes(str_replace("\n", "", p_render("xhtml", p_get_instructions($text), $info)));
321			$overlay[] = array($lat, $lon, $text, $angle, $opacity, $img);
322		}
323		return $overlay;
324	}
325
326	/**
327	 * Create a MapQuest static map API image url.
328	 * @param array $gmap
329	 * @param array $overlay
330	 */
331	private function _getMapQuest($gmap,$overlay) {
332		$sUrl=$this->getConf('iconUrlOverload');
333		if (!$sUrl){
334			$sUrl=DOKU_URL;
335		}
336		switch ($gmap['baselyr']){
337			case 'mapquest hybrid':
338				$maptype='hyb';
339				break;
340			case 'mapquest sat':
341				// because sat coverage is very limited use 'hyb' instead of 'sat' so we don't get a blank map
342				$maptype='hyb';
343				break;
344			case 'mapquest road':
345			default:
346				$maptype='map';
347				break;
348		}
349		$imgUrl = "http://open.mapquestapi.com/staticmap/v3/getmap?declutter=true&";
350		if (count($overlay)< 1){
351			$imgUrl .= "?center=".$gmap['lat'].",".$gmap['lon'];
352			//max level for mapquest is 16
353			if ($gmap['zoom']>16) {
354				$imgUrl .= "&zoom=16";
355			} else			{
356				$imgUrl .= "&zoom=".$gmap['zoom'];
357			}
358		}
359		// use bestfit instead of center/zoom, needs upperleft/lowerright corners
360		//$bbox=$this->_calcBBOX($overlay, $gmap['lat'], $gmap['lon']);
361		//$imgUrl .= "bestfit=".$bbox['minlat'].",".$bbox['maxlon'].",".$bbox['maxlat'].",".$bbox['minlon'];
362
363		// TODO declutter option works well for square maps but not for rectangular, maybe compensate for that or compensate the mbr..
364		$imgUrl .= "&size=".str_replace("px", "",$gmap['width']).",".str_replace("px", "",$gmap['height']);
365
366		// TODO mapquest allows using one image url with a multiplier $NUMBER eg:
367		// $NUMBER = 2
368		// $imgUrl .= DOKU_URL."/".DOKU_PLUGIN."/".getPluginName()."/icons/".$img.",$NUMBER,C,".$lat1.",".$lon1.",0,0,0,0,C,".$lat2.",".$lon2.",0,0,0,0";
369		if (!empty ($overlay)) {
370			$imgUrl .= "&xis=";
371			foreach ($overlay as $data) {
372				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
373				//$imgUrl .= $sUrl."lib/plugins/openlayersmap/icons/".$img.",1,C,".$lat.",".$lon.",0,0,0,0,";
374				$imgUrl .= $sUrl."lib/plugins/openlayersmap/icons/".$img.",1,C,".$lat.",".$lon.",";
375			}
376			$imgUrl = substr($imgUrl,0,-1);
377		}
378		$imgUrl .= "&imageType=png&type=".$maptype;
379		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getMapQuest: MapQuest image url is:');
380		return $imgUrl;
381	}
382
383	/**
384	 * Create a Google maps static image url w/ the poi.
385	 * @param array $gmap
386	 * @param array $overlay
387	 */
388	private function _getGoogle($gmap, $overlay){
389		$sUrl=$this->getConf('iconUrlOverload');
390		if (!$sUrl){
391			$sUrl=DOKU_URL;
392		}
393		switch ($gmap['baselyr']){
394			case 'google hybrid':
395				$maptype='hybrid';
396				break;
397			case 'google sat':
398				$maptype='satellite';
399				break;
400			case 'google relief':
401				$maptype='terrain';
402				break;
403			case 'google road':
404			default:
405				$maptype='roadmap';
406				break;
407		}
408		// TODO maybe use viewport / visible instead of center/zoom,
409		//		see: https://code.google.com/intl/nl/apis/maps/documentation/staticmaps/#ImplicitPositioning
410		//http://maps.google.com/maps/api/staticmap?center=51.565690,5.456756&zoom=16&size=600x400&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/marker.png|label:1|51.565690,5.456756&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/marker-blue.png|51.566197,5.458966|label:2&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/parking.png|51.567177,5.457909|label:3&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/parking.png|51.566283,5.457330|label:4&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/parking.png|51.565630,5.457695|label:5&sensor=false&format=png&maptype=roadmap
411		$imgUrl = "http://maps.google.com/maps/api/staticmap?sensor=false";
412		$imgUrl .= "&size=".str_replace("px", "",$gmap['width'])."x".str_replace("px", "",$gmap['height']);
413		$imgUrl .= "&center=".$gmap['lat'].",".$gmap['lon'];
414		// max is 21 (== building scale), but that's overkill..
415		if ($gmap['zoom']>17) {
416			$imgUrl .= "&zoom=17";
417		} else			{
418			$imgUrl .= "&zoom=".$gmap['zoom'];
419		}
420
421		if (!empty ($overlay)) {
422			$rowId=0;
423			foreach ($overlay as $data) {
424				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
425				$imgUrl .= "&markers=icon%3a".$sUrl."lib/plugins/openlayersmap/icons/".$img."%7c".$lat.",".$lon."%7clabel%3a".++$rowId;
426			}
427		}
428		$imgUrl .= "&format=png&maptype=".$maptype;
429		global $conf;
430		$imgUrl .= "&language=".$conf['lang'];
431		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getGoogle: Google image url is:');
432		return $imgUrl;
433	}
434
435	/**
436	 * Create a Bing maps static image url w/ the poi.
437	 * @param array $gmap
438	 * @param array $overlay
439	 */
440	private function _getBing($gmap, $overlay){
441		if(!$this->getConf('bingAPIKey')){
442			// in case there is no Bing api key we'll use MapQuest
443			$this->_getMapQuest($gmap, $overlay);
444		}
445		switch ($gmap['baselyr']){
446			case 've hybrid':
447			case 'bing hybrid':
448				$maptype='AerialWithLabels';
449				break;
450			case 've sat':
451			case 'bing sat':
452				$maptype='Aerial';
453				break;
454			case 've normal':
455			case 've road':
456			case 've':
457			case 'bing road':
458			default:
459				$maptype='Road';
460				break;
461		}
462		$bbox=$this->_calcBBOX($overlay, $gmap['lat'], $gmap['lon']);
463		//$imgUrl = "http://dev.virtualearth.net/REST/v1/Imagery/Map/".$maptype."/".$gmap['lat'].",".$gmap['lon']."/".$gmap['zoom'];
464		$imgUrl = "http://dev.virtualearth.net/REST/v1/Imagery/Map/".$maptype."/";
465		$imgUrl .= "?mapArea=".$bbox['minlat'].",".$bbox['minlon'].",".$bbox['maxlat'].",".$bbox['maxlon'];
466		// TODO declutter option works well for square maps but not for rectangular, maybe compensate for that or compensate the mbr..
467		$imgUrl .= "&declutter=1";
468		$imgUrl .= "&ms=".str_replace("px", "",$gmap['width']).",".str_replace("px", "",$gmap['height']);
469		$imgUrl .= "&key=".$this->getConf('bingAPIKey');
470		if (!empty ($overlay)) {
471			$rowId=0;
472			foreach ($overlay as $data) {
473				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
474				// TODO icon style lookup, see: http://msdn.microsoft.com/en-us/library/ff701719.aspx for iconStyle
475				$iconStyle=32;
476				$rowId++;
477				// NOTE: the max number of pushpins is 18!
478				if ($rowId==18) {
479					break;
480				}
481				$imgUrl .= "&pp=$lat,$lon;$iconStyle;$rowId";
482			}
483		}
484		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getBing: bing image url is:');
485		return $imgUrl;
486	}
487
488	/**
489	 * Create a static OSM map image url w/ the poi from http://staticmap.openstreetmap.de (staticMapLite)
490	 * use http://staticmap.openstreetmap.de "staticMapLite" or a local version
491	 * @param array $gmap
492	 * @param array $overlay
493	 *
494	 * @todo implementation for http://ojw.dev.openstreetmap.org/StaticMapDev/
495	 */
496	private function _getStaticOSM($gmap, $overlay){
497		//http://staticmap.openstreetmap.de/staticmap.php?center=47.000622235634,10.117187497601&zoom=5&size=500x350
498		// &markers=48.999812532766,8.3593749976708,lightblue1|43.154850037315,17.499999997306,lightblue1|49.487527053077,10.820312497573,ltblu-pushpin|47.951071133739,15.917968747369,ol-marker|47.921629720114,18.027343747285,ol-marker-gold|47.951071133739,19.257812497236,ol-marker-blue|47.180141361692,19.257812497236,ol-marker-green
499		global $conf;
500
501		switch ( $this->getConf('optionStaticMapGenerator')){
502			case 'local':
503				$imgUrl =$conf['baseurl']."/lib/plugins/openlayersmap/staticmap/map.php";
504				$imgUrl .= '?kml='.$gmap['kmlfile'];
505				$imgUrl .= '&gpx='.$gmap['gpxfile'];
506				break;
507			case 'remote':
508				// fall through
509			default:
510				$imgUrl = "http://staticmap.openstreetmap.de/staticmap.php";
511				break;
512		}
513
514		$imgUrl .= "&center=".$gmap['lat'].",".$gmap['lon'];
515		$imgUrl .= "&size=".str_replace("px", "",$gmap['width'])."x".str_replace("px", "",$gmap['height']);
516		$size =str_replace("px", "",$gmap['width'])."x".str_replace("px", "",$gmap['height']);
517
518		if ($gmap['zoom']>16) {
519			// actually this could even be 18, but that seems overkill
520			$imgUrl .= "&zoom=16";
521		} else {
522			$imgUrl .= "&zoom=".$gmap['zoom'];
523		}
524
525		switch ($gmap['baselyr']){
526			case 'mapnik':
527			case 'openstreetmap':
528				$maptype='openstreetmap';
529				break;
530			case 'transport':
531				$maptype='transport';
532				break;
533			case 'landscape':
534				$maptype='landscape';
535				break;
536			case 'cycle map':
537				$maptype='cycle';
538				break;
539			case 'cloudmade':
540				$maptype='cloudmade';
541				break;
542			case 'cloudmade fresh':
543				$maptype='fresh';
544				break;
545			case 'hike and bike map':
546				$maptype='hikeandbike';
547				break;
548			case 'mapquest hybrid':
549			case 'mapquest road':
550			case 'mapquest sat':
551				$maptype='mapquest';
552				break;
553			default:
554				$maptype='';
555				break;
556		}
557		$imgUrl .= "&maptype=".$maptype;
558
559		$markers='';
560		if (!empty ($overlay)) {
561			$rowId=0;
562			$imgUrl .= "&markers=";
563			foreach ($overlay as $data) {
564				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
565				$rowId++;
566				if ($this->getConf('optionStaticMapGenerator')=='remote') $iconStyle = "lightblue$rowId";
567				if ($this->getConf('optionStaticMapGenerator')=='local') $iconStyle = substr($img, 0,strlen($img)-4);
568				$imgUrl .= "$lat,$lon,$iconStyle%7c";
569				$markers[] = array(
570						'lat'=>$lat,
571						'lon'=>$lon,
572						'type'=>$iconStyle);
573			}
574			$imgUrl = substr($imgUrl,0,-3);
575		}
576
577		if (!$my = &plugin_load('helper', 'openlayersmap_staticmap')){
578			dbglog($my,'syntax_plugin_openlayersmap_olmap::_getStaticOSM: openlayersmap_staticmap plugin is false');
579			return false;
580		}
581
582		if ($this->getConf('optionStaticMapGenerator')=='local') {
583			$result = $my->getMap($gmap['lat'],$gmap['lon'],$gmap['zoom'],$size,$maptype,$markers,$gmap['gpxfile'],$gmap['kmlfile']);
584		}else{
585			$result= $imgUrl;
586		}
587
588		dbglog($result,'syntax_plugin_openlayersmap_olmap::_getStaticOSM: osm image url is:');
589		return $result;
590	}
591
592	/**
593	 * Calculate the minimum bbox for a start location + poi.
594	 *
595	 * @param array $overlay  multi-dimensional array of array($lat, $lon, $text, $angle, $opacity, $img)
596	 * @param float $lat latitude for map center
597	 * @param float $lon longitude for map center
598	 * @return multitype:float array describing the mbr and center point
599	 */
600	private function _calcBBOX($overlay, $lat, $lon){
601		$lats[] = $lat;
602		$lons[] = $lon;
603		foreach ($overlay as $data) {
604			list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
605			$lats[] = $lat;
606			$lons[] = $lon;
607		}
608		sort($lats);
609		sort($lons);
610		// TODO: make edge/wrap around cases work
611		$centerlat = $lats[0]+($lats[count($lats)-1]-$lats[0]);
612		$centerlon = $lons[0]+($lons[count($lats)-1]-$lons[0]);
613		return array('minlat'=>$lats[0], 'minlon'=>$lons[0],
614				'maxlat'=>$lats[count($lats)-1], 'maxlon'=>$lons[count($lats)-1],
615				'centerlat'=>$centerlat,'centerlon'=>$centerlon);
616	}
617
618	/**
619	 * Figures out the base filename of a media path.
620	 * @param String $mediaLink
621	 */
622	private function getFileName($mediaLink){
623		$mediaLink=str_replace('[[','',$mediaLink);
624		$mediaLink=str_replace(']]','',$mediaLink);
625		$mediaLink = substr($mediaLink, 0, -4);
626		$parts=explode(':',$mediaLink);
627		$mediaLink = end($parts);
628		return str_replace('_',' ',$mediaLink);
629	}
630}