BaseLayer类介绍
# 概述
Layer图层是 Openlayers 中很重要的一个概念和部分,而无论是VectorLayer矢量图层还是VectorTileLayer瓦片图层都是继承BaseLayer实现的。本文主要介绍BaseLayer的核心部分以及实现。
# 源码剖析
BaseLayer类继承自BaseObject类,其实现如下:
class BaseLayer extends BaseObject {
constructor(options) {
super();
this.on;
this.once;
this.un;
this.background_ = options.background;
const properties = Object.assign({}, options);
if (typeof options.properties === "object") {
delete properties.properties;
Object.assign(properties, options.properties);
}
properties[LayerProperty.OPACITY] =
options.opacity !== undefined ? options.opacity : 1;
assert(
typeof properties[LayerProperty.OPACITY] === "number",
"Layer opacity must be a number"
);
properties[LayerProperty.VISIBLE] =
options.visible !== undefined ? options.visible : true;
properties[LayerProperty.Z_INDEX] = options.zIndex;
properties[LayerProperty.MAX_RESOLUTION] =
options.maxResolution !== undefined ? options.maxResolution : Infinity;
properties[LayerProperty.MIN_RESOLUTION] =
options.minResolution !== undefined ? options.minResolution : 0;
properties[LayerProperty.MIN_ZOOM] =
options.minZoom !== undefined ? options.minZoom : -Infinity;
properties[LayerProperty.MAX_ZOOM] =
options.maxZoom !== undefined ? options.maxZoom : Infinity;
this.className_ =
properties.className !== undefined ? properties.className : "ol-layer";
delete properties.className;
this.setProperties(properties);
this.state_ = null;
}
getBackground() {
return this.background_;
}
getClassName() {
return this.className_;
}
getLayerState(managed) {
const state = this.state_ || {
layer: this,
managed: managed === undefined ? true : managed,
};
const zIndex = this.getZIndex();
state.opacity = clamp(Math.round(this.getOpacity() * 100) / 100, 0, 1); // clamp函数保证了透明度的区间在[0,1]之间
state.visible = this.getVisible();
state.extent = this.getExtent();
state.zIndex = zIndex === undefined && !state.managed ? Infinity : zIndex;
state.maxResolution = this.getMaxResolution();
state.minResolution = Math.max(this.getMinResolution(), 0);
state.minZoom = this.getMinZoom();
state.maxZoom = this.getMaxZoom();
this.state_ = state;
return state;
}
getLayersArray(array) {
return abstract();
}
getLayerStatesArray(states) {
return abstract();
}
getExtent() {
return this.get(LayerProperty.EXTENT);
}
getMaxResolution() {
return this.get(LayerProperty.MAX_RESOLUTION);
}
getMinResolution() {
return this.get(LayerProperty.MIN_RESOLUTION);
}
getMinZoom() {
return this.get(LayerProperty.MIN_ZOOM);
}
getMaxZoom() {
return this.get(LayerProperty.MAX_ZOOM);
}
getOpacity() {
return this.get(LayerProperty.OPACITY);
}
getSourceState() {
return abstract();
}
getVisible() {
return this.get(LayerProperty.VISIBLE);
}
getZIndex() {
return this.get(LayerProperty.Z_INDEX);
}
setBackground(background) {
this.background_ = background;
this.changed();
}
setExtent(extent) {
this.set(LayerProperty.EXTENT, extent);
}
setMaxResolution(maxResolution) {
this.set(LayerProperty.MAX_RESOLUTION, maxResolution);
}
setMinResolution(minResolution) {
this.set(LayerProperty.MIN_RESOLUTION, minResolution);
}
setMaxZoom(maxZoom) {
this.set(LayerProperty.MAX_ZOOM, maxZoom);
}
setMinZoom(minZoom) {
this.set(LayerProperty.MIN_ZOOM, minZoom);
}
setOpacity(opacity) {
assert(typeof opacity === "number", "Layer opacity must be a number");
this.set(LayerProperty.OPACITY, opacity);
}
setVisible(visible) {
this.set(LayerProperty.VISIBLE, visible);
}
setZIndex(zindex) {
this.set(LayerProperty.Z_INDEX, zindex);
}
disposeInternal() {
if (this.state_) {
this.state_.layer = null;
this.state_ = null;
}
super.disposeInternal();
}
}
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
BaseLayer类主要就是设置属性,然后提供get和set两大类方法分别用于获取属性和设置属性
# BaseLayer类的构造函数
BaseLayer类的构造函数接受一个options对象类型的参数,显示声明三个变量on,once和un,然后设置this.background_。然后将参数options赋值给properties,再判断options.properties的类型是否是Object,若它存在且其类型是Object,则将其赋值给properties。
再通过一些列的三元判断设置properties的属性,若options中的对应的属性不存在,则设置默认值,如:properties.opacity:透明度,默认为1;properties.visible:是否可见,默认为true;properties.zIndex:层级,无默认值;properties.maxResolution:最大分辨率,默认为无穷大;properties.minResolution:最小分辨率,默认为0;properties.minZoom:最小缩放级别,默认为负无穷;properties.maxZoom:最大缩放级别,默认为正无穷大;
设置对象的className_,若options.className存在,则不存在,则赋值为ol-layer.
最后调用this.setProperties方法,设置一组集合键值对,然后初始化对象的state_为null
# BaseLayer类的方法
# get类方法
get类方法就是调用this.get方法通过key去查找对应值,get方法是在BaseObject类中定义的,只会返回自身的属性值,若不存在则返回undefined
getBackground方法:获取this.background_getClassName方法:获取this.className_getLayerState方法
源码中写道getLayerState方法并不应由图层或图层渲染器调用,因为如果图层被包含在图层组中,状态会不正确。其内部就是调用各种get类方法,获取图层的当前值,组成在一个对象中,作为状态返回。
getLayersArray,getLayerStatesArray和getSourceState方法均暂未实现getExtent方法:获取对象的边界范围extentgetMaxResolution方法:获取对象的最大分辨率maxResolutiongetMinResolution方法:获取对象的最小分辨率minResolutiongetMinZoom方法:获取对象的最小缩放级别minZoomgetMaxZoom方法:获取对象的最大缩放级别maxZoomgetOpacity方法:获取对象的透明度opacitygetVisible方法:获取对象的可见性,返回一个布尔值getZIndex方法:获取对象的层级,返回一个数字
# set类方法
set类方法主要是调用set方法实现的,该方法是在BaseObject类中定义的,如果新值和旧值不等,会调用notify方法,若存在该属性的监听事件,就会调用dispatchEvent派发相应事件。
setBackground方法
setBackground方法用于设置对象的background_,然后调用changed方法
该方法是在Observable类中定义的,其内部会调用dispatchEvent方法,派发类型为change的注册事件
setExtent方法:设置对象的范围extentsetMaxResolution方法:设置对象的最大分辨率maxResolutionsetMinResolution方法:设置对象的最小分辨率minResolutionsetMaxZoom方法:设置对象的最大缩放级别maxZoomsetMinZoom方法:设置对象的最小缩放级别minZoomsetOpacity方法:设置对象的透明度,参数必须是一个数字类型的值setVisible方法:设置对象的可见性setZIndex方法:设置对象的层级
# 总结
BaseLayer类中的方法主要分为两大类,而它们又是基于BaseObject类中的get和set方法实现的。