WME Split POI

Split POI with a new seg

/* eslint-disable max-len */
/* eslint-disable prefer-destructuring */
/* eslint-disable camelcase */
// ==UserScript==
// @name            WME Split POI
// @namespace       https://greasyfork.org/fr/scripts/13008-wme-split-poi
// @description     Split POI with a new seg
// @description:fr  Découpage d'un POI en deux en utisant un nouveau segment
// @include         https://www.waze.com/editor*
// @include         https://www.waze.com/*/editor*
// @include         https://beta.waze.com/editor*
// @include         https://beta.waze.com/*/editor*
// @exclude         https://www.waze.com/user*
// @exclude         https://www.waze.com/*/user*
// @require         https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// eslint-disable-next-line max-len
// @icon            data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gYFFhEcAw1y8AAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAB43SURBVHja7Zp5lF11le8/+3eGO9atulWVoaqSVFIZKglTSJgbCaEFQSYbhAi0onbb3doueU2v7tZHq4XPoVG7nVo0S0UxTmFQQRAFBQRlCEgImQhJSEhlqkrNdacz/H6/98e5VQmKmNC23b3eO2vdVbXucM7Z3/3de3/33gf+//H/9iH/2Re47bbbnDee/dQUP35xBmbPIolG2h2JrgMRI7mfajv7bpvqClx3noyqkx5paTlt7H88AJs29fhd/tNnpHPrfmRCySu/jDUaItDaABpHWcBCaLAWtAbHL6JS54RR9szTf7XxmOdWrFgR/48C4LbbrnAuO6X6iCPPnqFrBidVgzBODLUxhHWj4RX+Tvwv4DSBzIWpV2wYluWva24+afS/PQCVbZfNyKj1vUZVUXGE1hobxLiOTozHHPpyKL9h+G8A4QtIBkwRsm3E2RM/6LV/6aP/7QDYtKmHLv9ppLL53b5budnaGGyMig8z1upDxlvz2yeJOOwze+i2GlKgsuC0gvhgW/orzVefmJvyF/v+kACo10h14l0XcfvtKGWe2uio0s2EGiUxqgqE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37/rBe2bJMvfv4rnHzaEnvfj38m/YMDnPUnp/bOmFGoHu2gs1QqMTAwQH9/P8cccwzd3d3MnTuXQqEwGQ6O4yROfqUTpNNpOmfP5vktW5qGh4cLmUxmfhRFeWvtMmutF8exaK1P9Dwvv23rtuI73/ae7m3bdilVz/IxhrbpLfart/x7ecnJxw1HOrDYiTW5IFZhrRVRSvXu6s3/xdvf27D9hd1K6kOwGhGtxRa+8rUvVM5accbWWrVWFhHjOI5rrd3vOM523/e1iKzzPK9sjBk2xuzM5/N9v2nLjTfeSFNTE11dXSxevJj29vZkV1HPaa8IQCqVoq+v7ztDQ0PnB0HQYK11jakrxfrfiSOX9endtd984yvfsxvXb1DGall0zHx97V9cw7xFc51StYqtDzYnQkkdNujNpFPs6d1jbln1Hdb/+jmxVsuc+bPMte98GycsO0GVKhXEHtoPymEPFyilJvOU67rVXC63o729/bhXsmn16tVUKhUWL17M3LlzKRaLpNPp372CGRsb+5fR0dELgyAoGmOmaa1drfXLVmUTRvm+j+M4jI+PTyYcYwxhGCTjvcNyycs3x8n7vu+hlMPIyAhGG5qKTYgIQRAcAu4w4ydFzASNRcq+7/dnMpnNU6dOvejVwuPOO+8klUrR2dlJW1vb7wagUCgwOjoqQCYMw844jnPACcYYV2st1tplcRw3AFZrLVpr31rrWGwXloy11hwGkmit5TeMt4BNPOgAVpRSopSyxhillAqstdscxwld1w1FxCqlXGvtPtd1t7uuGwVBsMX3/VIulxsB9olIeKR54sEHH0zC4NW+1NraSmtrK9Qz5iT6SlGpVNize/fk+692FItFpkyZgnKcQ4/D1L0YhCH79uyZ9PYf+/i9j5Ll83mKzc14vo8CojimXCoxPDz88vXZ7znS6TQtLS2kMxkE0MZQLpUYHBw8qvP8lx0T8fbf5Tx/qOP/AsMtDLAda4jhAAAAAElFTkSuQmCC
// @author          seb-d59, WazeDev (2023-?)
// @version         2024-02-06-001
// @license         GPLv3
// @grant           GM_xmlhttpRequest
// @connect         greasyfork.org
// ==/UserScript==

/* global W */
/* global OpenLayers */
/* global WazeWrap */

(function main() {
    'use strict';

    const DEBUG = false;
    const SCRIPT_VERSION = GM_info.script.version;
    const SCRIPT_NAME = GM_info.script.name;
    const DOWNLOAD_URL = 'https://greasyfork.org/scripts/13008-wme-split-poi/code/WME%20Split%20POI.user.js';
    const MINIMUM_AREA = 100.0;

    let LandmarkVectorFeature;
    let DeleteObjectAction;
    let DeleteSegmentAction;
    let UpdateFeatureAddressAction;
    let MultiAction;

    function bootstrap() {
        if (WazeWrap.Ready) {
            initialize();
        } else {
            setTimeout(bootstrap, 100);
        }
    }

    function getId(node) {
        return document.getElementById(node);
    }

    function log(msg, obj) {
        if (obj == null) {
            console.log(`WME Split POI v${SCRIPT_VERSION} - ${msg}`);
        } else if (DEBUG) {
            console.debug(`WME Split POI v${SCRIPT_VERSION} - ${msg} `, obj);
        }
    }

    function initialize() {
        log('init');
        startScriptUpdateMonitor();
        initializeWazeObjects();
    }

    function startScriptUpdateMonitor() {
        let updateMonitor;
        try {
            updateMonitor = new WazeWrap.Alerts.ScriptUpdateMonitor(SCRIPT_NAME, SCRIPT_VERSION, DOWNLOAD_URL, GM_xmlhttpRequest);
            updateMonitor.start();
        } catch (ex) {
            // Report, but don't stop if ScriptUpdateMonitor fails.
            console.error(`${SCRIPT_NAME}:`, ex);
        }
    }

    function initializeWazeObjects() {
        DeleteObjectAction = require('Waze/Action/DeleteObject');
        DeleteSegmentAction = require('Waze/Action/DeleteSegment');
        LandmarkVectorFeature = require('Waze/Feature/Vector/Landmark');
        UpdateFeatureAddressAction = require('Waze/Action/UpdateFeatureAddress');
        MultiAction = require('Waze/Action/MultiAction');
        W.selectionManager.events.register('selectionchanged', null, onSelectionChanged);
    }

    function onSelectionChanged() {
        try {
            if (W.selectionManager.getSelectedDataModelObjects().length !== 1) return;

            const selectedObject = W.selectionManager.getSelectedDataModelObjects()[0];
            if (selectedObject.type !== 'venue' || selectedObject.isPoint()) return;

            // const landmarkPoi = '(NATURAL_FEATURES|ISLAND|SEA_LAKE_POOL|RIVER_STREAM|FOREST_GROVE|FARM|CANAL|SWAMP_MARSH|DAM|PARK)';
            // if (new RegExp(landmarkPoi).test(attributes.categories) === false) return;

            log('selectionManager', W.selectionManager);

            const editPanel = getId('edit-panel');
            if (editPanel.firstElementChild.style.display === 'none') {
                window.setTimeout(onSelectionChanged, 100);
            }

            // ok: 1 selected item and pannel is shown

            // On verifie que le segment est éditable
            if (!objIsEditable(selectedObject)) return;

            // Exclude gas station and EVCS categories (don't ever want to delete those by splitting):
            if (selectedObject.attributes.categories.some(cat => ['GAS_STATION', 'CHARGING_STATION'].includes(cat))) return;

            if (selectedObject.type === 'venue' && !$('#split-poi-button').length) {
                let addAfter = true;
                let $btnHandle = $('.geometry-type-control-area')[0];
                if (!$btnHandle) {
                    $btnHandle = $('.external-providers-control')[0];
                    addAfter = false;
                }
                const WMESP_Controle = document.createElement('wz-button');
                WMESP_Controle.color = 'secondary';
                WMESP_Controle.size = 'sm';
                WMESP_Controle.id = 'split-poi-button';
                WMESP_Controle.className = 'geometry-type-control-button geometry-type-control-point';
                WMESP_Controle.innerHTML = '<i class="fa fa-cut" style="font-size:24px;" title="Split POI"></i>';
                if (addAfter) {
                    $btnHandle.after(WMESP_Controle);
                } else {
                    $btnHandle.before(WMESP_Controle);
                }
                WMESP_Controle.onclick = onSplitPoiButtonClick;
            }
        } catch (ex) {
            console.error('Split POI:', ex);
        }
    }

    function onScreen(obj) {
        if (obj.geometry) {
            return (W.map.getExtent().intersectsBounds(obj.geometry.getBounds()));
        }
        return false;
    }

    function objIsEditable(obj) {
        if (obj == null) return false;
        if (W.loginManager.user.isCountryManager()) return true;
        if (obj.attributes.permissions === 0) return false;

        return true;
    }

    // This will return null if more than one object is selected
    function getSelectedAreaPlace() {
        const selectedObjects = W.selectionManager.getSelectedDataModelObjects();
        if (selectedObjects.length > 1) return null;
        const object = selectedObjects[0];
        if (object.type !== 'venue' || object.isPoint()) return null;
        return object;
    }

    function getNewestUnconnectedOnScreenSegment() {
        const newSegs = W.model.segments.getObjectArray(seg => seg.isNew());
        let newestSeg;
        let newestId = 0;
        newSegs.forEach(seg => {
            const hasConnections = seg.getToNode().getSegmentIds().length > 1 || seg.getFromNode().getSegmentIds().length > 1;
            if (seg.getID() < newestId && onScreen(seg) && !hasConnections) {
                newestSeg = seg;
                newestId = seg.getID();
            }
        });
        return newestSeg;
    }

    function getPoiAndSegIntersectionPoints(poi, seg) {
        function clearComponent(geometry) {
            geometry.removeComponent(0);
            geometry.removeComponent(1);
        }

        function copyComponent(sourceGeometry, sourceIndex, targetGeometry) {
            targetGeometry.components[0] = sourceGeometry.components[sourceIndex].clone();
            targetGeometry.components[1] = sourceGeometry.components[sourceIndex + 1].clone();
        }

        const poiAttr = poi.attributes;
        const poiGeo = poiAttr.geometry.clone();
        const poiLineString = poiGeo.components[0].clone();
        const segLineString = seg.attributes.geometry.clone();

        const intersectPoint = [];
        const poiLine = new OpenLayers.Geometry.LinearRing();
        const segLine = new OpenLayers.Geometry.LinearRing();

        // Calcul des point d'intersection seg // poi
        for (let n = 0; n < poiLineString.components.length - 1; n++) {
            copyComponent(poiLineString, n, poiLine);
            for (let m = 0; m < segLineString.components.length - 1; m++) {
                copyComponent(segLineString, m, segLine);
                if (poiLine.intersects(segLine)) {
                    intersectPoint.push({ index: n, intersect: intersection(poiLine, segLine) });
                }
                clearComponent(segLine);
            }
            clearComponent(poiLine);
        }

        return intersectPoint;
    }

    function createTwoPolygonsFromIntersectPoints(poi, intersectPoints) {
        const poiLineString = poi.attributes.geometry.components[0].clone();
        // intégration des points au contour du POI avec memo du nouvel index
        let i = 1;
        for (let n = 0; n < intersectPoints.length; n++) {
            const point = intersectPoints[n].intersect;
            const index = intersectPoints[n].index + i;
            poiLineString.addComponent(point, index);
            intersectPoints[n].newIndex = index;
            i++;
        }

        // création des deux nouvelles géométries
        const lineString1 = [];
        const lineString2 = [];

        const index1 = intersectPoints[0].newIndex;
        const index2 = intersectPoints[1].newIndex;

        for (let n = 0; n < poiLineString.components.length; n++) {
            const x = poiLineString.components[n].x;
            const y = poiLineString.components[n].y;
            const point = new OpenLayers.Geometry.Point(x, y);

            if (n < index1) {
                lineString1.push(point);
            } else if (n === index1) {
                lineString1.push(point);
                lineString2.push(point.clone());
            } else if ((index1 < n) && (n < index2)) {
                lineString2.push(point);
            } else if (n === index2) {
                lineString1.push(point);
                lineString2.push(point.clone());
            } else if (index2 < n) {
                lineString1.push(point);
            }
        }

        return [
            new OpenLayers.Geometry.Polygon(new OpenLayers.Geometry.LinearRing(lineString1)),
            new OpenLayers.Geometry.Polygon(new OpenLayers.Geometry.LinearRing(lineString2))
        ];
    }

    function cloneAttribute(poi, attrName, newAttributesObject) {
        if (poi.attributes.hasOwnProperty(attrName)) {
            let value = poi.attributes[attrName];

            if (Array.isArray(value)) {
                value = value.slice(0); // copy array
            }
            newAttributesObject[attrName] = poi.attributes[attrName];
        }
    }

    function addClonePoiAction(poi, newGeometry, nameSuffixIndex, actions) {
        const clonePoi = new LandmarkVectorFeature({ geoJSONGeometry: W.userscripts.toGeoJSONGeometry(newGeometry) });
        [
            'aliases',
            'categories',
            'description',
            'entryExitPoints',
            'externalProviderIDs',
            'houseNumber',
            'lockRank',
            'name',
            'openingHours',
            'phone',
            'services',
            'streetID',
            'url'
        ].forEach(attrName => cloneAttribute(poi, attrName, clonePoi.attributes));
        if (clonePoi.attributes.name) clonePoi.attributes.name += ` (copy ${nameSuffixIndex})`; // IMPORTANT! Won't save for some reason without changing the names (at least for PLAs).
        if (poi.attributes.categoryAttributes.PARKING_LOT) {
            clonePoi.attributes.categoryAttributes.PARKING_LOT = JSON.parse(JSON.stringify(poi.attributes.categoryAttributes.PARKING_LOT));
        }

        const WazeActionAddLandmark = require('Waze/Action/AddLandmark');
        actions.push(new WazeActionAddLandmark(clonePoi));

        const street = W.model.streets.getObjectById(poi.attributes.streetID);
        const streetName = street.attributes.name;
        const cityID = street.attributes.cityID;
        const city = W.model.cities.getObjectById(cityID);
        const stateID = city.attributes.stateID;
        const countryID = city.attributes.countryID;
        const houseNumber = poi.attributes.houseNumber;
        if (!street.attributes.isEmpty || !city.attributes.isEmpty) { // nok
            const newAtts = {
                emptyStreet: street.attributes.isEmpty, // TODO: fix this
                stateID,
                countryID,
                cityName: city.attributes.name,
                houseNumber,
                streetName,
                emptyCity: city.attributes.isEmpty // TODO: fix this
            };
            const updateAddressAction = new UpdateFeatureAddressAction(clonePoi, newAtts);
            updateAddressAction.options.updateHouseNumber = true;
            actions.push(updateAddressAction);
        }
    }

    function confirmBeforeSplitting(poi) {
        const entryExitPointsLen = poi.attributes.entryExitPoints?.length;
        const imagesLen = poi.attributes.images?.length;
        const extProvidersLen = poi.attributes.externalProviderIDs?.length;
        let warningText = 'WARNING: The original place will be deleted!';

        if (imagesLen) {
            warningText += '\n\nThe following property(s) will be lost:';
            if (imagesLen) warningText += `\n • ${imagesLen} photo${imagesLen === 1 ? '' : 's'} (permanently deleted after saving)`;
        }
        warningText += '\n\nThe following properties likely need to be changed after splitting:';
        warningText += '\n • name ("copy #" will be appended)';
        if (entryExitPointsLen) warningText += `\n • ${entryExitPointsLen} entry/exit point${entryExitPointsLen === 1 ? '' : 's'}`;
        if (extProvidersLen) warningText += `\n • ${extProvidersLen} linked Google place${extProvidersLen === 1 ? '' : 's'}`;
        warningText += '\n\nReview <i>all</i> properties of both new places before saving.';
        warningText += '\n';
        return new Promise(resolve => {
            WazeWrap.Alerts.confirm(
                SCRIPT_NAME,
                warningText,
                () => resolve(true),
                () => resolve(false)
            );
        });
    }

    async function onSplitPoiButtonClick() {
        const poi = getSelectedAreaPlace();
        if (!poi) return;

        // This is needed in case the category is changed to GS or EVCS and the split button is still there.
        if (poi.attributes.categories.some(cat => ['GAS_STATION', 'CHARGING_STATION'].includes(cat))) {
            WazeWrap.Alerts.error(SCRIPT_NAME, 'Cannot split gas stations or EV charging stations');
            return;
        }

        const seg = getNewestUnconnectedOnScreenSegment();
        if (!seg) {
            WazeWrap.Alerts.error(SCRIPT_NAME, 'Create a temporary unconnected road segment through the area place first.');
            return;
        }

        if (seg.geometry.components.some(pt => poi.geometry.containsPoint(pt))) {
            WazeWrap.Alerts.error(SCRIPT_NAME, 'The splitting road segment must be straight (no geometry handles within the POI).');
            return;
        }

        const intersectPoints = getPoiAndSegIntersectionPoints(poi, seg);
        if (intersectPoints.length !== 2) {
            WazeWrap.Alerts.error(SCRIPT_NAME, 'The temporary road segment must intersect the area place boundary at two points.');
            return;
        }

        const newPolygons = createTwoPolygonsFromIntersectPoints(poi, intersectPoints);
        if (newPolygons[0].getArea() < MINIMUM_AREA || newPolygons[1].getArea() < MINIMUM_AREA) {
            WazeWrap.Alerts.error(SCRIPT_NAME, 'New area place would be too small. Move the temporary road segment.');
            return;
        }

        const confirm = await confirmBeforeSplitting(poi);
        if (confirm) {
            const actions = [];
            addClonePoiAction(poi, newPolygons[0], 1, actions);
            addClonePoiAction(poi, newPolygons[1], 2, actions);
            actions.push(new DeleteObjectAction(poi, null));
            actions.push(new DeleteSegmentAction(seg));
            const multiaction = new MultiAction(actions, { description: 'Split POI' });
            W.model.actionManager.add(multiaction);
        }
    }

    function intersection(D1, D2) {
        // let a, b, c, d, x, y;
        // const seg = {}; // {x1, y1, x2, y2};
        const seg1 = {}; // {x1, y1, x2, y2};
        const seg2 = {}; // {x1, y1, x2, y2};
        const options = {};
        options.point = true;

        if (D1.components[0].x <= D1.components[1].x) {
            seg1.x1 = D1.components[0].x;
            seg1.y1 = D1.components[0].y;
            seg1.x2 = D1.components[1].x;
            seg1.y2 = D1.components[1].y;
        } else if (D1.components[0].x > D1.components[1].x) {
            seg1.x1 = D1.components[1].x;
            seg1.y1 = D1.components[1].y;
            seg1.x2 = D1.components[0].x;
            seg1.y2 = D1.components[0].y;
        }

        if (D2.components[0].x <= D2.components[1].x) {
            seg2.x1 = D2.components[0].x;
            seg2.y1 = D2.components[0].y;
            seg2.x2 = D2.components[1].x;
            seg2.y2 = D2.components[1].y;
        } else if (D2.components[0].x > D2.components[1].x) {
            seg2.x1 = D2.components[1].x;
            seg2.y1 = D2.components[1].y;
            seg2.x2 = D2.components[0].x;
            seg2.y2 = D2.components[0].y;
        }
        return OpenLayers.Geometry.segmentsIntersect(seg1, seg2, options);
    }

    bootstrap();
})();