forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonDataManager.js
More file actions
298 lines (280 loc) · 10.9 KB
/
CommonDataManager.js
File metadata and controls
298 lines (280 loc) · 10.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
import { CesiumZondy } from '../core/Base';
import LayerManager from '../layer/LayerManager';
/**
* @author 三维基础平台研发中心·冯桂英
* @class module:客户端公共方法.CommonDataManager
* @category CommonDataManager
* @classdesc 通用数据管理类
* @description 该类实现了通用数据的添加与删除功能
* @param option.viewer 视图
*/
export default class CommonDataManager extends LayerManager {
constructor(optionsParam) {
const options = optionsParam;
super(options);
}
/**
* 通过地址添加图片,包括本地图片和网络图片
* @function module:客户端公共方法.CommonDataManager.prototype.appendImageByUrl
* @param {String} url 图片地址
* @param {Number} west 西经
* @param {Number} south 南纬
* @param {Number} east 东经
* @param {Number} north 北纬
* @param {Object} options 扩展参数
* @example
* let cesiumFun = new CommonDataManager({viewer:viewer});
* let singleImage = cesiumFun.appendImageByUrl('.../../../../../../../../static/libs/Cesium/MapGIS/image/2.5D2.png',110,20,114,30);
*/
appendImageByUrl(url, west, south, east, north, options) {
const providerOpts = {
url,
rectangle: Cesium.Rectangle.fromDegrees(west, south, east, north)
};
if (Cesium.defined(options)) {
Object.extend(providerOpts, options);
}
const singleImage = this.viewer.imageryLayers.addImageryProvider(new Cesium.SingleTileImageryProvider(providerOpts));
return singleImage;
}
/**
*移除添加的图片
* @function module:客户端公共方法.CommonDataManager.prototype.removeImage
* @param {Object} imageryLayer 添加的图片对象
* @param {Boolean} isDestroy 是否销毁图片对象
*/
removeImage(imageryLayer, isDestroy = true) {
if (this.viewer.imageryLayers.contains(imageryLayer)) {
this.viewer.imageryLayers.remove(imageryLayer, isDestroy);
}
}
/**
* 通过路径添加3DTile数据
* @function module:客户端公共方法.CommonDataManager.prototype.append3DTile
* @param {String} url 路径
* @param {Function} onsuccess 成功回调
* @param {Object} options 扩展参数
* @see {@link https://cesium.com/docs/cesiumjs-ref-doc/Cesium3DTileset.html }
* @returns {Object} kml数据对象
* @example
* function load(layer) {
viewer.flyTo(layer);
console.log("这是一个加载成功回调");
}
tiles = cesiumFun.append3DTile('../../../../../../static/data/BatchedTilesets/tileset.json', load);
*/
append3DTile(url, onsuccess, options) {
if (url === null) {
return null;
}
const para = {
url
};
if (Cesium.defined(options)) {
Object.extend(para, options);
}
const tileset = this.viewer.scene.primitives.add(new Cesium.Cesium3DTileset(para));
if (Object.prototype.toString.call(onsuccess) === '[object Function]') {
tileset.readyPromise.then(() => {
onsuccess(tileset);
});
}
return tileset;
}
/**
*移除3dTile对象
* @function module:客户端公共方法.CommonDataManager.prototype.remove3DTile
* @param {Object} tileset 3dTile对象
*/
remove3DTile(tileset) {
this.viewer.scene.primitives.remove(tileset);
}
/**
* 添加模型(gltf文件)
* @function module:客户端公共方法.CommonDataManager.prototype.appendModel
* @param {Number} id 模型id
* @param {String} url 模型url路径
* @param {Number} lon 模型所在经度
* @param {Number} lat 模型坐在纬度
* @param {Number} height 高度
* @param {Number} scale 缩放比
* @param {Object} options 包含以下参数
* @param {Color} [options.color] 颜色
* @param {ColorBlendMode} [options.colorBlendMode] 颜色混合模式 Cesium.ColorBlendMode.MIX
* @param {Number} [options.colorBlendAmount] 颜色混合程度
* @returns {Object} model 移除通过 removeModel()
* @example
* let modelUrl = '../../../../../../static/data/CesiumAir/Cesium_Air.gltf';
cesiumFun.appendModels('1', modelUrl, 114, 28, 200, 100000, {
color: Cesium.Color.RED,
colorBlendMode: Cesium.ColorBlendMode.MIX,// MIX是混合 Cesium.ColorBlendMode.HIGHLIGHT, HIGHLIGHT是高亮
colorBlendAmount: 0.4 //这个是程度
});
*/
appendModel(id, url, lon, lat, height = 1000.0, scale = 1.0, options) {
const position = Cesium.Cartesian3.fromDegrees(lon, lat, height);
const modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(position, new Cesium.HeadingPitchRoll());
const modelObj = {
id,
url,
modelMatrix,
scale
};
if (Cesium.defined(options)) {
Object.extend(modelObj, options);
}
const modelSingle = this.viewer.scene.primitives.add(Cesium.Model.fromGltf(modelObj));
modelSingle.readyPromise.then(() => {
modelSingle.activeAnimations.addAll({
speedup: 0.5,
loop: Cesium.ModelAnimationLoop.REPEAT
});
});
return modelSingle;
}
/**
* 批量添加模型
* @function module:客户端公共方法.CommonDataManager.prototype.appendModels
* @param {String} modelsString 模型组织
* @param {Function} successCall 成功后的回调
* @returns {Object} 模型层对象
*/
appendModels(modelsString, successCall) {
const models = new Cesium.CzmlDataSource();
const datasourcePromise = this.viewer.dataSources.add(models.load(modelsString));
datasourcePromise.then((datasource) => {
if (successCall !== null && typeof successCall === 'function') {
successCall(datasource.entities);
}
});
return models;
}
/**
* 通过文件批量添加模型
* @function module:客户端公共方法.CommonDataManager.prototype.appendModels
* @param {String} filePath 模型组织文件
* @param {Function} successCall 成功后的回调
* @returns {Object} 模型层对象
*/
appendModelsByFile(filePath, successCall) {
const models = new Cesium.CzmlDataSource();
const datasourcePromise = this.viewer.dataSources.add(models.load(filePath));
datasourcePromise.then((datasource) => {
if (successCall !== null && typeof successCall === 'function') {
successCall(datasource.entities);
}
});
return models;
}
/**
* 加载Kml、kmz数据
* @function module:客户端公共方法.CommonDataManager.prototype.appendKml
* @param {String} url 路径
* @param {Object} options 参数
* @see {@link https://cesium.com/docs/cesiumjs-ref-doc/KmlDataSource.html }
* @returns {KmlDataSource} kml数据对象
*/
appendKml(url, options, successCall) {
const opt = {
camera: this.viewer.scene.camera,
canvas: this.viewer.scene.canvas
};
if (Cesium.defined(options)) {
Object.extend(opt, options);
}
const kmlData = new Cesium.KmlDataSource(opt);
const datasourcePromise = this.viewer.dataSources.add(kmlData.load(url, opt));
datasourcePromise.then((datasource) => {
if (successCall !== null && typeof successCall === 'function') {
successCall(datasource.entities);
}
this.viewer.clock.multiplier = 1;
this.viewer.clock.shouldAnimate = true;
});
return kmlData;
}
/**
* 添加czml文件
* @function module:客户端公共方法.CommonDataManager.prototype.appendCZML
* @param {String} url 文件地址
* @param {Function} successCall 成功后的回调
* @returns {CzmlDataSource} czml 对象
* @example
* var czml = cesiumFun.addCZML('SampleData/model.czml');
*/
appendCZML(url, successCall) {
const dataSource = new Cesium.CzmlDataSource();
const datasourcePromise = this.viewer.dataSources.add(dataSource.load(url));
datasourcePromise.then((datasource) => {
if (successCall !== null && typeof successCall === 'function') {
successCall(datasource.entities);
}
});
return dataSource;
}
/**
* 添加GeoJson文件
* @function module:客户端公共方法.CommonDataManager.prototype.appendGeoJson
* @param {String} url 文件地址
* @returns {GeoJsonDataSource} 数据对象
* @example
* let geoJson = cesiumFun.appendGeoJson('SampleData/china.topojson');
*/
appendGeoJson(url, successCall, options) {
const dataSource = new Cesium.GeoJsonDataSource();
const dataOptions = {
stroke: Cesium.Color.HOTPINK,
fill: Cesium.Color.PINK.withAlpha(0.5),
strokeWidth: 3
};
if (Cesium.defined(options)) {
Object.extend(dataOptions, options);
}
const datasourcePromise = this.viewer.dataSources.add(dataSource.load(url, dataOptions));
datasourcePromise.then((datasource) => {
if (successCall !== null && typeof successCall === 'function') {
successCall(datasource.entities);
}
});
return dataSource;
}
/**
* 移除模型
* @function module:客户端公共方法.CommonDataManager.prototype.removeModel
* @param {Model} model 模型对象
*/
removeModel(model) {
this.viewer.entities.remove(model);
this.viewer.scene.primitives.remove(model);
}
/**
* 移除通过appendModelsByFile()和appendModels()添加的模型
* @function module:客户端公共方法.CommonDataManager.prototype.removeModels
* @param {DataSource} models 模型组织
*/
removeModels(models) {
this.viewer.dataSources.remove(models);
}
/**
* 移除数据对象
* @function module:客户端公共方法.CommonDataManager.prototype.removeDataSource
* @param {DataSource} datasource 模型组织
* @param {Boolean} isDestroy 是否销毁
*/
removeDataSource(datasource, isDestroy = false) {
if (Cesium.defined(datasource)) {
if (this.viewer.dataSources.contains(datasource)) {
this.viewer.dataSources.remove(datasource, isDestroy);
}
}
}
/**
* 删除所有数据源:与以上几个接口配合使用
* @function module:客户端公共方法.CommonDataManager.prototype.removeAllDataSource
* @param {Boolean} isDestroy 是否销毁
*/
removeAllDataSource(isDestroy = false) {
this.viewer.dataSources.removeAll(isDestroy);
}
}
CesiumZondy.Manager.CommonDataManager = CommonDataManager;