xref: /plugin/openlayersmap/helper/staticmap.php (revision cc74a83ce15a73db93d0109fec7ef672a03e2e99)
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/**
21*cc74a83cSMark Prins * DokuWiki Plugin openlayersmap (staticmap Helper Component).
22*cc74a83cSMark 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 {
27628e43ccSMark Prins	private $maxWidth = 1024;
28628e43ccSMark Prins	private $maxHeight = 1024;
29*cc74a83cSMark Prins
30628e43ccSMark Prins	function getMethods(){
31628e43ccSMark Prins		$result = array();
32628e43ccSMark Prins		$result[] = array(
33628e43ccSMark Prins				'name'   => 'getMap',
34628e43ccSMark Prins				'desc'   => 'returns url to the image',
35628e43ccSMark Prins				'params' => array(
36628e43ccSMark Prins						'center' => 'string',
37628e43ccSMark Prins						'zoom' => 'integer',
38628e43ccSMark Prins						'size' => 'string',
39628e43ccSMark Prins						'maptype' => 'string',
40628e43ccSMark Prins						'markers' => 'string',
41628e43ccSMark Prins						'gpx' => 'string',
42628e43ccSMark Prins						'kml' => 'string'),
43628e43ccSMark Prins				'return' => array('image' => 'string'),
44628e43ccSMark Prins		);
45628e43ccSMark Prins		return $result;
46628e43ccSMark Prins	}
47628e43ccSMark Prins
48628e43ccSMark Prins	/**
49628e43ccSMark Prins	 * Create the map.
50628e43ccSMark Prins	 *
51628e43ccSMark Prins	 * @param number lat the latitude of the map's center, eg. 40.714728
52628e43ccSMark Prins	 * @param number lon the longitude of the map's center, eg -73.998672
53628e43ccSMark Prins	 * @param number zoom the zoom level in the tile cache, eg. 14
54628e43ccSMark Prins	 * @param mixed size the size in WxH px, eg. 512x512
55628e43ccSMark Prins	 * @param string maptype the maptype, eg. cycle
56628e43ccSMark 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);
57628e43ccSMark Prins	 * @param string gpx media link
58628e43ccSMark Prins	 * @param string kml media link
59628e43ccSMark Prins	 *
60628e43ccSMark Prins	 * @return the media id url
61628e43ccSMark Prins	 */
62628e43ccSMark Prins	public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml){
63628e43ccSMark Prins		global $conf;
64e61425c7SMark Prins		dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :');
65628e43ccSMark Prins
66628e43ccSMark Prins		// normalize zoom
67628e43ccSMark Prins		$zoom = $zoom?intval($zoom):0;
68628e43ccSMark Prins		if($zoom > 18) $zoom = 18;
69628e43ccSMark Prins		// normalize WxH
70628e43ccSMark Prins		list($width, $height) = split('x',$size);
71628e43ccSMark Prins		$width = intval($width);
72628e43ccSMark Prins		if($width > $this->maxWidth) $width = $this->maxWidth;
73628e43ccSMark Prins		$height = intval($height);
74628e43ccSMark Prins		if($height > $this->maxHeight) $height = $this->maxHeight;
75e61425c7SMark Prins		// cleanup/validate gpx/kml
76e61425c7SMark Prins		$kml = $this->mediaIdToPath($kml);
77e61425c7SMark Prins		dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:');
78e61425c7SMark Prins		$gpx = $this->mediaIdToPath($gpx);
79e61425c7SMark Prins		dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:');
806c6bb022SMark Prins
81628e43ccSMark Prins		// create map
82628e43ccSMark Prins		$map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype, $markers, $gpx, $kml,
83628e43ccSMark Prins				$conf['mediadir'],
84628e43ccSMark Prins				$conf['cachedir']
85628e43ccSMark Prins		);
86628e43ccSMark Prins		// return the media id url
87628e43ccSMark Prins		$mediaId = str_replace('/', ':',  $map->getMap());
88628e43ccSMark Prins		return $mediaId;
89628e43ccSMark Prins	}
90e61425c7SMark Prins
91e61425c7SMark Prins	private function startsWith($haystack, $needle)	{
92e61425c7SMark Prins		$length = strlen($needle);
93e61425c7SMark Prins		return (substr($haystack, 0, $length) === $needle);
94e61425c7SMark Prins	}
95e61425c7SMark Prins
96*cc74a83cSMark Prins	/**
97*cc74a83cSMark Prins	 * Constructs the path to a file.
98*cc74a83cSMark Prins	 * @param string $id the DW media id
99*cc74a83cSMark Prins	 * @return the path to the file
100*cc74a83cSMark Prins	 */
101e61425c7SMark Prins	private function mediaIdToPath($id){
102e61425c7SMark Prins		global $conf;
103e61425c7SMark Prins		if(empty($id)) {
104e61425c7SMark Prins			return "";
105e61425c7SMark Prins		}
106e61425c7SMark Prins		$id=str_replace("[[","",$id);
107e61425c7SMark Prins		$id=str_replace("]]","",$id);
108e61425c7SMark Prins		if($this->startsWith($id,':')) {
109e61425c7SMark Prins			$id = substr($id, 1);
110e61425c7SMark Prins		}
111e61425c7SMark Prins		$id=str_replace(":","/",$id);
112e61425c7SMark Prins		return $conf['mediadir'].'/'.$id;
113e61425c7SMark Prins	}
114628e43ccSMark Prins}