WME Segment Shift Utility

Utility for shifting street segments in WME without disconnecting nodes

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

  1. // ==UserScript==
  2. // @name WME Segment Shift Utility
  3. // @namespace https://github.com/kid4rm90s/Segment-Shift-Utility
  4. // @version 2025.05.26.02
  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. // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. /*Scripts modified from WME RA Util (https://greasyfork.org/en/scripts/23616-wme-ra-util)
  15. orgianl author: JustinS83 Waze*/
  16. (function() {
  17. let sdkVersion = "";
  18. unsafeWindow.SDK_INITIALIZED.then(() => {
  19. let sdk = unsafeWindow.getWmeSdk({
  20. scriptId: "wme-ss-util",
  21. scriptName: "WME Segment Shift Utility",
  22. });
  23. sdkVersion = sdk.getSDKVersion()
  24. });
  25. var SSUtilWindow = null;
  26. var UpdateSegmentGeometry;
  27. var MoveNode, MultiAction;
  28. var drc_layer;
  29. let wEvents;
  30. const SSUTIL_VERSION = `${GM_info.script.version}`;
  31. //const SCRIPT_NAME = GM_info.script.name;
  32. const GF_LINK = 'https://update.greasyfork.org/scripts/537258/WME%20Segment%20Shift%20Utility.user.js';
  33. const DOWNLOAD_URL = 'https://update.greasyfork.org/scripts/537258/WME%20Segment%20Shift%20Utility.user.js';
  34. //var totalActions = 0;
  35. var _settings;
  36. const updateMessage = "Minor changes:<br><br>Minor updates!<br><br>Thanks for using it!";
  37.  
  38. function bootstrap(tries = 1) {
  39.  
  40. if (W.map && W.model && require && WazeWrap.Ready){
  41. startScriptUpdateMonitor();
  42. init();
  43. }
  44. else if (tries < 1000)
  45. setTimeout(function () {bootstrap(++tries);}, 200);
  46. }
  47.  
  48. bootstrap();
  49.  
  50. function startScriptUpdateMonitor() {
  51. let updateMonitor;
  52. try {
  53. updateMonitor = new WazeWrap.Alerts.ScriptUpdateMonitor(GM_info.script.name, GM_info.script.version, DOWNLOAD_URL, GM_xmlhttpRequest, DOWNLOAD_URL);
  54. updateMonitor.start();
  55. } catch (ex) {
  56. // Report, but don't stop if ScriptUpdateMonitor fails.
  57. console.error('WME SSUtil:', ex);
  58. }
  59. }
  60.  
  61. function init(){
  62. injectCss();
  63. UpdateSegmentGeometry = require('Waze/Action/UpdateSegmentGeometry');
  64. MoveNode = require("Waze/Action/MoveNode");
  65. MultiAction = require("Waze/Action/MultiAction");
  66.  
  67. console.log("SS UTIL");
  68. console.log(GM_info.script);
  69. if(W.map.events)
  70. wEvents = W.map.events;
  71. else
  72. wEvents = W.map.getMapEventsListener();
  73.  
  74. SSUtilWindow = document.createElement('div');
  75. SSUtilWindow.id = "SSUtilWindow";
  76. SSUtilWindow.style.position = 'fixed';
  77. SSUtilWindow.style.visibility = 'hidden';
  78. SSUtilWindow.style.top = '15%';
  79. SSUtilWindow.style.left = '25%';
  80. SSUtilWindow.style.width = '250px';
  81. SSUtilWindow.style.zIndex = 100;
  82. SSUtilWindow.style.backgroundColor = '#FFFFFE';
  83. SSUtilWindow.style.borderWidth = '0px';
  84. SSUtilWindow.style.borderStyle = 'solid';
  85. SSUtilWindow.style.borderRadius = '10px';
  86. SSUtilWindow.style.boxShadow = '5px 5px 10px Silver';
  87. SSUtilWindow.style.padding = '4px';
  88.  
  89. var alertsHTML = '<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>';
  90. // start collapse // I put it al the beginning
  91. alertsHTML += '<div id="divWrappers1" class="collapse in">';
  92. //***************** Disconnect Nodes Checkbox **************************
  93. alertsHTML += '<p style="margin: 10px 0px 0px 20px;"><input type="checkbox" id="chkDisconnectNodes"> Disconnect Nodes</p>';
  94. //***************** Shift Amount **************************
  95. // Define BOX
  96. alertsHTML += '<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;}">';
  97. alertsHTML += '<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)';
  98. // Shift amount controls
  99. alertsHTML += '<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;">';
  100. //Single Shift Up Button
  101. alertsHTML += '<span id="SSShiftUpBtn" style="cursor:pointer;font-size:14px;">';
  102. alertsHTML += '<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>';
  103. alertsHTML += '<span id="UpBtnCaption" style="font-weight: bold;"></span>';
  104. alertsHTML += '</span><br>';
  105. //Single Shift Left Button
  106. alertsHTML += '<span id="SSShiftLeftBtn" style="cursor:pointer;font-size:14px;margin-left:-40px;">';
  107. alertsHTML += '<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>';
  108. alertsHTML += '<span id="LeftBtnCaption" style="font-weight: bold;"></span>';
  109. alertsHTML += '</span>';
  110. //Single Shift Right Button
  111. alertsHTML += '<span id="SSShiftRightBtn" style="float: right;cursor:pointer;font-size:14px;margin-right:5px;">';
  112. alertsHTML += '<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>';
  113. alertsHTML += '<span id="RightBtnCaption" style="font-weight: bold;"></span>';
  114. alertsHTML += '</span><br>';
  115. //Single Shift Down Button
  116. alertsHTML += '<span id="SSShiftDownBtn" style="cursor:pointer;font-size:14px;margin-top:0px;">';
  117. alertsHTML += '<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>';
  118. alertsHTML += '<span id="DownBtnCaption" style="font-weight: bold;"></span>';
  119. alertsHTML += '</span>';
  120. alertsHTML += '</div></div></div>';
  121.  
  122. SSUtilWindow.innerHTML = alertsHTML;
  123. document.body.appendChild(SSUtilWindow);
  124.  
  125. $('#SSShiftLeftBtn').click(SSShiftLeftBtnClick);
  126. $('#SSShiftRightBtn').click(SSShiftRightBtnClick);
  127. $('#SSShiftUpBtn').click(SSShiftUpBtnClick);
  128. $('#SSShiftDownBtn').click(SSShiftDownBtnClick);
  129.  
  130. $('#shiftAmount').keypress(function(event) {
  131. if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57))
  132. event.preventDefault();
  133. });
  134.  
  135. $('#collapserLink1').click(function(){
  136. $("#divWrappers1").slideToggle("fast");
  137. if($('#collapser1').attr('class') == "fa fa-caret-square-o-down"){
  138. $("#collapser1").removeClass("fa-caret-square-o-down");
  139. $("#collapser1").addClass("fa-caret-square-o-up");
  140. }
  141. else{
  142. $("#collapser1").removeClass("fa-caret-square-o-up");
  143. $("#collapser1").addClass("fa-caret-square-o-down");
  144. }
  145. saveSettingsToStorage();
  146. });
  147.  
  148. W.selectionManager.events.register("selectionchanged", null, checkDisplayTool);
  149.  
  150. var loadedSettings = $.parseJSON(localStorage.getItem("WME_SSUtil"));
  151. var defaultSettings = {
  152. divTop: "15%",
  153. divLeft: "25%",
  154. Expanded: true,
  155. DisconnectNodes: false // default to false (normal behavior)
  156. };
  157. _settings = loadedSettings ? loadedSettings : defaultSettings;
  158.  
  159. $('#SSUtilWindow').css('left', _settings.divLeft);
  160. $('#SSUtilWindow').css('top', _settings.divTop);
  161. $('#chkDisconnectNodes').prop('checked', _settings.DisconnectNodes); // Set checkbox state from settings
  162.  
  163. if(!_settings.Expanded){
  164. // $("#divWrappers1").removeClass("in");
  165. // $("#divWrappers1").addClass("collapse");
  166. $("#divWrappers1").hide();
  167. $("#collapser1").removeClass("fa-caret-square-o-up");
  168. $("#collapser1").addClass("fa-caret-square-o-down");
  169. }
  170.  
  171. 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");
  172. }
  173.  
  174. function saveSettingsToStorage() {
  175. if (localStorage) {
  176. var settings = {
  177. divTop: "15%",
  178. divLeft: "25%",
  179. Expanded: true,
  180. DisconnectNodes: false // default value
  181. };
  182.  
  183. settings.divLeft = $('#SSUtilWindow').css('left');
  184. settings.divTop = $('#SSUtilWindow').css('top');
  185. settings.Expanded = $("#collapser1").attr('class').indexOf("fa-caret-square-o-up") > -1;
  186. settings.DisconnectNodes = $('#chkDisconnectNodes').is(':checked'); // Save checkbox state
  187. localStorage.setItem("WME_SSUtil", JSON.stringify(settings));
  188. }
  189. }
  190.  
  191. function checkDisplayTool(){
  192. if(WazeWrap.hasSelectedFeatures() && WazeWrap.getSelectedFeatures()[0].WW.getType() === 'segment'){
  193. if(WazeWrap.getSelectedFeatures().length === 0)
  194. $('#SSUtilWindow').css({'visibility': 'hidden'});
  195. else{
  196. $('#SSUtilWindow').css({'visibility': 'visible'});
  197. if(typeof jQuery.ui !== 'undefined')
  198. $('#SSUtilWindow' ).draggable({ //Gotta nuke the height setting the dragging inserts otherwise the panel cannot collapse
  199. stop: function(event, ui) {
  200. $('#SSUtilWindow').css("height", "");
  201. saveSettingsToStorage();
  202. }
  203. });
  204. }
  205. }
  206. else{
  207. $('#SSUtilWindow').css({'visibility': 'hidden'});
  208. if(typeof jQuery.ui !== 'undefined')
  209. $('#SSUtilWindow' ).draggable({
  210. stop: function(event, ui) {
  211. $('#SSUtilWindow').css("height", "");
  212. saveSettingsToStorage();
  213. }
  214. });
  215. }
  216. }
  217.  
  218. function ShiftSegmentNodesLat(latOffset) {
  219. var multiaction = new MultiAction();
  220. var selectedFeatures = WazeWrap.getSelectedFeatures();
  221. var disconnectNodes = $('#chkDisconnectNodes').is(':checked'); // Checkbox state
  222.  
  223. if (!disconnectNodes) {
  224. // Normal behavior: Shift segments and connected nodes
  225.  
  226. var uniqueNodes = new Set();
  227.  
  228. // 1. Collect Unique Nodes from Selected Segments
  229. for (let i = 0; i < selectedFeatures.length; i++) {
  230. var segObj = W.model.segments.getObjectById(selectedFeatures[i].WW.getObjectModel().attributes.id);
  231. if (!segObj) continue;
  232. uniqueNodes.add(segObj.attributes.fromNodeID);
  233. uniqueNodes.add(segObj.attributes.toNodeID);
  234. }
  235.  
  236. // 2. Shift Unique Nodes
  237. for (let nodeId of uniqueNodes) {
  238. var node = W.model.nodes.objects[nodeId];
  239. if (!node) continue;
  240.  
  241. var newNodeGeometry = structuredClone(node.attributes.geoJSONGeometry);
  242. newNodeGeometry.coordinates[1] += latOffset;
  243.  
  244. var connectedSegObjs = {};
  245. var emptyObj = {};
  246. for (let j = 0; j < node.attributes.segIDs.length; j++) {
  247. var segid = node.attributes.segIDs[j];
  248. connectedSegObjs[segid] = structuredClone(W.model.segments.getObjectById(segid).attributes.geoJSONGeometry);
  249. }
  250. multiaction.doSubAction(W.model, new MoveNode(node, node.attributes.geoJSONGeometry, newNodeGeometry, connectedSegObjs, emptyObj));
  251. }
  252. } // else - if disconnectNodes is checked, we skip node shifting
  253.  
  254. // 3. Update Segment Geometries (always update segment geometry)
  255. for (let i = 0; i < selectedFeatures.length; i++) {
  256. var segObj = W.model.segments.getObjectById(selectedFeatures[i].WW.getObjectModel().attributes.id);
  257. if (!segObj) continue;
  258. var newGeometry = structuredClone(segObj.attributes.geoJSONGeometry);
  259. var originalLength = segObj.attributes.geoJSONGeometry.coordinates.length;
  260.  
  261. if (disconnectNodes) {
  262. // Shift all points when disconnecting
  263. for (let j = 0; j < originalLength; j++) {
  264. newGeometry.coordinates[j][1] += latOffset;
  265. }
  266. } else {
  267. // Shift only inner points when not disconnecting (normal behavior)
  268. for (let j = 1; j < originalLength - 1; j++) {
  269. newGeometry.coordinates[j][1] += latOffset;
  270. }
  271. }
  272. multiaction.doSubAction(W.model, new UpdateSegmentGeometry(segObj, segObj.attributes.geoJSONGeometry, newGeometry));
  273. }
  274.  
  275.  
  276. W.model.actionManager.add(multiaction);
  277. }
  278.  
  279.  
  280. function ShiftSegmentsNodesLong(longOffset) {
  281. var multiaction = new MultiAction();
  282. var selectedFeatures = WazeWrap.getSelectedFeatures();
  283. var disconnectNodes = $('#chkDisconnectNodes').is(':checked'); // Checkbox state
  284.  
  285. if (!disconnectNodes) {
  286. // Normal behavior: Shift segments and connected nodes
  287.  
  288. var uniqueNodes = new Set();
  289.  
  290. // 1. Collect Unique Nodes from Selected Segments
  291. for (let i = 0; i < selectedFeatures.length; i++) {
  292. var segObj = W.model.segments.getObjectById(selectedFeatures[i].WW.getObjectModel().attributes.id);
  293. if (!segObj) continue;
  294. uniqueNodes.add(segObj.attributes.fromNodeID);
  295. uniqueNodes.add(segObj.attributes.toNodeID);
  296. }
  297.  
  298. // 2. Shift Unique Nodes
  299. for (let nodeId of uniqueNodes) {
  300. var node = W.model.nodes.objects[nodeId];
  301. if (!node) continue;
  302.  
  303. var newNodeGeometry = structuredClone(node.attributes.geoJSONGeometry);
  304. newNodeGeometry.coordinates[0] += longOffset;
  305.  
  306. var connectedSegObjs = {};
  307. var emptyObj = {};
  308. for (let j = 0; j < node.attributes.segIDs.length; j++) {
  309. var segid = node.attributes.segIDs[j];
  310. connectedSegObjs[segid] = structuredClone(W.model.segments.getObjectById(segid).attributes.geoJSONGeometry);
  311. }
  312. multiaction.doSubAction(W.model, new MoveNode(node, node.attributes.geoJSONGeometry, newNodeGeometry, connectedSegObjs, emptyObj));
  313. }
  314. } // else - if disconnectNodes is checked, we skip node shifting
  315.  
  316.  
  317. // 3. Update Segment Geometries (always update segment geometry)
  318. for (let i = 0; i < selectedFeatures.length; i++) {
  319. var segObj = W.model.segments.getObjectById(selectedFeatures[i].WW.getObjectModel().attributes.id);
  320. if (!segObj) continue;
  321.  
  322. var newGeometry = structuredClone(segObj.attributes.geoJSONGeometry);
  323. var originalLength = segObj.attributes.geoJSONGeometry.coordinates.length;
  324.  
  325. if (disconnectNodes) {
  326. // Shift all points when disconnecting
  327. for (let j = 0; j < originalLength; j++) {
  328. newGeometry.coordinates[j][0] += longOffset;
  329. }
  330. } else {
  331. // Shift only inner points when not disconnecting (normal behavior)
  332. for (let j = 1; j < originalLength - 1; j++) {
  333. newGeometry.coordinates[j][0] += longOffset;
  334. }
  335. }
  336. multiaction.doSubAction(W.model, new UpdateSegmentGeometry(segObj, segObj.attributes.geoJSONGeometry, newGeometry));
  337. }
  338.  
  339. W.model.actionManager.add(multiaction);
  340. }
  341.  
  342.  
  343. //Left
  344. function SSShiftLeftBtnClick(e){
  345. e.stopPropagation();
  346. var segObj = WazeWrap.getSelectedFeatures()[0];
  347. if (!segObj) return;
  348. var convertedCoords = WazeWrap.Geometry.ConvertTo4326(segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][0], segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][1]);
  349. var gpsOffsetAmount = WazeWrap.Geometry.CalculateLongOffsetGPS(-$('#shiftAmount').val(), convertedCoords.lon, convertedCoords.lat);
  350. ShiftSegmentsNodesLong(gpsOffsetAmount);
  351. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the left.`, false, false, 2000);
  352. }
  353. //Right
  354. function SSShiftRightBtnClick(e){
  355. e.stopPropagation();
  356. var segObj = WazeWrap.getSelectedFeatures()[0];
  357. if (!segObj) return;
  358. var convertedCoords = WazeWrap.Geometry.ConvertTo4326(segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][0], segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][1]);
  359. var gpsOffsetAmount = WazeWrap.Geometry.CalculateLongOffsetGPS($('#shiftAmount').val(), convertedCoords.lon, convertedCoords.lat);
  360. ShiftSegmentsNodesLong(gpsOffsetAmount);
  361. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the right.`, false, false, 2000);
  362. }
  363. //Up
  364. function SSShiftUpBtnClick(e){
  365. e.stopPropagation();
  366. var segObj = WazeWrap.getSelectedFeatures()[0];
  367. if (!segObj) return;
  368. var gpsOffsetAmount = WazeWrap.Geometry.CalculateLatOffsetGPS($('#shiftAmount').val(), WazeWrap.Geometry.ConvertTo4326(segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][0], segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][1]));
  369. ShiftSegmentNodesLat(gpsOffsetAmount);
  370. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the up.`, false, false, 2000);
  371. }
  372. //Down
  373. function SSShiftDownBtnClick(e){
  374. e.stopPropagation();
  375. var segObj = WazeWrap.getSelectedFeatures()[0];
  376. if (!segObj) return;
  377. var gpsOffsetAmount = WazeWrap.Geometry.CalculateLatOffsetGPS(-$('#shiftAmount').val(), WazeWrap.Geometry.ConvertTo4326(segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][0], segObj.WW.getAttributes().geoJSONGeometry.coordinates[0][1]));
  378. ShiftSegmentNodesLat(gpsOffsetAmount);
  379. WazeWrap.Alerts.info('WME Segment Shift Utility', `The segments are shifted by <b>${$('#shiftAmount').val()} Metres</b> to the down.`, false, false, 2000);
  380. }
  381.  
  382. function injectCss() {
  383. var css = [
  384. '.btnMoveNode {width=25px; height=25px; background-color:#92C3D3; cursor:pointer; padding:5px; font-size:14px; border:thin outset black; border-style:solid; border-width: 1px;border-radius:50%; -moz-border-radius:50%; -webkit-border-radius:50%; box-shadow:inset 0px 0px 20px -14px rgba(0,0,0,1); -moz-box-shadow:inset 0px 0px 20px -14px rgba(0,0,0,1); -webkit-box-shadow: inset 0px 0px 20px -14px rgba(0,0,0,1);}',
  385. '.btnRotate { width=45px; height=45px; background-color:#92C3D3; cursor:pointer; padding: 5px; font-size:14px; border:thin outset black; border-style:solid; border-width: 1px;border-radius: 50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;box-shadow: inset 0px 0px 20px -14px rgba(0,0,0,1);-moz-box-shadow: inset 0px 0px 20px -14px rgba(0,0,0,1);-webkit-box-shadow: inset 0px 0px 20px -14px rgba(0,0,0,1);}'
  386. ].join(' ');
  387. $('<style type="text/css">' + css + '</style>').appendTo('head');
  388. }
  389.  
  390. })();