forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheTileLayer.js
More file actions
214 lines (186 loc) · 7.31 KB
/
cacheTileLayer.js
File metadata and controls
214 lines (186 loc) · 7.31 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
import {Zondy} from '../../service/common/Base';
import {newGuid} from '../../service/common/Util';
import TileImage from 'ol/source/TileImage.js';
import * as ol_extent from 'ol/extent.js';
import TileGrid from 'ol/tilegrid/TileGrid.js';
import {inherits} from 'ol/util.js';
import {assign} from 'ol/obj.js';
import TileLayer from 'ol/layer/Tile.js';
/// <summary>动态裁图的矢量地图文档资源构造函数</summary>
/// <param name="opt_options" type="Object">属性键值对</param>
var CacheTileLayerSource = function (opt_options) {
var options = opt_options ? opt_options : {};
TileImage.call(this, {
attributions: options.attributions,
logo: options.logo,
opaque: options.opaque,
projection: options.projection,
state: options.state !== undefined ?
/** @type {ol.source.State} */(options.state) : undefined,
tilePixelRatio: options.tilePixelRatio,
wrapX: options.wrapX,
crossOrigin: options.crossOrigin !== undefined ? options.crossOrigin : null //"anonymous"为跨域调用,
});
this.token = options.token;
/**
* @public
* @type {string}
* 地图服务请求地址(可通过初始对象的options赋值)
*/
this.ip = options.ip !== undefined ? options.ip : "127.0.0.1";
/**
* @public
* @type {string}
* 地图服务请求端口(可通过初始对象的options赋值)
*/
this.port = options.port !== undefined ? options.port : "6163";
/**
* @public
* @type {string}
* 地图名称,必须赋值
*/
this.name = options.name !== undefined ? options.name : null;
/**
* @public
* @type {number}
* 最大分辨率,新瓦片必须指定
*/
this.maxResolution = options.maxResolution !== undefined ? options.maxResolution : null;
//根据投影获取地图范围
var tileProjection = options.projection !== undefined ? options.projection : null;
//地图范围
var tileExtent = [-180, -90, 180, 90];
if (tileProjection != null) {
tileExtent = tileProjection.getExtent();
}
//设置地图范围
this.extent = options.extent !== undefined ? options.extent : tileExtent;
/**
* @public
* @type {number}
* 动态裁图瓦片地图总级数
*/
this.maxZoom = options.maxZoom !== undefined ? options.maxZoom : 16;
/**
* @public
* @type {number}
* 地图图片大小
*/
this.tileSize = options.tileSize !== undefined ? options.tileSize : 256;
//分辨率数组,根据传入的分辨率或范围计算得到
this.resolutions = options.resolutions !== undefined ? options.resolutions : this.getResolutions();
/**
* @public
* @type {Array.<number>}
* 地图的原点,可由外部指定,动态裁图默认左上角
*/
this.origin = options.origin !== undefined ? options.origin : ol_extent.getTopLeft(this.extent);
/**
* @public
* @type {string}
* 请求使用的网格协议,默认http协议
*/
this.networkProtocol = options.networkProtocol !== undefined ? options.networkProtocol : location.protocol.split(":")[0]||"http";
/**
* @public
* @type {string}
* 请求使用的域名,基地址
*/
this.domain = options && options.domain ? options.domain : '';
/**
* @private
* @type {string}
* 客户端标识,用以服务器缓存地图,一般情况下无需赋值
*/
this.guid = options.guid !== undefined ? options.guid : newGuid();
/**
* @private
* true表示动态裁图的方式显示出图
*/
this.cache = true;
/**
* @private
* @type {Array.<number>}
* 创建网格(内部调用)
*/
this.tileGrid = new TileGrid({
origin: this.origin, //数组类型,如[0,0],
resolutions: this.resolutions, //分辨率
tileSize: this.tileSize //瓦片图片大小
});
/**
* @protected
* @type {ol.TileUrlFunctionType}
* 拼接取图地址方法
*/
this.tileUrlFunction = options.tileUrlFunction !== undefined ? options.tileUrlFunction : this.tileUrlFunctionExtend;
};
inherits(CacheTileLayerSource, TileImage);
/**
* 创建分辨率数组
*/
CacheTileLayerSource.prototype.getResolutions = function () {
if (this.maxResolution == null) {
var width = ol_extent.getWidth(this.extent);
var height = ol_extent.getHeight(this.extent);
this.maxResolution = (width >= height ? width : height) / (this.tileSize);
}
var opt_resolutions = new Array(this.maxZoom);
for (var z = 0; z < this.maxZoom; ++z) {
opt_resolutions[z] = this.maxResolution / Math.pow(2, z);
}
return opt_resolutions;
};
/**
* 拼接url取图地址
* @param {Array.<number>} tileCoord 数据格式包含级数、行号、列号.
* @param {string} pixelRatio 像素比率
* @param {ol.proj.Projection} projection 投影
*/
CacheTileLayerSource.prototype.tileUrlFunctionExtend = function (tileCoord, pixelRatio, projection) {
//判断返回的当前级数的行号和列号是否包含在整个地图范围内
if (this.tileGrid != null) {
var tileRange = this.tileGrid.getTileRangeForExtentAndZ(this.extent, tileCoord[0], tileRange);
if (!tileRange.contains(tileCoord)) {
return;
}
}
this.cache = true;
//var urlTemplate = 'http://' + this.ip + ':' + this.port + '/igs/rest/mrms/docs/' + this.name + '?f=' + this.f + '&cache=' + this.cache + '&guid=' + this.guid + '&level={z}&col={x}&row={y}&w=' + this.tileSize + '&h=' + this.tileSize;
var urlTemplate;
if (this.domain === '') {
urlTemplate = this.networkProtocol + '://' + this.ip + ':' + this.port + '/igs/rest/mrms/docs/' + this.name + '?f=' + this.f + '&cache=' + this.cache + '&guid=' + this.guid + '&level={z}&col={x}&row={y}&w=' + this.tileSize + '&h=' + this.tileSize;
}else {
urlTemplate = this.domain + '/igs/rest/mrms/docs/' + this.name + '?f=' + this.f + '&cache=' + this.cache + '&guid=' + this.guid + '&level={z}&col={x}&row={y}&w=' + this.tileSize + '&h=' + this.tileSize;
}
if(this.token){
urlTemplate += '&token=' + this.token
}
var z = tileCoord[0];
var x = tileCoord[1];
var y = -(tileCoord[2] + 1);
if (x >= Math.pow(2, z) || y >= Math.pow(2, z)) {
return;
}
return urlTemplate.replace('{x}', x.toString()).replace('{y}', y.toString()).replace('{z}', z.toString());
};
export { CacheTileLayerSource };
Zondy.Source.CacheTileLayerSource = CacheTileLayerSource;
/// <summary>显示动态裁图的矢量地图文档的功能服务构造函数</summary>
/// <param {string} opt_name 图层名称,无实际意义可为null.</param>
/// <param {string} opt_docName 要显示的地图文档名称.</param>
/// <param name="opt_options" type="Object">属性键值对</param>
var CacheTileLayer = function (opt_name, opt_docName, opt_options) {
var options = opt_options ? opt_options : {};
assign(options, { 'layerName': opt_name });
assign(options, { 'name': opt_docName });
options.source = !!options.source ? options.source : new CacheTileLayerSource(options);
var options_clone = assign({}, options);
options_clone.maxResolution = Infinity;
TileLayer.call(this, (options_clone));
assign(this, options);
};
inherits(CacheTileLayer, TileLayer);
export { CacheTileLayer };
Zondy.Map.CacheTileLayer = CacheTileLayer;