WME TopPlus Overlay

Fügt die TopPlus Open WMS-Karte vom Geodatenzentrum zum Waze Map Editor hinzu

当前为 2025-05-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         WME TopPlus Overlay
// @namespace    https://greasyfork.org/de/users/863740-horst-wittlich
// @version      2025.05.12
// @description  Fügt die TopPlus Open WMS-Karte vom Geodatenzentrum zum Waze Map Editor hinzu
// @author       Hiwi234
// @match        https://www.waze.com/editor*
// @match        https://www.waze.com/*/editor*
// @match        https://beta.waze.com/editor*
// @match        https://beta.waze.com/*/editor*
// @grant        unsafeWindow
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Konfiguration
    const SCRIPT_NAME = 'WME TopPlus WMS';
    const SCRIPT_ID = 'wme-topplus-wms';
    const LAYER_NAME = 'TopPlus WMS';
    const DEFAULT_OPACITY = 0.7;

    let sidebarTab = null;

    // Warte auf WME
    function initTopPlusWMS() {
        if (typeof W === 'undefined' ||
            typeof W.map === 'undefined' ||
            typeof W.map.getOLMap === 'undefined' ||
            typeof OpenLayers === 'undefined' ||
            typeof W.userscripts === 'undefined') {
            console.log(SCRIPT_NAME + ': Warte auf WME...');
            setTimeout(initTopPlusWMS, 1000);
            return;
        }

        // Initialisiere Layer und Sidebar
        console.log(SCRIPT_NAME + ': Initialisiere...');
        try {
            createLayer();
            createSidebarTab();
            console.log(SCRIPT_NAME + ': Erfolgreich initialisiert');
        } catch (e) {
            console.error(SCRIPT_NAME + ': Fehler bei der Initialisierung', e);
        }
    }

    // Prüfe WME-Umgebung
    function checkWazeEnvironment() {
        console.log(SCRIPT_NAME + ': Prüfe WME-Umgebung...');
        if (typeof unsafeWindow !== 'undefined') {
            window.W = unsafeWindow.W;
            window.OpenLayers = unsafeWindow.OpenLayers;
        }

        initTopPlusWMS();
    }

    // Erstelle den WMS-Layer
    function createLayer() {
        const olMap = W.map.getOLMap();

        const wmsLayer = new OpenLayers.Layer.WMS(
            LAYER_NAME,
            "https://sgx.geodatenzentrum.de/wms_topplus_open",
            {
                layers: 'web',
                format: 'image/png',
                transparent: true
            },
            {
                transitionEffect: 'resize',
                isBaseLayer: false,
                visibility: true,
                opacity: DEFAULT_OPACITY,
                projection: new OpenLayers.Projection("EPSG:3857"),
                zIndex: 20
            }
        );

        wmsLayer.setOpacity(DEFAULT_OPACITY);
        olMap.addLayer(wmsLayer);

        const layerIndex = Math.max(5, olMap.layers.length - 5);
        olMap.setLayerIndex(wmsLayer, layerIndex);

        window.topPlusWMSLayer = wmsLayer;

        olMap.events.register('changelayer', null, function(evt) {
            if (window.topPlusWMSLayer && evt.property === "order") {
                setTimeout(function() {
                    const currentIndex = olMap.getLayerIndex(window.topPlusWMSLayer);
                    const desiredIndex = Math.max(5, olMap.layers.length - 5);
                    if (currentIndex < desiredIndex) {
                        olMap.setLayerIndex(window.topPlusWMSLayer, desiredIndex);
                    }
                }, 100);
            }
        });
    }

    // Erstelle den Sidebar-Tab
    function createSidebarTab() {
        try {
            sidebarTab = W.userscripts.registerSidebarTab(SCRIPT_ID);

            // Tab-Label mit Icon
            const icon = document.createElement('i');
            icon.className = 'w-icon w-icon-map';
            icon.style.marginRight = '5px';

            const label = document.createElement('span');
            label.textContent = 'TopPlus';

            sidebarTab.tabLabel.appendChild(icon);
            sidebarTab.tabLabel.appendChild(label);

            // Warte auf Tab-Pane
            W.userscripts.waitForElementConnected(sidebarTab.tabPane).then(() => {
                // Erstelle den Tab-Inhalt
                const content = document.createElement('div');
                content.className = 'side-panel-section';
                content.innerHTML = `
                    <div class="form-group">
                        <div class="controls-container">
                            <div class="toggle-switch">
                                <input type="checkbox" id="wms-toggle" checked>
                                <label for="wms-toggle" class="checkbox-label">
                                    Layer anzeigen
                                </label>
                            </div>

                            <div class="opacity-control" style="margin-top: 15px;">
                                <label class="control-label">
                                    Deckkraft: <span id="opacity-value">70%</span>
                                </label>
                                <input type="range"
                                       id="wms-opacity"
                                       class="form-control"
                                       min="0"
                                       max="100"
                                       step="1"
                                       value="70"
                                       style="width: 100%;">
                            </div>
                        </div>
                    </div>
                `;

                sidebarTab.tabPane.appendChild(content);

                // Event-Handler für Toggle
                const toggleSwitch = document.getElementById('wms-toggle');
                toggleSwitch.addEventListener('change', function() {
                    if (window.topPlusWMSLayer) {
                        window.topPlusWMSLayer.setVisibility(this.checked);
                    }
                });

                // Event-Handler für Opacity Slider
                const opacitySlider = document.getElementById('wms-opacity');
                const opacityValue = document.getElementById('opacity-value');

                opacitySlider.addEventListener('input', function() {
                    if (window.topPlusWMSLayer) {
                        const value = this.value;
                        window.topPlusWMSLayer.setOpacity(value / 100);
                        opacityValue.textContent = value + '%';
                    }
                });
            });

            console.log(SCRIPT_NAME + ': Sidebar-Tab erfolgreich erstellt');
        } catch (error) {
            console.error(SCRIPT_NAME + ': Fehler beim Erstellen des Sidebar-Tabs:', error);
        }
    }

    // Script starten
    checkWazeEnvironment();
})();