/
geomap-lib.js
2649 lines (2293 loc) · 81.9 KB
/
geomap-lib.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
/*
* @title WET-BOEW Geomap
* @overview Displays a dynamic map over which information from additional sources can be overlaid.
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
* @author @pjackson28
*/
/*global wet_boew_geomap: false, OpenLayers: false, proj4: false*/
(function( $, window, document, wb ) {
"use strict";
var componentName = "wb-geomap",
selector = "." + componentName,
$document = wb.doc,
// timeout for overlay loading in milliseconds
overlayTimeout = 2000,
colourIndex = 0,
mapArray = [],
selectedFeature, geomap, i18n, i18nText,
/*
* Plugin users can override these defaults by setting attributes on the html elements that the
* selector matches.
* For example, adding the attribute data-option1="false", will override option1 for that plugin instance.
*/
defaults = {
config: {
controls: [],
autoUpdateSize: true,
fractionalZoom: true,
theme: null
},
overlays: [],
features: [],
tables: [],
useScaleLine: false,
useMousePosition: false,
useLegend: false,
useTab: false,
useMapControls: true,
useGeocoder: false,
useGeolocation: false,
useAOI: false
},
/**
* @method init
* @param {jQuery Event} event Event that triggered this handler
*/
init = function( event ) {
var elm = event.target,
className = elm.className,
settings = {},
$elm, overrides;
// Filter out any events triggered by descendants
if ( event.currentTarget === elm ) {
$elm = $( elm );
// Only initialize the i18nText once
if ( !i18nText ) {
i18n = wb.i18n;
i18nText = {
close: i18n( "close" ),
colon: i18n( "colon" ),
hiddenLayer: i18n( "geo-hdnlyr" ),
toggleLayer: i18n( "geo-tgllyr" ),
labelSelect: i18n( "geo-lblsel" ),
select: i18n( "geo-sel" ),
zoomFeature: i18n( "geo-zmfeat" ),
zoomin: i18n( "geo-zmin" ),
zoomout: i18n( "geo-zmout" ),
zoomworld: i18n( "geo-zmwrld" ),
baseMapTitle: i18n( "geo-bmapttl" ),
baseMapURL: i18n( "geo-bmapurl" ),
baseMapURLTxt: i18n( "geo-bmapurltxt" ),
scaleline: i18n( "geo-sclln" ),
mouseposition: i18n( "geo-msepos" ),
access: i18n( "geo-ally" ),
accessTitle: i18n( "geo-allyttl" ),
attribLink: i18n( "geo-attrlnk" ),
attribTitle: i18n( "geo-attrttl" ),
ariaMap: i18n( "geo-ariamap"),
geoLocationURL: i18n( "geo-locurl-geogratis" ),
geoCoderPlaceholder: i18n( "geo-loc-placeholder" ),
geoCoderLabel: i18n( "geo-loc-label" ),
aoiNorth: i18n( "geo-aoi-north" ),
aoiEast: i18n( "geo-aoi-east" ),
aoiSouth: i18n( "geo-aoi-south" ),
aoiWest: i18n( "geo-aoi-west" ),
aoiInstructions: i18n( "geo-aoi-instructions" ),
aoiBtnDraw: i18n( "geo-aoi-btndraw" ),
aoiBtnClear: i18n( "geo-aoi-btnclear" ),
aoiBtnClose: i18n( "close" ),
geolocBtn: i18n( "geo-geoloc-btn" ),
geolocFail: i18n( "geo-geoloc-fail" ),
geolocUncapable: i18n( "geo-geoloc-uncapable" ),
geoLgndGrphc: i18n( "geo-lgnd-grphc" )
};
}
// Class-based overrides - use undefined where no override should occur
overrides = {
useScaleLine: className.indexOf( "scaleline" ) !== -1 ? true : undefined,
useMousePosition: className.indexOf( "position" ) !== -1 ? true : undefined,
useLegend: className.indexOf( "legend" ) !== -1,
useTab: className.indexOf( "tab" ) !== -1,
useMapControls: className.indexOf( "static" ) !== -1 ? false : true,
useGeocoder: className.indexOf( "geocoder" ) !== -1 ? true : false,
useGeolocation: className.indexOf( "geolocation" ) !== -1 ? true : false,
useAOI: className.indexOf( "aoi" ) !== -1 ? true : false
};
// Merge default settings with overrides from the selected plugin element.
$.extend( settings, defaults, overrides, window[ componentName ], wb.getData( $elm, componentName ) );
// Bind the merged settings to the element node for faster access in other events.
$elm.data( { settings: settings } );
// Set the proj4s object so that openlayers can use proj4.
window.Proj4js = {
Proj: function( code ) {
var newProj4 = proj4( window.Proj4js.defs[ code ] );
newProj4.srsCode = code;
return newProj4;
},
defs: proj4.defs,
transform: proj4
};
// Set the language for OpenLayers
OpenLayers.Lang.setCode( document.documentElement.lang );
// Set the image path for OpenLayers
OpenLayers.ImgPath = wb.getPath( "/assets" ) + "/";
// Add projection for default base map
proj4.defs( "EPSG:3978", "+proj=lcc +lat_1=49 +lat_2=77 +lat_0=49 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs");
// Set the Geomap object
geomap = setGeomapObject( $elm );
// Load configuration file
if ( settings.layersFile ) {
$.ajax({
url: settings.layersFile,
dataType: "script",
async: false,
success: function() {
// Extend settings with data loaded from the
// configuration file (through wet_boew_geomap)
$.extend( settings, wet_boew_geomap );
createMap( geomap, settings );
}
});
} else {
createMap( geomap, settings );
}
// If there are overlays, wait before calling the plugins
if ( !geomap.overlays ) {
refreshPlugins( geomap );
}
}
},
/*
* Set the geomap array that will be use to generate Geomap
*/
setGeomapObject = function( $elm ) {
var geomap = {
mapid: $elm.attr( "id" ),
map: null,
selectControl: null,
showAttribNRCan: false,
queryLayers: [],
overlays: 0,
overlaysLoaded: 0,
// status of overlayLoading (true = still loading)
overlaysLoading: {}
},
$elmMap = $elm.find( ".wb-geomap-map" );
geomap.gmap = $elmMap.attr( "id", "geomap-map-" + geomap.mapid ).height( $elmMap.width() * 0.8 );
geomap.glegend = $elm.find( ".wb-geomap-legend" ).attr( "id", "geomap-legend-" + geomap.mapid );
geomap.glayers = $elm.find( ".wb-geomap-layers" ).attr( "id", "geomap-layers-" + geomap.mapid );
return geomap;
},
addPanZoom = function( geomap ) {
var panZoom = new OpenLayers.Control.PanZoom();
OpenLayers.Util.extend( panZoom, {
draw: function() {
// Initialize our internal div
var oButtons = this,
buttonArray = [
"zoomin",
"zoomout",
"zoomworld"
],
buttonImg = [
"zoom-plus-mini",
"zoom-minus-mini",
"zoom-world-mini"
],
len = buttonArray.length,
i;
OpenLayers.Control.prototype.draw.apply( oButtons, arguments );
// Place the controls
oButtons.buttons = [];
for ( i = 0; i !== len; i += 1 ) {
oButtons._addButton( buttonArray[ i ], buttonImg[ i ] + ".png" );
}
return oButtons.div;
}
});
geomap.map.addControl( panZoom );
setPanZoom( geomap );
},
setPanZoom = function( geomap ) {
/*
* Add alt text to map controls and make tab-able
* TODO: Fix in OpenLayers so alt text loaded there rather than overriden here (needs to be i18n)
*/
var panZoom = geomap.gmap.find( ".olControlPanZoom" )[ 0 ],
controls = panZoom.getElementsByTagName( "div" ),
len = controls.length,
i, control, img, altTxt, actn;
panZoom.setAttribute( "role", "toolbar" );
for ( i = 0; i !== len; i += 1 ) {
control = controls[ i ];
img = control.getElementsByTagName( "img" )[ 0 ];
if ( img ) {
actn = control.action;
// Add alt text
altTxt = i18nText[ actn ];
control.setAttribute( "aria-label", altTxt );
control.setAttribute( "title", altTxt );
control.setAttribute( "role", "button" );
control.className += " olControl" + actn;
control.tabIndex = 0;
img.setAttribute( "alt", altTxt );
img.className += " olControl" + actn;
}
}
},
/*
* Map feature select
*/
onFeatureSelect = function( feature ) {
var featureId = feature.id.replace( /\W/g, "_" );
$( "#" + featureId ).addClass( "background-highlight" );
$( "#cb_" + featureId ).prop( "checked", true );
},
/*
* Map feature unselect
*/
onFeatureUnselect = function( feature ) {
var featureId = feature.id.replace( /\W/g, "_" ),
popup = feature.popup;
$( "#" + featureId ).removeClass( "background-highlight" );
$( "#cb_" + featureId ).prop( "checked", false );
// If there is a popup attached, hide it.
if ( popup && popup.visible() ) {
popup.hide();
}
},
/*
* Select and unselect map feature on click
*/
onFeatureClick = function( feature ) {
var selectControl = feature.layer.map.getControlsByClass( "OpenLayers.Control.SelectFeature" )[ 0 ];
if ( feature._lastHighlighter ) {
selectControl.unselect( feature );
} else {
selectControl.select( feature );
if ( feature.layer.popups ) {
// If a popup is already shown, hide it
if ( selectedFeature && selectedFeature.popup && selectedFeature.popup.visible() ) {
selectedFeature.popup.hide();
}
// If no popup, create it, otherwise show it.
if ( !feature.popup ) {
createPopup( feature );
} else {
feature.popup.toggle();
}
}
}
},
/*
* Create popup
*/
createPopup = function( feature ) {
var popupsInfo = feature.layer.popupsInfo,
featureid = feature.id.replace( /\W/g, "_" ),
buttonText = i18nText.close,
colon = i18nText.colon,
mapSize = feature.layer.map.size,
content = "<h3>" + document.getElementById( feature.layer.name ).getAttribute( "aria-label" ) + "</h3>",
id, height, width, close, name, popup, icon, regex,
popupsInfoId, popupsInfoWidth, popupsInfoHeight;
if ( popupsInfo ) {
popupsInfoId = popupsInfo.id;
popupsInfoWidth = popupsInfo.width;
popupsInfoHeight = popupsInfo.height;
id = ( popupsInfoId ? popupsInfoId : "popup_" ) + "_" + featureid;
height = popupsInfoHeight ? popupsInfoHeight : mapSize.h / 2;
width = popupsInfoWidth ? popupsInfoWidth : mapSize.w / 2;
close = popupsInfoWidth ? popupsInfo.close : true;
content += popupsInfo.content;
// update content from feature
for ( name in feature.attributes ) {
if ( feature.attributes.hasOwnProperty( name ) && name.length !== 0 ) {
regex = new RegExp( "_" + name, "igm" );
content = content.replace( regex, feature.attributes[ name ] );
}
}
} else {
id = "popup_" + featureid;
height = mapSize.h / 2;
width = mapSize.w / 2;
close = true;
// Update content from feature
for ( name in feature.attributes ) {
if ( feature.attributes.hasOwnProperty( name ) && name.length !== 0 ) {
content += "<p><strong>" + name + colon + "</strong><br />" + feature.attributes[ name ] + "</p>";
}
}
}
// create the popup
popup = new OpenLayers.Popup.FramedCloud(
id,
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size( width, height ),
content,
null,
close,
null
);
popup.maxSize = new OpenLayers.Size( width, height );
feature.popup = popup;
feature.layer.map.addPopup( popup );
// add wb-icon class
icon = document.createElement( "span" );
icon.className = "glyphicon glyphicon-remove-circle close_" + featureid;
icon.setAttribute( "data-map", geomap.mapid );
icon.setAttribute( "data-layer", feature.layer.id );
icon.setAttribute( "data-feature", feature.id );
icon.setAttribute( "aria-label", buttonText );
icon.setAttribute( "title", buttonText );
icon.setAttribute( "role", "button" );
icon.setAttribute( "tabindex", "0" );
feature.popup.closeDiv.appendChild( icon );
},
/*
* Create layer holder to add all tabs data (HTML and overlay) and overlay data.
*/
createLayerHolder = function( geomap, tab ) {
// User wants tabs
if ( tab ) {
// User has specified where they want to put the tabs
var $tabs = geomap.glayers.find( ".wb-geomap-tabs" );
if ( $tabs.length !== 0 ) {
$tabs
.attr({
"class": "wb-tabs auto-height-none",
id: "geomap-tabs-" + geomap.mapid
});
// User hasn't specified where they want the tabs
} else {
geomap
.glayers
.prepend( "<div id='geomap-tabs-" + geomap.mapid +
"' class='wb-geomap-tabs wb-tabs auto-height-none' style='width: " +
geomap.glayers.width() + "px;'>" );
}
}
},
/*
* Create a table for vector features added in Load Overlays
*/
createTable = function( index, title, caption, datatable ) {
return $( "<table class='table " + ( datatable ? " wb-tables" : " table-condensed" ) +
"' aria-label='" + title + "' id='overlay_" + index + "'>" + "<caption>" +
caption + "</caption><thead></thead><tbody></tbody>" + "</table>" );
},
/*
* Add layer data
*/
addLayerData = function( geomap, featureTable, enabled, olLayerId, tab ) {
// Add layer to legend
if ( geomap.glegend.length !== 0 ) {
addToLegend( geomap, featureTable, enabled, olLayerId );
}
var $divLayer = geomap.glayers,
$parent = $("<div class='wb-geomap-table-wrapper'></div>"),
featureTableId = featureTable[ 0 ].id,
$layerTab = $( "<div id='tabs_" + featureTableId + "'>" ),
title = featureTable[ 0 ].attributes[ "aria-label" ].value,
$layerTitle = $( "<h3>" + title + "</h3>" ),
$alert = $( "<div id='msg_" + featureTableId + "'><p>" +
i18nText.hiddenLayer + "</p></div>" );
// If tabs are specified
if ( tab && $( ".wb-geomap-tabs" ).length !== 0 ) {
addToTabs( geomap, featureTable, enabled, olLayerId );
// Tabs are not specified
} else {
$layerTab.append( $layerTitle, $parent.append(featureTable) );
$divLayer.append( "<div class='col-md-12'>" + $layerTab.html() + "</div>" );
}
// if layer visibility is false, add the hidden layer message and hide the table data
if ( !enabled ) {
$layerTab.append( $alert );
$parent.hide();
}
},
/*
* Create Legend
*/
addToLegend = function( geomap, featureTable, enabled, olLayerId ) {
var $featureTable = $( featureTable ),
featureTableId = featureTable[ 0 ].id,
glegend = geomap.glegend,
$fieldset, $ul, checked, $chkBox, $label, $li;
if ( geomap.glegend ) {
// If no legend or fieldset add them
$fieldset = glegend.find( "fieldset" );
if ( $fieldset.length === 0 ) {
$fieldset = glegend.append( "<fieldset name='legend'><legend class='wb-inv'>" +
i18nText.toggleLayer + "</legend></fieldset>" );
}
checked = enabled ? "checked='checked'" : "";
$ul = glegend.find( "ul" );
if ( $ul.length === 0 ) {
$ul = $( "<ul class='list-unstyled'></ul>" ).appendTo( $fieldset );
}
$chkBox = $( "<input type='checkbox' id='cb_" + featureTableId +
"' class='geomap-lgnd-cbx' value='" + featureTableId +
"' " + checked + " data-map='" + geomap.mapid +
"' data-layer='" + olLayerId + "' />" );
$label = $( "<label>", {
"for": "cb_" + featureTableId,
text: $featureTable.attr( "aria-label" )
}).prepend( $chkBox );
$li = $( "<li class='checkbox geomap-lgnd-layer'>" )
.append( $label, "<div id='sb_" + featureTableId + "'></div>" );
$ul.append( $li );
$( "#sb_" + featureTableId ).toggle( enabled );
}
},
/*
* Add the layer symbology to the legend
*/
symbolizeLegend = function( geomap ) {
var len = geomap.map.layers.length,
colon = i18nText.colon,
symbolItems = [],
ruleLen, $symbol, symbolList, symbolText, layer, style, styleDefault,
filter, filterType, symbolizer, i, j, rule, spanId;
for ( i = 0; i !== len; i += 1 ) {
layer = geomap.map.layers[ i ];
if ( !layer.isBaseLayer && layer.CLASS_NAME !== "OpenLayers.Layer.WMS" ) {
$symbol = $( "#sb_" + layer.name );
symbolText = "";
if ( $symbol.length ) {
style = layer.styleMap.styles[ "default" ];
styleDefault = style.defaultStyle;
ruleLen = style.rules.length;
if ( ruleLen ) {
symbolList = "<ul class='list-unstyled'>";
for ( j = 0; j !== ruleLen; j += 1 ) {
rule = style.rules[ j ];
filter = rule.filter;
filterType = filter.type;
if ( filterType === "==" ) {
filterType = colon;
}
symbolizer = rule.symbolizer;
spanId = "ls_" + layer.name + "_" + j;
symbolList += "<li><div class='row'>" +
"<div id='" + spanId + "' class='col-md-2 geomap-legend-symbol'></div>" +
"<div class='col-md-10'><small>" +
filter.property + " " + (
filter.value !== null ?
filterType + " " + filter.value :
filter.lowerBoundary + " " + filterType +
" " + filter.upperBoundary ) + "</small></div></div></li>";
symbolItems.push( { "id": spanId, "feature": layer.features[ 0 ], "symbolizer": symbolizer } );
}
$symbol.append( symbolList );
} else {
symbolItems.push( { "id": "sb_" + layer.name, "feature": layer.features[ 0 ], "symbolizer": styleDefault } );
}
}
} else if ( layer.CLASS_NAME === "OpenLayers.Layer.WMS" ) {
if ( layer.legendUrl ) {
$("#sb_" + layer.name ).append( "<img src='" + layer.legendUrl + "' alt='" + i18nText.geoLgndGrphc + "'/>" );
} else if ( layer.legendHTML ) {
$("#sb_" + layer.name ).append( layer.legendHTML );
}
}
}
createLegendSymbols( symbolItems );
},
/*
* Create legend symbols
*/
createLegendSymbols = function( symbolItems ) {
var i,
len = symbolItems.length,
symbol;
for ( i = 0, len; i !== len; i += 1 ) {
symbol = symbolItems[ i ];
getLegendSymbol( symbol.id, symbol.feature, symbol.symbolizer );
}
},
/*
* Get legend symbols
*/
getLegendSymbol = function( id, feature, symbolizer ) {
var pseudoFeature,
RendererClass,
rendererIcon,
featureType = feature && feature.geometry ? feature.geometry.CLASS_NAME : "OpenLayers.Geometry.Polygon",
height = 20,
strokeWidth = symbolizer.strokeWidth ? symbolizer.strokeWidth : 0,
renderers = [ "SVG", "VML", "Canvas" ],
width = 20,
i, len;
for ( i = 0, len = renderers.length; i !== len; i += 1 ) {
RendererClass = OpenLayers.Renderer[ renderers[ i ] ];
if ( RendererClass && RendererClass.prototype.supported() ) {
rendererIcon = new RendererClass( id, null );
break;
}
}
rendererIcon.map = {
resolution: 1,
getResolution: function() {
return this.resolution;
} };
switch ( featureType ) {
case "OpenLayers.Geometry.Polygon" || "OpenLayers.Geometry.MultiPolygon":
pseudoFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon(
[ new OpenLayers.Geometry.LinearRing( [
new OpenLayers.Geometry.Point( 2, 2),
new OpenLayers.Geometry.Point( 2, 18 ),
new OpenLayers.Geometry.Point( 18, 18 ),
new OpenLayers.Geometry.Point( 18, 2 ),
new OpenLayers.Geometry.Point( 2, 2 ) ] ) ] ) );
break;
case "OpenLayers.Geometry.Point" || "OpenLayers.Geometry.MultiPoint":
height = symbolizer.graphicHeight ? symbolizer.graphicHeight : symbolizer.pointRadius ? ( symbolizer.pointRadius * 2 ) + ( strokeWidth * 2 ) : 20;
width = symbolizer.graphicWidth ? symbolizer.graphicWidth : symbolizer.pointRadius ? ( symbolizer.pointRadius * 2 ) + ( strokeWidth * 2 ) : 20;
pseudoFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( width / 2, height / 2 ) );
break;
case "OpenLayers.Geometry.LineString" || "OpenLayers.Geometry.MultiLineString":
pseudoFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString( [
new OpenLayers.Geometry.Point( 2, 18 ),
new OpenLayers.Geometry.Point( 6, 2 ),
new OpenLayers.Geometry.Point( 12, 18 ),
new OpenLayers.Geometry.Point( 18, 2 ) ] ) );
break;
default:
pseudoFeature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon(
[ new OpenLayers.Geometry.LinearRing( [
new OpenLayers.Geometry.Point( 2, 2 ),
new OpenLayers.Geometry.Point( 2, 18 ),
new OpenLayers.Geometry.Point( 18, 18 ),
new OpenLayers.Geometry.Point( 18, 2 ),
new OpenLayers.Geometry.Point( 2, 2 ) ] ) ] ) );
break;
}
rendererIcon.setSize( new OpenLayers.Size( width, height ) );
rendererIcon.resolution = 1;
rendererIcon.setExtent( new OpenLayers.Bounds( 0, 0, width, height ), true );
rendererIcon.clear();
rendererIcon.drawFeature( pseudoFeature, symbolizer );
},
/*
* Create tabs - one for each layer added
*/
addToTabs = function( geomap, featureTable, enabled ) {
var $div = geomap.glayers.find( ".wb-geomap-tabs" ),
$tabs = $div.find( "ul" ),
featureTableId = featureTable[ 0 ].id,
$parent = $( "<div class='wb-geomap-table-wrapper'></div>" ).append( featureTable ),
title = featureTable.attr( "aria-label" ),
$details;
$details = $( "<details>", {
id: "details-" + featureTableId
}).append( "<summary>" + title + "</summary>", $parent );
$tabs.append( "<li><a href='#tabs_" + featureTableId + "'>" + title + "</a></li>" );
$div.append( $details );
if ( !enabled ) {
$details.append( "<div id='msg_" + featureTableId + "'><p>" +
i18nText.hiddenLayer + "</p></div>" );
$parent.hide();
}
},
/*
* Generate StyleMap
*/
getStyleMap = function( elm ) {
var styleMap, filterPrefs, rules, rule, i, len, style, styleType,
stylePrefs, styleRule, styleSelect, ruleFilter,
strokeColor = wb.drawColours[ colourIndex ],
fillColor = strokeColor,
defaultStyle = {
strokeColor: strokeColor,
fillColor: fillColor,
fillOpacity: 0.5,
pointRadius: 5,
strokeWidth: 0.5
},
selectStyle = {
strokeColor: "#00f",
fillColor: "#00f",
fillOpacity: 0.4,
strokeWidth: 2.0
},
elmStyle = elm.style;
// Increment the colour index
colourIndex += 1;
if ( colourIndex === wb.drawColours.length ) {
colourIndex = 0;
}
// If style is supplied, create it. If not, create the default one.
if ( elmStyle ) {
// Check the style type (by default, no type are supplied).
styleType = elmStyle.type;
styleSelect = elmStyle.select;
stylePrefs = {
select: new OpenLayers.Style( styleSelect ? styleSelect : selectStyle )
};
if ( styleType === "rule" ) {
// set the rules and add to the style
rules = [];
style = new OpenLayers.Style();
styleRule = elmStyle.rule;
len = styleRule.length;
for ( i = 0; i !== len; i += 1 ) {
// Set the filter
rule = styleRule[ i ];
ruleFilter = rule.filter;
filterPrefs = {
type: OpenLayers.Filter.Comparison[ ruleFilter ],
property: rule.field
};
if ( ruleFilter !== "BETWEEN" ) {
filterPrefs.value = rule.value[ 0 ];
} else {
filterPrefs.lowerBoundary = rule.value[ 0 ];
filterPrefs.upperBoundary = rule.value[ 1 ];
}
rules.push(
new OpenLayers.Rule({
filter: new OpenLayers.Filter.Comparison( filterPrefs ),
symbolizer: rule.init
})
);
}
style.addRules( rules );
stylePrefs[ "default" ] = style;
} else if ( styleType !== "unique" ) {
stylePrefs[ "default" ] = new OpenLayers.Style( elmStyle.init );
}
} else {
stylePrefs = {
"default": new OpenLayers.Style( defaultStyle ),
select: new OpenLayers.Style( selectStyle )
};
}
styleMap = new OpenLayers.StyleMap( stylePrefs );
if ( elmStyle && styleType === "unique" ) {
styleMap.addUniqueValueRules( "default", elmStyle.field, elmStyle.init );
}
return styleMap;
},
/*
* Create a linked table row
*
* TODO: provide for an array of configured table columns.
*/
createRow = function( geomap, context, zoom, mapControl ) {
// Add a row for each feature
var feature = context.feature,
attributes = feature.attributes,
isHead = context.type === "head",
row, key,
// Replace periods with underscores for jQuery!
featureId = feature.id.replace( /\W/g, "_" );
if ( isHead ) {
row = "<tr><th>" + i18nText.select + "</th>";
} else {
row = "<tr id='featureId'>" + addChkBox( geomap, feature, featureId );
}
for ( key in attributes ) {
if ( attributes.hasOwnProperty( key ) ) {
// TODO: add regex to replace text links with hrefs.
if ( isHead ) {
row += "<th>" + key + "</th>";
} else {
row += "<td>" + attributes[ key ] + "</td>";
}
}
}
if ( zoom ) {
if ( !isHead ) {
row += addZoomTo( geomap, context.feature );
} else if ( mapControl ) {
row += "<th>" + i18nText.zoomFeature + "</th>";
}
}
return row + "</tr>";
},
/*
* Handle features once they have been added to the map
*
*/
onFeaturesAdded = function( geomap, $table, evt, zoom, datatable, mapControl ) {
var rowObj = {
type: "head",
feature: evt.features[ 0 ]
},
targetTable = document.getElementById( $table.attr( "id" ) ),
targetTableHead = targetTable.getElementsByTagName( "thead" )[ 0 ],
targetTableBody = targetTable.getElementsByTagName( "tbody" )[ 0 ],
selectControl = geomap.selectControl,
features = evt.features,
len = features.length,
geoRegex = /\W/g,
headRow = createRow( geomap, rowObj, zoom, mapControl ),
tableBody = targetTableBody.innerHTML,
feature, i;
for ( i = 0; i !== len; i += 1 ) {
feature = features[ i ];
tableBody += createRow(
geomap,
{
type: "body",
id: feature.id.replace( geoRegex, "_" ),
feature: feature,
selectControl: selectControl
},
zoom,
mapControl
);
}
// Temporary fix for unknown runtime error in IE8
if ( wb.ielt9 ) {
$( targetTableHead ).html( headRow );
$( targetTableBody ).html( tableBody );
} else {
//targetTableHead.innerHTML = headRow; // this is not working in IE9
//targetTableBody.innerHTML += tableBody; // this is not working in IE9
$( targetTableHead ).html( headRow );
$( targetTableBody ).html( tableBody );
}
},
/*
* Handle overlays once loading has ended
*
*/
onLoadEnd = function( geomap ) {
// TODO: fix no alt attribute on tile image in OpenLayers rather than use this override
geomap.gmap.find( ".olTileImage" ).attr( "alt", "" );
// We need to call it here as well because if we use a config outside the domain it is called
// before the table is created. We need to call it only once loading for all overlays has ended
geomap.overlaysLoaded += 1;
if ( geomap.overlays === geomap.overlaysLoaded ) {
refreshPlugins( geomap );
geomap.overlays = 0;
geomap.overlaysLoaded = 0;
}
},
/*
* Handle features once they have been added to the map for tabular data
*
*/
onTabularFeaturesAdded = function( geomap, feature, zoom, mapControl ) {
// Find the row
var featureId = feature.id.replace( /\W/g, "_" ),
tr = document.getElementById( featureId ),
newTr = addChkBox( geomap, feature, featureId ) + tr.innerHTML +
( mapControl && zoom ? addZoomTo( geomap, feature ) : "" );
// Add select checkbox and zoom column
// Temporary fix for IE8 unknown runtime error bug
if ( wb.ielt9 ) {
$( tr ).html( newTr );
} else {
// tr.innerHTML = newTr; // this doesn't work in IE9???
$( tr ).html( newTr );
}
},
/*
* Add the checkbox to the column
*
*/
addChkBox = function( geomap, feature, featureId ) {
return "<td><label class='wb-inv' for='cb_" + featureId + "'>" +
i18nText.labelSelect + "</label><input type='checkbox' id='cb_" +
featureId + "' class='geomap-cbx' data-map='" + geomap.mapid +
"' data-layer='" + feature.layer.id + "' data-feature='" +
feature.id + "' /></td>";
},
/*
* Add the zoom to the column
*
*/
addZoomTo = function( geomap, feature ) {
return "<td><a href='javascript:;' data-map='" + geomap.mapid +
"' data-layer='" + feature.layer.id + "' data-feature='" + feature.id +
"' class='btn btn-default btn-sm geomap-zoomto'>" + i18nText.zoomFeature + "</a></td>";
},
/*
* Set the default basemap
*/
setDefaultBaseMap = function( geomap ) {
var mapWidth = geomap.gmap.width(),
offset,
option = {
name: i18nText.baseMapTitle,
url: i18nText.baseMapURL,
layer: i18nText.baseMapTitle,
matrixSet: "nativeTileMatrixSet",
tileSize: new OpenLayers.Size( 256, 256 ),
format: "image/jpg",
style: "default",
requestEncoding: "REST",
isBaseLayer: true,
isSingleTile: false,
tileOrigin: new OpenLayers.LonLat( -3.46558E7, 3.931E7 ),
zoomOffset: 5,
resolutions: [
38364.660062653464,
22489.62831258996,
13229.193125052918,
7937.5158750317505,
4630.2175937685215,
2645.8386250105837,
1587.5031750063501,
926.0435187537042,
529.1677250021168,
317.50063500127004,
185.20870375074085,
111.12522225044451,
66.1459656252646,
38.36466006265346,
22.48962831258996,
13.229193125052918,
7.9375158750317505,
4.6302175937685215
],
transitionEffect: "resize"
};
// In function of map width size, set the proper resolution and zoom offset
if ( mapWidth > 260 && mapWidth <= 500 ) {
option.zoomOffset = 1;
} else if ( mapWidth > 500 && mapWidth <= 725 ) {
option.zoomOffset = 2;
} else if ( mapWidth > 725 && mapWidth <= 1175 ) {
option.zoomOffset = 3;
} else if ( mapWidth > 1175 && mapWidth <= 2300 ) {
option.zoomOffset = 4;
}
for ( offset = option.zoomOffset - 1; offset !== -1; offset -= 1 ) {
option.resolutions.shift();
}
// Add the Canada Transportation Base Map (CBMT) data and text
geomap.map.addLayer( new OpenLayers.Layer.WMTS( option ) );
option.url = i18nText.baseMapURLTxt;
option.isBaseLayer = false;
delete option.transitionEffect;
geomap.map.addLayer( new OpenLayers.Layer.WMTS( option ) );
},
/*
* Set default map option
*/
setDefaultMapOptions = function() {
// Use map options for the Canada Transportation Base Map (CBMT)