xref: /plugin/openlayersmap/helper/staticmap.php (revision f204b8ca21a3ff1a5b655b65aceab085168cc9d8)
1628e43ccSMark Prins<?php
2a760825cSgithub-actions[bot]
3628e43ccSMark Prins/*
4*f204b8caSMark 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 */
20*f204b8caSMark 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;
75*f204b8caSMark Prins        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: markers :',$markers);
76628e43ccSMark Prins
77628e43ccSMark Prins        // normalize zoom
7857f8d5bbSMark Prins        $zoom = $zoom ?: 0;
7957f8d5bbSMark Prins        if ($zoom > 18) {
8057f8d5bbSMark Prins            $zoom = 18;
8157f8d5bbSMark Prins        }
82628e43ccSMark Prins        // normalize WxH
83a760825cSgithub-actions[bot]        [$width, $height] = explode('x', $size);
8457f8d5bbSMark Prins        $width = (int) $width;
8557f8d5bbSMark Prins        if ($width > $this->maxWidth) {
8657f8d5bbSMark Prins            $width = $this->maxWidth;
8757f8d5bbSMark Prins        }
8857f8d5bbSMark Prins        $height = (int) $height;
8957f8d5bbSMark Prins        if ($height > $this->maxHeight) {
9057f8d5bbSMark Prins            $height = $this->maxHeight;
9157f8d5bbSMark Prins        }
926eb3157eSMark Prins
93e61425c7SMark Prins        // cleanup/validate gpx/kml
94e61425c7SMark Prins        $kml = $this->mediaIdToPath($kml);
95*f204b8caSMark Prins        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: kml file:',$kml);
96e61425c7SMark Prins        $gpx = $this->mediaIdToPath($gpx);
97*f204b8caSMark Prins        // Logger::debug('helper_plugin_openlayersmap_staticmap::getMap: gpx file:',$gpx);
986914b920SMark Prins        $geojson = $this->mediaIdToPath($geojson);
996c6bb022SMark Prins
100628e43ccSMark Prins        // create map
10157f8d5bbSMark Prins        $map = new StaticMap(
102a760825cSgithub-actions[bot]            $lat,
103a760825cSgithub-actions[bot]            $lon,
104a760825cSgithub-actions[bot]            $zoom,
105a760825cSgithub-actions[bot]            $width,
106a760825cSgithub-actions[bot]            $height,
107a760825cSgithub-actions[bot]            $maptype,
108a760825cSgithub-actions[bot]            $markers,
109a760825cSgithub-actions[bot]            $gpx,
110a760825cSgithub-actions[bot]            $kml,
111a760825cSgithub-actions[bot]            $geojson,
112a760825cSgithub-actions[bot]            $conf['mediadir'],
113a760825cSgithub-actions[bot]            $conf['cachedir'],
114a760825cSgithub-actions[bot]            $this->getConf('autoZoomMap'),
115a760825cSgithub-actions[bot]            $apikey
116628e43ccSMark Prins        );
1172d11d700SMark Prins
118628e43ccSMark Prins        // return the media id url
11957f8d5bbSMark Prins        // $mediaId = str_replace('/', ':', $map->getMap());
120fc16f3cdSMark Prins        // if($this->startsWith($mediaId,':')) {
121fc16f3cdSMark Prins        //     $mediaId = substr($mediaId, 1);
122fc16f3cdSMark Prins        // }
12357f8d5bbSMark Prins        // return $mediaId;
12457f8d5bbSMark Prins        return str_replace('/', ':', $map->getMap());
125e61425c7SMark Prins    }
126e61425c7SMark Prins
127cc74a83cSMark Prins    /**
128cc74a83cSMark Prins     * Constructs the path to a file.
129cc74a83cSMark Prins     * @param string $id the DW media id
13057f8d5bbSMark Prins     * @return string the path to the file
131cc74a83cSMark Prins     */
132a760825cSgithub-actions[bot]    private function mediaIdToPath(string $id): string
133a760825cSgithub-actions[bot]    {
134e61425c7SMark Prins        global $conf;
135e61425c7SMark Prins        if (empty($id)) {
136e61425c7SMark Prins            return "";
137e61425c7SMark Prins        }
138a760825cSgithub-actions[bot]        $id = str_replace(["[[", "]]"], "", $id);
13957f8d5bbSMark Prins        if ((strpos($id, ':') === 0)) {
140e61425c7SMark Prins            $id = substr($id, 1);
141e61425c7SMark Prins        }
142e61425c7SMark Prins        $id = str_replace(":", "/", $id);
143e61425c7SMark Prins        return $conf['mediadir'] . '/' . $id;
144e61425c7SMark Prins    }
145628e43ccSMark Prins}
146