forked from bedo2991/svl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svl.user.js
1482 lines (1351 loc) · 60.4 KB
/
svl.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name Street Vector Layer
// @namespace wme-champs-it
// @version 2.4
// @description Adds a vector layer for drawing streets on the Waze Map editor
// @include /^https:\/\/(www|editor-beta).waze.com(\/(?!user)\w*-?\w*)?\/editor\/\w*\/?\??[\w|=|&|.]*/
// @updateURL http://www.wazeitalia.it/script/svl.user.js
// @author bedo2991
// @grant none
// @copyright 2015+, bedo2991
// ==/UserScript==
(function() {
var DEBUG_ENABLED = false; //set it to false for production mode
if(DEBUG_ENABLED)
consoleDebug= function()
{
if(DEBUG_ENABLED)
for (var i = 0; i < arguments.length; ++i)
console.debug(arguments[i]);
}
else
consoleDebug = function(){}
function wbwWazeBits() {
//Utilities variable to avoid writing long names
Waze = unsafeWindow.W;
if(typeof(Waze) !== "undefined")
{
wazeMap = unsafeWindow.W.map;
if(typeof(wazeMap) !== "undefined")
{
wazeModel = Waze.model;
selectionManager = Waze.selectionManager;
if(typeof(wazeModel) !== "undefined" && typeof(selectionManager) !== "undefined")
return;
}
}
throw "Model Not ready";
}
function wbwGlobals() {
arrowDeclutter = 25;
clutterMax = 700;
farZoom = wazeMap.zoom <5?true:false;
svlVersion = GM_info.script.version;
preferences=null;
svlIgnoredStreets = [8,10,16,17,19,20];
svlStreetTypes = {
1: 'Street',
20:'Parking Lot Road',
4: 'Ramp',
3: 'Freeway',
7: 'Minor Highway',
6: 'Major Highway',
16: 'Stairway',
5: 'Walking Trail',
8: 'Dirt Road',
15: 'Ferry',
18: 'Railroad',
17: 'Private Road',
19: 'Runway',
2: 'Primary Street',
/*'service': 21,*/
10: 'Pedestrian Boardwalk'
};
OpenLayers.Renderer.symbol.mytriangle = [-2, 0, 2, 0, 0, -6, -2, 0];
nonEditableStyle =
{
strokeColor: "#000",
strokeWidth: 2,
strokeDashstyle: "solid",
pointerEvents: "visiblePainted",
}
tunnelFlagStyle2 =
{
strokeColor: "#C90",
strokeWidth: 1,
strokeDashstyle: "longdash",
pointerEvents: "visiblePainted",
}
tunnelFlagStyle1 =
{
strokeColor: "#fff",
strokeWidth: 2,
strokeDashstyle: "longdash",
pointerEvents: "visiblePainted",
}
}
function hasToBeSkipped(roadid)
{
if(preferences.hideMinorRoads && W.map.getZoom() == 3 && svlIgnoredStreets.indexOf(roadid)>-1)
return true;
return false;
}
function rollbackPreferences()
{
loadPreferences();
updateStylesFromPreferences(preferences);
closePrefPanel();
}
function closePrefPanel()
{
$('#PrefDiv').remove();
}
function bestBackground(color)
{
'use strict';
var oppositeColor = parseInt(color.substring(1,3),16)*0.299 + parseInt(color.substring(3,5),16)*0.587 + parseInt(color.substring(5,7),16)*0.114;
if(oppositeColor<127)
{
return "#FFF";
}
return "#000";
}
function updateStylesFromPreferences(preferences)
{
for(var i=0; i<preferences.streets.length; i++)
{
if(preferences.streets[i] != null){
streetStyle[i].strokeColor = preferences.streets[i].strokeColor;
streetStyle[i].strokeWidth = preferences.streets[i].strokeWidth;
streetStyle[i].strokeDashstyle = preferences.streets[i].strokeDashstyle;
streetStyle[i].outlineColor=bestBackground(preferences.streets[i].strokeColor);
}
}
//Red
redStyle.strokeColor = preferences.red.strokeColor;
redStyle.strokeWidth = preferences.red.strokeWidth;
redStyle.strokeDashstyle = preferences.red.strokeDashstyle;
//Toll
tollStyle.strokeColor = preferences.toll.strokeColor;
tollStyle.strokeWidth = preferences.toll.strokeWidth;
tollStyle.strokeDashstyle = preferences.toll.strokeDashstyle;
//Restrictions
restrStyle.strokeColor = preferences.restriction.strokeColor;
restrStyle.strokeWidth = preferences.restriction.strokeWidth;
restrStyle.strokeDashstyle = preferences.restriction.strokeDashstyle;
//Closures
closureStyle.strokeColor = preferences.closure.strokeColor;
closureStyle.strokeWidth = preferences.closure.strokeWidth;
closureStyle.strokeDashstyle = preferences.closure.strokeDashstyle;
//Rendering
//Labels
clutterCostantNearZoom = preferences.clutterCostantNearZoom;
clutterCostantFarZoom = preferences.clutterCostantFarZoom;
clutterConstant = farZoom ? preferences.clutterCostantFarZoom : preferences.clutterCostantNearZoom;
thresholdDistance = getThreshold();
//ArrowDeclutter
arrowDeclutter = preferences.arrowDeclutter;
labelOutlineWidth = preferences.labelOutlineWidth;
//showSLtext = preferences.showSLtext;
//showSLcolor = preferences.showSLcolor;
doDraw();
}
function exportPreferences()
{
prompt("Please copy this string (CTRL+C):", JSON.stringify(preferences));
}
function importPreferences()
{
var pastedText = prompt("N.B: your current preferences will be overwritten with the new ones. Export them first in case you want to go back to the previous status!\n\nPaste your string here:");
if(pastedText != null && pastedText != ""){
try{
preferences = JSON.parse(pastedText);
}
catch(ex)
{
alert("Your string seems to be somehow wrong. Place check that is a valid JSON string");
return;
}
updateStylesFromPreferences(preferences);
savePreferences(preferences);
closePrefPanel();
}
}
function updatePref()
{
$('#saveNewPref').attr("style", "background-color:#FFBD24");
for(var i=0; i<preferences.streets.length; i++)
{
if(preferences.streets[i] != null){
preferences.streets[i].strokeColor = $('#streetColor_'+i).val();
preferences.streets[i].strokeWidth = $('#streetWidth_'+i).val();
preferences.streets[i].strokeDashstyle = $('#strokeDashstyle_'+i+ ' option:selected').val();
}
}
//Red
preferences.red.strokeColor = $('#streetColor_red').val();
preferences.red.strokeWidth = $('#streetWidth_red').val();
preferences.red.strokeDashstyle = $('#strokeDashstyle_red option:selected').val();
//Toll
preferences.toll.strokeColor = $('#streetColor_toll').val();
preferences.toll.strokeWidth = $('#streetWidth_toll').val();
preferences.toll.strokeDashstyle = $('#strokeDashstyle_toll option:selected').val();
//Restrictions
preferences.restriction.strokeColor = $('#streetColor_restriction').val();
preferences.restriction.strokeWidth = $('#streetWidth_restriction').val();
preferences.restriction.strokeDashstyle = $('#strokeDashstyle_restriction option:selected').val();
//Closures
preferences.closure.strokeColor = $('#streetColor_closure').val();
preferences.closure.strokeWidth = $('#streetWidth_closure').val();
preferences.closure.strokeDashstyle = $('#strokeDashstyle_closure option:selected').val();
preferences.clutterCostantNearZoom = $('#clutterCostantNearZoom').val();
preferences.clutterCostantFarZoom = $('#clutterCostantFarZoom').val();
preferences.arrowDeclutter = $('#arrowDeclutter').val();
preferences.labelOutlineWidth = $('#labelOutlineWidth').val();
preferences.disableRoadLayers = $('#disableRoadLayers').prop('checked');
preferences.startDisabled = $('#startdisabled').prop('checked');
preferences.showSLtext = $('#showSLtext').prop('checked');
preferences.showSLcolor = $('#showSLcolor').prop('checked');
preferences.hideMinorRoads = $('#hideMinorRoads').prop('checked');
updateStylesFromPreferences(preferences);
}
function saveNewPref()
{
updatePref();
savePreferences(preferences);
closePrefPanel();
}
function rollbackDefault(dontask){
if(dontask===true || confirm("Are you sure you want to rollback to the default style?\nANY CHANGE WILL BE LOST!"))
{
saveDefaultPreferences();
updateStylesFromPreferences(preferences);
closePrefPanel();
}
}
function getColorSpeed(speed)
{
return (speed * 3) %360; // :150 * 450
}
function editPreferences()
{
if($('div#PrefDiv').length >0)
return;
var $style = $('<style>.prefElement{margin-right:3px;}summary{font-weight:bold}</style>');
var $mainDiv = $('<div id="PrefDiv" style="padding:10px; border-radius:15px; background:white; opacity:0.9; width:300px; position:absolute; top:61px; left:80px; z-index:200"></div>');
$mainDiv.append($('<div><button id="saveNewPref">Save</button> <button id="rollbackPreferences">Rollback</button> <button id="rollbackDefault">Reset</button><button style="float:right" id="close">X</button></div>'));
var $elementDiv = $('<div id="PrefDiv" style="padding:5px; width:280px; max-height:450px; overflow:auto"></div>');
var $streets = $('<details open></details>');
var $decorations = $('<details></details>');
var $labels = $('<details></details>');
var $speedLimits = $('<details></details>');
$streets.append('<summary>Road Types</summary>');
for(var i=0; i<preferences.streets.length; i++)
{
if(preferences.streets[i] != null)
{
$streets.append($('<b>'+svlStreetTypes[i]+'</b><br>'));
$streets.append($('<input class="prefElement" title="Colour" id="streetColor_'+i+'" value="'+preferences.streets[i].strokeColor+'" type="color"></input>  '));
$streets.append($('<input class="prefElement" title="Width" id="streetWidth_'+i+'" value="'+preferences.streets[i].strokeWidth+'" type="number" min="0" max="15"></input>  '));
var $select = $('<select class="prefElement" title="Stroke style" id="strokeDashstyle_'+i+'"><option value="solid">Solid</option><option value="dash">Dashed</option><option value="dashdot">Dash Dot</option><option value="longdash">Long Dash</option><option value="longdashdot">Long Dash Dot</option><option value="dot">Dot</option></select>');
$select.val(preferences.streets[i].strokeDashstyle);
$streets.append($select);
$streets.append('<hr>');
}
}
//Red segments
$streets.append($('<b>Unnamed Segments</b><br>'));
$streets.append($('<input class="prefElement" title="Colour" id="streetColor_red" value="'+preferences.red.strokeColor+'" type="color"></input>'));
$streets.append($('<input class="prefElement" title="Width" id="streetWidth_red" value="'+preferences.red.strokeWidth+'" type="number" min="0" max="15"></input>'));
var $select = $('<select class="prefElement" title="Stroke style" id="strokeDashstyle_red"><option value="solid">Solid</option><option value="dash">Dashed</option><option value="dashdot">Dash Dot</option><option value="longdash">Long Dash</option><option value="longdashdot">Long Dash Dot</option><option value="dot">Dot</option></select>');
$select.val(preferences.red.strokeDashstyle);
$streets.append($select);
$streets.append('<hr>');
$elementDiv.append($streets);
$decorations.append('<summary>Decorations</summary>');
//Toll
$decorations.append($('<b>Toll</b><br>'));
$decorations.append($('<input class="prefElement" title="Colour" id="streetColor_toll" value="'+preferences.toll.strokeColor+'" type="color"></input>'));
$decorations.append($('<input class="prefElement" title="Width" id="streetWidth_toll" value="'+preferences.toll.strokeWidth+'" type="number" min="0" max="15"></input>'));
var $select = $('<select class="prefElement" title="Stroke style" id="strokeDashstyle_toll"><option value="solid">Solid</option><option value="dash">Dashed</option><option value="dashdot">Dash Dot</option><option value="longdash">Long Dash</option><option value="longdashdot">Long Dash Dot</option><option value="dot">Dot</option></select>');
$select.val(preferences.toll.strokeDashstyle);
$decorations.append($select);
$decorations.append('<hr>');
//Restrictions
$decorations.append($('<b>Restrictions</b><br>'));
$decorations.append($('<input class="prefElement" title="Colour" id="streetColor_restriction" value="'+preferences.restriction.strokeColor+'" type="color"></input>'));
$decorations.append($('<input class="prefElement" title="Width" id="streetWidth_restriction" value="'+preferences.restriction.strokeWidth+'" type="number" min="0" max="15"></input>'));
var $select = $('<select class="prefElement" title="Stroke style" id="strokeDashstyle_restriction"><option value="solid">Solid</option><option value="dash">Dashed</option><option value="dashdot">Dash Dot</option><option value="longdash">Long Dash</option><option value="longdashdot">Long Dash Dot</option><option value="dot">Dot</option></select>');
$select.val(preferences.restriction.strokeDashstyle);
$decorations.append($select);
$decorations.append('<hr>');
//Closures
$decorations.append($('<b>Closures</b><br>'));
$decorations.append($('<input class="prefElement" title="Colour" id="streetColor_closure" value="'+preferences.closure.strokeColor+'" type="color"></input>'));
$decorations.append($('<input class="prefElement" title="Width" id="streetWidth_closure" value="'+preferences.closure.strokeWidth+'" type="number" min="0" max="15"></input>'));
var $select = $('<select class="prefElement" title="Stroke style" id="strokeDashstyle_closure"><option value="solid">Solid</option><option value="dash">Dashed</option><option value="dashdot">Dash Dot</option><option value="longdash">Long Dash</option><option value="longdashdot">Long Dash Dot</option><option value="dot">Dot</option></select>');
$select.val(preferences.closure.strokeDashstyle);
$decorations.append($select);
$decorations.append('<hr>');
$elementDiv.append($decorations);
//Labels
$labels.append('<summary>Rendering Parameters</summary>');
$labels.append($('<b>Near Zoom (the highest, the less)</b><br>'));
$labels.append($('<input class="prefElement" title="Quantity" id="clutterCostantNearZoom" value="'+preferences.clutterCostantNearZoom+'" type="range" min="10" max="'+clutterMax+'"></input>'));
$labels.append('<hr>');
$labels.append($('<b>Far Zoom (the highest, the less)</b><br>'));
$labels.append($('<input class="prefElement" title="Quantity" id="clutterCostantFarZoom" value="'+preferences.clutterCostantFarZoom+'" type="range" min="10" max="'+clutterMax+'"></input>'));
$labels.append('<hr>');
$labels.append($('<b>Label outline width</b><br>'));
$labels.append($('<input class="prefElement" title="Quantity" id="labelOutlineWidth" value="'+preferences.labelOutlineWidth+'" type="range" min="0" max="10"></input>'));
$labels.append('<hr>');
$labels.append($('<b>Hide minor roads at zoom 3</b>'));
$labels.append($('<input class="prefElement" title="True or False" id="hideMinorRoads" type="checkbox" '+(preferences.hideMinorRoads?'checked':'')+' ></input>'));
$labels.append('<hr>');
//Arrow declutter
$labels.append($('<b>Arrows (the highest, the less)</b><br>'));
$labels.append($('<input class="prefElement" title="Quantity" id="arrowDeclutter" value="'+preferences.arrowDeclutter+'" type="range" min="1" max="200"></input>'));
$labels.append('<hr>');
$labels.append($('<b>Hide other road layers </b>'));
$labels.append($('<input class="prefElement" title="True or False" id="disableRoadLayers" type="checkbox" '+(preferences.disableRoadLayers?'checked':'')+' ></input>'));
$labels.append('<hr>');
$labels.append($('<b>Layer initially disabled</b>'));
$labels.append($('<input class="prefElement" title="True or False" id="startdisabled" type="checkbox" '+(preferences.startDisabled?'checked':'')+' ></input>'));
$labels.append('<hr>');
$elementDiv.append($labels);
//Speed limits
$speedLimits.append('<summary>Speed Limits</summary>');
$speedLimits.append($('<b>Show text on streetname</b>'));
$speedLimits.append($('<input class="prefElement" title="True or False" id="showSLtext" type="checkbox" '+(preferences.showSLtext?'checked':'')+' ></input>'));
$speedLimits.append('<hr>');
$speedLimits.append($('<b>Show using colours</b>'));
$speedLimits.append($('<input class="prefElement" title="True or False" id="showSLcolor" type="checkbox" '+(preferences.showSLcolor?'checked':'')+' ></input>'));
$speedLimits.append('<hr>');
$speedLimits.append($('<b>Reference colours</b>'));
$speedLimits.append('<br/>');
for(var k=W.prefs.attributes.isImplerial?10:15; k>1; k--){
$speedLimits.append($('<span style="color:hsl('+getColorSpeed(k*10)+',100%,50%)">'+k*10+' </span>'));
}
$speedLimits.append('<hr>');
$elementDiv.append($speedLimits);
$mainDiv.append($elementDiv);
$mainDiv.append($('<div style="margin-top:2px"><button id="exportPreferences">Export</button> <button id="importPreferences"">Import</button><div>'));
$('body').append($mainDiv);
$('body').append($style);
$('.prefElement').change(updatePref);
$('#close').click(closePrefPanel);
$('#saveNewPref').click(saveNewPref);
$('#updatePref').click(updatePref);
$('#exportPreferences').click(exportPreferences);
$('#importPreferences').click(importPreferences);
$('#rollbackPreferences').click(rollbackPreferences);
$('#rollbackDefault').click(rollbackDefault);
}
function saveDefaultPreferences()
{
preferences = {};
preferences.hideMinorRoads = false;
preferences.showSLcolor = true;
preferences.showSLtext = true;
preferences.version = svlVersion;
preferences.disableRoadLayers=true;
preferences.startDisabled=false;
preferences.clutterCostantNearZoom=400.0;
preferences.labelOutlineWidth="3";
preferences.clutterCostantFarZoom=410.0;
preferences.streets = [];
//Street: 1
preferences.streets[1] ={
strokeColor: "#FFFFFF",
outlineColor: "#000",
strokeWidth: 5,
strokeDashstyle: "solid"
};
//Parking: 20
preferences.streets[20] ={
strokeColor: "#2282ab",
strokeWidth: 5,
strokeDashstyle: "solid"
};
//Ramp: 4
preferences.streets[4] ={
strokeColor: "#3FC91C",
strokeWidth: 6,
strokeDashstyle: "solid"
};
//Freeway: 3
preferences.streets[3] ={
strokeColor: "#387FB8",
strokeWidth: 9,
strokeDashstyle: "solid"
};
//Minor: 7
preferences.streets[7] ={
strokeColor: "#ECE589",
strokeWidth: 7,
strokeDashstyle: "solid"
};
//Major: 6
preferences.streets[6] ={
strokeColor: "#C13040",
strokeWidth: 8,
strokeDashstyle: "solid"
};
//Stairway: 16
preferences.streets[16] ={
strokeColor: "#B700FF",
strokeWidth: 3,
strokeDashstyle: "dash"
};
//Walking: 5
preferences.streets[5] ={
strokeColor: "#00FF00",
strokeWidth: 3,
strokeDashstyle: "dash"
};
//Dirty: 8
preferences.streets[8] ={
strokeColor: "#82614A",
strokeWidth: 5,
strokeDashstyle: "solid"
};
//Ferry: 15
preferences.streets[15] ={
strokeColor: "#FF8000",
strokeWidth: 3,
strokeDashstyle: "dashdot"
};
//Railroad: 18
preferences.streets[18] ={
strokeColor: "#FFFFFF",
strokeWidth: 4,
strokeDashstyle: "dash"
};
//Private: 17
preferences.streets[17] ={
strokeColor: "#00FFB3",
strokeWidth: 4,
strokeDashstyle: "solid"
};
//Runway: 19
preferences.streets[19] ={
strokeColor: "#00FF00",
strokeWidth: 4,
strokeDashstyle: "dashdot"
};
//Primary: 2
preferences.streets[2] ={
strokeColor: "#CBA12E",
strokeWidth: 6,
strokeDashstyle: "solid"
};
//Pedestrian: 10
preferences.streets[10] ={
strokeColor: "#0000FF",
strokeWidth: 6,
strokeDashstyle: "dash"
};
//Red segments (without names)
preferences.red ={
strokeColor: "#FF0000",
strokeWidth: 6,
strokeDashstyle: "solid"
};
preferences.roundabout ={
strokeColor: "#111",
strokeWidth: 1,
strokeDashstyle: "dash"
};
preferences.toll ={
strokeColor: "#00E1FF",
strokeWidth: 2,
strokeDashstyle: "solid"
};
preferences.closure ={
strokeColor: "#FF00FF",
strokeWidth: 4,
strokeDashstyle: "dash"
};
preferences.restriction ={
strokeColor: "#F2FF00",
strokeWidth: 2,
strokeDashstyle: "dash"
};
preferences.arrowDeclutter=10;
savePreferences(preferences);
}
function savePreferences(preferences)
{
preferences.version = svlVersion;
localStorage.setItem('svl', JSON.stringify(preferences));
}
function loadPreferences()
{
preferences = JSON.parse(localStorage.getItem('svl'));
consoleDebug("Loading preferences");
if(preferences == null)
{
consoleDebug("Creating new preferences from default");
saveDefaultPreferences();
return false;
}
return true;
}
function checkZoomLayer()
{
var zoom = wazeMap.getZoom();
consoleDebug("Zoom: "+ zoom);
if(preferences.disableRoadLayers && zoom > 1 && vectorAutomDisabled)
{
roadLayer.setVisibility(false);
}
if(zoom>1)
{
if(streetVector.visibility==false && vectorAutomDisabled)
{
vectorAutomDisabled=false;
consoleDebug("Setting vector visibility to true");
streetVector.setVisibility(true);
doDraw();
//streetVector.display(true)
}
}
if(zoom >= 5)
{
//Close zoom
clutterConstant = preferences.clutterCostantNearZoom;
labelFontSize = "13px";
if(farZoom)
{//Switched from far to close zoom
farZoom=false;
thresholdDistance=getThreshold();
W.model.nodes.events.register("objectsremoved", nodesVector, removeNodes);
W.model.nodes.events.register("objectsadded", nodesVector, addNodes);
doDraw();
}
}
else
{
//Far zoom
var zoomChanged = !farZoom ? true : false;
farZoom=true;
clutterConstant = preferences.clutterCostantFarZoom;
labelFontSize = "11px";
thresholdDistance=getThreshold();
if(zoomChanged){
W.model.nodes.events.unregister("objectsremoved", nodesVector, removeNodes);
W.model.nodes.events.unregister("objectsadded", nodesVector, addNodes);
nodesVector.destroyFeatures();
doDraw();
}
if(zoom <= 1)
{ //There is nothing to draw, enable road layer
consoleDebug("Road layer automatically enabled because of zoom out");
consoleDebug("Vector visibility: ", streetVector.visibility);
if(streetVector.visibility==true)
{
consoleDebug("Setting vector visibility to false");
streetVector.setVisibility(false);
vectorAutomDisabled=true;
}
roadLayer.setVisibility(true);
}
}
}
function initSVL() {
//Initialize variables
try{
wbwWazeBits();
}
catch (e)
{
svlAttempts++;
if(svlAttempts<10){
console.warn(e);
console.warn("Could not initialize SVL correctly. Maybe the Waze model was not ready. Retrying in 500ms...");
setTimeout(initSVL,500);
return;
}
else
{
console.error(e);
alert("Street Vector Layer failed to inizialize. Maybe the Editor has been updated or your connection/pc is really slow.");
return;
}
}
wbwGlobals();
if(loadPreferences() == false)
{
//First run, or new broswer
alert("This is the first time that you run Street Vector Layer in this browser.\n"
+"Some info about it:\n"
+"Use ALT+L to toggle the layer.\n"
+"You can change the streets colour, thickness and style by clicking on the attribution bar at the bottom of the editor.\n"
+"Your preferences will be saved for the next time in your browser.\n"
+"The other road layers will be automatically hidden. (You can change this behaviour in the preference panel).\n"
+"Have fun and tell us if you liked the script!");
}
else{
if(preferences.showSLcolor == null || preferences.showSLtext == null || preferences.clutterCostantNearZoom == null || preferences.labelOutlineWidth==null || preferences.disableRoadLayers==null || preferences.startDisabled == null)
{
preferences.showSLcolor = preferences.showSLcolor?preferences.showSLcolor: true;
preferences.showSLtext = preferences.showSLtext? preferences.showSLtext:true;
preferences.startDisabled=preferences.startDisabled?preferences.startDisabled:false;
preferences.labelOutlineWidth= preferences.labelOutlineWidth? preferences.labelOutlineWidth:"3";
preferences.disableRoadLayers=preferences.disableRoadLayers?preferences.disableRoadLayers:true;
preferences.clutterCostantNearZoom =preferences.clutterCostantNearZoom?preferences.clutterCostantNearZoom: 350; //float value, the highest the less label will be generated. Zoom >=5
preferences.clutterCostantFarZoom = preferences.clutterCostantFarZoom? preferences.clutterCostantFarZoom: 410; //float value, the highest the less label will be generated. Zoom <5
prompt("!!! IMPORTANT !!!\nStreet Vector layer got updated to the version "+svlVersion+ " and needs to update your saved preferences in order to keep working.\nHere is a backup of your previous settings.\nCopy it and import it later if you have made any change that you'd like to keep.", JSON.stringify(preferences));
saveDefaultPreferences();
}
}
clutterConstant = farZoom ? preferences.clutterCostantFarZoom : preferences.clutterCostantNearZoom;
thresholdDistance = getThreshold();
labelFontSize = farZoom?"11px":"13px";
$('.olControlAttribution').click(editPreferences);
$('.olControlScaleLineTop').click(editPreferences);
$('.olControlScaleLineBottom').click(editPreferences);
var labelStyleMap = new OpenLayers.StyleMap({
fontFamily: "Open Sans, Alef, helvetica, sans-serif, monospace",
fontWeight: "800",
fontColor: "${color}",
labelOutlineColor: "${outlinecolor}",
fontSize: labelFontSize,
labelXOffset: 0,
labelYOffset:0,//(attributes.id%2==0?1:-2.6)*7,
// fontColor: streetStyle[attributes.roadType]?streetStyle[attributes.roadType].strokeColor:"#f00",
//labelOutlineColor: streetStyle[attributes.roadType]?streetStyle[attributes.roadType].outlineColor:"#fff",
labelOutlineWidth:preferences.labelOutlineWidth,
label : "${label}",
//label: directionArrow + " " + streetPart+ " " + directionArrow+ " "+speedPart,
angle: "${angle}",
//angle: degrees,
labelAlign: "cm"//set to center middle
/* cursor: "pointer",
labelXOffset: 0,
fontColor: "black",
fontSize: "24px",
fontFamily: "Arial",
labelSelect: true,*/
});
streetVector = new OpenLayers.Layer.Vector("Street (Vector)",
{
styleMap: labelStyleMap,
uniqueName: 'vectorStreet',
shortcutKey:'A+l',
visibility: true,
isBaseLayer: false,
isVector: true,
sphericalMercator: true,
attribution: "Street Vector Layer"
});
streetVector.renderer.drawText = function (e,t,i){var n=!!t.labelOutlineWidth;if(n){var s=OpenLayers.Util.extend({},t);s.fontColor=s.labelOutlineColor,s.fontStrokeColor=s.labelOutlineColor,s.fontStrokeWidth=t.labelOutlineWidth,delete s.labelOutlineWidth,this.drawText(e,s,i)}var r=this.getResolution();var layer=this.map.getLayer(this.container.id);var feature=layer.getFeatureById(e);i=(feature.attributes.centroid?feature.attributes.centroid:i);o=(i.x-this.featureDx)/r+this.left,a=i.y/r-this.top,l=n?this.LABEL_OUTLINE_SUFFIX:this.LABEL_ID_SUFFIX,u=this.nodeFactory(e+l,"text");u.setAttributeNS(null,"x",o),u.setAttributeNS(null,"y",-a);if(t.angle||t.angle==0){var rotate='rotate('+t.angle+','+o+","+-a+')';u.setAttributeNS(null,"transform",rotate);}t.fontColor&&u.setAttributeNS(null,"fill",t.fontColor),t.fontStrokeColor&&u.setAttributeNS(null,"stroke",t.fontStrokeColor),t.fontStrokeWidth&&u.setAttributeNS(null,"stroke-width",t.fontStrokeWidth),t.fontOpacity&&u.setAttributeNS(null,"opacity",t.fontOpacity),t.fontFamily&&u.setAttributeNS(null,"font-family",t.fontFamily),t.fontSize&&u.setAttributeNS(null,"font-size",t.fontSize),t.fontWeight&&u.setAttributeNS(null,"font-weight",t.fontWeight),t.fontStyle&&u.setAttributeNS(null,"font-style",t.fontStyle),t.labelSelect===!0?(u.setAttributeNS(null,"pointer-events","visible"),u._featureId=e):u.setAttributeNS(null,"pointer-events","none");var h=t.labelAlign||OpenLayers.Renderer.defaultSymbolizer.labelAlign;u.setAttributeNS(null,"text-anchor",OpenLayers.Renderer.SVG.LABEL_ALIGN[h[0]]||"middle"),OpenLayers.IS_GECKO===!0&&u.setAttributeNS(null,"dominant-baseline",OpenLayers.Renderer.SVG.LABEL_ALIGN[h[1]]||"central");for(var c=t.label.split("\n"),d=c.length;u.childNodes.length>d;)u.removeChild(u.lastChild);for(var p=0;d>p;p++){var g=this.nodeFactory(e+l+"_tspan_"+p,"tspan");if(t.labelSelect===!0&&(g._featureId=e,g._geometry=i,g._geometryClass=i.CLASS_NAME),OpenLayers.IS_GECKO===!1&&g.setAttributeNS(null,"baseline-shift",OpenLayers.Renderer.SVG.LABEL_VSHIFT[h[1]]||"-35%"),g.setAttribute("x",o),0==p){var f=OpenLayers.Renderer.SVG.LABEL_VFACTOR[h[1]];null==f&&(f=-.5),g.setAttribute("dy",f*(d-1)+"em")}else g.setAttribute("dy","1em");g.textContent=""===c[p]?" ":c[p],g.parentNode||u.appendChild(g)}u.parentNode||this.textRoot.appendChild(u)}
nodesVector = new OpenLayers.Layer.Vector("Nodes (Vector)",
{
uniqueName: 'vectorNodes',
shortcutKey:'A+n',
visibility: true,
displayInLayerSwitcher:false,
isBaseLayer: false,
sphericalMercator: true
});
streetStyle = [];
//Street types
for(var i=0; i<preferences.streets.length; i++)
{
if(preferences.streets[i] != null){
streetStyle[i] = {
strokeColor: preferences.streets[i].strokeColor,
strokeWidth: preferences.streets[i].strokeWidth,
strokeDashstyle: preferences.streets[i].strokeDashstyle,
outlineColor: bestBackground(preferences.streets[i].strokeColor),
pointerEvents: "none"
};
}
}
roundaboutStyle = {
strokeColor: preferences.roundabout.strokeColor,
strokeWidth: preferences.roundabout.strokeWidth,
strokeDashstyle: preferences.roundabout.strokeDashstyle,
strokeOpacity: 0.9,
pointerEvents: "none"
};
tollStyle = {
strokeColor: preferences.toll.strokeColor,
strokeWidth: preferences.toll.strokeWidth,
strokeDashstyle: preferences.toll.strokeDashstyle,
strokeOpacity: 0.9,
pointerEvents: "none"
};
closureStyle = {
strokeColor: preferences.closure.strokeColor,
strokeWidth: preferences.closure.strokeWidth,
strokeDashstyle: preferences.closure.strokeDashstyle,
pointerEvents: "none"
};
validatedStyle = {
strokeColor: "#F53BFF",
strokeWidth: 3,
strokeDashstyle: "solid",
pointerEvents: "none"
};
restrStyle = {
strokeColor: preferences.restriction.strokeColor,
strokeWidth: preferences.restriction.strokeWidth,
strokeDashstyle: preferences.restriction.strokeDashstyle,
pointerEvents: "none"
};
redStyle = {
strokeColor: preferences.red.strokeColor,
strokeWidth: preferences.red.strokeWidth,
strokeDashstyle: preferences.red.strokeDashstyle,
pointerEvents: "none"
};
nodeStyle = {
stroke:false,
fillColor: "#0015FF",
fillOpacity: 0.6,
pointRadius: 4.0,
pointerEvents: "none"
};
unknownDirStyle = {
graphicName:"x",
strokeColor:"#f00",
strokeWidth:1.5,
fillColor:"#FFFF40",
fillOpacity:0.7,
pointRadius: 7,
pointerEvents: "none"
};
clutterCostantNearZoom = preferences.clutterCostantNearZoom ? preferences.clutterCostantNearZoom : 350.0; //float value, the highest the less label will be generated. Zoom >=5
clutterCostantFarZoom = preferences.clutterCostantFarZoom ? preferences.clutterCostantFarZoom: 410.0; //float value, the highest the less label will be generated. Zoom <5
arrowDeclutter = preferences.arrowDeclutter? preferences.arrowDeclutter: 25;
var segmentLayer = wazeMap.getLayersByName("Segments");
wazeMap.addLayer(streetVector);
wazeMap.addLayer(nodesVector);
wazeMap.raiseLayer(streetVector, -2);
wazeMap.raiseLayer(nodesVector, -1);
streetVector.events.register("visibilitychanged",streetVector, manageNodes);
manageNodes({object:streetVector});
try{
roadLayer = wazeMap.getLayersBy("uniqueName","roads")[0];
}
catch(e)
{
console.warn("An error happened while retrieving the road layers");
roadLayer=null;
}
if(roadLayer!=null){
if(wazeMap.getZoom() <= 1)
{
roadLayer.setVisibility(true);
}
else if(roadLayer.getVisibility())
{
if(preferences.disableRoadLayers)
{
roadLayer.setVisibility(false);
console.log("Roads layers were disabled by Street Vector Layer. You can change this behaviour in the preference panel.");
}
}
}
vectorAutomDisabled=false;
wazeMap.events.register("zoomend", null, checkZoomLayer);
if(preferences.startDisabled)
{
streetVector.setVisibility(false);
}
console.log("Street Vector Layer v. "+svlVersion+" initialized correctly." );
}
function getThreshold()
{
if(clutterConstant == clutterMax)
return 0;
return clutterConstant / wazeMap.getZoom();
}
function manageNodes(e)
{
//Toggle node layer visibility accordingly
consoleDebug("Manage nodes", e);
nodesVector.setVisibility(e.object.visibility);
if(e.object.visibility)
{
consoleDebug("Registering events");
W.model.segments.events.register("objectsadded",streetVector, addSegments); //TODO: This makes the creation of the segment not possible
W.model.segments.events.register("objectschanged", streetVector, editSegments);
W.model.segments.events.register("objectsremoved",streetVector, removeSegments);
W.model.nodes.events.register("objectsremoved", nodesVector, removeNodes);
W.model.nodes.events.register("objectsadded", nodesVector, addNodes);
doDraw();
}
else
{
consoleDebug("Unregistering events");
W.model.segments.events.unregister("objectsadded",streetVector, addSegments); //TODO: This makes the creation of the segment not possible
W.model.segments.events.unregister("objectschanged", streetVector, editSegments);
W.model.segments.events.unregister("objectsremoved",streetVector, removeSegments);
W.model.nodes.events.unregister("objectsremoved", nodesVector, removeNodes);
W.model.nodes.events.unregister("objectsadded", nodesVector, addNodes);
nodesVector.destroyFeatures();
streetVector.destroyFeatures();
}
}
function drawSegment(model)
{
var pointList = [];
var attributes = model.attributes;
if(hasToBeSkipped(attributes.roadType))
return [];
var points = attributes.geometry.components;
var pointList =attributes.geometry.getVertices();
var simplified = new OpenLayers.Geometry.LineString(pointList).simplify(1.5).components;
var maxDistance = 0;
var maxDistanceIndex=-1;
var address = model.getAddress();
var myFeatures = [];
consoleDebug(address);
if(null != address.street && !address.street.isEmpty || (preferences.showSLtext && attributes.fwdMaxSpeed | attributes.revMaxSpeed)){
for(var p=0; p<simplified.length-1; ++p) {
var distance = simplified[p].distanceTo(simplified[p+1]);
if(distance > maxDistance)
{
maxDistance = distance;
maxDistanceIndex = p;
lineString = new OpenLayers.Geometry.LineString([simplified[p],simplified[p+1]]);
}
}
}
var lineFeature = null;
if(null == attributes.primaryStreetID)
{
consoleDebug("RED segment", model);
consoleDebug(pointList);
lineFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString(pointList), {myId:attributes.id}, redStyle);
myFeatures.push(lineFeature);
}else if(typeof streetStyle[attributes.roadType] !== undefined){
var locked = false;
var speed = null;
speed = attributes.fwdMaxSpeed?attributes.fwdMaxSpeed:attributes.revMaxSpeed;
consoleDebug("Road Type: ", attributes.roadType);
if(attributes.level>0) //it is a bridge
{
consoleDebug("Bridge");
var bridgeStyle =
{
strokeColor: "#000",
strokeWidth: parseInt(streetStyle[attributes.roadType].strokeWidth)+(speed&&preferences.showSLcolor&&!farZoom?6:4),
//strokeDashstyle: "solid",
pointerEvents: "visiblePainted",
}
lineFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString(pointList), {myId:attributes.id}, bridgeStyle);
myFeatures.push(lineFeature);
}
if(speed && !farZoom && preferences.showSLcolor) //it has a speed limit
{
consoleDebug("SpeedLimit");
speed = getColorSpeed(attributes.fwdMaxSpeed?attributes.fwdMaxSpeed:attributes.revMaxSpeed);
speedStyle =
{
strokeColor: "hsl("+speed+", 100%, 50%)",
strokeWidth: parseInt(streetStyle[attributes.roadType].strokeWidth)+4,
strokeDashstyle: "solid",//(attributes.fwdMaxSpeedUnverified|attributes.revMaxSpeedUnverified ? "dash":"solid"),
pointerEvents: "visiblePainted",
}
if(attributes.fwdMaxSpeed && attributes.revMaxSpeed && attributes.fwdMaxSpeed != attributes.revMaxSpeed)
{
consoleDebug("The segment has 2 different speed limits");
speed = getColorSpeed(attributes.fwdMaxSpeed);
speedStyleLeft =
{
strokeColor: "hsl("+speed+", 100%, 50%)",
strokeWidth: streetStyle[attributes.roadType].strokeWidth,
strokeDashstyle: "solid",
pointerEvents: "visiblePainted",
}
speed = getColorSpeed(attributes.revMaxSpeed);
speedStyleRight =
{
strokeColor: "hsl("+speed+", 100%, 50%)",
strokeWidth: streetStyle[attributes.roadType].strokeWidth,
strokeDashstyle: "solid",
pointerEvents: "visiblePainted",
}
//It has 2 different speeds:
var left = [];
var right = [];
var maxdx=streetStyle[attributes.roadType].strokeWidth/4;
var maxdy=streetStyle[attributes.roadType].strokeWidth/4;
for(var k=0; k< pointList.length-1; k++)
{
var dx = pointList[k].x-pointList[k+1].x;
var dy = pointList[k].y-pointList[k+1].y;
left[0] = pointList[k].clone();
right[0] = pointList[k].clone();
left[1] = pointList[k+1].clone();
right[1] = pointList[k+1].clone();
var offset = (streetStyle[attributes.roadType].strokeWidth/5.0)*(18/(W.map.getZoom()*W.map.getZoom()))//((W.map.getZoom()+1)/11)+0.6*(1/(11-W.map.getZoom()));// (10-W.map.getZoom()/3)/(10-W.map.getZoom());
if(Math.abs(dx)<0.5)
{//segment is vertical
if(dy>0){
left[0].move(-offset, 0);
left[1].move(-offset, 0);
right[0].move(offset, 0);
right[1].move(offset, 0);
}
else
{
left[0].move(offset, 0);
left[1].move(offset, 0);
right[0].move(-offset, 0);
right[1].move(-offset, 0);
}