xref: /plugin/openlayersmap/helper/staticmap.php (revision 53bfe4a342e02d38a4640c8ecc84aa672eeb55ac)
1628e43ccSMark Prins<?php
2628e43ccSMark Prins/*
3628e43ccSMark Prins * Copyright (c) 2008-2012 Mark C. Prins <mprins@users.sf.net>
4628e43ccSMark Prins*
5628e43ccSMark Prins* Permission to use, copy, modify, and distribute this software for any
6628e43ccSMark Prins* purpose with or without fee is hereby granted, provided that the above
7628e43ccSMark Prins* copyright notice and this permission notice appear in all copies.
8628e43ccSMark Prins*
9628e43ccSMark Prins* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10628e43ccSMark Prins* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11628e43ccSMark Prins* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12628e43ccSMark Prins* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13628e43ccSMark Prins* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14628e43ccSMark Prins* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15628e43ccSMark Prins* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16628e43ccSMark Prins*/
17628e43ccSMark Prinsif (!defined('DOKU_INC')) die();
18628e43ccSMark Prinsif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19628e43ccSMark Prinsrequire_once DOKU_PLUGIN.'openlayersmap/StaticMap.php';
20628e43ccSMark Prins/**
21cc74a83cSMark Prins * DokuWiki Plugin openlayersmap (staticmap Helper Component).
22cc74a83cSMark Prins * This provides the interface to generate a static map based on predefined OSM layers.
23628e43ccSMark Prins *
24628e43ccSMark Prins * @author Mark Prins
25628e43ccSMark Prins */
26628e43ccSMark Prinsclass helper_plugin_openlayersmap_staticmap extends DokuWiki_Plugin {
276eb3157eSMark Prins	/** maximum width of the resulting image. */
28628e43ccSMark Prins	private $maxWidth = 1024;
296eb3157eSMark Prins	/** maximum heigth of the resulting image. */
30628e43ccSMark Prins	private $maxHeight = 1024;
31cc74a83cSMark Prins
326eb3157eSMark Prins	/**
336eb3157eSMark Prins	 * Provide metadata of the public methods of this class.
346eb3157eSMark Prins	 *
356eb3157eSMark Prins	 * @return array Information to all provided methods.
366eb3157eSMark Prins	 */
37628e43ccSMark Prins	function getMethods(){
38628e43ccSMark Prins		$result = array();
39628e43ccSMark Prins		$result[] = array(
40628e43ccSMark Prins				'name'   => 'getMap',
41628e43ccSMark Prins				'desc'   => 'returns url to the image',
42628e43ccSMark Prins				'params' => array(
43628e43ccSMark Prins						'center' => 'string',
44628e43ccSMark Prins						'zoom' => 'integer',
45628e43ccSMark Prins						'size' => 'string',
46628e43ccSMark Prins						'maptype' => 'string',
47628e43ccSMark Prins						'markers' => 'string',
48628e43ccSMark Prins						'gpx' => 'string',
49628e43ccSMark Prins						'kml' => 'string'),
50628e43ccSMark Prins				'return' => array('image' => 'string'),
51628e43ccSMark Prins		);
52628e43ccSMark Prins		return $result;
53628e43ccSMark Prins	}
54628e43ccSMark Prins
55628e43ccSMark Prins	/**
56628e43ccSMark Prins	 * Create the map.
57628e43ccSMark Prins	 *
58628e43ccSMark Prins	 * @param number lat the latitude of the map's center, eg. 40.714728
59628e43ccSMark Prins	 * @param number lon the longitude of the map's center, eg -73.998672
60628e43ccSMark Prins	 * @param number zoom the zoom level in the tile cache, eg. 14
61628e43ccSMark Prins	 * @param mixed size the size in WxH px, eg. 512x512
62628e43ccSMark Prins	 * @param string maptype the maptype, eg. cycle
63628e43ccSMark Prins	 * @param mixed markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle), eg. array(	'lat'=>40.702147,	'lon'=>-74.015794,	'type'=>lightblue1);
64628e43ccSMark Prins	 * @param string gpx media link
65628e43ccSMark Prins	 * @param string kml media link
66628e43ccSMark Prins	 *
67628e43ccSMark Prins	 * @return the media id url
68628e43ccSMark Prins	 */
69628e43ccSMark Prins	public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml){
70628e43ccSMark Prins		global $conf;
716eb3157eSMark Prins		// dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :');
72628e43ccSMark Prins
73628e43ccSMark Prins		// normalize zoom
74628e43ccSMark Prins		$zoom = $zoom?intval($zoom):0;
75628e43ccSMark Prins		if($zoom > 18) $zoom = 18;
76628e43ccSMark Prins		// normalize WxH
77628e43ccSMark Prins		list($width, $height) = split('x',$size);
78628e43ccSMark Prins		$width = intval($width);
79628e43ccSMark Prins		if($width > $this->maxWidth) $width = $this->maxWidth;
80628e43ccSMark Prins		$height = intval($height);
81628e43ccSMark Prins		if($height > $this->maxHeight) $height = $this->maxHeight;
826eb3157eSMark Prins
83e61425c7SMark Prins		// cleanup/validate gpx/kml
84e61425c7SMark Prins		$kml = $this->mediaIdToPath($kml);
856eb3157eSMark Prins		// dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:');
86e61425c7SMark Prins		$gpx = $this->mediaIdToPath($gpx);
876eb3157eSMark Prins		// dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:');
886c6bb022SMark Prins
89628e43ccSMark Prins		// create map
90*53bfe4a3SMark Prins		$map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype,
91*53bfe4a3SMark Prins				$markers, $gpx, $kml, $conf['mediadir'], $conf['cachedir'],
922d11d700SMark Prins				$this->getConf('autoZoomMap')
93628e43ccSMark Prins		);
942d11d700SMark Prins
95628e43ccSMark Prins		// return the media id url
96628e43ccSMark Prins		$mediaId = str_replace('/', ':',  $map->getMap());
97fc16f3cdSMark Prins		// 	if($this->startsWith($mediaId,':')) {
98fc16f3cdSMark Prins		// 		$mediaId = substr($mediaId, 1);
99fc16f3cdSMark Prins		// 	}
100628e43ccSMark Prins		return $mediaId;
101628e43ccSMark Prins	}
102e61425c7SMark Prins
103e61425c7SMark Prins	private function startsWith($haystack, $needle)	{
104e61425c7SMark Prins		$length = strlen($needle);
105e61425c7SMark Prins		return (substr($haystack, 0, $length) === $needle);
106e61425c7SMark Prins	}
107e61425c7SMark Prins
108cc74a83cSMark Prins	/**
109cc74a83cSMark Prins	 * Constructs the path to a file.
110cc74a83cSMark Prins	 * @param string $id the DW media id
111cc74a83cSMark Prins	 * @return the path to the file
112cc74a83cSMark Prins	 */
113e61425c7SMark Prins	private function mediaIdToPath($id){
114e61425c7SMark Prins		global $conf;
115e61425c7SMark Prins		if(empty($id)) {
116e61425c7SMark Prins			return "";
117e61425c7SMark Prins		}
118e61425c7SMark Prins		$id=str_replace("[[","",$id);
119e61425c7SMark Prins		$id=str_replace("]]","",$id);
120e61425c7SMark Prins		if($this->startsWith($id,':')) {
121e61425c7SMark Prins			$id = substr($id, 1);
122e61425c7SMark Prins		}
123e61425c7SMark Prins		$id=str_replace(":","/",$id);
124e61425c7SMark Prins		return $conf['mediadir'].'/'.$id;
125e61425c7SMark Prins	}
126628e43ccSMark Prins}