xref: /plugin/openlayersmap/helper/staticmap.php (revision d2f4772aabf3d008005ac2a7cced06742a8f59a8)
1628e43ccSMark Prins<?php
2628e43ccSMark Prins/*
35c603532SMark Prins * Copyright (c) 2008-2017 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(
43*d2f4772aSMark Prins						'lat' => 'float',
44*d2f4772aSMark Prins						'lon' => 'float',
45628e43ccSMark Prins						'zoom' => 'integer',
46628e43ccSMark Prins						'size' => 'string',
47628e43ccSMark Prins						'maptype' => 'string',
48628e43ccSMark Prins						'markers' => 'string',
49628e43ccSMark Prins						'gpx' => 'string',
506914b920SMark Prins						'kml' => 'string',
51*d2f4772aSMark Prins						'geojson' => 'string',
52*d2f4772aSMark Prins						'apikey' => 'string'),
53628e43ccSMark Prins				'return' => array('image' => 'string'),
54628e43ccSMark Prins		);
55628e43ccSMark Prins		return $result;
56628e43ccSMark Prins	}
57628e43ccSMark Prins
58628e43ccSMark Prins	/**
59628e43ccSMark Prins	 * Create the map.
60628e43ccSMark Prins	 *
61628e43ccSMark Prins	 * @param number lat the latitude of the map's center, eg. 40.714728
62628e43ccSMark Prins	 * @param number lon the longitude of the map's center, eg -73.998672
63628e43ccSMark Prins	 * @param number zoom the zoom level in the tile cache, eg. 14
64628e43ccSMark Prins	 * @param mixed size the size in WxH px, eg. 512x512
65628e43ccSMark Prins	 * @param string maptype the maptype, eg. cycle
66628e43ccSMark 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);
67628e43ccSMark Prins	 * @param string gpx media link
68628e43ccSMark Prins	 * @param string kml media link
696914b920SMark Prins	 * @param string geojson media link
70*d2f4772aSMark Prins	 * @param string optional API key eg. for Thunderforest maps
71628e43ccSMark Prins	 *
72628e43ccSMark Prins	 * @return the media id url
73628e43ccSMark Prins	 */
74*d2f4772aSMark Prins	public function getMap($lat, $lon, $zoom, $size, $maptype, $markers, $gpx, $kml, $geojson, $apikey = ''){
75628e43ccSMark Prins		global $conf;
766eb3157eSMark Prins		// dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :');
77628e43ccSMark Prins
78628e43ccSMark Prins		// normalize zoom
79628e43ccSMark Prins		$zoom = $zoom?intval($zoom):0;
80628e43ccSMark Prins		if($zoom > 18) $zoom = 18;
81628e43ccSMark Prins		// normalize WxH
82c81a89cdSMark Prins		list($width, $height) = explode('x',$size);
83628e43ccSMark Prins		$width = intval($width);
84628e43ccSMark Prins		if($width > $this->maxWidth) $width = $this->maxWidth;
85628e43ccSMark Prins		$height = intval($height);
86628e43ccSMark Prins		if($height > $this->maxHeight) $height = $this->maxHeight;
876eb3157eSMark Prins
88e61425c7SMark Prins		// cleanup/validate gpx/kml
89e61425c7SMark Prins		$kml = $this->mediaIdToPath($kml);
906eb3157eSMark Prins		// dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:');
91e61425c7SMark Prins		$gpx = $this->mediaIdToPath($gpx);
926eb3157eSMark Prins		// dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:');
936914b920SMark Prins		$geojson = $this->mediaIdToPath($geojson);
946c6bb022SMark Prins
95628e43ccSMark Prins		// create map
9653bfe4a3SMark Prins		$map = new StaticMap($lat, $lon, $zoom, $width, $height, $maptype,
976914b920SMark Prins				$markers, $gpx, $kml, $geojson, $conf['mediadir'], $conf['cachedir'],
985c603532SMark Prins				$this->getConf('autoZoomMap'),
99*d2f4772aSMark Prins				$apikey
100628e43ccSMark Prins		);
1012d11d700SMark Prins
102628e43ccSMark Prins		// return the media id url
103628e43ccSMark Prins		$mediaId = str_replace('/', ':',  $map->getMap());
104fc16f3cdSMark Prins		// 	if($this->startsWith($mediaId,':')) {
105fc16f3cdSMark Prins		// 		$mediaId = substr($mediaId, 1);
106fc16f3cdSMark Prins		// 	}
107628e43ccSMark Prins		return $mediaId;
108628e43ccSMark Prins	}
109e61425c7SMark Prins
110e61425c7SMark Prins	private function startsWith($haystack, $needle)	{
111e61425c7SMark Prins		$length = strlen($needle);
112e61425c7SMark Prins		return (substr($haystack, 0, $length) === $needle);
113e61425c7SMark Prins	}
114e61425c7SMark Prins
115cc74a83cSMark Prins	/**
116cc74a83cSMark Prins	 * Constructs the path to a file.
117cc74a83cSMark Prins	 * @param string $id the DW media id
118cc74a83cSMark Prins	 * @return the path to the file
119cc74a83cSMark Prins	 */
120e61425c7SMark Prins	private function mediaIdToPath($id){
121e61425c7SMark Prins		global $conf;
122e61425c7SMark Prins		if(empty($id)) {
123e61425c7SMark Prins			return "";
124e61425c7SMark Prins		}
125e61425c7SMark Prins		$id=str_replace("[[","",$id);
126e61425c7SMark Prins		$id=str_replace("]]","",$id);
127e61425c7SMark Prins		if($this->startsWith($id,':')) {
128e61425c7SMark Prins			$id = substr($id, 1);
129e61425c7SMark Prins		}
130e61425c7SMark Prins		$id=str_replace(":","/",$id);
131e61425c7SMark Prins		return $conf['mediadir'].'/'.$id;
132e61425c7SMark Prins	}
133628e43ccSMark Prins}
134