OpenLayers源码学习17:图层Layer.HTTPRequest

本来想直接看wmts这个协议的实现, 结果发现Layer.WMTS继承于Layer.Grid 继承于Layer.HTTPRequest
东西太多 所以还是按顺序看比较好…

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
OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
//哈希因子, 注释说是应对多个wms用的 全代码只有一个
URL_HASH_FACTOR: (Math.sqrt(5) - 1) / 2,
// 可能string或者数组
url: null,
//哈希的 k/v
params: null,
// 是否将此图层按base layer转换投影, 默认false
reproject: false,
//构造, 继承于OpenLayers.Layer. 参数名字挺好不用再解释了...
initialize: function(name, url, params, options) {
//调用父对象的initialize, 传新参数构造
OpenLayers.Layer.prototype.initialize.apply(this, [name, options]);
//处理子类配置
this.url = url;
if (!this.params) {
this.params = OpenLayers.Util.extend({}, params);
}
},
//销毁
destroy: function() {
//先处理构造时后添加的
this.url = null;
this.params = null;
//再调用父类的析构
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
},
//克隆
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.HTTPRequest(this.name,
this.url,
this.params,
this.getOptions());
}
//创建一个新的之后,
obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
return obj;
},
setUrl: function(newUrl) {
this.url = newUrl;
},
// 合并新参数, 触发图层重绘
mergeNewParams:function(newParams) {
this.params = OpenLayers.Util.extend(this.params, newParams);
var ret = this.redraw();
if(this.map != null) {
this.map.events.triggerEvent("changelayer", {
layer: this,
property: "params"
});
}
return ret;
},
//重绘图层, 强制重绘会mergeNewParams
redraw: function(force) {
if (force) {
return this.mergeNewParams({"_olSalt": Math.random()});
} else {
return OpenLayers.Layer.prototype.redraw.apply(this, []);
}
},
//选择url,
//没太看懂, 应该是个哈希算法, 最终取到指定的url[idx]
selectUrl: function(paramString, urls) {
var product = 1;
for (var i=0, len=paramString.length; i<len; i++) {
product *= paramString.charCodeAt(i) * this.URL_HASH_FACTOR;
product -= Math.floor(product);
}
return urls[Math.floor(product * urls.length)];
},
// 最终返回url形如: server?key1=value1&key2=value2&key3=value3
getFullRequestString:function(newParams, altUrl) {
// 如果没有altUrl就用本图层的url
var url = altUrl || this.url;
// 克隆this.params, 合并传参中的newParams, 剥离k/v
var allParams = OpenLayers.Util.extend({}, this.params);
allParams = OpenLayers.Util.extend(allParams, newParams);
var paramsString = OpenLayers.Util.getParameterString(allParams);
// 如果url是数组, 利用哈希剥离
if (OpenLayers.Util.isArray(url)) {
url = this.selectUrl(paramsString, url);
}
// 忽略大小写
var urlParams =
OpenLayers.Util.upperCaseObject(OpenLayers.Util.getParameters(url));
for(var key in allParams) {
if(key.toUpperCase() in urlParams) {
delete allParams[key];
}
}
paramsString = OpenLayers.Util.getParameterString(allParams);
return OpenLayers.Util.urlAppend(url, paramsString);
},
CLASS_NAME: "OpenLayers.Layer.HTTPRequest"
});