xref: /plugin/openlayersmap/helper/staticmap.php (revision e299f0b739dd8395dd450353bd7e5c1cec99aa5c)
1<?php
2
3/*
4 * Copyright (c) 2008-2026 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 int $maxWidth = 1024;
34    /** maximum heigth of the resulting image. */
35    private int $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 [
45            [
46                'name' => 'getMap',
47                'desc' => 'returns url to the image',
48                'params' => [
49                    'lat' => 'float',
50                    'lon' => 'float',
51                    'zoom' => 'integer',
52                    'size' => 'string',
53                    'maptype' => 'string',
54                    'markers' => 'string',
55                    'gpx' => 'string',
56                    'kml' => 'string',
57                    'geojson' => 'string',
58                    'apikey' => 'string'
59                ],
60                'return' => [
61                    'image' => 'string'
62                ]
63            ]
64        ];
65    }
66
67    /**
68     * Create the map.
69     *
70     * @param float  $lat     the latitude of the map's center, eg. 40.714728
71     * @param float  $lon     the longitude of the map's center, eg -73.998672
72     * @param int    $zoom    the zoom level in the tile cache, eg. 14
73     * @param string $size    the size in WxH px, eg. 512x512
74     * @param string $maptype the maptype, eg. cycle
75     * @param array  $markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle),
76     *                        eg. array('lat'=>40.702147,'lon'=>-74.015794,'type'=>lightblue1);
77     * @param string $gpx     media link
78     * @param string $kml     media link
79     * @param string $geojson media link
80     * @param string $apikey  optional API key eg. for Thunderforest maps
81     */
82    public function getMap(
83        float $lat,
84        float $lon,
85        int $zoom,
86        string $size,
87        string $maptype,
88        array $markers,
89        string $gpx,
90        string $kml,
91        string $geojson,
92        string $apikey = ''
93    ): string {
94        global $conf;
95        if ($zoom > 18) {
96            $zoom = 18;
97        }
98        // normalize WxH
99        [$width, $height] = explode('x', $size);
100        $width = (int) $width;
101        if ($width > $this->maxWidth) {
102            $width = $this->maxWidth;
103        }
104        $height = (int) $height;
105        if ($height > $this->maxHeight) {
106            $height = $this->maxHeight;
107        }
108
109        // cleanup/validate gpx/kml
110        $kml = $this->mediaIdToPath($kml);
111        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: kml file:',$kml);
112        $gpx = $this->mediaIdToPath($gpx);
113        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: gpx file:',$gpx);
114        $geojson = $this->mediaIdToPath($geojson);
115
116        // create map
117        $map = new StaticMap(
118            $lat,
119            $lon,
120            $zoom,
121            $width,
122            $height,
123            $maptype,
124            $markers,
125            $gpx,
126            $kml,
127            $geojson,
128            $conf['mediadir'],
129            $conf['cachedir'],
130            $this->getConf('autoZoomMap'),
131            $apikey
132        );
133
134        // return the media id url
135        // $mediaId = str_replace('/', ':', $map->getMap());
136        // if($this->startsWith($mediaId,':')) {
137        //     $mediaId = substr($mediaId, 1);
138        // }
139        // return $mediaId;
140        return str_replace('/', ':', $map->getMap());
141    }
142
143    /**
144     * Constructs the path to a file.
145     * @param string $id the DW media id
146     * @return string the path to the file
147     */
148    private function mediaIdToPath(string $id): string
149    {
150        global $conf;
151        if (empty($id)) {
152            return "";
153        }
154        $id = str_replace(["[[", "]]"], "", $id);
155        if ((str_starts_with($id, ':'))) {
156            $id = substr($id, 1);
157        }
158        $id = str_replace(":", "/", $id);
159        return $conf['mediadir'] . '/' . $id;
160    }
161}
162