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