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