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