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