xref: /plugin/openlayersmap/helper/staticmap.php (revision d29478df98b8b62dbce5a269cf53c4deae926744)
1628e43ccSMark Prins<?php
2a760825cSgithub-actions[bot]
3628e43ccSMark Prins/*
4f204b8caSMark Prins * Copyright (c) 2008-2023 Mark C. Prins <mprins@users.sf.net>
5628e43ccSMark Prins *
6628e43ccSMark Prins * Permission to use, copy, modify, and distribute this software for any
7628e43ccSMark Prins * purpose with or without fee is hereby granted, provided that the above
8628e43ccSMark Prins * copyright notice and this permission notice appear in all copies.
9628e43ccSMark Prins *
10628e43ccSMark Prins * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11628e43ccSMark Prins * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12628e43ccSMark Prins * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13628e43ccSMark Prins * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14628e43ccSMark Prins * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15628e43ccSMark Prins * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16628e43ccSMark Prins * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1757f8d5bbSMark Prins *
1857f8d5bbSMark Prins * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
19628e43ccSMark Prins */
20f204b8caSMark Prinsuse dokuwiki\Logger;
21a760825cSgithub-actions[bot]use dokuwiki\Extension\Plugin;
22d2f1674eSMark Prinsuse dokuwiki\plugin\openlayersmap\StaticMap;
23d2f1674eSMark Prins
24628e43ccSMark Prins/**
25cc74a83cSMark Prins * DokuWiki Plugin openlayersmap (staticmap Helper Component).
26cc74a83cSMark Prins * This provides the interface to generate a static map based on predefined OSM layers.
27628e43ccSMark Prins *
28628e43ccSMark Prins * @author Mark Prins
29628e43ccSMark Prins */
30a760825cSgithub-actions[bot]class helper_plugin_openlayersmap_staticmap extends Plugin
31a760825cSgithub-actions[bot]{
326eb3157eSMark Prins    /** maximum width of the resulting image. */
33628e43ccSMark Prins    private $maxWidth = 1024;
346eb3157eSMark Prins    /** maximum heigth of the resulting image. */
35628e43ccSMark Prins    private $maxHeight = 1024;
36cc74a83cSMark Prins
376eb3157eSMark Prins    /**
386eb3157eSMark Prins     * Provide metadata of the public methods of this class.
396eb3157eSMark Prins     *
406eb3157eSMark Prins     * @return array Information to all provided methods.
416eb3157eSMark Prins     */
42a760825cSgithub-actions[bot]    public function getMethods(): array
43a760825cSgithub-actions[bot]    {
44a760825cSgithub-actions[bot]        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']]];
45628e43ccSMark Prins    }
46628e43ccSMark Prins
47628e43ccSMark Prins    /**
48628e43ccSMark Prins     * Create the map.
49628e43ccSMark Prins     *
5057f8d5bbSMark Prins     * @param float  $lat     the latitude of the map's center, eg. 40.714728
5157f8d5bbSMark Prins     * @param float  $lon     the longitude of the map's center, eg -73.998672
5257f8d5bbSMark Prins     * @param int    $zoom    the zoom level in the tile cache, eg. 14
5357f8d5bbSMark Prins     * @param string $size    the size in WxH px, eg. 512x512
5457f8d5bbSMark Prins     * @param string $maptype the maptype, eg. cycle
5557f8d5bbSMark Prins     * @param array  $markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle),
5657f8d5bbSMark Prins     *                        eg. array('lat'=>40.702147,'lon'=>-74.015794,'type'=>lightblue1);
5757f8d5bbSMark Prins     * @param string $gpx     media link
5857f8d5bbSMark Prins     * @param string $kml     media link
5957f8d5bbSMark Prins     * @param string $geojson media link
6057f8d5bbSMark Prins     * @param string $apikey  optional API key eg. for Thunderforest maps
61628e43ccSMark Prins     */
6257f8d5bbSMark Prins    public function getMap(
6357f8d5bbSMark Prins        float $lat,
6457f8d5bbSMark Prins        float $lon,
6557f8d5bbSMark Prins        int $zoom,
6657f8d5bbSMark Prins        string $size,
6757f8d5bbSMark Prins        string $maptype,
6857f8d5bbSMark Prins        array $markers,
6957f8d5bbSMark Prins        string $gpx,
7057f8d5bbSMark Prins        string $kml,
7157f8d5bbSMark Prins        string $geojson,
7257f8d5bbSMark Prins        string $apikey = ''
7357f8d5bbSMark Prins    ): string {
74628e43ccSMark Prins        global $conf;
7557f8d5bbSMark Prins        if ($zoom > 18) {
7657f8d5bbSMark Prins            $zoom = 18;
7757f8d5bbSMark Prins        }
78628e43ccSMark Prins        // normalize WxH
79a760825cSgithub-actions[bot]        [$width, $height] = explode('x', $size);
8057f8d5bbSMark Prins        $width = (int) $width;
8157f8d5bbSMark Prins        if ($width > $this->maxWidth) {
8257f8d5bbSMark Prins            $width = $this->maxWidth;
8357f8d5bbSMark Prins        }
8457f8d5bbSMark Prins        $height = (int) $height;
8557f8d5bbSMark Prins        if ($height > $this->maxHeight) {
8657f8d5bbSMark Prins            $height = $this->maxHeight;
8757f8d5bbSMark Prins        }
886eb3157eSMark Prins
89e61425c7SMark Prins        // cleanup/validate gpx/kml
90e61425c7SMark Prins        $kml = $this->mediaIdToPath($kml);
91f204b8caSMark Prins        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: kml file:',$kml);
92e61425c7SMark Prins        $gpx = $this->mediaIdToPath($gpx);
93f204b8caSMark Prins        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: gpx file:',$gpx);
946914b920SMark Prins        $geojson = $this->mediaIdToPath($geojson);
956c6bb022SMark Prins
96628e43ccSMark Prins        // create map
9757f8d5bbSMark Prins        $map = new StaticMap(
98a760825cSgithub-actions[bot]            $lat,
99a760825cSgithub-actions[bot]            $lon,
100a760825cSgithub-actions[bot]            $zoom,
101a760825cSgithub-actions[bot]            $width,
102a760825cSgithub-actions[bot]            $height,
103a760825cSgithub-actions[bot]            $maptype,
104a760825cSgithub-actions[bot]            $markers,
105a760825cSgithub-actions[bot]            $gpx,
106a760825cSgithub-actions[bot]            $kml,
107a760825cSgithub-actions[bot]            $geojson,
108a760825cSgithub-actions[bot]            $conf['mediadir'],
109a760825cSgithub-actions[bot]            $conf['cachedir'],
110a760825cSgithub-actions[bot]            $this->getConf('autoZoomMap'),
111a760825cSgithub-actions[bot]            $apikey
112628e43ccSMark Prins        );
1132d11d700SMark Prins
114628e43ccSMark Prins        // return the media id url
11557f8d5bbSMark Prins        // $mediaId = str_replace('/', ':', $map->getMap());
116fc16f3cdSMark Prins        // if($this->startsWith($mediaId,':')) {
117fc16f3cdSMark Prins        //     $mediaId = substr($mediaId, 1);
118fc16f3cdSMark Prins        // }
11957f8d5bbSMark Prins        // return $mediaId;
12057f8d5bbSMark Prins        return str_replace('/', ':', $map->getMap());
121e61425c7SMark Prins    }
122e61425c7SMark Prins
123cc74a83cSMark Prins    /**
124cc74a83cSMark Prins     * Constructs the path to a file.
125cc74a83cSMark Prins     * @param string $id the DW media id
12657f8d5bbSMark Prins     * @return string the path to the file
127cc74a83cSMark Prins     */
128a760825cSgithub-actions[bot]    private function mediaIdToPath(string $id): string
129a760825cSgithub-actions[bot]    {
130e61425c7SMark Prins        global $conf;
131e61425c7SMark Prins        if (empty($id)) {
132e61425c7SMark Prins            return "";
133e61425c7SMark Prins        }
134a760825cSgithub-actions[bot]        $id = str_replace(["[[", "]]"], "", $id);
135*d29478dfSmprins        if ((str_starts_with($id, ':'))) {
136e61425c7SMark Prins            $id = substr($id, 1);
137e61425c7SMark Prins        }
138e61425c7SMark Prins        $id = str_replace(":", "/", $id);
139e61425c7SMark Prins        return $conf['mediadir'] . '/' . $id;
140e61425c7SMark Prins    }
141628e43ccSMark Prins}
142