xref: /plugin/openlayersmap/helper/staticmap.php (revision d29478df98b8b62dbce5a269cf53c4deae926744)
1<?php
2
3/*
4 * Copyright (c) 2008-2023 Mark C. Prins <mprins@users.sf.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
19 */
20use dokuwiki\Logger;
21use dokuwiki\Extension\Plugin;
22use dokuwiki\plugin\openlayersmap\StaticMap;
23
24/**
25 * DokuWiki Plugin openlayersmap (staticmap Helper Component).
26 * This provides the interface to generate a static map based on predefined OSM layers.
27 *
28 * @author Mark Prins
29 */
30class helper_plugin_openlayersmap_staticmap extends Plugin
31{
32    /** maximum width of the resulting image. */
33    private $maxWidth = 1024;
34    /** maximum heigth of the resulting image. */
35    private $maxHeight = 1024;
36
37    /**
38     * Provide metadata of the public methods of this class.
39     *
40     * @return array Information to all provided methods.
41     */
42    public function getMethods(): array
43    {
44        return [['name'   => 'getMap', 'desc'   => 'returns url to the image', 'params' => ['lat'     => 'float', 'lon'     => 'float', 'zoom'    => 'integer', 'size'    => 'string', 'maptype' => 'string', 'markers' => 'string', 'gpx'     => 'string', 'kml'     => 'string', 'geojson' => 'string', 'apikey'  => 'string'], 'return' => ['image' => 'string']]];
45    }
46
47    /**
48     * Create the map.
49     *
50     * @param float  $lat     the latitude of the map's center, eg. 40.714728
51     * @param float  $lon     the longitude of the map's center, eg -73.998672
52     * @param int    $zoom    the zoom level in the tile cache, eg. 14
53     * @param string $size    the size in WxH px, eg. 512x512
54     * @param string $maptype the maptype, eg. cycle
55     * @param array  $markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle),
56     *                        eg. array('lat'=>40.702147,'lon'=>-74.015794,'type'=>lightblue1);
57     * @param string $gpx     media link
58     * @param string $kml     media link
59     * @param string $geojson media link
60     * @param string $apikey  optional API key eg. for Thunderforest maps
61     */
62    public function getMap(
63        float $lat,
64        float $lon,
65        int $zoom,
66        string $size,
67        string $maptype,
68        array $markers,
69        string $gpx,
70        string $kml,
71        string $geojson,
72        string $apikey = ''
73    ): string {
74        global $conf;
75        if ($zoom > 18) {
76            $zoom = 18;
77        }
78        // normalize WxH
79        [$width, $height] = explode('x', $size);
80        $width = (int) $width;
81        if ($width > $this->maxWidth) {
82            $width = $this->maxWidth;
83        }
84        $height = (int) $height;
85        if ($height > $this->maxHeight) {
86            $height = $this->maxHeight;
87        }
88
89        // cleanup/validate gpx/kml
90        $kml = $this->mediaIdToPath($kml);
91        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: kml file:',$kml);
92        $gpx = $this->mediaIdToPath($gpx);
93        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: gpx file:',$gpx);
94        $geojson = $this->mediaIdToPath($geojson);
95
96        // create map
97        $map = new StaticMap(
98            $lat,
99            $lon,
100            $zoom,
101            $width,
102            $height,
103            $maptype,
104            $markers,
105            $gpx,
106            $kml,
107            $geojson,
108            $conf['mediadir'],
109            $conf['cachedir'],
110            $this->getConf('autoZoomMap'),
111            $apikey
112        );
113
114        // return the media id url
115        // $mediaId = str_replace('/', ':', $map->getMap());
116        // if($this->startsWith($mediaId,':')) {
117        //     $mediaId = substr($mediaId, 1);
118        // }
119        // return $mediaId;
120        return str_replace('/', ':', $map->getMap());
121    }
122
123    /**
124     * Constructs the path to a file.
125     * @param string $id the DW media id
126     * @return string the path to the file
127     */
128    private function mediaIdToPath(string $id): string
129    {
130        global $conf;
131        if (empty($id)) {
132            return "";
133        }
134        $id = str_replace(["[[", "]]"], "", $id);
135        if ((str_starts_with($id, ':'))) {
136            $id = substr($id, 1);
137        }
138        $id = str_replace(":", "/", $id);
139        return $conf['mediadir'] . '/' . $id;
140    }
141}
142