xref: /plugin/openlayersmap/helper/staticmap.php (revision 53bfe4a342e02d38a4640c8ecc84aa672eeb55ac)
1<?php
2/*
3 * Copyright (c) 2008-2012 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')) die();
18if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19require_once DOKU_PLUGIN.'openlayersmap/StaticMap.php';
20/**
21 * DokuWiki Plugin openlayersmap (staticmap Helper Component).
22 * This provides the interface to generate a static map based on predefined OSM layers.
23 *
24 * @author Mark Prins
25 */
26class helper_plugin_openlayersmap_staticmap extends DokuWiki_Plugin {
27	/** maximum width of the resulting image. */
28	private $maxWidth = 1024;
29	/** maximum heigth of the resulting image. */
30	private $maxHeight = 1024;
31
32	/**
33	 * Provide metadata of the public methods of this class.
34	 *
35	 * @return array Information to all provided methods.
36	 */
37	function getMethods(){
38		$result = array();
39		$result[] = array(
40				'name'   => 'getMap',
41				'desc'   => 'returns url to the image',
42				'params' => array(
43						'center' => 'string',
44						'zoom' => 'integer',
45						'size' => 'string',
46						'maptype' => 'string',
47						'markers' => 'string',
48						'gpx' => 'string',
49						'kml' => 'string'),
50				'return' => array('image' => 'string'),
51		);
52		return $result;
53	}
54
55	/**
56	 * Create the map.
57	 *
58	 * @param number lat the latitude of the map's center, eg. 40.714728
59	 * @param number lon the longitude of the map's center, eg -73.998672
60	 * @param number zoom the zoom level in the tile cache, eg. 14
61	 * @param mixed size the size in WxH px, eg. 512x512
62	 * @param string maptype the maptype, eg. cycle
63	 * @param mixed markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), eg. array(	'lat'=>40.702147,	'lon'=>-74.015794,	'type'=>lightblue1);
64	 * @param string gpx media link
65	 * @param string kml media link
66	 *
67	 * @return the media id url
68	 */
69	public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml){
70		global $conf;
71		// dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :');
72
73		// normalize zoom
74		$zoom = $zoom?intval($zoom):0;
75		if($zoom > 18) $zoom = 18;
76		// normalize WxH
77		list($width, $height) = split('x',$size);
78		$width = intval($width);
79		if($width > $this->maxWidth) $width = $this->maxWidth;
80		$height = intval($height);
81		if($height > $this->maxHeight) $height = $this->maxHeight;
82
83		// cleanup/validate gpx/kml
84		$kml = $this->mediaIdToPath($kml);
85		// dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:');
86		$gpx = $this->mediaIdToPath($gpx);
87		// dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:');
88
89		// create map
90		$map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype,
91				$markers, $gpx, $kml, $conf['mediadir'], $conf['cachedir'],
92				$this->getConf('autoZoomMap')
93		);
94
95		// return the media id url
96		$mediaId = str_replace('/', ':',  $map->getMap());
97		// 	if($this->startsWith($mediaId,':')) {
98		// 		$mediaId = substr($mediaId, 1);
99		// 	}
100		return $mediaId;
101	}
102
103	private function startsWith($haystack, $needle)	{
104		$length = strlen($needle);
105		return (substr($haystack, 0, $length) === $needle);
106	}
107
108	/**
109	 * Constructs the path to a file.
110	 * @param string $id the DW media id
111	 * @return the path to the file
112	 */
113	private function mediaIdToPath($id){
114		global $conf;
115		if(empty($id)) {
116			return "";
117		}
118		$id=str_replace("[[","",$id);
119		$id=str_replace("]]","",$id);
120		if($this->startsWith($id,':')) {
121			$id = substr($id, 1);
122		}
123		$id=str_replace(":","/",$id);
124		return $conf['mediadir'].'/'.$id;
125	}
126}