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