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