Layer.js
43.9 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/BaseTypes/Class.js
* @requires OpenLayers/Map.js
* @requires OpenLayers/Projection.js
*/
/**
* Class: OpenLayers.Layer
*/
OpenLayers.Layer = OpenLayers.Class({
/**
* APIProperty: id
* {String}
*/
id: null,
/**
* APIProperty: name
* {String}
*/
name: null,
/**
* APIProperty: div
* {DOMElement}
*/
div: null,
/**
* Property: opacity【浑浊度】
* {Float} The layer's opacity. Float number between 0.0 and 1.0.
*/
opacity: null,
/**
* APIProperty: alwaysInRange 【指定图层的最大范围】
* {Boolean} If a layer's display should not be scale-based, this should
* be set to true. This will cause the layer, as an overlay, to always
* be 'active', by always returning true from the calculateInRange()
* function.
*
* If not explicitly specified for a layer, its value will be
* determined on startup in initResolutions() based on whether or not
* any scale-specific properties have been set as options on the
* layer. If no scale-specific options have been set on the layer, we
* assume that it should always be in range.
*
* See #987 for more info.
*/
alwaysInRange: null,
/**
* Constant: EVENT_TYPES
* {Array(String)} Supported application event types. Register a listener
* for a particular event with the following syntax:
* (code)
* layer.events.register(type, obj, listener);
* (end)
*
* Listeners will be called with a reference to an event object. The
* properties of this event depends on exactly what happened.
*
* All event objects have at least the following properties:
* object - {Object} A reference to layer.events.object.
* element - {DOMElement} A reference to layer.events.element.
*
* Supported map event types:
* loadstart - Triggered when layer loading starts.
* loadend - Triggered when layer loading ends.
* loadcancel - Triggered when layer loading is canceled.
* visibilitychanged - Triggered when layer visibility is changed.
* move - Triggered when layer moves (triggered with every mousemove
* during a drag).
* moveend - Triggered when layer is done moving, object passed as
* argument has a zoomChanged boolean property which tells that the
* zoom has changed.
* added - Triggered after the layer is added to a map. Listeners will
* receive an object with a *map* property referencing the map and a
* *layer* property referencing the layer.
* removed - Triggered after the layer is removed from the map. Listeners
* will receive an object with a *map* property referencing the map and
* a *layer* property referencing the layer.
*/
EVENT_TYPES: ["loadstart", "loadend", "loadcancel", "visibilitychanged",
"move", "moveend", "added", "removed"],
/**
* Constant: RESOLUTION_PROPERTIES
* {Array} The properties that are used for calculating resolutions
* information.
*/
RESOLUTION_PROPERTIES: [
'scales', 'resolutions',
'maxScale', 'minScale',
'maxResolution', 'minResolution',
'numZoomLevels', 'maxZoomLevel'
],
/**
* APIProperty: events
* {<OpenLayers.Events>}
*/
events: null,
/**
* APIProperty: map【指定图层添加给的地图容器】
* {<OpenLayers.Map>} This variable is set when the layer is added to
* the map, via the accessor function setMap().
*/
map: null,
/**
* APIProperty: isBaseLayer
* {Boolean} Whether or not the layer is a base layer. This should be set
* individually by all subclasses. Default is false
*/
isBaseLayer: false,
/**
* Property: alpha
* {Boolean} The layer's images have an alpha channel. Default is false.
*/
alpha: false,
/**
* APIProperty: displayInLayerSwitcher【是否出现在图层开关控件中】
* {Boolean} Display the layer's name in the layer switcher. Default is
* true.
*/
displayInLayerSwitcher: true,
/**
* APIProperty: visibility 【图层是否可见】
* {Boolean} The layer should be displayed in the map. Default is true.
*/
visibility: true,
/**
* APIProperty: attribution
* {String} Attribution string, displayed when an
* <OpenLayers.Control.Attribution> has been added to the map.
*/
attribution: null,
/**
* Property: inRange
* {Boolean} The current map resolution is within the layer's min/max
* range. This is set in <OpenLayers.Map.setCenter> whenever the zoom
* changes.
*/
inRange: false,
/**
* Propery: imageSize
* {<OpenLayers.Size>} For layers with a gutter, the image is larger than
* the tile by twice the gutter in each dimension.
*/
imageSize: null,
/**
* Property: imageOffset
* {<OpenLayers.Pixel>} For layers with a gutter, the image offset
* represents displacement due to the gutter.
*/
imageOffset: null,
// OPTIONS
/**
* Property: options
* {Object} An optional object whose properties will be set on the layer.
* Any of the layer properties can be set as a property of the options
* object and sent to the constructor when the layer is created.
*/
options: null,
/**
* APIProperty: eventListeners
* {Object} If set as an option at construction, the eventListeners
* object will be registered with <OpenLayers.Events.on>. Object
* structure must be a listeners object as shown in the example for
* the events.on method.
*/
eventListeners: null,
/**
* APIProperty: gutter
* {Integer} Determines the width (in pixels) of the gutter around image
* tiles to ignore. By setting this property to a non-zero value,
* images will be requested that are wider and taller than the tile
* size by a value of 2 x gutter. This allows artifacts of rendering
* at tile edges to be ignored. Set a gutter value that is equal to
* half the size of the widest symbol that needs to be displayed.
* Defaults to zero. Non-tiled layers always have zero gutter.
*/
gutter: 0,
/**
* APIProperty: projection
* {<OpenLayers.Projection>} or {<String>} Set in the layer options to
* override the default projection string this layer - also set maxExtent,
* maxResolution, and units if appropriate. Can be either a string or
* an <OpenLayers.Projection> object when created -- will be converted
* to an object when setMap is called if a string is passed.
*/
projection: null,
/**
* APIProperty: units
* {String} The layer map units. Defaults to 'degrees'. Possible values
* are 'degrees' (or 'dd'), 'm', 'ft', 'km', 'mi', 'inches'.
*/
units: null,
/**
* APIProperty: scales
* {Array} An array of map scales in descending order. The values in the
* array correspond to the map scale denominator. Note that these
* values only make sense if the display (monitor) resolution of the
* client is correctly guessed by whomever is configuring the
* application. In addition, the units property must also be set.
* Use <resolutions> instead wherever possible.
*/
scales: null,
/**
* APIProperty: resolutions
* {Array} A list of map resolutions (map units per pixel) in descending
* order. If this is not set in the layer constructor, it will be set
* based on other resolution related properties (maxExtent,
* maxResolution, maxScale, etc.).
*/
resolutions: null,
/**
* APIProperty: maxExtent
* {<OpenLayers.Bounds>} The center of these bounds will not stray outside
* of the viewport extent during panning. In addition, if
* <displayOutsideMaxExtent> is set to false, data will not be
* requested that falls completely outside of these bounds.
*/
maxExtent: null,
/**
* APIProperty: minExtent
* {<OpenLayers.Bounds>}
*/
minExtent: null,
/**
* APIProperty: maxResolution
* {Float} Default max is 360 deg / 256 px, which corresponds to
* zoom level 0 on gmaps. Specify a different value in the layer
* options if you are not using a geographic projection and
* displaying the whole world.当世界地图不是地理坐标系时就要指定一个最大分辨率
*/
maxResolution: null,
/**
* APIProperty: minResolution
* {Float}
*/
minResolution: null,
/**
* APIProperty: numZoomLevels
* {Integer}
*/
numZoomLevels: null,
/**
* APIProperty: minScale
* {Float}
*/
minScale: null,
/**
* APIProperty: maxScale
* {Float}
*/
maxScale: null,
/**
* APIProperty: displayOutsideMaxExtent
* {Boolean} Request map tiles that are completely outside of the max
* extent for this layer. Defaults to false.
*/
displayOutsideMaxExtent: false,
/**
* APIProperty: wrapDateLine
* {Boolean} #487 for more info.
*/
wrapDateLine: false,
/**
* APIProperty: transitionEffect
* {String} The transition effect to use when the map is panned or
* zoomed.
*
* There are currently two supported values:
* - *null* No transition effect (the default).
* - *resize* Existing tiles are resized on zoom to provide a visual
* effect of the zoom having taken place immediately. As the
* new tiles become available, they are drawn over top of the
* resized tiles.
*/
transitionEffect: null,
/**
* Property: SUPPORTED_TRANSITIONS
* {Array} An immutable (that means don't change it!) list of supported
* transitionEffect values.
*/
SUPPORTED_TRANSITIONS: ['resize'],
/**
* Property: metadata
* {Object} This object can be used to store additional information on a
* layer object.
*/
metadata: {},
/**
* Constructor: OpenLayers.Layer
*
* Parameters:
* name - {String} The layer name
* options - {Object} Hashtable of extra options to tag onto the layer
*/
initialize: function(name, options) {
this.addOptions(options);
this.name = name;
if (this.id == null) {
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
this.div = OpenLayers.Util.createDiv(this.id);
this.div.style.width = "100%";
this.div.style.height = "100%";
this.div.dir = "ltr";
this.events = new OpenLayers.Events(this, this.div,
this.EVENT_TYPES);
if(this.eventListeners instanceof Object) {
this.events.on(this.eventListeners);
}
}
if (this.wrapDateLine) {
this.displayOutsideMaxExtent = true;
}
},
/**
* Method: destroy
* Destroy is a destructor: this is to alleviate cyclic references which
* the Javascript garbage cleaner can not take care of on its own.
*
* Parameters:
* setNewBaseLayer - {Boolean} Set a new base layer when this layer has
* been destroyed. Default is true.
*/
destroy: function(setNewBaseLayer) {
if (setNewBaseLayer == null) {
setNewBaseLayer = true;
}
if (this.map != null) {
this.map.removeLayer(this, setNewBaseLayer);
}
this.projection = null;
this.map = null;
this.name = null;
this.div = null;
this.options = null;
if (this.events) {
if(this.eventListeners) {
this.events.un(this.eventListeners);
}
this.events.destroy();
}
this.eventListeners = null;
this.events = null;
},
/**
* Method: clone
*
* Parameters:
* obj - {<OpenLayers.Layer>} The layer to be cloned
*
* Returns:
* {<OpenLayers.Layer>} An exact clone of this <OpenLayers.Layer>
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer(this.name, this.getOptions());
}
// catch any randomly tagged-on properties
OpenLayers.Util.applyDefaults(obj, this);
// a cloned layer should never have its map property set
// because it has not been added to a map yet.
obj.map = null;
return obj;
},
/**
* Method: getOptions
* Extracts an object from the layer with the properties that were set as
* options, but updates them with the values currently set on the
* instance.
*
* Returns:
* {Object} the <options> of the layer, representing the current state.
*/
getOptions: function() {
var options = {};
for(var o in this.options) {
options[o] = this[o];
}
return options;
},
/**
* APIMethod: setName
* Sets the new layer name for this layer. Can trigger a changelayer event
* on the map.
*
* Parameters:
* newName - {String} The new name.
*/
setName: function(newName) {
if (newName != this.name) {
this.name = newName;
if (this.map != null) {
this.map.events.triggerEvent("changelayer", {
layer: this,
property: "name"
});
}
}
},
/**
* APIMethod: addOptions
*
* Parameters:
* newOptions - {Object}
* reinitialize - {Boolean} If set to true, and if resolution options of the
* current baseLayer were changed, the map will be recentered to make
* sure that it is displayed with a valid resolution, and a
* changebaselayer event will be triggered.
*/
addOptions: function (newOptions, reinitialize) {
if (this.options == null) {
this.options = {};
}
// update our copy for clone
OpenLayers.Util.extend(this.options, newOptions);
// add new options to this
OpenLayers.Util.extend(this, newOptions);
// make sure this.projection references a projection object
if(typeof this.projection == "string") {
this.projection = new OpenLayers.Projection(this.projection);
}
// get the units from the projection, if we have a projection
// and it it has units
if(this.projection && this.projection.getUnits()) {
this.units = this.projection.getUnits();
}
// re-initialize resolutions if necessary, i.e. if any of the
// properties of the "properties" array defined below is set
// in the new options
if(this.map) {
// store current resolution so we can try to restore it later
var resolution = this.map.getResolution();
var properties = this.RESOLUTION_PROPERTIES.concat(
["projection", "units", "minExtent", "maxExtent"]
);
for(var o in newOptions) {
if(newOptions.hasOwnProperty(o) &&
OpenLayers.Util.indexOf(properties, o) >= 0) {
this.initResolutions();
if (reinitialize && this.map.baseLayer === this) {
// update map position, and restore previous resolution
this.map.setCenter(this.map.getCenter(),
this.map.getZoomForResolution(resolution),
false, true
);
// trigger a changebaselayer event to make sure that
// all controls (especially
// OpenLayers.Control.PanZoomBar) get notified of the
// new options
this.map.events.triggerEvent("changebaselayer", {
layer: this
});
}
break;
}
}
}
},
/**
* APIMethod: onMapResize
* This function can be implemented by subclasses
*/
onMapResize: function() {
//this function can be implemented by subclasses
},
/**
* APIMethod: redraw
* Redraws the layer. Returns true if the layer was redrawn, false if not.
*
* Returns:
* {Boolean} The layer was redrawn.
*/
redraw: function() {
var redrawn = false;
if (this.map) {
// min/max Range may have changed
this.inRange = this.calculateInRange();
// map's center might not yet be set
var extent = this.getExtent();
if (extent && this.inRange && this.visibility) {
var zoomChanged = true;
this.moveTo(extent, zoomChanged, false);
this.events.triggerEvent("moveend",
{"zoomChanged": zoomChanged});
redrawn = true;
}
}
return redrawn;
},
/**
* Method: moveTo
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
* zoomChanged - {Boolean} Tells when zoom has changed, as layers have to
* do some init work in that case.
* dragging - {Boolean}
*/
moveTo:function(bounds, zoomChanged, dragging) {
var display = this.visibility;
if (!this.isBaseLayer) {
display = display && this.inRange;
}
this.display(display);
},
/**
* Method: moveByPx
* Move the layer based on pixel vector. To be implemented by subclasses.
*
* Parameters:
* dx - {Number} The x coord of the displacement vector.
* dy - {Number} The y coord of the displacement vector.
*/
moveByPx: function(dx, dy) {
},
/**
* Method: setMap
* Set the map property for the layer. This is done through an accessor
* so that subclasses can override this and take special action once
* they have their map variable set.
*
* Here we take care to bring over any of the necessary default
* properties from the map.
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
setMap: function(map) {
if (this.map == null) {
this.map = map;
// grab some essential layer data from the map if it hasn't already
// been set
this.maxExtent = this.maxExtent || this.map.maxExtent;
this.minExtent = this.minExtent || this.map.minExtent;
this.projection = this.projection || this.map.projection;
if (typeof this.projection == "string") {
this.projection = new OpenLayers.Projection(this.projection);
}
// Check the projection to see if we can get units -- if not, refer
// to properties.
this.units = this.projection.getUnits() ||
this.units || this.map.units;
this.initResolutions();
if (!this.isBaseLayer) {
this.inRange = this.calculateInRange();
var show = ((this.visibility) && (this.inRange));
this.div.style.display = show ? "" : "none";
}
// deal with gutters
this.setTileSize();
}
},
/**
* Method: afterAdd
* Called at the end of the map.addLayer sequence. At this point, the map
* will have a base layer. To be overridden by subclasses.
*/
afterAdd: function() {
},
/**
* APIMethod: removeMap
* Just as setMap() allows each layer the possibility to take a
* personalized action on being added to the map, removeMap() allows
* each layer to take a personalized action on being removed from it.
* For now, this will be mostly unused, except for the EventPane layer,
* which needs this hook so that it can remove the special invisible
* pane.
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
removeMap: function(map) {
//to be overridden by subclasses
},
/**
* APIMethod: getImageSize
*
* Parameters:
* bounds - {<OpenLayers.Bounds>} optional tile bounds, can be used
* by subclasses that have to deal with different tile sizes at the
* layer extent edges (e.g. Zoomify)
*
* Returns:
* {<OpenLayers.Size>} The size that the image should be, taking into
* account gutters.
*/
getImageSize: function(bounds) {
return (this.imageSize || this.tileSize);
},
/**
* APIMethod: setTileSize
* Set the tile size based on the map size. This also sets layer.imageSize
* and layer.imageOffset for use by Tile.Image.
*
* Parameters:
* size - {<OpenLayers.Size>}
*/
setTileSize: function(size) {
var tileSize = (size) ? size :
((this.tileSize) ? this.tileSize :
this.map.getTileSize());
this.tileSize = tileSize;
if(this.gutter) {
// layers with gutters need non-null tile sizes
//if(tileSize == null) {
// OpenLayers.console.error("Error in layer.setMap() for " +
// this.name + ": layers with " +
// "gutters need non-null tile sizes");
//}
this.imageOffset = new OpenLayers.Pixel(-this.gutter,
-this.gutter);
this.imageSize = new OpenLayers.Size(tileSize.w + (2*this.gutter),
tileSize.h + (2*this.gutter));
}
},
/**
* APIMethod: getVisibility
*
* Returns:
* {Boolean} The layer should be displayed (if in range).
*/
getVisibility: function() {
return this.visibility;
},
/**
* APIMethod: setVisibility
* Set the visibility flag for the layer and hide/show & redraw
* accordingly. Fire event unless otherwise specified
*
* Note that visibility is no longer simply whether or not the layer's
* style.display is set to "block". Now we store a 'visibility' state
* property on the layer class, this allows us to remember whether or
* not we *desire* for a layer to be visible. In the case where the
* map's resolution is out of the layer's range, this desire may be
* subverted.
*
* Parameters:
* visibility - {Boolean} Whether or not to display the layer (if in range)
*/
setVisibility: function(visibility) {
if (visibility != this.visibility) {
this.visibility = visibility;
this.display(visibility);
this.redraw();
if (this.map != null) {
this.map.events.triggerEvent("changelayer", {
layer: this,
property: "visibility"
});
}
this.events.triggerEvent("visibilitychanged");
}
},
/**
* APIMethod: display
* Hide or show the Layer. This is designed to be used internally, and
* is not generally the way to enable or disable the layer. For that,
* use the setVisibility function instead..
*
* Parameters:
* display - {Boolean}
*/
display: function(display) {
if (display != (this.div.style.display != "none")) {
this.div.style.display = (display && this.calculateInRange()) ? "block" : "none";
}
},
/**
* APIMethod: calculateInRange
*
* Returns:
* {Boolean} The layer is displayable at the current map's current
* resolution. Note that if 'alwaysInRange' is true for the layer,
* this function will always return true.
*/
calculateInRange: function() {
var inRange = false;
if (this.alwaysInRange) {
inRange = true;
} else {
if (this.map) {
var resolution = this.map.getResolution();
inRange = ( (resolution >= this.minResolution) &&
(resolution <= this.maxResolution) );
}
}
return inRange;
},
/**
* APIMethod: setIsBaseLayer
*
* Parameters:
* isBaseLayer - {Boolean}
*/
setIsBaseLayer: function(isBaseLayer) {
if (isBaseLayer != this.isBaseLayer) {
this.isBaseLayer = isBaseLayer;
if (this.map != null) {
this.map.events.triggerEvent("changebaselayer", {
layer: this
});
}
}
},
/********************************************************/
/* */
/* Baselayer Functions */
/* */
/********************************************************/
/**
* Method: initResolutions
* This method's responsibility is to set up the 'resolutions' array
* for the layer -- this array is what the layer will use to interface
* between the zoom levels of the map and the resolution display
* of the layer.
*
* The user has several options that determine how the array is set up.
*
* For a detailed explanation, see the following wiki from the
* openlayers.org homepage:
* http://trac.openlayers.org/wiki/SettingZoomLevels
*/
initResolutions: function() {
// ok we want resolutions, here's our strategy:
//
// 1. if resolutions are defined in the layer config, use them
// 2. else, if scales are defined in the layer config then derive
// resolutions from these scales
// 3. else, attempt to calculate resolutions from maxResolution,
// minResolution, numZoomLevels, maxZoomLevel set in the
// layer config
// 4. if we still don't have resolutions, and if resolutions
// are defined in the same, use them
// 5. else, if scales are defined in the map then derive
// resolutions from these scales
// 6. else, attempt to calculate resolutions from maxResolution,
// minResolution, numZoomLevels, maxZoomLevel set in the
// map
// 7. hope for the best!
var i, len, p;
var props = {}, alwaysInRange = true;
// get resolution data from layer config
// (we also set alwaysInRange in the layer as appropriate)
for(i=0, len=this.RESOLUTION_PROPERTIES.length; i<len; i++) {
p = this.RESOLUTION_PROPERTIES[i];
props[p] = this.options[p];
if(alwaysInRange && this.options[p]) {
alwaysInRange = false;
}
}
if(this.alwaysInRange == null) {
this.alwaysInRange = alwaysInRange;
}
// if we don't have resolutions then attempt to derive them from scales
if(props.resolutions == null) {
props.resolutions = this.resolutionsFromScales(props.scales);
}
// if we still don't have resolutions then attempt to calculate them
if(props.resolutions == null) {
props.resolutions = this.calculateResolutions(props);
}
// if we couldn't calculate resolutions then we look at we have
// in the map
if(props.resolutions == null) {
for(i=0, len=this.RESOLUTION_PROPERTIES.length; i<len; i++) {
p = this.RESOLUTION_PROPERTIES[i];
props[p] = this.options[p] != null ?
this.options[p] : this.map[p];
}
if(props.resolutions == null) {
props.resolutions = this.resolutionsFromScales(props.scales);
}
if(props.resolutions == null) {
props.resolutions = this.calculateResolutions(props);
}
}
// ok, we new need to set properties in the instance
// get maxResolution from the config if it's defined there
var maxResolution;
if(this.options.maxResolution &&
this.options.maxResolution !== "auto") {
maxResolution = this.options.maxResolution;
}
if(this.options.minScale) {
maxResolution = OpenLayers.Util.getResolutionFromScale(
this.options.minScale, this.units);
}
// get minResolution from the config if it's defined there
var minResolution;
if(this.options.minResolution &&
this.options.minResolution !== "auto") {
minResolution = this.options.minResolution;
}
if(this.options.maxScale) {
minResolution = OpenLayers.Util.getResolutionFromScale(
this.options.maxScale, this.units);
}
if(props.resolutions) {
//sort resolutions array descendingly
props.resolutions.sort(function(a, b) {
return (b - a);
});
// if we still don't have a maxResolution get it from the
// resolutions array
if(!maxResolution) {
maxResolution = props.resolutions[0];
}
// if we still don't have a minResolution get it from the
// resolutions array
if(!minResolution) {
var lastIdx = props.resolutions.length - 1;
minResolution = props.resolutions[lastIdx];
}
}
this.resolutions = props.resolutions;
if(this.resolutions) {
len = this.resolutions.length;
this.scales = new Array(len);
for(i=0; i<len; i++) {
this.scales[i] = OpenLayers.Util.getScaleFromResolution(
this.resolutions[i], this.units);
}
this.numZoomLevels = len;
}
this.minResolution = minResolution;
if(minResolution) {
this.maxScale = OpenLayers.Util.getScaleFromResolution(
minResolution, this.units);
}
this.maxResolution = maxResolution;
if(maxResolution) {
this.minScale = OpenLayers.Util.getScaleFromResolution(
maxResolution, this.units);
}
},
/**
* Method: resolutionsFromScales
* Derive resolutions from scales.
*
* Parameters:
* scales - {Array(Number)} Scales
*
* Returns
* {Array(Number)} Resolutions
*/
resolutionsFromScales: function(scales) {
if(scales == null) {
return;
}
var resolutions, i, len;
len = scales.length;
resolutions = new Array(len);
for(i=0; i<len; i++) {
resolutions[i] = OpenLayers.Util.getResolutionFromScale(
scales[i], this.units);
}
return resolutions;
},
/**
* Method: calculateResolutions
* Calculate resolutions based on the provided properties.
*
* Parameters:
* props - {Object} Properties
*
* Return:
* {Array({Number})} Array of resolutions.
*/
calculateResolutions: function(props) {
var viewSize, wRes, hRes;
// determine maxResolution
var maxResolution = props.maxResolution;
if(props.minScale != null) {
maxResolution =
OpenLayers.Util.getResolutionFromScale(props.minScale,
this.units);
} else if(maxResolution == "auto" && this.maxExtent != null) {
viewSize = this.map.getSize();
wRes = this.maxExtent.getWidth() / viewSize.w;
hRes = this.maxExtent.getHeight() / viewSize.h;
maxResolution = Math.max(wRes, hRes);
}
// determine minResolution
var minResolution = props.minResolution;
if(props.maxScale != null) {
minResolution =
OpenLayers.Util.getResolutionFromScale(props.maxScale,
this.units);
} else if(props.minResolution == "auto" && this.minExtent != null) {
viewSize = this.map.getSize();
wRes = this.minExtent.getWidth() / viewSize.w;
hRes = this.minExtent.getHeight()/ viewSize.h;
minResolution = Math.max(wRes, hRes);
}
// determine numZoomLevels
var maxZoomLevel = props.maxZoomLevel;
var numZoomLevels = props.numZoomLevels;
if(typeof minResolution === "number" &&
typeof maxResolution === "number" && numZoomLevels === undefined) {
var ratio = maxResolution / minResolution;
numZoomLevels = Math.floor(Math.log(ratio) / Math.log(2)) + 1;
} else if(numZoomLevels === undefined && maxZoomLevel != null) {
numZoomLevels = maxZoomLevel + 1;
}
// are we able to calculate resolutions?
if(typeof numZoomLevels !== "number" || numZoomLevels <= 0 ||
(typeof maxResolution !== "number" &&
typeof minResolution !== "number")) {
return;
}
// now we have numZoomLevels and at least one of maxResolution
// or minResolution, we can populate the resolutions array
var resolutions = new Array(numZoomLevels);
var base = 2;
if(typeof minResolution == "number" &&
typeof maxResolution == "number") {
// if maxResolution and minResolution are set, we calculate
// the base for exponential scaling that starts at
// maxResolution and ends at minResolution in numZoomLevels
// steps.
base = Math.pow(
(maxResolution / minResolution),
(1 / (numZoomLevels - 1))
);
}
var i;
if(typeof maxResolution === "number") {
for(i=0; i<numZoomLevels; i++) {
resolutions[i] = maxResolution / Math.pow(base, i);
}
} else {
for(i=0; i<numZoomLevels; i++) {
resolutions[numZoomLevels - 1 - i] =
minResolution * Math.pow(base, i);
}
}
return resolutions;
},
/**
* APIMethod: getResolution
*
* Returns:
* {Float} The currently selected resolution of the map, taken from the
* resolutions array, indexed by current zoom level.
*/
getResolution: function() {
var zoom = this.map.getZoom();
return this.getResolutionForZoom(zoom);
},
/**
* APIMethod: getExtent
*
* Returns:
* {<OpenLayers.Bounds>} A Bounds object which represents the lon/lat
* bounds of the current viewPort.
*/
getExtent: function() {
// just use stock map calculateBounds function -- passing no arguments
// means it will user map's current center & resolution
//
return this.map.calculateBounds();
},
/**
* APIMethod: getZoomForExtent
*
* Parameters:
* extent - {<OpenLayers.Bounds>}
* closest - {Boolean} Find the zoom level that most closely fits the
* specified bounds. Note that this may result in a zoom that does
* not exactly contain the entire extent.
* Default is false.
*
* Returns:
* {Integer} The index of the zoomLevel (entry in the resolutions array)
* for the passed-in extent. We do this by calculating the ideal
* resolution for the given extent (based on the map size) and then
* calling getZoomForResolution(), passing along the 'closest'
* parameter.
*/
getZoomForExtent: function(extent, closest) {
var viewSize = this.map.getSize();
var idealResolution = Math.max( extent.getWidth() / viewSize.w,
extent.getHeight() / viewSize.h );
return this.getZoomForResolution(idealResolution, closest);
},
/**
* Method: getDataExtent
* Calculates the max extent which includes all of the data for the layer.
* This function is to be implemented by subclasses.
*
* Returns:
* {<OpenLayers.Bounds>}
*/
getDataExtent: function () {
//to be implemented by subclasses
},
/**
* APIMethod: getResolutionForZoom
*
* Parameter:
* zoom - {Float}
*
* Returns:
* {Float} A suitable resolution for the specified zoom.
*/
getResolutionForZoom: function(zoom) {
zoom = Math.max(0, Math.min(zoom, this.resolutions.length - 1));
var resolution;
if(this.map.fractionalZoom) {
var low = Math.floor(zoom);
var high = Math.ceil(zoom);
resolution = this.resolutions[low] -
((zoom-low) * (this.resolutions[low]-this.resolutions[high]));
} else {
resolution = this.resolutions[Math.round(zoom)];
}
return resolution;
},
/**
* APIMethod: getZoomForResolution
*
* Parameters:
* resolution - {Float}
* closest - {Boolean} Find the zoom level that corresponds to the absolute
* closest resolution, which may result in a zoom whose corresponding
* resolution is actually smaller than we would have desired (if this
* is being called from a getZoomForExtent() call, then this means that
* the returned zoom index might not actually contain the entire
* extent specified... but it'll be close).
* Default is false.
*
* Returns:
* {Integer} The index of the zoomLevel (entry in the resolutions array)
* that corresponds to the best fit resolution given the passed in
* value and the 'closest' specification.
*/
getZoomForResolution: function(resolution, closest) {
var zoom, i, len;
if(this.map.fractionalZoom) {
var lowZoom = 0;
var highZoom = this.resolutions.length - 1;
var highRes = this.resolutions[lowZoom];
var lowRes = this.resolutions[highZoom];
var res;
for(i=0, len=this.resolutions.length; i<len; ++i) {
res = this.resolutions[i];
if(res >= resolution) {
highRes = res;
lowZoom = i;
}
if(res <= resolution) {
lowRes = res;
highZoom = i;
break;
}
}
var dRes = highRes - lowRes;
if(dRes > 0) {
zoom = lowZoom + ((highRes - resolution) / dRes);
} else {
zoom = lowZoom;
}
} else {
var diff;
var minDiff = Number.POSITIVE_INFINITY;
for(i=0, len=this.resolutions.length; i<len; i++) {
if (closest) {
diff = Math.abs(this.resolutions[i] - resolution);
if (diff > minDiff) {
break;
}
minDiff = diff;
} else {
if (this.resolutions[i] < resolution) {
break;
}
}
}
zoom = Math.max(0, i-1);
}
return zoom;
},
/**
* APIMethod: getLonLatFromViewPortPx
*
* Parameters:
* viewPortPx - {<OpenLayers.Pixel>}
*
* Returns:
* {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in
* view port <OpenLayers.Pixel>, translated into lon/lat by the layer.
*/
getLonLatFromViewPortPx: function (viewPortPx) {
var lonlat = null;
var map = this.map;
if (viewPortPx != null && map.minPx) {
var res = map.getResolution();
var maxExtent = map.getMaxExtent({restricted: true});
var lon = (viewPortPx.x - map.minPx.x) * res + maxExtent.left;
var lat = (map.minPx.y - viewPortPx.y) * res + maxExtent.top;
lonlat = new OpenLayers.LonLat(lon, lat);
if (this.wrapDateLine) {
lonlat = lonlat.wrapDateLine(this.maxExtent);
}
}
return lonlat;
},
/**
* APIMethod: getViewPortPxFromLonLat
* Returns a pixel location given a map location. This method will return
* fractional pixel values.
*
* Parameters:
* lonlat - {<OpenLayers.LonLat>}
*
* Returns:
* {<OpenLayers.Pixel>} An <OpenLayers.Pixel> which is the passed-in
* <OpenLayers.LonLat>,translated into view port pixels.
*/
getViewPortPxFromLonLat: function (lonlat) {
var px = null;
if (lonlat != null) {
var resolution = this.map.getResolution();
var extent = this.map.getExtent();
px = new OpenLayers.Pixel(
(1/resolution * (lonlat.lon - extent.left)),
(1/resolution * (extent.top - lonlat.lat))
);
}
return px;
},
/**
* APIMethod: setOpacity
* Sets the opacity for the entire layer (all images)
*
* Parameter:
* opacity - {Float}
*/
setOpacity: function(opacity) {
if (opacity != this.opacity) {
this.opacity = opacity;
for(var i=0, len=this.div.childNodes.length; i<len; ++i) {
var element = this.div.childNodes[i].firstChild;
OpenLayers.Util.modifyDOMElement(element, null, null, null,
null, null, null, opacity);
}
if (this.map != null) {
this.map.events.triggerEvent("changelayer", {
layer: this,
property: "opacity"
});
}
}
},
/**
* Method: getZIndex
*
* Returns:
* {Integer} the z-index of this layer
*/
getZIndex: function () {
return this.div.style.zIndex;
},
/**
* Method: setZIndex
*
* Parameters:
* zIndex - {Integer}
*/
setZIndex: function (zIndex) {
this.div.style.zIndex = zIndex;
},
/**
* Method: adjustBounds
* This function will take a bounds, and if wrapDateLine option is set
* on the layer, it will return a bounds which is wrapped around the
* world. We do not wrap for bounds which *cross* the
* maxExtent.left/right, only bounds which are entirely to the left
* or entirely to the right.
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
*/
adjustBounds: function (bounds) {
if (this.gutter) {
// Adjust the extent of a bounds in map units by the
// layer's gutter in pixels.
var mapGutter = this.gutter * this.map.getResolution();
bounds = new OpenLayers.Bounds(bounds.left - mapGutter,
bounds.bottom - mapGutter,
bounds.right + mapGutter,
bounds.top + mapGutter);
}
if (this.wrapDateLine) {
// wrap around the date line, within the limits of rounding error
var wrappingOptions = {
'rightTolerance':this.getResolution(),
'leftTolerance':this.getResolution()
};
bounds = bounds.wrapDateLine(this.maxExtent, wrappingOptions);
}
return bounds;
},
CLASS_NAME: "OpenLayers.Layer"
});