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