xref: /plugin/openlayersmap/helper/staticmap.php (revision a760825c0aa6eee7ec491397b3516b6237e4ad78)
1628e43ccSMark Prins<?php
2*a760825cSgithub-actions[bot]
3628e43ccSMark Prins/*
4d2f1674eSMark Prins * Copyright (c) 2008-2022 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 */
20*a760825cSgithub-actions[bot]use dokuwiki\Extension\Plugin;
21d2f1674eSMark Prinsuse dokuwiki\plugin\openlayersmap\StaticMap;
22d2f1674eSMark Prins
23628e43ccSMark Prins/**
24cc74a83cSMark Prins * DokuWiki Plugin openlayersmap (staticmap Helper Component).
25cc74a83cSMark Prins * This provides the interface to generate a static map based on predefined OSM layers.
26628e43ccSMark Prins *
27628e43ccSMark Prins * @author Mark Prins
28628e43ccSMark Prins */
29*a760825cSgithub-actions[bot]class helper_plugin_openlayersmap_staticmap extends Plugin
30*a760825cSgithub-actions[bot]{
316eb3157eSMark Prins    /** maximum width of the resulting image. */
32628e43ccSMark Prins    private $maxWidth = 1024;
336eb3157eSMark Prins    /** maximum heigth of the resulting image. */
34628e43ccSMark Prins    private $maxHeight = 1024;
35cc74a83cSMark Prins
366eb3157eSMark Prins    /**
376eb3157eSMark Prins     * Provide metadata of the public methods of this class.
386eb3157eSMark Prins     *
396eb3157eSMark Prins     * @return array Information to all provided methods.
406eb3157eSMark Prins     */
41*a760825cSgithub-actions[bot]    public function getMethods(): array
42*a760825cSgithub-actions[bot]    {
43*a760825cSgithub-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']]];
44628e43ccSMark Prins    }
45628e43ccSMark Prins
46628e43ccSMark Prins    /**
47628e43ccSMark Prins     * Create the map.
48628e43ccSMark Prins     *
4957f8d5bbSMark Prins     * @param float  $lat     the latitude of the map's center, eg. 40.714728
5057f8d5bbSMark Prins     * @param float  $lon     the longitude of the map's center, eg -73.998672
5157f8d5bbSMark Prins     * @param int    $zoom    the zoom level in the tile cache, eg. 14
5257f8d5bbSMark Prins     * @param string $size    the size in WxH px, eg. 512x512
5357f8d5bbSMark Prins     * @param string $maptype the maptype, eg. cycle
5457f8d5bbSMark Prins     * @param array  $markers associative array of markers, array('lat'=>$lat,'lon'=>$lon,'type'=>$iconStyle),
5557f8d5bbSMark Prins     *                        eg. array('lat'=>40.702147,'lon'=>-74.015794,'type'=>lightblue1);
5657f8d5bbSMark Prins     * @param string $gpx     media link
5757f8d5bbSMark Prins     * @param string $kml     media link
5857f8d5bbSMark Prins     * @param string $geojson media link
5957f8d5bbSMark Prins     * @param string $apikey  optional API key eg. for Thunderforest maps
60628e43ccSMark Prins     */
6157f8d5bbSMark Prins    public function getMap(
6257f8d5bbSMark Prins        float $lat,
6357f8d5bbSMark Prins        float $lon,
6457f8d5bbSMark Prins        int $zoom,
6557f8d5bbSMark Prins        string $size,
6657f8d5bbSMark Prins        string $maptype,
6757f8d5bbSMark Prins        array $markers,
6857f8d5bbSMark Prins        string $gpx,
6957f8d5bbSMark Prins        string $kml,
7057f8d5bbSMark Prins        string $geojson,
7157f8d5bbSMark Prins        string $apikey = ''
7257f8d5bbSMark Prins    ): string {
73628e43ccSMark Prins        global $conf;
746eb3157eSMark Prins        // dbglog($markers,'helper_plugin_openlayersmap_staticmap::getMap: markers :');
75628e43ccSMark Prins
76628e43ccSMark Prins        // normalize zoom
7757f8d5bbSMark Prins        $zoom = $zoom ?: 0;
7857f8d5bbSMark Prins        if ($zoom > 18) {
7957f8d5bbSMark Prins            $zoom = 18;
8057f8d5bbSMark Prins        }
81628e43ccSMark Prins        // normalize WxH
82*a760825cSgithub-actions[bot]        [$width, $height] = explode('x', $size);
8357f8d5bbSMark Prins        $width = (int) $width;
8457f8d5bbSMark Prins        if ($width > $this->maxWidth) {
8557f8d5bbSMark Prins            $width = $this->maxWidth;
8657f8d5bbSMark Prins        }
8757f8d5bbSMark Prins        $height = (int) $height;
8857f8d5bbSMark Prins        if ($height > $this->maxHeight) {
8957f8d5bbSMark Prins            $height = $this->maxHeight;
9057f8d5bbSMark Prins        }
916eb3157eSMark Prins
92e61425c7SMark Prins        // cleanup/validate gpx/kml
93e61425c7SMark Prins        $kml = $this->mediaIdToPath($kml);
946eb3157eSMark Prins        // dbglog($kml,'helper_plugin_openlayersmap_staticmap::getMap: kml file:');
95e61425c7SMark Prins        $gpx = $this->mediaIdToPath($gpx);
966eb3157eSMark Prins        // dbglog($gpx,'helper_plugin_openlayersmap_staticmap::getMap: gpx file:');
976914b920SMark Prins        $geojson = $this->mediaIdToPath($geojson);
986c6bb022SMark Prins
99628e43ccSMark Prins        // create map
10057f8d5bbSMark Prins        $map = new StaticMap(
101*a760825cSgithub-actions[bot]            $lat,
102*a760825cSgithub-actions[bot]            $lon,
103*a760825cSgithub-actions[bot]            $zoom,
104*a760825cSgithub-actions[bot]            $width,
105*a760825cSgithub-actions[bot]            $height,
106*a760825cSgithub-actions[bot]            $maptype,
107*a760825cSgithub-actions[bot]            $markers,
108*a760825cSgithub-actions[bot]            $gpx,
109*a760825cSgithub-actions[bot]            $kml,
110*a760825cSgithub-actions[bot]            $geojson,
111*a760825cSgithub-actions[bot]            $conf['mediadir'],
112*a760825cSgithub-actions[bot]            $conf['cachedir'],
113*a760825cSgithub-actions[bot]            $this->getConf('autoZoomMap'),
114*a760825cSgithub-actions[bot]            $apikey
115628e43ccSMark Prins        );
1162d11d700SMark Prins
117628e43ccSMark Prins        // return the media id url
11857f8d5bbSMark Prins        // $mediaId = str_replace('/', ':', $map->getMap());
119fc16f3cdSMark Prins        // if($this->startsWith($mediaId,':')) {
120fc16f3cdSMark Prins        //     $mediaId = substr($mediaId, 1);
121fc16f3cdSMark Prins        // }
12257f8d5bbSMark Prins        // return $mediaId;
12357f8d5bbSMark Prins        return str_replace('/', ':', $map->getMap());
124e61425c7SMark Prins    }
125e61425c7SMark Prins
126cc74a83cSMark Prins    /**
127cc74a83cSMark Prins     * Constructs the path to a file.
128cc74a83cSMark Prins     * @param string $id the DW media id
12957f8d5bbSMark Prins     * @return string the path to the file
130cc74a83cSMark Prins     */
131*a760825cSgithub-actions[bot]    private function mediaIdToPath(string $id): string
132*a760825cSgithub-actions[bot]    {
133e61425c7SMark Prins        global $conf;
134e61425c7SMark Prins        if (empty($id)) {
135e61425c7SMark Prins            return "";
136e61425c7SMark Prins        }
137*a760825cSgithub-actions[bot]        $id = str_replace(["[[", "]]"], "", $id);
13857f8d5bbSMark Prins        if ((strpos($id, ':') === 0)) {
139e61425c7SMark Prins            $id = substr($id, 1);
140e61425c7SMark Prins        }
141e61425c7SMark Prins        $id = str_replace(":", "/", $id);
142e61425c7SMark Prins        return $conf['mediadir'] . '/' . $id;
143e61425c7SMark Prins    }
144628e43ccSMark Prins}
145