WME Segment Shift Utility

Utility for shifting street segments in WME without disconnecting nodes

当前为 2025-06-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Segment Shift Utility
  3. // @namespace https://github.com/kid4rm90s/Segment-Shift-Utility
  4. // @version 2025.06.02.04
  5. // @description Utility for shifting street segments in WME without disconnecting nodes
  6. // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/*
  7. // @author kid4rm90s
  8. // @connect greasyfork.org
  9. // @grant GM_xmlhttpRequest
  10. // @grant unsafeWindow
  11. // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
  12. // @require https://cdn.jsdelivr.net/gh/wazeSpace/wme-sdk-plus@06108853094d40f67e923ba0fe0de31b1cec4412/wme-sdk-plus.js
  13. // @exclude https://cdn.jsdelivr.net/gh/WazeSpace/wme-sdk-plus@latest/wme-sdk-plus.js
  14. // @require https://cdn.jsdelivr.net/npm/@turf/turf@7.2.0/turf.min.js
  15. // @license MIT
  16. // ==/UserScript==
  17.  
  18. /* global getWmeSdk */
  19. /* global initWmeSdkPlus */
  20. /* global WazeWrap */
  21. /* global turf */
  22. /* global $ */
  23. /* global jQuery */
  24. /* global I18n */
  25. /* eslint curly: ["warn", "multi-or-nest"] */
  26.  
  27. /*Scripts modified from WME RA Util (https://greasyfork.org/en/scripts/23616-wme-ra-util)
  28. orgianl author: JustinS83 Waze*/
  29. (function () {
  30.  
  31. const updateMessage = 'Conversion to WME SDK. Now uses turf for calculations and geometry.';
  32. const SCRIPT_VERSION = GM_info.script.version.toString();
  33. const SCRIPT_NAME = GM_info.script.name;
  34. const DOWNLOAD_URL = GM_info.script.downloadURL;
  35. const DIRECTION = {
  36. NORTH: 0,
  37. EAST: 90,
  38. SOUTH: 180,
  39. WEST: 270
  40. };
  41.  
  42. let sdk;
  43. let _settings;
  44.  
  45. async function bootstrap() {
  46. const wmeSdk = getWmeSdk({ scriptId: 'wme-ss-util', scriptName: 'WME SS Util' });
  47. const sdkPlus = await initWmeSdkPlus(wmeSdk, {
  48. hooks: ['Editing.Transactions']
  49. });
  50. sdk = sdkPlus || wmeSdk;
  51. sdk.Events.once({ eventName: 'wme-ready' }).then(() => {
  52. loadScriptUpdateMonitor();
  53. init();
  54. });
  55. }
  56.  
  57. function waitForWME() {
  58. if (!unsafeWindow.SDK_INITIALIZED) {
  59. setTimeout(waitForWME, 500);
  60. return;
  61. }
  62. unsafeWindow.SDK_INITIALIZED.then(bootstrap);
  63. }
  64. waitForWME();
  65.  
  66. function loadScriptUpdateMonitor() {
  67. try {
  68. const updateMonitor = new WazeWrap.Alerts.ScriptUpdateMonitor(SCRIPT_NAME, SCRIPT_VERSION, DOWNLOAD_URL, GM_xmlhttpRequest);
  69. updateMonitor.start();
  70. } catch (ex) {
  71. // Report, but don't stop if ScriptUpdateMonitor fails.
  72. console.error(`${SCRIPT_NAME}:`, ex);
  73. }
  74. }
  75.  
  76. function init() {
  77. console.log('SS UTIL', GM_info.script);
  78. injectCss();
  79. // UpdateSegmentGeometry = require('Waze/Action/UpdateSegmentGeometry'); // Replaced by SDK
  80. // MoveNode = require("Waze/Action/MoveNode"); // Replaced by SDK
  81. // MultiAction = require("Waze/Action/MultiAction"); // Replaced by SDK
  82.  
  83. SSUtilWindow = document.createElement('div');
  84. SSUtilWindow.id = "SSUtilWindow"; // Consistent ID
  85. SSUtilWindow.style.position = 'fixed';
  86. SSUtilWindow.style.visibility = 'hidden';
  87. SSUtilWindow.style.top = '15%';
  88. SSUtilWindow.style.left = '25%';
  89. SSUtilWindow.style.width = '250px';
  90. SSUtilWindow.style.zIndex = 100;
  91. SSUtilWindow.style.backgroundColor = '#FFFFFE';
  92. SSUtilWindow.style.borderWidth = '0px';
  93. SSUtilWindow.style.borderStyle = 'solid';
  94. SSUtilWindow.style.borderRadius = '10px';
  95. SSUtilWindow.style.boxShadow = '5px 5px 10px Silver';
  96. SSUtilWindow.style.padding = '4px';
  97.  
  98. let SSUtilWindowHTML = '<div id="header" style="padding: 4px; background-color:#92C3D3; border-radius: 5px;-moz-border-radius: 5px;-webkit-border-radius: 5px; color: white; font-weight: bold; text-align:center; letter-spacing: 1px;text-shadow: black 0.1em 0.1em 0.2em;"><img src="https://storage.googleapis.com/wazeopedia-files/1/1e/RA_Util.png" style="float:left"></img> Segment Shift Utility <a data-toggle="collapse" href="#divWrappers1" id="collapserLink1" style="float:right"><span id="collapser1" style="cursor:pointer;padding:2px;color:white;" class="fa fa-caret-square-o-up"></a></span></div>';
  99. // start collapse // I put it al the beginning
  100. SSUtilWindowHTML += '<div id="divWrappers1" class="collapse in">';
  101. //***************** Disconnect Nodes Checkbox **************************
  102. SSUtilWindowHTML += '<p style="margin: 10px 0px 0px 20px;"><input type="checkbox" id="chkDisconnectNodes"> Disconnect Nodes</p>';
  103. //***************** Shift Amount **************************
  104. // Define BOX
  105. SSUtilWindowHTML += '<div id="contentShift" style="text-align:center;float:left; width: 120px;max-width: 49%;height: 170px;margin: 1em 5px 0px 0px;opacity:1;border-radius: 2px;-moz-border-radius: 2px;-webkit-border-radius: 4px;border-width:1px;border-style:solid;border-color:#92C3D3;padding:2px;}">';
  106. SSUtilWindowHTML += '<b>Shift amount</b></br><input type="text" name="shiftAmount" id="shiftAmount" size="1" style="float: left; text-align: center;font: inherit; line-height: normal; width: 30px; height: 20px; margin: 5px 4px; box-sizing: border-box; display: block; padding-left: 0; border-bottom-color: rgba(black,.3); background: transparent; outline: none; color: black;" value="1"/> <div style="margin: 5px 4px;">Metre(s)';
  107. // Shift amount controls
  108. SSUtilWindowHTML += '<div id="controls" style="text-align:center; padding:06px 4px;width=100px; height=100px;margin: 5px 0px;border-style:solid; border-width: 2px;border-radius: 50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;box-shadow: inset 0px 0px 50px -14px rgba(0,0,0,1);-moz-box-shadow: inset 0px 0px 50px -14px rgba(0,0,0,1);-webkit-box-shadow: inset 0px 0px 50px -14px rgba(0,0,0,1); background:#92C3D3;align:center;">';
  109. //Single Shift Up Button
  110. SSUtilWindowHTML += '<span id="SSShiftUpBtn" style="cursor:pointer;font-size:14px;">';
  111. SSUtilWindowHTML += '<i class="fa fa-angle-double-up fa-2x" style="color: white; text-shadow: black 0.1em 0.1em 0.2em; vertical-align: top;"> </i>';
  112. SSUtilWindowHTML += '<span id="UpBtnCaption" style="font-weight: bold;"></span>';
  113. SSUtilWindowHTML += '</span><br>';
  114. //Single Shift Left Button
  115. SSUtilWindowHTML += '<span id="SSShiftLeftBtn" style="cursor:pointer;font-size:14px;margin-left:-40px;">';
  116. SSUtilWindowHTML += '<i class="fa fa-angle-double-left fa-2x" style="color: white; text-shadow: black 0.1em 0.1em 0.2em; vertical-align: middle"> </i>';
  117. SSUtilWindowHTML += '<span id="LeftBtnCaption" style="font-weight: bold;"></span>';
  118. SSUtilWindowHTML += '</span>';
  119. //Single Shift Right Button
  120. SSUtilWindowHTML += '<span id="SSShiftRightBtn" style="float: right;cursor:pointer;font-size:14px;margin-right:5px;">';
  121. SSUtilWindowHTML += '<i class="fa fa-angle-double-right fa-2x" style="color: white;text-shadow: black 0.1em 0.1em 0.2em; vertical-align: middle"> </i>';
  122. SSUtilWindowHTML += '<span id="RightBtnCaption" style="font-weight: bold;"></span>';
  123. SSUtilWindowHTML += '</span><br>';
  124. //Single Shift Down Button
  125. SSUtilWindowHTML += '<span id="SSShiftDownBtn" style="cursor:pointer;font-size:14px;margin-top:0px;">';
  126. SSUtilWindowHTML += '<i class="fa fa-angle-double-down fa-2x" style="color: white;text-shadow: black 0.1em 0.1em 0.2em; vertical-align: middle"> </i>';
  127. SSUtilWindowHTML += '<span id="DownBtnCaption" style="font-weight: bold;"></span>';
  128. SSUtilWindowHTML += '</span>';
  129. SSUtilWindowHTML += '</div></div></div>';
  130.  
  131. SSUtilWindow.innerHTML = SSUtilWindowHTML;
  132. document.body.appendChild(SSUtilWindow);
  133.  
  134. $('#SSShiftLeftBtn').click(SSShiftLeftClick);
  135. $('#SSShiftRightBtn').click(SSShiftRightClick);
  136. $('#SSShiftUpBtn').click(SSShiftUpClick);
  137. $('#SSShiftDownBtn').click(SSShiftDownClick);
  138.  
  139. $('#shiftAmount').keypress(function (event) {
  140. if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) event.preventDefault();
  141. });
  142.  
  143. $('#collapserLink1').click(function () {
  144. $("#divWrappers1").slideToggle("fast");
  145. if ($('#collapser1').attr('class') == "fa fa-caret-square-o-down"){
  146. $("#collapser1").removeClass("fa-caret-square-o-down");
  147. $("#collapser1").addClass("fa-caret-square-o-up");
  148. }
  149. else{
  150. $("#collapser1").removeClass("fa-caret-square-o-up");
  151. $("#collapser1").addClass("fa-caret-square-o-down");
  152. }
  153. saveSettingsToStorage();
  154. });
  155.  
  156. const loadedSettings = JSON.parse(localStorage.getItem("WME_SSUtil"));
  157. const defaultSettings = {
  158. divTop: "15%",
  159. divLeft: "25%",
  160. Expanded: true,
  161. DisconnectNodes: false // default to false (normal behavior)
  162. };
  163. _settings = loadedSettings ?? defaultSettings;
  164.  
  165. $('#SSUtilWindow').css('left', _settings.divLeft);
  166. $('#SSUtilWindow').css('top', _settings.divTop);
  167. $('#chkDisconnectNodes').prop('checked', _settings.DisconnectNodes); // Set checkbox state from settings
  168.  
  169. if(!_settings.Expanded){
  170. $("#divWrappers1").hide();
  171. $("#collapser1").removeClass("fa-caret-square-o-up");
  172. $("#collapser1").addClass("fa-caret-square-o-down");
  173. }
  174.  
  175. sdk.Events.on({ eventName: 'wme-selection-changed', eventHandler: checkDisplayTool });
  176. WazeWrap.Interface.ShowScriptUpdate("WME SS Util", GM_info.script.version, updateMessage, "https://update.greasyfork.org/scripts/537258/WME%20Segment%20Shift%20Utility.user.js", "https://github.com/kid4rm90s/Segment-Shift-Utility");
  177. }
  178.  
  179. function saveSettingsToStorage() {
  180. if (localStorage) {
  181. _settings.divLeft = $('#SSUtilWindow').css('left');
  182. _settings.divTop = $('#SSUtilWindow').css('top');
  183. _settings.Expanded = $("#collapser1").attr('class').indexOf("fa-caret-square-o-up") > -1;
  184. _settings.DisconnectNodes = $('#chkDisconnectNodes').is(':checked'); // Save checkbox state
  185. localStorage.setItem("WME_SSUtil", JSON.stringify(_settings));
  186. }
  187. }
  188.  
  189. function checkDisplayTool() {
  190. if (sdk.Editing.getSelection()?.objectType === 'segment') {
  191. if (sdk.Editing.getSelection().length === 0){
  192. $('#SSUtilWindow').css({ visibility: 'hidden' });
  193. } else {
  194. $('#SSUtilWindow').css({ visibility: 'visible' });
  195. if (typeof jQuery.ui !== 'undefined') {
  196. $('#SSUtilWindow' ).draggable({ //Gotta nuke the height setting the dragging inserts otherwise the panel cannot collapse
  197. stop: () => {
  198. $('#SSUtilWindow').css("height", "");
  199. saveSettingsToStorage();
  200. }
  201. });
  202. }
  203. }
  204. } else {
  205. $('#SSUtilWindow').css({ visibility: 'hidden' });
  206. if (typeof jQuery.ui !== 'undefined') {
  207. $('#SSUtilWindow').draggable({
  208. stop: () => {
  209. $('#SSUtilWindow').css("height", "");
  210. saveSettingsToStorage();
  211. }
  212. });
  213. }
  214. }
  215. }
  216.  
  217. function ShiftSegmentNodesLat(offset) {
  218. const selectedSegmentIds = sdk.Editing.getSelection()?.ids;
  219. if (!selectedSegmentIds || selectedSegmentIds.length === 0) {
  220. return;
  221. }
  222.  
  223. const numOffset = parseFloat(offset);
  224. if (isNaN(numOffset)) {
  225. console.error("SS UTIL: Invalid shift amount for Latitude.");
  226. return;
  227. }
  228.  
  229. const disconnectNodes = $('#chkDisconnectNodes').is(':checked'); // Checkbox state
  230.  
  231. sdk.Editing.doActions(() => {
  232. const uniqueNodeIds = new Set();
  233.  
  234. if (!disconnectNodes) {
  235. // Collect unique nodes from selected segments
  236. for (const segmentId of selectedSegmentIds) {
  237. const currentSegment = sdk.DataModel.Segments.getById({ segmentId });
  238. if (currentSegment) {
  239. uniqueNodeIds.add(currentSegment.fromNodeId);
  240. uniqueNodeIds.add(currentSegment.toNodeId);
  241. }
  242. }
  243.  
  244. // Shift unique nodes
  245. for (const nodeId of uniqueNodeIds) {
  246. const node = sdk.DataModel.Nodes.getById({ nodeId });
  247. if (node) {
  248. let newNodeGeometry = structuredClone(node.geometry);
  249. const nodeBearing = numOffset > 0 ? DIRECTION.NORTH : DIRECTION.SOUTH;
  250. const nodeDistance = Math.abs(numOffset);
  251. const currentNodePoint = node.geometry.coordinates;
  252. const newNodePoint = turf.destination(currentNodePoint, nodeDistance, nodeBearing, { units: 'meters' });
  253. newNodeGeometry.coordinates = newNodePoint.geometry.coordinates;
  254. sdk.DataModel.Nodes.moveNode({ id: node.id, geometry: newNodeGeometry });
  255. }
  256. }
  257. }
  258.  
  259. // Update Segment Geometries
  260. for (const segmentId of selectedSegmentIds) {
  261. const currentSegment = sdk.DataModel.Segments.getById({ segmentId });
  262. if (currentSegment) {
  263. let newGeometry = structuredClone(currentSegment.geometry);
  264. const originalLength = currentSegment.geometry.coordinates.length;
  265. const shiftDistance = Math.abs(numOffset);
  266. const shiftBearing = numOffset > 0 ? DIRECTION.NORTH : DIRECTION.SOUTH;
  267.  
  268. if (disconnectNodes) {
  269. // Shift all points including end nodes
  270. for (let j = 0; j < originalLength; j++) {
  271. const currentPoint = currentSegment.geometry.coordinates[j];
  272. const newPoint = turf.destination(currentPoint, shiftDistance, shiftBearing, { units: 'meters' });
  273. newGeometry.coordinates[j] = newPoint.geometry.coordinates;
  274. }
  275. } else {
  276. // Shift only inner points
  277. for (let j = 1; j < originalLength - 1; j++) {
  278. const currentPoint = currentSegment.geometry.coordinates[j];
  279. const newPoint = turf.destination(currentPoint, shiftDistance, shiftBearing, { units: 'meters' });
  280. newGeometry.coordinates[j] = newPoint.geometry.coordinates;
  281. }
  282. // Update end points to match (potentially) moved nodes
  283. const fromNodeAfterMove = sdk.DataModel.Nodes.getById({ nodeId: currentSegment.fromNodeId });
  284. const toNodeAfterMove = sdk.DataModel.Nodes.getById({ nodeId: currentSegment.toNodeId });
  285.  
  286. if (fromNodeAfterMove && newGeometry.coordinates.length > 0) {
  287. newGeometry.coordinates[0] = fromNodeAfterMove.geometry.coordinates;
  288. }
  289. if (toNodeAfterMove && newGeometry.coordinates.length > 1) {
  290. newGeometry.coordinates[originalLength - 1] = toNodeAfterMove.geometry.coordinates;
  291. }
  292. }
  293. sdk.DataModel.Segments.updateSegment({ segmentId: currentSegment.id, geometry: newGeometry });
  294. }
  295. }
  296. }, 'Shifted segments vertically');
  297. }
  298.  
  299. function ShiftSegmentNodesLon(offset) {
  300. const selectedSegmentIds = sdk.Editing.getSelection()?.ids;
  301. if (!selectedSegmentIds || selectedSegmentIds.length === 0) {
  302. return;
  303. }
  304.  
  305. const numOffset = parseFloat(offset);
  306. if (isNaN(numOffset)) {
  307. console.error("SS UTIL: Invalid shift amount for Longitude.");
  308. return;
  309. }
  310.  
  311. const disconnectNodes = $('#chkDisconnectNodes').is(':checked'); // Checkbox state
  312.  
  313. sdk.Editing.doActions(() => {
  314. const uniqueNodeIds = new Set();
  315.  
  316. if (!disconnectNodes) {
  317. for (const segmentId of selectedSegmentIds) {
  318. const currentSegment = sdk.DataModel.Segments.getById({ segmentId });
  319. if (currentSegment) {
  320. uniqueNodeIds.add(currentSegment.fromNodeId);
  321. uniqueNodeIds.add(currentSegment.toNodeId);
  322. }
  323. }
  324.  
  325. for (const nodeId of uniqueNodeIds) {
  326. const node = sdk.DataModel.Nodes.getById({ nodeId });
  327. if (node) {
  328. let newNodeGeometry = structuredClone(node.geometry);
  329. const nodeBearing = numOffset > 0 ? DIRECTION.EAST : DIRECTION.WEST;
  330. const nodeDistance = Math.abs(numOffset);
  331. const currentNodePoint = node.geometry.coordinates;
  332. const newNodePoint = turf.destination(currentNodePoint, nodeDistance, nodeBearing, { units: 'meters' });
  333. newNodeGeometry.coordinates = newNodePoint.geometry.coordinates;
  334. sdk.DataModel.Nodes.moveNode({ id: node.id, geometry: newNodeGeometry });
  335. }
  336. }
  337. }
  338.  
  339. for (const segmentId of selectedSegmentIds) {
  340. const currentSegment = sdk.DataModel.Segments.getById({ segmentId });
  341. if (currentSegment) {
  342. let newGeometry = structuredClone(currentSegment.geometry);
  343. const originalLength = currentSegment.geometry.coordinates.length;
  344. const shiftDistance = Math.abs(numOffset);
  345. const shiftBearing = numOffset > 0 ? DIRECTION.EAST : DIRECTION.WEST;
  346.  
  347. if (disconnectNodes) {
  348. for (let j = 0; j < originalLength; j++) {
  349. const currentPoint = currentSegment.geometry.coordinates[j];
  350. const newPoint = turf.destination(currentPoint, shiftDistance, shiftBearing, { units: 'meters' });
  351. newGeometry.coordinates[j] = newPoint.geometry.coordinates;
  352. }
  353. } else {
  354. for (let j = 1; j < originalLength - 1; j++) {
  355. const currentPoint = currentSegment.geometry.coordinates[j];
  356. const newPoint = turf.destination(currentPoint, shiftDistance, shiftBearing, { units: 'meters' });
  357. newGeometry.coordinates[j] = newPoint.geometry.coordinates;
  358. }
  359. const fromNodeAfterMove = sdk.DataModel.Nodes.getById({ nodeId: currentSegment.fromNodeId });
  360. const toNodeAfterMove = sdk.DataModel.Nodes.getById({ nodeId: currentSegment.toNodeId });
  361.  
  362. if (fromNodeAfterMove && newGeometry.coordinates.length > 0) {
  363. newGeometry.coordinates[0] = fromNodeAfterMove.geometry.coordinates;
  364. }
  365. if (toNodeAfterMove && newGeometry.coordinates.length > 1) {
  366. newGeometry.coordinates[originalLength - 1] = toNodeAfterMove.geometry.coordinates;
  367. }
  368. }
  369. sdk.DataModel.Segments.updateSegment({ segmentId: currentSegment.id, geometry: newGeometry });
  370. }
  371. }
  372. }, 'Shifted segments horizontally');
  373. }
  374.  
  375. //Left
  376. function SSShiftLeftClick(e){
  377. e.stopPropagation();
  378. ShiftSegmentNodesLon(-parseFloat($('#shiftAmount').val())); // Negative for West
  379. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the left.`, false, false, 1500);
  380. }
  381. //Right
  382. function SSShiftRightClick(e){
  383. e.stopPropagation();
  384. ShiftSegmentNodesLon(parseFloat($('#shiftAmount').val())); // Positive for East
  385. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the right.`, false, false, 1500);
  386. }
  387. //Up
  388. function SSShiftUpClick(e){
  389. e.stopPropagation();
  390. ShiftSegmentNodesLat(parseFloat($('#shiftAmount').val()));
  391. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the up.`, false, false, 1500);
  392. }
  393. //Down
  394. function SSShiftDownClick(e){
  395. e.stopPropagation();
  396. ShiftSegmentNodesLat(-parseFloat($('#shiftAmount').val()));
  397. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the down.`, false, false, 1500);
  398. }
  399.  
  400. function injectCss() {
  401. const css = [].join(' '); // No custom CSS needed if these were the only ones
  402. $('<style type="text/css">' + css + '</style>').appendTo('head');
  403. }
  404.  
  405. })();