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