WME E85 Simplify Street Geometry

Simplify Street Geometry, looks like fork

Install this script?
Author's suggested script

You may also like WME E50 Fetch POI Data.

Install this script
// ==UserScript==
// @name         WME E85 Simplify Street Geometry
// @name:uk      WME 🇺🇦 E85 Simplify Street Geometry
// @version      0.2.1
// @description  Simplify Street Geometry, looks like fork
// @description:uk Спрощуємо та вирівнюємо геометрію вулиць
// @license      MIT License
// @author       Anton Shevchuk
// @namespace    https://greasyfork.org/users/227648-anton-shevchuk
// @supportURL   https://github.com/AntonShevchuk/wme-e85/issues
// @match        https://*.waze.com/editor*
// @match        https://*.waze.com/*/editor*
// @exclude      https://*.waze.com/user/editor*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAZ23pUWHRSYXcgcHJvZmlsZSB0eXBlIGV4aWYAAHja7ZtZlhy5ckT/sQotITADy8F4jnag5esakDWxi3zdXfoUi6xMZkRicndzMwfCrP/5723+iz8lPc6EmEuqKT38CTVU13hTnvunnd/2Cef3+RNel/j/l8/N+wXHR55Xf/9b0uv+t8/tewP3pfEufmqojNeF/vVCffXgyi8NufviNSK9n6+G6qsh7+4F+2qg3Wk9qZb8eQp93df5NpNy/xn9CuXrsP/y/8zqzUg/3rnlrX/47b27A/D654xvXHD3Nzfe99GX8zu9GmNBvlun9z+VEe31MsVfb/pilfd39vvPza/WCu51i/9lkdP767efGxt/ueDf+3Ff/Ke83rlfPu/XjOb5ZfX1b+9Z9pkzs2ghsdTpNam3qZx33NdpSl0Xw9DSk/kXaSKfn8pPwasHrjCf8XR+hq3WYa5tg5222W3XeR12MMTglnGZN84N58+HxWdX3fCyX9CP3S776id2dH4cswfv3sdiT7f1Geb0Vuh5Wm51lsbkAv/4x/zTL+ytULD2Ke9rxbic02IzDFlOv7kNi9j9WtR4Fvjt59c/sqvHglGrrBCpLGy/TfRoP5DAH0N7boy83hi0eb4aYInoOjIY67EAVrM+2mSf7Fy2loUsGKgxdOeD61jAxugmg3TB+4RtilPXfC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ogICBkYzpGb3JtYXQ9ImltYWdlL3BuZyIKICAgR0lNUDpBUEk9IjIuMCIKICAgR0lNUDpQbGF0Zm9ybT0iTWFjIE9TIgogICBHSU1QOlRpbWVTdGFtcD0iMTY3MDg0NjMzNzI5MjY2NyIKICAgR0lNUDpWZXJzaW9uPSIyLjEwLjMyIgogICB0aWZmOk9yaWVudGF0aW9uPSIxIgogICB4bXA6Q3JlYXRvclRvb2w9IkdJTVAgMi4xMCIKICAgeG1wOk1ldGFkYXRhRGF0ZT0iMjAyMjoxMjoxMlQxMjo1ODo1NiswMTowMCIKICAgeG1wOk1vZGlmeURhdGU9IjIwMjI6MTI6MTJUMTI6NTg6NTYrMDE6MDAiPgogICA8eG1wTU06SGlzdG9yeT4KICAgIDxyZGY6U2VxPgogICAgIDxyZGY6bGkKICAgICAgc3RFdnQ6YWN0aW9uPSJzYXZlZCIKICAgICAgc3RFdnQ6Y2hhbmdlZD0iLyIKICAgICAgc3RFdnQ6aW5zdGFuY2VJRD0ieG1wLmlpZDplZDRiNjEzOS1iYmU3LTQ3MzgtODZkYi1kZTYwMzk2OWM3NjciCiAgICAgIHN0RXZ0OnNvZnR3YXJlQWdlbnQ9IkdpbXAgMi4xMCAoTWFjIE9TKSIKICAgICAgc3RFdnQ6d2hlbj0iMjAyMi0xMi0xMlQxMjo1ODo1NyswMTowMCIvPgogICAgPC9yZGY6U2VxPgogICA8L3htcE1NOkhpc3Rvcnk+CiAgPC9yZGY6RGVzY3JpcHRpb24+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgCjw/eHBhY2tldCBlbmQ9InciPz4PVOYgAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH5gwMCzo59Ra5FgAABz9JREFUeNrtmntMVFcexz+8QXQE1GEGmFVR44NHLXEayqPBWLdpt6nUB1oXLbQ10G1KbZvQVLtAXItNY//QbBraVB7bVNpx1bGGTReycbUzhAUJ0YLSAiXKY4b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IrhN8E+ze+Ik2miwmwdGC5XtzpxmA16Jes3sHjNlo7jRjwfK9I/DVd+XfjZ6fQyIx7jaSHJA8euiwo19yQDLG3UYiQyIBuG/zV/fZx8L9RGYsDe7R5muL84ryGNsO9ghzp5m8ojy0+dpiYI/0dL2QPpMT+JRjjCBDhgwZMmTIkCFDhowFi/8ADJ4wMQj+858AAAAASUVORK5CYII=
// @grant        none
// @require      https://update.greasyfork.org/scripts/389765/1090053/CommonUtils.js
// @require      https://update.greasyfork.org/scripts/450160/1218867/WME-Bootstrap.js
// @require      https://update.greasyfork.org/scripts/452563/1218878/WME.js
// @require      https://update.greasyfork.org/scripts/450221/1137043/WME-Base.js
// @require      https://update.greasyfork.org/scripts/450320/1281847/WME-UI.js
// ==/UserScript==

/* jshint esversion: 8 */

/* global require */
/* global $, jQuery */
/* global W */
/* global I18n */
/* global OpenLayers */
/* global WME, WMEBase */
/* global WMEUI, WMEUIHelper, WMEUIHelperPanel, WMEUIHelperModal, WMEUIHelperTab, WMEUIShortcut */
/* global Container, Settings, SimpleCache, Tools  */

(function () {
  'use strict'

  // Script name, uses as unique index
  const NAME = 'E85'

  // Translations
  const TRANSLATION = {
    'en': {
      title: 'Street Geometry',
      description: 'Simplify and straighten up streets',
      buttons: {
        A: 'Simplify',
        B: 'Straighten',
        C: '∡90°',
      },
      settings: {
        title: 'Settings',
        description: 'Settings for simplifying segments',
        simplifyShort: 'Remove a fragment shorter than',
        simplifyAngle: 'If the angle is bigger than',
        simplifyTwoShort: 'and fragments shorter than',
      },
    },
    'uk': {
      title: 'Геометрія вулиць',
      description: 'Спрощуйте та вирівнюйте вулиці',
      buttons: {
        A: 'Спростити',
        B: 'Вирівняти',
        C: '∡90°',
      },
      settings: {
        title: 'Налаштування',
        description: 'Для спрощення сегментів будуть враховані наступні параметри',
        simplifyShort: 'Видаляти фрагменти менші ніж',
        simplifyAngle: 'Або якщо кут більше ніж',
        simplifyTwoShort: 'та фрагменти меньші ніж',
      },
    },
    'ru': {
      title: 'Геометрия улиц',
      description: 'Упрощайте и выравнивайте геометрию улиц',
      buttons: {
        A: 'Упростить',
        B: 'Выровнять',
        C: '∡90°',
      },
      settings: {
        title: 'Настройки',
        description: 'Параметры для упрощения геометрии сегмента',
        simplifyShort: 'Если фрагмент короче, чем',
        simplifyAngle: 'Или угол больше чем',
        simplifyTwoShort: 'и фрагменты меньше, чем',
      },
    }
  }

  const STYLE =
    'button.e85.e85-A { background-color: #0f9; margin-right: 2px }' +
    'button.e85.e85-B { background-color: #09f; color: #fff }' +
    'button.e85.e85-C { background-color: #f99; margin-left: 2px }' +
    'button.e85.e85-A:disabled, button.e85.e85-B:disabled { background-color: #ccc }' +
    '.e85 legend { cursor:pointer; font-size: 12px; font-weight: bold; width: auto; text-align: right; border: 0; margin: 0; padding: 0 8px; }' +
    '.e85 fieldset { border: 1px solid #ddd; padding: 8px; }' +
    '.e85 fieldset.e85 div.controls label { white-space: normal; font-weight: normal; line-height: 32px; font-size: 13px; }' +
    '.e85 fieldset.e85 div.controls input[type="number"] { float:right; wight: 32px }' +
    'p.e85-info { border-top: 1px solid #ccc; color: #777; font-size: x-small; margin-top: 15px; padding-top: 10px; text-align: center; }'

  WMEUI.addTranslation(NAME, TRANSLATION)
  WMEUI.addStyle(STYLE)

  const BUTTONS = {
    A: {
      title: I18n.t(NAME).buttons.A,
      description: I18n.t(NAME).buttons.A,
      shortcut: '',
    },
    B: {
      title: I18n.t(NAME).buttons.B,
      description: I18n.t(NAME).buttons.B,
      shortcut: '',
    },
    C: {
      title: I18n.t(NAME).buttons.C,
      description: I18n.t(NAME).buttons.C,
      shortcut: '',
    },
  }

  // Default settings
  const SETTINGS = {
    simplifyShort: 5,
    simplifyAngle: 176,
    simplifyTwoShort: 50,
  }

  let WazeActionUpdateSegmentGeometry
  let WazeActionMoveNode
  let WazeActionAddNode

  class E85 extends WMEBase {
    /**
     * Initial UI elements
     * @param {Object} buttons
     */
    init (buttons) {
      /** @type {WMEUIHelper} */
      this.helper = new WMEUIHelper(this.name)

      /** @type {WMEUIHelperTab} */
      this.tab = this.helper.createTab(
        I18n.t(this.name).title,
        {
          image: GM_info.script.icon
        }
      )

      // Setup options for script
      let fieldset = this.helper.createFieldset(I18n.t(NAME).settings.title)
      fieldset.addText('description', I18n.t(NAME).settings.description)
      let settings = this.settings.get()
      for (let item in settings) {
        if (settings.hasOwnProperty(item)) {
          fieldset.addNumber(
            'settings-' + item,
            I18n.t(NAME).settings[item],
            event => this.settings.set([item], event.target.value),
            this.settings.get(item),
            (item === 'simplifyAngle') ? 150 : 0,
            (item === 'simplifyAngle') ? 180 : 200,
            1
          )
        }
      }
      this.tab.addElement(fieldset)
      this.tab.addText(
        'info',
        '<a href="' + GM_info.scriptUpdateURL + '">' + GM_info.script.name + '</a> ' + GM_info.script.version
      )

      // Inject custom HTML to container in the WME interface
      this.tab.inject()
    }

    /**
     * Handler for `segment.wme` event
     * @param {jQuery.Event} event
     * @param {HTMLElement} element
     * @param {W.model} model
     * @return {void}
     */
    onSegment (event, element, model) {
      // Skip for blocked roads
      if (model.isLockedByHigherRank() || !model.isGeometryEditable()) {
        return
      }

      let panel = this.helper.createPanel(I18n.t(this.name).title)
      let simplifyButton = panel.addButton(
        'A',
        BUTTONS.A.title,
        BUTTONS.A.description,
        () => this.simplifySegmentGeometry(model),
        BUTTONS.A.shortcut
      )

      let straightenButton = panel.addButton(
        'B',
        BUTTONS.B.title,
        BUTTONS.B.description,
        () => this.straightenSegmentGeometry(model),
        BUTTONS.B.shortcut
      )
      if (model.getGeometry().coordinates.length < 3) {
        simplifyButton.html().disabled = true
        straightenButton.html().disabled = true
      }

      const existingFormGroup = element.querySelector('div.form-group.e85');
      if (existingFormGroup) {
        existingFormGroup.replaceWith(panel.html());
      } else {
        element.prepend(panel.html());
      }
    }

    /**
     * Handler for `segments.wme` event
     * @param {jQuery.Event} event
     * @param {HTMLElement} element
     * @param {Array} models
     * @return {void}
     */
    onSegments (event, element, models) {
      // Skip for locked roads
      if (models.filter((model) => model.isLockedByHigherRank() || !model.isGeometryEditable()).length > 0) {
        element.querySelector('div.form-group.e85')?.remove()
        return
      }

      let panel = this.helper.createPanel(I18n.t(this.name).title)
      let simplifyButton = panel.addButton(
        'A',
        BUTTONS.A.title,
        BUTTONS.A.description,
        () => this.simplifyStreetGeometry(models),
        BUTTONS.A.shortcut
      )

      // Don't straighten multiple components
      let straightenButton = panel.addButton(
        'B',
        BUTTONS.B.title,
        BUTTONS.B.description,
        () => this.straightenStreetGeometry(models),
        BUTTONS.B.shortcut
      )

      let modelWithComponents = models.filter(model => model.getGeometry().coordinates.length > 2)

      if (modelWithComponents.length === 0) {
        simplifyButton.html().disabled = true
      }

      if (W.selectionManager.getSegmentSelection().multipleConnectedComponents) {
        straightenButton.html().disabled = true
      }

      if (!W.selectionManager.getSegmentSelection().multipleConnectedComponents
        && models.length === 2) {
        panel.addButton(
          'C',
          BUTTONS.C.title,
          BUTTONS.C.description,
          () => this.orthogonalizeStreetGeometry(models[0], models[1]),
          BUTTONS.C.shortcut
        )
      }

      const existingFormGroup = element.querySelector('div.form-group.e85');
      if (existingFormGroup) {
        existingFormGroup.replaceWith(panel.html());
      } else {
        element.prepend(panel.html());
      }
    }

    /**
     * Remove geometry nodes on the target segment
     * @param {Object} model
     * @return {void}
     */
    simplifySegmentGeometry (model) {
      if (model.getGeometry().coordinates.length < 3) {
        return
      }

      this.group('simplify segment geometry')
      this.log('check geometry of the segment with ID ' + model.getID())
      let nodes = []

      // calculate angles for every inside point
      for (let i = 0; i < model.getGeometry().coordinates.length - 2; i++) {
        let nodeStart = model.getGeometry().coordinates[i],
          nodeCenter = model.getGeometry().coordinates[i + 1],
          nodeEnd = model.getGeometry().coordinates[i + 2]

        nodes[i] = {
          angle: Math.round(this.findAngle(nodeStart, nodeCenter, nodeEnd)),
          start: Math.round(this.findLength(nodeStart, nodeCenter)),
          end: Math.round(this.findLength(nodeCenter, nodeEnd)),
        }
        this.log('point ' + (i+1) + ' : ' + nodes[i].angle + '°, ' + nodes[i].start + 'm, ' + nodes[i].end + 'm')
      }

      let removeNodes = []

      for (let i = 0; i < nodes.length; i++) {
        let node = nodes[i]

        // mark to remove a node with short START segment
        if (node.start < this.settings.get('simplifyShort')) {
          this.log('found too short segment: ' + node.start + 'm')
          removeNodes.push(i+1)
          continue // skip next rule
        }
        // mark to remove a node with short END segment and big ANGLE
        if (node.angle >= this.settings.get('simplifyAngle')
          && node.end < this.settings.get('simplifyShort')) {
          this.log('found too short fragment: ' + node.end + 'm')
          removeNodes.push(i+1)
          i++ // skip next node
          continue // skip next rule
        }
        // mark to remove a node with big angle and short segments
        if (node.angle >= this.settings.get('simplifyAngle')
          && node.start + node.end < this.settings.get('simplifyTwoShort')) {
          this.log(
            'found point with short fragment: ' + node.start + ' + ' + node.end + ' = ' +
            (node.start + node.end) + 'm and angle equal to ' + node.angle + '°'
          )
          removeNodes.push(i+1)
          // continue // skip next rule
        }
      }

      // remove nodes from geometry
      if (removeNodes.length) {
        let newGeometry = { ... model.getGeometry() }
        let coordinates = []
        for (let i = 0; i < newGeometry.coordinates.length; i++) {
          if (removeNodes.indexOf(i) === -1) {
            coordinates.push(newGeometry.coordinates[i])
          }
        }
        newGeometry.coordinates = coordinates
        W.model.actionManager.add(new WazeActionUpdateSegmentGeometry(model, model.getGeometry(), newGeometry))
      }
      this.groupEnd()
    }

    /**
     * Calculates the angle (in radians) between two vectors pointing outward from one center
     *
     * @param {Object} start first point
     * @param {Object} center second point
     * @param {Object} end third point
     */
    findAngle (start, center, end) {
      let b = Math.pow(center[0] - start[0], 2) + Math.pow(center[1] - start[1], 2),
        a = Math.pow(center[0] - end[0], 2) + Math.pow(center[1] - end[1], 2),
        c = Math.pow(end[0] - start[0], 2) + Math.pow(end[1] - start[1], 2)
      return Math.acos((a + b - c) / Math.sqrt(4 * a * b)) * (180 / Math.PI)
    }

    /**
     * Get the length of the line by point coordinates
     * @param {Array<number,number>} start point
     * @param {Array<number,number>} end point
     * @return {Number} length in meters
     */
    findLength (start, end) {
      return distance(start[0], start[1], end[0], end[1])
    }

    /**
     * Remove geometry nodes on the target segments
     * @param {Array} models
     * @return {void}
     */
    simplifyStreetGeometry (models) {
      this.group('simplify street geometry')
      for (let i = 0; i < models.length; i++) {
        this.simplifySegmentGeometry(models[i])
      }
      this.groupEnd()
    }

    /**
     * Выравнивает сегменты в прямую линию, перемещая промежуточные узлы
     * в точки пересечения перпендикуляров к вычисленной прямой, проходящей через
     * начальный и конечный узлы выделения
     * A,B,C - параметры вычисленной прямой уравнения Аx + By + C = 0
     *
     * @param {Array} models
     * @return {void}
     */
    straightenStreetGeometry (models) {
      this.group('straighten street geometry')
      this.log('calculating the formula for the straight line')

      let segmentSelection = W.selectionManager.getSegmentSelection()

      if (segmentSelection.multipleConnectedComponents) {
        this.log('don\'t try to straighten multiple segments without connection')
      }

      let
        allNodeIds = [], // all nodes for selected segments
        dupNodeIds = [], // only nodes inside connections
        virtualNodes = [] // virtual nodes of segments

      models.forEach(segment => {
        this.log('straighten segment #' + segment.getID())

        // simplify segment to straight
        this.straightenSegmentGeometry(segment)

        // collect the nodes
        allNodeIds.push(segment.getFromNode().getID())
        allNodeIds.push(segment.getToNode().getID())
        virtualNodes = virtualNodes.concat(segment.getVirtualNodes())
      })

      if (virtualNodes.length ) {
        this.log('⚠️ virtual nodes are present, please disconnect all trails and rails from the segments and try again')

        // doesn't work, but why? what is wrong with this code?
        // virtualNodes.forEach(node => {
        //   let element = document.getElementById(node.getOLGeometry.id)
        //   element.setAttribute("fill","#dd7700")
        //
        //   element.addEventListener("click", () => {
        //     element.setAttribute("fill","#00ece3")
        //   });
        // })

        return
      }

      allNodeIds.forEach((nodeId, idx) => {
        if (allNodeIds.indexOf(nodeId, idx + 1) > -1) {
          if (!dupNodeIds.includes(nodeId))
            dupNodeIds.push(nodeId);
        }
      });

      let distinctNodeIds = [...new Set(allNodeIds)];
      let endPointNodeIds = distinctNodeIds.filter((nodeId) => !dupNodeIds.includes(nodeId));
      let endPointNodes = W.model.nodes.getByIds(endPointNodeIds),
        endPointNode1Geo = endPointNodes[0].getGeometry().coordinates,
        endPointNode2Geo = endPointNodes[1].getGeometry().coordinates

      const a = endPointNode2Geo[1] - endPointNode1Geo[1],
        b = endPointNode1Geo[0] - endPointNode2Geo[0],
        c = endPointNode2Geo[0] * endPointNode1Geo[1] - endPointNode1Geo[0] * endPointNode2Geo[1];

      dupNodeIds.forEach((nodeId) => {
        const node = W.model.nodes.getObjectById(nodeId),
          nodeCoordinates = node.getGeometry().coordinates;
        const d = nodeCoordinates[1] * a - nodeCoordinates[0] * b,
          newCoordinates = getIntersectCoordinates(a, b, c, d);

        this.log('move node #' + nodeId + ' to [' + newCoordinates[0] + ';' + newCoordinates[1] + ']')
        this.moveNode(node, newCoordinates)
      });

      // I don't understand why doesn't it work, in the WME all looks good, but it fails when try to save changes
      // virtualNodes.forEach((node) => {
      //   const nodeCoordinates = node.getGeometry().coordinates;
      //   const d = nodeCoordinates[1] * a - nodeCoordinates[0] * b,
      //     newCoordinates = getIntersectCoordinates(a, b, c, d);
      //
      //   this.log('move node #' + node.getID() + ' to [' + newCoordinates[0] + ';' + newCoordinates[1] + ']')
      //   this.moveNode(node, newCoordinates)
      // });

      this.groupEnd()
    }

    /**
     * Orthogonalize two segments
     * This method move the node to new point
     *
     * @param {Object} segment1
     * @param {Object} segment2
     * @return {void}
     */
    orthogonalizeStreetGeometry (segment1, segment2) {
      this.log('orthogonalize street geometry')

      if (segment1.getType() !== 'segment'
        || segment2.getType() !== 'segment') {
        this.log('only segments must be selected')
        return
      }

      /**
       * Extract coordinates from components
       * @param {Object} segment
       * @param {'first'|'second'|'last-but-one'|'last'} position
       * @return {*[]}
       */
      function getCoordinatesFromComponent(segment, position) {
        let pos = 0
        switch (position) {
          case 'first':
            pos = 0
            break
          case 'second':
            pos = 1
            break
          case 'last-but-one':
            pos = segment.getOLGeometry().components.length - 2
            break
          case 'last':
            pos = segment.getOLGeometry().components.length - 1
            break
        }
        return [
          segment.getOLGeometry().components[pos].x,
          segment.getOLGeometry().components[pos].y,
        ]
      }

      let A, B, C, commonNode

      if (segment1.getToNode().getID() === segment2.getFromNode().getID()) {
        // A → B → C
        commonNode = segment1.getToNode()
        A = getCoordinatesFromComponent(segment1, 'last-but-one')
        B = getCoordinatesFromComponent(segment1, 'last')
        C = getCoordinatesFromComponent(segment2, 'second')
      } else if (segment1.getFromNode().getID() === segment2.getFromNode().getID()) {
        // B ← A → C
        commonNode = segment1.getFromNode()
        A = getCoordinatesFromComponent(segment1, 'second')
        B = getCoordinatesFromComponent(segment1, 'first')
        C = getCoordinatesFromComponent(segment2, 'second')
      } else if (segment1.getToNode().getID() === segment2.getToNode().getID()) {
        // A → B ← C
        commonNode = segment1.getToNode()
        A = getCoordinatesFromComponent(segment1, 'last-but-one')
        B = getCoordinatesFromComponent(segment1, 'last')
        C = getCoordinatesFromComponent(segment2, 'last-but-one')
      } else if (segment1.getFromNode().getID() === segment2.getToNode().getID()) {
        // B ← A ← C
        commonNode = segment1.getFromNode()
        A = getCoordinatesFromComponent(segment1, 'second')
        B = getCoordinatesFromComponent(segment1, 'first')
        C = getCoordinatesFromComponent(segment2, 'last-but-one')
      }

      if (!commonNode) {
        this.log('segments does not have common node')
        return
      }

      this.log('common node coords [' + B[0] + ';' + B[1] + ']')

      // Coordinates of points A, B and C
      // First selected segment uses it as line for calculation
      let intersection = findIntersection(A, B, C)

      // Uses OpenLayers with convertation, because
      intersection = W.userscripts.toGeoJSONGeometry(new OpenLayers.Geometry.Point( ...intersection ))

      this.log('point of the intersection is [' + intersection[0] + ', ' + intersection[1] +']')

      this.moveNode(commonNode, intersection.coordinates)
    }

    /**
     * Straighten up segment, remove all geometry nodes except first and last
     * @param {Object} segment
     */
    straightenSegmentGeometry (segment) {
      if (segment.getGeometry().coordinates.length > 2) {
        let newGeometry = { ...segment.getGeometry() }
        newGeometry.coordinates.splice(1, newGeometry.coordinates.length - 2)
        W.model.actionManager.add(new WazeActionUpdateSegmentGeometry(segment, segment.getGeometry(), newGeometry))
      }
    }

    /**
     * Move node to new position
     * @param {Object} node target
     * @param {Array<2>} coords of the new position, array of the wo elements
     */
    moveNode (node, coords) {
      let nodeGeometry = node.getGeometry()
      nodeGeometry.coordinates = coords

      let connectedSegObjs = {}
      let emptyObj = {}

      node.getSegmentIds().forEach((id) => {
        connectedSegObjs[id] = { ...W.model.segments.getObjectById(id).getGeometry() }
      })

      W.model.actionManager.add(
        new WazeActionMoveNode(
          node,
          node.getGeometry(),
          nodeGeometry,
          connectedSegObjs,
          emptyObj
        )
      )
    }
  }

  /**
   * @param {Array<number,number>} A point of line
   * @param {Array<number,number>} B point of line
   * @param {Array<number,number>} C point
   * @return {(number|*)[]}
   */
  function findIntersection(A, B, C) {
    // Функция для вычисления углового коэффициента прямой
    function slope(point1, point2) {
      return (point2[1] - point1[1]) / (point2[0] - point1[0]);
    }

    // Функция для вычисления c (точка пересечения с осью Y) для уравнения прямой
    function intercept(point, slope) {
      return point[1] - slope * point[0];
    }

    // Вычисляем угловой коэффициент для прямой AB
    let mAB = slope(A, B);
    // Вычисляем c для прямой AB
    let cAB = intercept(A, mAB);

    // Для перпендикуляра угловой коэффициент будет обратным и противоположным
    let mPerpendicular = -1 / mAB;
    // Вычисляем c для перпендикулярной прямой, проходящей через C
    let cPerpendicular = intercept(C, mPerpendicular);

    // Находим точку пересечения прямых
    let x = (cPerpendicular - cAB) / (mAB - mPerpendicular);
    let y = mAB * x + cAB;

    return [x, y];
  }


  /**
   * Find intersection point
   * @param {Number} A
   * @param {Number} B
   * @param {Number} C
   * @param {Number} D
   * @return {Number[]}
   */
  function getIntersectCoordinates (A, B, C, D) {
    //  http://rsdn.ru/forum/alg/2589531.hot
    let r = [2]
    r[1] = -1.0 * (C * B - A * D) / (A * A + B * B)
    r[0] = (-r[1] * (B + A) - C + D) / (A - B)

    return r
  }

  /**
   * Detect direction
   * @param {Number} A
   * @param {Number} B
   * @return {Number}
   */
  function getDeltaDirect (A, B) {
    if (A < B) {
      return 1.0
    } else if (A > B) {
      return -1.0
    }

    return 0.0
  }

  /**
   * Calculate the approximate distance between two coordinates (lat/lon)
   *
   * © Chris Veness, MIT-licensed,
   * http://www.movable-type.co.uk/scripts/latlong.html#equirectangular
   *
   * @param {number} λ1 first point latitude
   * @param {number} φ1 first point longitude
   * @param {number} λ2 second point latitude
   * @param {number} φ2 second point longitude
   */
  function distance (λ1, φ1, λ2, φ2) {
    let R = 6371000;
    let Δλ = (λ2 - λ1) * Math.PI / 180;
    φ1 = φ1 * Math.PI / 180;
    φ2 = φ2 * Math.PI / 180;
    let x = Δλ * Math.cos((φ1+φ2)/2);
    let y = (φ2-φ1);
    let d = Math.sqrt(x*x + y*y);
    return R * d;
  };

  $(document).on('bootstrap.wme', () => {

    WazeActionUpdateSegmentGeometry = require('Waze/Action/UpdateSegmentGeometry')
    WazeActionMoveNode = require('Waze/Action/MoveNode')
    WazeActionAddNode = require('Waze/Action/AddNode')

    let Instance = new E85(NAME, SETTINGS)
    Instance.init(BUTTONS)

    // setup name for shortcut section
    WMEUIShortcut.setGroupTitle(NAME, I18n.t(NAME).title)
  })
})()