xref: /plugin/openlayersmap/helper/staticmap.php (revision 6914b920d9aba6fed5894176f6fa26ad74472d64)
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						'geojson' => 'string'),
51				'return' => array('image' => 'string'),
52		);
53		return $result;
54	}
55
56	/**
57	 * Create the map.
58	 *
59	 * @param number lat the latitude of the map's center, eg. 40.714728
60	 * @param number lon the longitude of the map's center, eg -73.998672
61	 * @param number zoom the zoom level in the tile cache, eg. 14
62	 * @param mixed size the size in WxH px, eg. 512x512
63	 * @param string maptype the maptype, eg. cycle
64	 * @param mixed markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), eg. array(	'lat'=>40.702147,	'lon'=>-74.015794,	'type'=>lightblue1);
65	 * @param string gpx media link
66	 * @param string kml media link
67	 * @param string geojson media link
68	 *
69	 * @return the media id url
70	 */
71	public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml, $geojson){
72		global $conf;
73		// dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :');
74
75		// normalize zoom
76		$zoom = $zoom?intval($zoom):0;
77		if($zoom > 18) $zoom = 18;
78		// normalize WxH
79		list($width, $height) = split('x',$size);
80		$width = intval($width);
81		if($width > $this->maxWidth) $width = $this->maxWidth;
82		$height = intval($height);
83		if($height > $this->maxHeight) $height = $this->maxHeight;
84
85		// cleanup/validate gpx/kml
86		$kml = $this->mediaIdToPath($kml);
87		// dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:');
88		$gpx = $this->mediaIdToPath($gpx);
89		// dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:');
90		$geojson = $this->mediaIdToPath($geojson);
91
92		// create map
93		$map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype,
94				$markers, $gpx, $kml, $geojson, $conf['mediadir'], $conf['cachedir'],
95				$this->getConf('autoZoomMap')
96		);
97
98		// return the media id url
99		$mediaId = str_replace('/', ':',  $map->getMap());
100		// 	if($this->startsWith($mediaId,':')) {
101		// 		$mediaId = substr($mediaId, 1);
102		// 	}
103		return $mediaId;
104	}
105
106	private function startsWith($haystack, $needle)	{
107		$length = strlen($needle);
108		return (substr($haystack, 0, $length) === $needle);
109	}
110
111	/**
112	 * Constructs the path to a file.
113	 * @param string $id the DW media id
114	 * @return the path to the file
115	 */
116	private function mediaIdToPath($id){
117		global $conf;
118		if(empty($id)) {
119			return "";
120		}
121		$id=str_replace("[[","",$id);
122		$id=str_replace("]]","",$id);
123		if($this->startsWith($id,':')) {
124			$id = substr($id, 1);
125		}
126		$id=str_replace(":","/",$id);
127		return $conf['mediadir'].'/'.$id;
128	}
129}