xref: /plugin/openlayersmap/syntax/olmap.php (revision 2a5c9dbfd2b5fc70300d709a4bdec43d8e68b1ce)
1<?php
2/*
3 * Copyright (c) 2008-2011 Mark C. Prins <mc.prins@gmail.com>
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*/
17
18/**
19 * Plugin OL Maps: Allow Display of a OpenLayers Map in a wiki page.
20 *
21 * @author Mark Prins
22 */
23
24if (!defined('DOKU_INC'))
25define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
26if (!defined('DOKU_PLUGIN'))
27define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
28require_once (DOKU_PLUGIN . 'syntax.php');
29
30/**
31 * All DokuWiki plugins to extend the parser/rendering mechanism
32 * need to inherit from this class
33 */
34class syntax_plugin_openlayersmap_olmap extends DokuWiki_Syntax_Plugin {
35	/** defaults of the known attributes of the olmap tag. */
36	private $dflt = array (
37		'id'		=> 'olmap',
38		'width'		=> '550px',
39		'height'	=> '450px',
40		'lat'		=> 50.0,
41		'lon'		=> 5.1,
42		'zoom'		=> 12,
43		'toolbar'	=> true,
44		'statusbar'	=> true,
45		'controls'	=> true,
46		'poihoverstyle'	=> false,
47		'baselyr'	=>'OpenStreetMap',
48	 	'gpxfile'	=> '',
49 		'kmlfile'	=> '',
50		'summary'	=>''
51	);
52
53	/**
54	 * Return the type of syntax this plugin defines.
55	 * @Override
56	 */
57	function getType() {
58		return 'substition';
59	}
60
61	/**
62	 * Defines how this syntax is handled regarding paragraphs.
63	 * @Override
64	 */
65	function getPType() {
66		//normal block stack
67		return 'block';
68	}
69
70	/**
71	 * Returns a number used to determine in which order modes are added.
72	 * @Override
73	 */
74	function getSort() {
75		return 901;
76	}
77
78	/**
79	 * This function is inherited from Doku_Parser_Mode.
80	 * Here is the place to register the regular expressions needed
81	 * to match your syntax.
82	 * @Override
83	 */
84	function connectTo($mode) {
85		$this->Lexer->addSpecialPattern('<olmap ?[^>\n]*>.*?</olmap>', $mode, 'plugin_openlayersmap_olmap');
86	}
87
88	/**
89	 * handle each olmap tag. prepare the matched syntax for use in the renderer.
90	 * @Override
91	 */
92	function handle($match, $state, $pos, &$handler) {
93		// break matched cdata into its components
94		list ($str_params, $str_points) = explode('>', substr($match, 7, -9), 2);
95		// get the lat/lon for adding them to the metadata (used by geotag)
96		preg_match('(lat[:|=]\"\d*\.\d*\")',$match,$mainLat);
97		preg_match('(lon[:|=]\"\d*\.\d*\")',$match,$mainLon);
98		$mainLat=substr($mainLat[0],5,-1);
99		$mainLon=substr($mainLon[0],5,-1);
100
101		$gmap = $this->_extract_params($str_params);
102		$overlay = $this->_extract_points($str_points);
103
104		$imgUrl = "{{";
105		// choose maptype based on tag
106		if (stripos($gmap['baselyr'],'google')>0){
107			// use google
108			$imgUrl .= $this->_getGoogle($gmap, $overlay);
109		}elseif (stripos($gmap['baselyr'],'ve')>0){
110			// use bing
111			$imgUrl .= $this->_getBing($gmap, $overlay);
112		}elseif (stripos($gmap['baselyr'],'mapquest')>0){
113			// use mapquest
114			$imgUrl .=$this->_getMapQuest($gmap,$overlay);
115		}else {
116			// use http://staticmap.openstreetmap.de
117			$imgUrl .=$this->_getStaticOSM($gmap,$overlay);
118		}
119
120		// append dw specific params
121		$imgUrl .="&.png?".$gmap['width']."x".$gmap['height'];
122		$imgUrl .= "&nolink";
123		$imgUrl .= " |".$gmap['summary']." }} ";
124		// remove 'px'
125		$imgUrl = str_replace("px", "",$imgUrl);
126
127		$imgUrl=p_render("xhtml", p_get_instructions($imgUrl), $info);
128
129		$mapid = $gmap['id'];
130
131		// determine width and height (inline styles) for the map image
132		// if ($gmap['width'] || $gmap['height']) {
133		//	$style = $gmap['width'] ? 'width: ' . $gmap['width'] . ";" : "";
134		//	$style .= $gmap['height'] ? 'height: ' . $gmap['height'] . ";" : "";
135		//	$style = "style='$style'";
136		// } else {
137		//	$style = '';
138		//}
139
140		// unset gmap values for width and height - they don't go into javascript
141		// unset ($gmap['width'], $gmap['height']);
142
143		// create a javascript parameter string for the map
144		$param = '';
145		foreach ($gmap as $key => $val) {
146			$param .= is_numeric($val) ? "$key: $val, " : "$key: '" . hsc($val) . "', ";
147		}
148		if (!empty ($param)) {
149			$param = substr($param, 0, -2);
150		}
151		unset ($gmap['id']);
152
153		// create a javascript serialisation of the point data
154		$poi = '';
155		$poitable='';
156		$rowId=0;
157		if (!empty ($overlay)) {
158			foreach ($overlay as $data) {
159				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
160				$rowId++;
161				$poi .= ", {lat: $lat, lon: $lon, txt: '$text', angle: $angle, opacity: $opacity, img: '$img', rowId: $rowId}";
162				$poitable .='
163			<tr>
164				<td class="rowId">'.$rowId.'</td>
165				<td class="icon"><img src="'.DOKU_BASE.'/lib/plugins/openlayersmap/icons/'.$img.'" alt="icon" /></td>
166				<td class="lat" title="'.$this->getLang('olmapPOIlatTitle').'">'.$lat.'</td>
167				<td class="lon" title="'.$this->getLang('olmapPOIlonTitle').'">'.$lon.'</td>
168				<td class="txt">'.$text.'</td>
169			</tr>';
170			}
171			$poi = substr($poi, 2);
172		}
173		$js .= "createMap({" . $param . " },[$poi]);";
174		// unescape the json
175		$poitable = stripslashes($poitable);
176
177		return array($mapid,$js,$mainLat,$mainLon,$poitable,$gmap['summary'],$imgUrl);
178	}
179
180	/**
181	 * render html tag/output. render the content.
182	 * @Override
183	 */
184	function render($mode, &$renderer, $data) {
185		static $initialised = false; // set to true after script initialisation
186		list ($mapid, $param, $mainLat, $mainLon, $poitable, $poitabledesc, $staticImgUrl) = $data;
187
188		if ($mode == 'xhtml') {
189			$olscript = '';
190			$olEnable = false;
191			$gscript = '';
192			$gEnable = $this->getConf('enableGoogle');
193			$vscript = '';
194			$vEnable = false;
195			//$yscript = '';
196			//$yEnable = false;
197			$mqEnable = $this->getConf('enableMapQuest');
198			$osmEnable = $this->getConf('enableOSM');
199			$enableBing = $this->getConf('enableBing');
200
201			$scriptEnable = '';
202
203			if (!$initialised) {
204				$initialised = true;
205				// render necessary script tags
206				// 				$gscript = $this->getConf('googleScriptUrl');
207				// 				$gscript = $gscript ? '<script type="text/javascript" src="' . $gscript . '"></script>' : "";
208				if($gEnable){
209					$gscript ='<script src="http://maps.google.com/maps/api/js?v=3&sensor=false"></script>';
210				}
211
212				$vscript = $this->getConf('veScriptUrl');
213				$vscript = $vscript ? '<script type="text/javascript" src="' . $vscript . '"></script>' : "";
214
215				//$yscript = $this->getConf('yahooScriptUrl');
216				//$yscript = $yscript ? '<script type="text/javascript" src="' . $yscript . '"></script>' : "";
217
218				$olscript = $this->getConf('olScriptUrl');
219				$olscript = $olscript ? '<script type="text/javascript" src="' . $olscript . '"></script>' : "";
220				$olscript = str_replace('DOKU_BASE/', DOKU_BASE, $olscript);
221
222				$scriptEnable = '<script type="text/javascript">' . "\n" . '<!--//--><![CDATA[//><!--' . "\n";
223				$scriptEnable .= $olscript ? 'olEnable = true;' : 'olEnable = false;';
224				//$scriptEnable .= $yscript ? ' yEnable = true;' : ' yEnable = false;';
225				$scriptEnable .= $vscript ? ' veEnable = true;' : ' veEnable = false;';
226				$scriptEnable .= 'gEnable = '.($gEnable ? 'true' : 'false').';';
227				$scriptEnable .= 'osmEnable = '.($osmEnable ? 'true' : 'false').';';
228				$scriptEnable .= 'mqEnable = '.($mqEnable ? 'true' : 'false').';';
229				$scriptEnable .= 'bEnable = '.($enableBing ? 'true' : 'false').';';
230				$scriptEnable .= 'bApiKey="'.$this->getConf('bingAPIKey').'";';
231				$scriptEnable .= 'OpenLayers.ImgPath = "'.DOKU_BASE.'lib/plugins/openlayersmap/lib/'.$this->getConf('olMapStyle').'/";';
232				$scriptEnable .= "\n" . '//--><!]]>' . "\n" . '</script>';
233			}
234			$renderer->doc .= "
235			$gscript
236			$vscript
237			$olscript
238			$scriptEnable";
239
240			$renderer->doc .= '
241				<div id="'.$mapid.'-static" class="olStaticMap">'.$staticImgUrl.'</div>
242				<div id="'.$mapid.'-clearer" class="clearer"></div>';
243
244			// render a (hidden) table of the POI for the print and a11y presentation
245			$renderer->doc .= ' 	<div class="olPOItableSpan" id="'.$mapid.'-table-span"><table class="olPOItable" id="'.$mapid.'-table" summary="'.$poitabledesc.'" title="'.$this->getLang('olmapPOItitle').'">
246		<caption class="olPOITblCaption">'.$this->getLang('olmapPOItitle').'</caption>
247		<thead class="olPOITblHeader">
248			<tr>
249				<th class="rowId" scope="col">id</th>
250				<th class="icon" scope="col">'.$this->getLang('olmapPOIicon').'</th>
251				<th class="lat" scope="col" title="'.$this->getLang('olmapPOIlatTitle').'">'.$this->getLang('olmapPOIlat').'</th>
252				<th class="lon" scope="col" title="'.$this->getLang('olmapPOIlonTitle').'">'.$this->getLang('olmapPOIlon').'</th>
253				<th class="txt" scope="col">'.$this->getLang('olmapPOItxt').'</th>
254			</tr>
255		</thead>
256		<tfoot class="olPOITblFooter"><tr><td colspan="5">'.$poitabledesc.'</td></tr></tfoot>
257		<tbody class="olPOITblBody">'.$poitable.'</tbody>
258	</table></div>';
259			//TODO no tfoot when $poitabledesc is empty
260
261			// render inline mapscript
262			$renderer->doc .="				<script type='text/javascript'><!--//--><![CDATA[//><!--
263			    var $mapid = $param
264			   //--><!]]></script>";
265
266		} elseif ($mode == 'metadata') {
267			// render metadata if available
268			if (!(($this->dflt['lat']==$mainLat)||($thisdflt['lon']==$mainLon))){
269				// unless they are the default
270				$renderer->meta['geo']['lat'] = $mainLat;
271				$renderer->meta['geo']['lon'] = $mainLon;
272			}
273			return true;
274		}
275		return false;
276	}
277
278	/**
279	 * extract parameters for the map from the parameter string
280	 *
281	 * @param   string    $str_params   string of key="value" pairs
282	 * @return  array                   associative array of parameters key=>value
283	 */
284	private function _extract_params($str_params) {
285		$param = array ();
286		preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);
287		// parse match for instructions, break into key value pairs
288		$gmap = $this->dflt;
289		foreach ($param as $kvpair) {
290			list ($match, $key, $val) = $kvpair;
291			$key = strtolower($key);
292			if (isset ($gmap[$key])){
293				if ($key == 'summary'){
294					// preserve case for summary field
295					$gmap[$key] = $val;
296				}else {
297					$gmap[$key] = strtolower($val);
298				}
299			}
300		}
301		return $gmap;
302	}
303
304	/**
305	 * extract overlay points for the map from the wiki syntax data
306	 *
307	 * @param   string    $str_points   multi-line string of lat,lon,text triplets
308	 * @return  array                   multi-dimensional array of lat,lon,text triplets
309	 */
310	private function _extract_points($str_points) {
311		$point = array ();
312		//preg_match_all('/^([+-]?[0-9].*?),\s*([+-]?[0-9].*?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER);
313		/*
314		group 1: ([+-]?[0-9]+(?:\.[0-9]*)?)
315		group 2: ([+-]?[0-9]+(?:\.[0-9]*)?)
316		group 3: (.*?)
317		group 4: (.*?)
318		group 5: (.*?)
319		group 6: (.*)
320		*/
321		preg_match_all('/^([+-]?[0-9]+(?:\.[0-9]*)?),\s*([+-]?[0-9]+(?:\.[0-9]*)?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER);
322		// create poi array
323		$overlay = array ();
324		foreach ($point as $pt) {
325			list ($match, $lat, $lon, $angle, $opacity, $img, $text) = $pt;
326			$lat = is_numeric($lat) ? $lat : 0;
327			$lon = is_numeric($lon) ? $lon : 0;
328			$angle = is_numeric($angle) ? $angle : 0;
329			$opacity = is_numeric($opacity) ? $opacity : 0.8;
330			$img = trim($img);
331			// TODO validate using exist & set up default img?
332			$text = addslashes(str_replace("\n", "", p_render("xhtml", p_get_instructions($text), $info)));
333			$overlay[] = array($lat, $lon, $text, $angle, $opacity, $img);
334		}
335		return $overlay;
336	}
337
338	/**
339	 * Create a MapQuest static map API image url.
340	 * @param array $gmap
341	 * @param array $overlay
342	 */
343	private function _getMapQuest($gmap,$overlay) {
344		$sUrl=$this->getConf('iconUrlOverload');
345		if (!$sUrl){
346			$sUrl=DOKU_URL;
347		}
348		switch ($gmap['baselyr']){
349			case 'mq hybrid':
350				$maptype='hyb (Hybrid)';
351				break;
352			case 'mq sat':
353				$maptype='sat (Satellite)';
354				break;
355			case 'mq normal':
356			default:
357				$maptype='map';
358				break;
359		}
360
361
362		$imgUrl = "http://open.mapquestapi.com/staticmap/v3/getmap";
363		$imgUrl .= "?center=".$gmap['lat'].",".$gmap['lon'];
364		$imgUrl .= "&size=".str_replace("px", "",$gmap['width']).",".str_replace("px", "",$gmap['height']);
365		// max level for mapquest is 16
366		if ($gmap['zoom']>16) {
367			$imgUrl .= "&zoom=16";
368		} else			{
369			$imgUrl .= "&zoom=".$gmap['zoom'];
370		}
371		// TODO mapquest allows using one image url with a multiplier $NUMBER eg:
372		// $NUMBER = 2
373		// $imgUrl .= DOKU_URL."/".DOKU_PLUGIN."/".getPluginName()."/icons/".$img.",$NUMBER,C,".$lat1.",".$lon1.",0,0,0,0,C,".$lat2.",".$lon2.",0,0,0,0";
374		if (!empty ($overlay)) {
375			$imgUrl .= "&xis=";
376			foreach ($overlay as $data) {
377				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
378				$imgUrl .= $sUrl."lib/plugins/openlayersmap/icons/".$img.",1,C,".$lat.",".$lon.",0,0,0,0,";
379			}
380			$imgUrl = substr($imgUrl,0,-1);
381		}
382		$imgUrl .= "&imageType=png&type=".$maptype;
383		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getMapQuest: MapQuest image url is:');
384		return $imgUrl;
385	}
386
387	private function _getGoogle($gmap, $overlay){
388		$sUrl=$this->getConf('iconUrlOverload');
389		if (!$sUrl){
390			$sUrl=DOKU_URL;
391		}
392		switch ($gmap['baselyr']){
393			case 'google hybrid':
394				$maptype='hybrid';
395				break;
396			case 'google sat':
397				$maptype='satellite';
398				break;
399			case 'google relief':
400				$maptype='terrain';
401				break;
402			case 'google normal':
403			default:
404				$maptype='roadmap';
405				break;
406		}
407
408		//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
409		$imgUrl = "http://maps.google.com/maps/api/staticmap";
410		$imgUrl .= "?center=".$gmap['lat'].",".$gmap['lon'];
411		$imgUrl .= "&size=".str_replace("px", "",$gmap['width'])."x".str_replace("px", "",$gmap['height']);
412		// don't need this anymore $imgUrl .= "&key=".$this->getConf('googleAPIKey');
413		// max is 21 (== building scale), but that's overkill..
414		if ($gmap['zoom']>16) {
415			$imgUrl .= "&zoom=16";
416		} else			{
417			$imgUrl .= "&zoom=".$gmap['zoom'];
418		}
419
420		if (!empty ($overlay)) {
421			$rowId=0;
422			foreach ($overlay as $data) {
423				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
424				$imgUrl .= "&markers=icon%3a".$sUrl."lib/plugins/openlayersmap/icons/".$img."%7c".$lat.",".$lon."%7clabel%3a".++$rowId;
425			}
426		}
427		$imgUrl .= "&format=png&maptype=".$maptype."&sensor=false";
428		global $conf;
429		$imgUrl .= "&language=".$conf['lang'];
430		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getGoogle: Google image url is:');
431		return $imgUrl;
432	}
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		switch ($gmap['baselyr']){
442			case 've hybrid':
443				$maptype='AerialWithLabels';
444				break;
445			case 've sat':
446				$maptype='Aerial';
447				break;
448			case 've normal':
449			case 've':
450			default:
451				$maptype='Road';
452				break;
453		}
454
455		// TODO since bing does not provide declutter or autozoom/fit we need to determine the bbox based on the poi and lat/lon ourselves
456		//http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/51.56573,5.45690/12?mapSize=400,400&key=Agm4PJzDOGz4Oy9CYKPlV-UtgmsfL2-zeSyfYjRhf57OQB_oj87j5pncKZSay5qY
457		$imgUrl = "http://dev.virtualearth.net/REST/v1/Imagery/Map/".$maptype."/".$gmap['lat'].",".$gmap['lon']."/".$gmap['zoom'];
458		$imgUrl .= "?ms=".str_replace("px", "",$gmap['width']).",".str_replace("px", "",$gmap['height']);
459		// create a bing api key at https://www.bingmapsportal.com/application
460		$imgUrl .= "&key=".$this->getConf('bingAPIKey');
461		if (!empty ($overlay)) {
462			$rowId=0;
463			foreach ($overlay as $data) {
464				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
465				// TODO icon style lookup, see: http://msdn.microsoft.com/en-us/library/ff701719.aspx for iconStyle
466				// NOTE: the max number of pushpins is 18!
467				$iconStyle=32;
468				$rowId++;
469				$imgUrl .= "&pp=$lat,$lon;$iconStyle;$rowId";
470			}
471		}
472		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getBing: bing image url is:');
473		return $imgUrl;
474	}
475
476	/**
477	 *
478	 * Create a static OSM map image url w/ the poi from http://staticmap.openstreetmap.de (staticMapLite)
479	 * @param array $gmap
480	 * @param array $overlay
481	 */
482	private function _getStaticOSM($gmap, $overlay){
483		//http://staticmap.openstreetmap.de/staticmap.php?center=47.000622235634,10.117187497601&zoom=5&size=500x350
484		// &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
485		$imgUrl = "http://staticmap.openstreetmap.de/staticmap.php";
486		$imgUrl .= "?center=".$gmap['lat'].",".$gmap['lon'];
487		$imgUrl .= "&size=".str_replace("px", "",$gmap['width'])."x".str_replace("px", "",$gmap['height']);
488		if ($gmap['zoom']>16) {
489			$imgUrl .= "&zoom=16";
490		} else			{
491			$imgUrl .= "&zoom=".$gmap['zoom'];
492		}
493
494		if (!empty ($overlay)) {
495			$rowId=0;
496			$imgUrl .= "&markers=";
497			foreach ($overlay as $data) {
498				list ($lat, $lon, $text, $angle, $opacity, $img) = $data;
499				$rowId++;
500				$iconStyle = "lightblue$rowId";
501				$imgUrl .= "$lat,$lon,$iconStyle%7c";
502			}
503			$imgUrl = substr($imgUrl,0,-3);
504		}
505
506		dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getStaticOSM: bing image url is:');
507		return $imgUrl;
508
509	}
510}