forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorTileLayer.js
More file actions
300 lines (273 loc) · 10.4 KB
/
VectorTileLayer.js
File metadata and controls
300 lines (273 loc) · 10.4 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
import { CesiumZondy } from '../core/Base';
import { getLayers, getFonts, getVectorTileSource, getSpritePng } from './vectortile/MapgisVectorTileLayer';
import VectorTileProvider from './vectortile/VectorTileProvider';
import VectorTileStyle from './vectortile/MapgisVectorTileStyle';
import axios from 'axios';
import { find } from 'ol/array';
/**
* @author 基础平台/创新中心 潘卓然 ParnDeedlit
* @class module:客户端渲染.VectorTileLayer
* @classdesc 基于Mapbox的矢量瓦片进行绘制渲染.
* @description CesiumZondy.zondy.VectorTileLayer, 前端实时绘制矢量瓦片
* @param viewer - {Object} 传入的cesium的地图viewer
* @param option - {Object} 属性键值对,地图属性字段。
* @param {String} [option.ip = localhost] 地图服务ip
* @param {String} [option.port = 6163] 地图服务port
* @param {String} [option.layerName] 地图名
* @param {String} option.mvtStyle 样式json文件路径或者MVT-JSON对象,当为url时等于styleUrl;当为vectortilejson等于vectortilejson
* @param {String} [option.styleUrl] 样式json文件路径,有styleUrl就可以直接读取styleUrl里的信息;不然就是加载中地发布的矢量瓦片,使用ip,port和layerName先拼接styleUrl路径再进行查询。
* @param {Object} [option.vectortilejson] 矢量瓦片json对象,直接取json对象,不需要再去请求。
* @param {Cesium.TilingScheme} [option.tilingScheme] 矢量瓦片瓦片切分规则:经纬度还是墨卡托
* @param {String} [option.token] 第三方需要的token,比如mapbox
* @param {String} [option.show=true] 是否可见
* @param {String} [option.callback] 加载矢量瓦片成功回调,返回Provider
* @example
* vectortileLayer = new CesiumZondy.Overlayer.VectorTileLayer(
webGlobe.viewer,
{
mvtStyle:"http://develop.smaryun.com:6163/igs/rest/mrms/vtiles/styles/街道-墨卡托.json",
token: "",
show: true,
}
);
*/
export class VectorTileLayer {
constructor(viewer, options) {
this.viewer = viewer;
this.scene = this.viewer.scene;
this.url = options.styleUrl;
if (!this.url && options.ip) {
this.url = 'http://' + options.ip + ':' + options.port + '/igs/rest/mrcs/vtiles/0/' + options.layerName;
}
this.options = options;
this.callback = options.callback;
this.token = options.token || '';
this.opacity = options.opacity || 1;
this.vectortilejson = options.vectortilejson;
this.threadId = options.threadId || Math.random() * 10000;
this.show = options.show;
this.mvtStyle = options.mvtStyle;
this.styleUrl = options.styleUrl;
this.tilingScheme = options.tilingScheme;
this.provider = null;
this.initDevicePixelRatio();
//this.bindEvent();
if (this.mvtStyle) {
if (typeof this.mvtStyle === 'string') {
//如果是个网络地址,就通过url请求获取矢量瓦片json对象
this.url = this.mvtStyle;
this.requestVectortileJson();
} else {
this.requestStyleData(this.mvtStyle);
}
} else if (this.styleUrl) {
if (typeof this.styleUrl === 'string') {
this.url = this.styleUrl;
this.requestVectortileJson();
} else {
this.requestStyleData();
}
} else {
if (!this.vectortilejson) {
//如果没有矢量瓦片json对象,就通过url请求获取矢量瓦片json对象
this.requestVectortileJson();
} else {
this.requestStyleData(this.vectortilejson);
}
}
}
initDevicePixelRatio() {
this.devicePixelRatio = window.devicePixelRatio || 1;
}
bindEvent() {
var viewer = this.viewer;
//下面几个是cesium专属事件,clickEvent和mousemoveEvent是mapv内部自带的方法不放出来
this.innerMoveStart = this.moveStartEvent.bind(this);
this.innerMoveEnd = this.moveEndEvent.bind(this);
this.innnerZoomStart = this.zoomStartEvent.bind(this);
this.innnerZoomEnd = this.zoomEndEvent.bind(this);
}
/**
* 通过url获取矢量瓦片json对象
*/
requestVectortileJson() {
if (!this.vectortilejson) {
axios.get(this.url).then(
function (res) {
this.vectortilejson = res.data;
this.requestStyleData(this.vectortilejson);
}.bind(this)
);
}
}
/**
* 通过矢量瓦片json对象,获取样式参数
* @function module:客户端渲染.VectorTileLayer.prototype.requestStyleData
* @param {Object} vectortilejson 矢量瓦片对象 Mapbox-Style-Spec样式对象
* @see https://docs.mapbox.com/mapbox-gl-js/style-spec/
*/
requestStyleData(vectortilejson) {
this.vectortilejson = vectortilejson;
var layers = getLayers(vectortilejson);
var sources = getVectorTileSource(vectortilejson);
var spritepng = getSpritePng(vectortilejson);
axios.get(vectortilejson.sprite + '.json').then(
function (result) {
let spritedata = result.data;
this.styleData = {
vectortilejson: vectortilejson,
layers: layers,
spritedata: spritedata,
spritepng: spritepng,
fonts: getFonts,
sources: sources
};
this.addLayer(this.styleData);
}.bind(this)
);
}
/**
* 首先构造矢量瓦片样式,再添加图层
* @function module:客户端渲染.VectorTileLayer.prototype.addLayer
* @param {Object} styleData 矢量瓦片样式参数 Mapbox-Style-Spec样式对象
* @see https://docs.mapbox.com/mapbox-gl-js/style-spec/
*/
addLayer(styleData) {
var vectortileStyle = VectorTileStyle(
styleData.vectortilejson,
Object.keys(styleData.vectortilejson.sources),
null,
styleData.spritedata,
styleData.spritepng,
styleData.fonts
);
if (styleData.sources.length > 0) {
var source = styleData.sources[0];
let vectortile = VectorTileProvider(Cesium, {
key: this.token,
url: source.tiles[0],
style: vectortileStyle,
opacity: this.opacity,
threadId: this.threadId,
show: this.show,
tilingScheme: this.tilingScheme
});
this.provider = this.viewer.imageryLayers.addImageryProvider(vectortile);
this.provider.show = this.show;
if (this.callback) {
this.callback({ imageryLayer: this.provider });
}
}
}
/**
* @description 设置布局属性
* @function module:客户端渲染.VectorTileLayer.prototype.updateStyle
* @param {Object} mvtStyle
*/
updateStyle(mvtStyle) {
if (!this.styleData) return;
this.styleData.vectortilejson = mvtStyle;
this.remove();
this.addLayer(this.styleData);
}
/**
* @description 设置布局属性
* @function module:客户端渲染.VectorTileLayer.prototype.setLayoutProperty
* @param {String} layer
* @param {String} key
* @param {Object} value
*/
setLayoutProperty(layerId, key, value) {
if (!this.vectortilejson || !this.vectortilejson.layers) return;
const { layers } = this.vectortilejson;
let finds = layers.filter((l) => {
return l.id === layerId;
});
let layer = finds && finds.length > 0 ? finds[0] : undefined;
if (!layer) return;
layer.layout = layer.layout || {};
layer.layout[key] = value;
this.styleData.vectortilejson = this.vectortilejson;
this.remove();
this.addLayer(this.styleData);
}
/**
* @description 设置画笔属性
* @function module:客户端渲染.VectorTileLayer.prototype.setPaintProperty
* @param {String} layerId
* @param {String} key
* @param {Object} value
*/
setPaintProperty(layerId, key, value) {
if (!this.vectortilejson || !this.vectortilejson.layers) return;
const { layers } = this.vectortilejson;
let finds = layers.filter((l) => {
return l.id === layerId;
});
let layer = finds && finds.length > 0 ? finds[0] : undefined;
if (!layer) return;
layer.paint = layer.paint || {};
layer.paint[key] = value;
this.remove();
this.addLayer(this.styleData);
}
/**
* @description 设置过滤属性
* @function module:客户端渲染.VectorTileLayer.prototype.setFilter
* @param {String} layerId
* @param {Array} rule
*/
setFilter(layerId, rule) {
if (!this.vectortilejson || !this.vectortilejson.layers) return;
const { layers } = this.vectortilejson;
let finds = layers.filter((l) => {
return l.id === layerId;
});
let layer = finds && finds.length > 0 ? finds[0] : undefined;
if (!layer) return;
layer.filter = layer.filter || {};
layer.filter = rule;
this.remove();
this.addLayer(this.styleData);
}
unbindEvent() {}
moveStartEvent() {}
moveEndEvent() {}
zoomStartEvent() {}
zoomEndEvent() {}
/**
* 销毁图层-实际调用remove,为了接口保持一致
* @function module:客户端渲染.VectorTileLayer.prototype.destroy
*/
destroy() {
this.remove();
}
/**
* 设置图层的可见性
* @param visible
*/
setVisible(visible) {
if (this.provider) {
this.provider.show = visible;
}
}
/**
* 移除图层
* @function module:客户端渲染.VectorTileLayer.prototype.remove
*/
remove() {
let self = this;
if (self.provider) {
self.viewer.imageryLayers.remove(self.provider, true);
self.provider.show = false;
}
/* let self = this;
window.setTimeout(() => {
if (self.provider) {
self.viewer.imageryLayers.remove(self.provider, true);
self.provider.show = false;
}
}, 1000); */
}
}
CesiumZondy.Overlayer.VectorTileLayer = VectorTileLayer;