my-openlayer 0.1.18 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  /**
2
- * 地图底图图层
2
+ * 地图底图图层管理类
3
+ * 提供天地图底图、注记图层、GeoServer图层等功能
4
+ * 支持图层切换、裁剪等高级功能
3
5
  */
4
6
  import { Tile as TileLayer } from "ol/layer";
5
7
  import { get as getProjection } from "ol/proj";
@@ -8,163 +10,499 @@ import { TileWMS } from "ol/source";
8
10
  import WMTSTileGrid from "ol/tilegrid/WMTS";
9
11
  import XYZ from "ol/source/XYZ";
10
12
  import MapTools from "./MapTools";
13
+ import { ErrorHandler, ErrorType } from "../utils/ErrorHandler";
14
+ /**
15
+ * 天地图服务器配置
16
+ */
17
+ const TIANDITU_CONFIG = {
18
+ BASE_URL: '//t{0-7}.tianditu.gov.cn/DataServer',
19
+ PROJECTION: 'EPSG:4326',
20
+ DEFAULT_ZINDEX: 9,
21
+ ANNOTATION_ZINDEX_OFFSET: 1
22
+ };
23
+ /**
24
+ * 地图底图图层管理类
25
+ */
11
26
  export default class MapBaseLayers {
27
+ /**
28
+ * 构造函数
29
+ * @param map OpenLayers地图实例
30
+ * @param options 图层配置选项
31
+ */
12
32
  constructor(map, options) {
13
- this.options = {
14
- zIndex: 0,
33
+ this.layers = {};
34
+ this.currentBaseLayerType = null;
35
+ this.errorHandler = ErrorHandler.getInstance();
36
+ try {
37
+ // 参数验证
38
+ this.validateConstructorParams(map, options);
39
+ this.map = map;
40
+ this.options = this.mergeDefaultOptions(options);
41
+ // 初始化图层
42
+ this.initializeLayers();
43
+ }
44
+ catch (error) {
45
+ this.errorHandler.createAndHandleError(`Failed to initialize MapBaseLayers: ${error}`, ErrorType.MAP_ERROR, { map, options, error });
46
+ throw error;
47
+ }
48
+ }
49
+ /**
50
+ * 验证构造函数参数
51
+ * @param map 地图实例
52
+ * @param options 配置选项
53
+ * @private
54
+ */
55
+ validateConstructorParams(map, options) {
56
+ ErrorHandler.validateMap(map);
57
+ if (!options || typeof options !== 'object') {
58
+ throw new Error('Valid options object is required');
59
+ }
60
+ }
61
+ /**
62
+ * 合并默认配置选项
63
+ * @param options 用户配置选项
64
+ * @returns 合并后的配置选项
65
+ * @private
66
+ */
67
+ mergeDefaultOptions(options) {
68
+ const defaultOptions = {
69
+ zIndex: TIANDITU_CONFIG.DEFAULT_ZINDEX,
15
70
  annotation: false,
16
71
  mapClip: false,
17
72
  mapClipData: undefined,
18
73
  };
19
- this.layers = {};
20
- this.map = map;
21
- this.options = { ...this.options, ...options };
22
- //如果没有配置底图,则默认使用天地图底图
74
+ return { ...defaultOptions, ...options };
75
+ }
76
+ /**
77
+ * 初始化图层
78
+ * @private
79
+ */
80
+ initializeLayers() {
81
+ // 如果没有配置底图,则默认使用天地图底图
23
82
  if (!Array.isArray(this.options.layers)) {
24
83
  this.layers = this.options.layers || {};
25
84
  if (!this.options.token) {
26
85
  throw new Error('请配置token后才能使用天地图底图');
27
86
  }
28
- else {
29
- this.initLayer();
30
- }
31
- if (this.layers && Object.keys(this.layers).length !== 0) {
87
+ this.initTiandituLayers();
88
+ if (this.layers && Object.keys(this.layers).length > 0) {
32
89
  this.addMapLayer();
33
- this.switchBaseLayer(Object.keys(this.layers)[0]);
90
+ const firstLayerType = Object.keys(this.layers)[0];
91
+ this.switchBaseLayer(firstLayerType);
34
92
  }
35
93
  }
36
94
  }
37
- switchBaseLayer(type) {
38
- if (Array.isArray(this.options.layers)) {
39
- console.error('需要按照键值对的方式配置底图才可使用切换底图功能');
40
- return;
95
+ /**
96
+ * 初始化天地图图层
97
+ * @private
98
+ */
99
+ initTiandituLayers() {
100
+ if (!this.options.token) {
101
+ throw new Error('Token is required for Tianditu layers');
41
102
  }
42
- for (const key in this.layers) {
43
- this.layers[key]?.forEach((layer) => {
44
- layer.setVisible(key === type);
45
- // console.log(layer)
103
+ const { token, zIndex = TIANDITU_CONFIG.DEFAULT_ZINDEX } = this.options;
104
+ try {
105
+ // 创建基础图层
106
+ this.layers.vec_c = [this.createTiandituLayer({ type: 'vec_c', token, zIndex, visible: false })];
107
+ this.layers.img_c = [this.createTiandituLayer({ type: 'img_c', token, zIndex, visible: false })];
108
+ this.layers.ter_c = [this.createTiandituLayer({ type: 'ter_c', token, zIndex, visible: false })];
109
+ // 添加注记图层
110
+ if (this.options.annotation) {
111
+ this.addAnnotationLayers(token, zIndex);
112
+ }
113
+ }
114
+ catch (error) {
115
+ this.errorHandler.createAndHandleError(`Failed to initialize Tianditu layers: ${error}`, ErrorType.LAYER_ERROR, { token, zIndex, error });
116
+ throw error;
117
+ }
118
+ }
119
+ /**
120
+ * 添加注记图层
121
+ * @param token 天地图token
122
+ * @param baseZIndex 基础层级
123
+ * @private
124
+ */
125
+ addAnnotationLayers(token, baseZIndex) {
126
+ const annotationZIndex = baseZIndex + TIANDITU_CONFIG.ANNOTATION_ZINDEX_OFFSET;
127
+ this.layers.vec_c?.push(this.createAnnotationLayer({
128
+ type: 'cva_c',
129
+ token,
130
+ zIndex: annotationZIndex,
131
+ visible: false
132
+ }));
133
+ this.layers.img_c?.push(this.createAnnotationLayer({
134
+ type: 'cia_c',
135
+ token,
136
+ zIndex: annotationZIndex,
137
+ visible: false
138
+ }));
139
+ this.layers.ter_c?.push(this.createAnnotationLayer({
140
+ type: 'cta_c',
141
+ token,
142
+ zIndex: annotationZIndex,
143
+ visible: false
144
+ }));
145
+ }
146
+ /**
147
+ * 切换底图图层
148
+ * @param type 图层类型
149
+ */
150
+ switchBaseLayer(type) {
151
+ try {
152
+ if (Array.isArray(this.options.layers)) {
153
+ this.errorHandler.createAndHandleError('需要按照键值对的方式配置底图才可使用切换底图功能', ErrorType.LAYER_ERROR, { layersType: 'array', requestedType: type });
154
+ return;
155
+ }
156
+ if (!this.layers[type]) {
157
+ this.errorHandler.createAndHandleError(`图层类型 '${type}' 不存在`, ErrorType.LAYER_ERROR, { availableTypes: Object.keys(this.layers), requestedType: type });
158
+ return;
159
+ }
160
+ // 隐藏所有图层
161
+ for (const key in this.layers) {
162
+ this.layers[key]?.forEach((layer) => {
163
+ layer.setVisible(false);
164
+ });
165
+ }
166
+ // 显示指定类型的图层
167
+ this.layers[type]?.forEach((layer) => {
168
+ layer.setVisible(true);
46
169
  });
170
+ this.currentBaseLayerType = type;
171
+ }
172
+ catch (error) {
173
+ this.errorHandler.createAndHandleError(`Failed to switch base layer to '${type}': ${error}`, ErrorType.LAYER_ERROR, { type, error });
47
174
  }
48
175
  }
49
- initLayer() {
50
- if (this.layers && this.options.token) {
51
- const { token, zIndex = 9 } = this.options;
52
- this.layers.vec_c = [MapBaseLayers.getTiandiTuLayer({ type: 'vec_c', token, zIndex, visible: false })];
53
- this.layers.img_c = [MapBaseLayers.getTiandiTuLayer({ type: 'img_c', token, zIndex, visible: false })];
54
- this.layers.ter_c = [MapBaseLayers.getTiandiTuLayer({ type: 'ter_c', token, zIndex, visible: false })];
55
- if (this.options.annotation) {
56
- this.layers.vec_c.push(MapBaseLayers.getAnnotationLayer({
57
- type: 'cva_c',
58
- token,
59
- zIndex: zIndex + 1,
60
- visible: false
61
- }));
62
- this.layers.img_c.push(MapBaseLayers.getAnnotationLayer({
63
- type: 'cia_c',
64
- token,
65
- zIndex: zIndex + 1,
66
- visible: false
67
- }));
68
- this.layers.ter_c.push(MapBaseLayers.getAnnotationLayer({
69
- type: 'cta_c',
70
- token,
71
- zIndex: zIndex + 1,
72
- visible: false
73
- }));
74
- }
75
- }
76
- }
77
- //添加注记图层
176
+ /**
177
+ * 获取当前底图类型
178
+ * @returns 当前底图类型
179
+ */
180
+ getCurrentBaseLayerType() {
181
+ return this.currentBaseLayerType;
182
+ }
183
+ /**
184
+ * 获取可用的图层类型列表
185
+ * @returns 图层类型数组
186
+ */
187
+ getAvailableLayerTypes() {
188
+ return Object.keys(this.layers);
189
+ }
190
+ /**
191
+ * 检查指定图层类型是否存在
192
+ * @param type 图层类型
193
+ * @returns 是否存在
194
+ */
195
+ hasLayerType(type) {
196
+ return type in this.layers && this.layers[type] !== undefined;
197
+ }
198
+ /**
199
+ * 添加注记图层(实例方法)
200
+ * @param options 注记图层选项(不包含token)
201
+ * @returns 创建的图层
202
+ */
78
203
  addAnnotationLayer(options) {
79
- MapBaseLayers.addAnnotationLayer(this.map, {
80
- ...options,
81
- token: this.options.token || ''
82
- });
204
+ try {
205
+ if (!this.options.token) {
206
+ throw new Error('Token is required for annotation layer');
207
+ }
208
+ return MapBaseLayers.addAnnotationLayer(this.map, {
209
+ ...options,
210
+ token: this.options.token
211
+ });
212
+ }
213
+ catch (error) {
214
+ this.errorHandler.createAndHandleError(`Failed to add annotation layer: ${error}`, ErrorType.LAYER_ERROR, { options, error });
215
+ throw error;
216
+ }
83
217
  }
84
- //添加注记图层
218
+ /**
219
+ * 添加注记图层(静态方法)
220
+ * @param map 地图实例
221
+ * @param options 注记图层选项
222
+ * @returns 创建的图层
223
+ */
85
224
  static addAnnotationLayer(map, options) {
86
- const layer = MapBaseLayers.getAnnotationLayer({
87
- type: options.type,
88
- token: options.token,
89
- zIndex: options.zIndex ?? 10,
90
- visible: options.visible ?? true
91
- });
92
- map.addLayer(layer);
93
- return layer;
225
+ try {
226
+ ErrorHandler.validateMap(map);
227
+ if (!options.token) {
228
+ throw new Error('Token is required for annotation layer');
229
+ }
230
+ const layer = MapBaseLayers.createAnnotationLayer({
231
+ type: options.type,
232
+ token: options.token,
233
+ zIndex: options.zIndex ?? TIANDITU_CONFIG.DEFAULT_ZINDEX,
234
+ visible: options.visible ?? true
235
+ });
236
+ map.addLayer(layer);
237
+ return layer;
238
+ }
239
+ catch (error) {
240
+ const errorHandler = ErrorHandler.getInstance();
241
+ errorHandler.createAndHandleError(`Failed to add annotation layer: ${error}`, ErrorType.LAYER_ERROR, { options, error });
242
+ throw error;
243
+ }
94
244
  }
245
+ /**
246
+ * 将所有图层添加到地图
247
+ * @private
248
+ */
95
249
  addMapLayer() {
96
- if (this.layers) {
250
+ try {
251
+ if (!this.layers || Object.keys(this.layers).length === 0) {
252
+ return;
253
+ }
97
254
  for (const key in this.layers) {
98
255
  this.layers[key]?.forEach((layer) => {
99
- layer = this.createLayer(layer);
100
- this.map.addLayer(layer);
256
+ const processedLayer = this.processLayer(layer);
257
+ this.map.addLayer(processedLayer);
101
258
  });
102
259
  }
103
260
  }
261
+ catch (error) {
262
+ this.errorHandler.createAndHandleError(`Failed to add map layers: ${error}`, ErrorType.LAYER_ERROR, { layersCount: Object.keys(this.layers).length, error });
263
+ throw error;
264
+ }
104
265
  }
105
- createLayer(layer) {
106
- if (this.options.mapClip && this.options.mapClipData) {
107
- layer = MapTools.setMapClip(layer, this.options.mapClipData);
108
- }
109
- return layer;
110
- }
111
- addGeoServerLayer(url, layer, options) {
112
- const wmsLayer = new TileLayer({
113
- source: new TileWMS({
114
- url: url,
115
- params: {
116
- 'LAYERS': layer,
117
- 'TILED': true,
118
- 'VERSION': '1.1.1' // 或 '1.3.0'
119
- },
120
- serverType: 'geoserver',
121
- crossOrigin: 'anonymous', // 允许跨域
122
- }),
123
- zIndex: options.zIndex ?? 10,
124
- visible: options.visible ?? true,
125
- });
126
- this.map.addLayer(wmsLayer);
127
- return wmsLayer;
128
- }
129
- //img_c 影像底图 ter_c 地形底图
266
+ /**
267
+ * 处理图层(应用裁剪等)
268
+ * @param layer 原始图层
269
+ * @returns 处理后的图层
270
+ * @private
271
+ */
272
+ processLayer(layer) {
273
+ try {
274
+ let processedLayer = layer;
275
+ if (this.options.mapClip && this.options.mapClipData) {
276
+ processedLayer = MapTools.setMapClip(layer, this.options.mapClipData);
277
+ }
278
+ return processedLayer;
279
+ }
280
+ catch (error) {
281
+ this.errorHandler.createAndHandleError(`Failed to process layer: ${error}`, ErrorType.LAYER_ERROR, { hasMapClip: !!this.options.mapClip, hasMapClipData: !!this.options.mapClipData, error });
282
+ throw error;
283
+ }
284
+ }
285
+ /**
286
+ * 添加GeoServer图层
287
+ * @param url GeoServer服务URL
288
+ * @param layerName 图层名称
289
+ * @param options 图层选项
290
+ * @returns 创建的WMS图层
291
+ */
292
+ addGeoServerLayer(url, layerName, options = {}) {
293
+ try {
294
+ if (!url || typeof url !== 'string') {
295
+ throw new Error('Valid URL is required for GeoServer layer');
296
+ }
297
+ if (!layerName || typeof layerName !== 'string') {
298
+ throw new Error('Valid layer name is required for GeoServer layer');
299
+ }
300
+ const wmsLayer = new TileLayer({
301
+ source: new TileWMS({
302
+ url: url,
303
+ params: {
304
+ 'LAYERS': layerName,
305
+ 'TILED': true,
306
+ 'VERSION': options.version || '1.1.1',
307
+ ...options.params
308
+ },
309
+ serverType: 'geoserver',
310
+ crossOrigin: options.crossOrigin || 'anonymous',
311
+ }),
312
+ zIndex: options.zIndex ?? TIANDITU_CONFIG.DEFAULT_ZINDEX,
313
+ visible: options.visible ?? true,
314
+ });
315
+ this.map.addLayer(wmsLayer);
316
+ return wmsLayer;
317
+ }
318
+ catch (error) {
319
+ this.errorHandler.createAndHandleError(`Failed to add GeoServer layer: ${error}`, ErrorType.LAYER_ERROR, { url, layerName, options, error });
320
+ throw error;
321
+ }
322
+ }
323
+ /**
324
+ * 创建天地图图层(实例方法)
325
+ * @param options 天地图图层选项
326
+ * @returns 创建的图层
327
+ * @private
328
+ */
329
+ createTiandituLayer(options) {
330
+ return MapBaseLayers.getTiandiTuLayer(options);
331
+ }
332
+ /**
333
+ * 创建注记图层(实例方法)
334
+ * @param options 注记图层选项
335
+ * @returns 创建的图层
336
+ * @private
337
+ */
338
+ createAnnotationLayer(options) {
339
+ return MapBaseLayers.createAnnotationLayer(options);
340
+ }
341
+ /**
342
+ * 创建天地图底图图层(静态方法)
343
+ * @param options 天地图图层选项
344
+ * @returns 创建的图层
345
+ */
130
346
  static getTiandiTuLayer(options) {
131
- return new TileLayer({
132
- source: new XYZ({
133
- url: `//t{0-7}.tianditu.gov.cn/DataServer?T=${options.type}&tk=${options.token}&x={x}&y={y}&l={z}`,
134
- projection: 'EPSG:4326'
135
- }),
136
- zIndex: options.zIndex,
137
- visible: options.visible
138
- });
347
+ try {
348
+ if (!options.token) {
349
+ throw new Error('Token is required for Tianditu layer');
350
+ }
351
+ if (!options.type) {
352
+ throw new Error('Layer type is required for Tianditu layer');
353
+ }
354
+ return new TileLayer({
355
+ source: new XYZ({
356
+ url: `//t{0-7}.tianditu.gov.cn/DataServer?T=${options.type}&tk=${options.token}&x={x}&y={y}&l={z}`,
357
+ projection: 'EPSG:4326'
358
+ }),
359
+ zIndex: options.zIndex ?? TIANDITU_CONFIG.DEFAULT_ZINDEX,
360
+ visible: options.visible ?? false
361
+ });
362
+ }
363
+ catch (error) {
364
+ const errorHandler = ErrorHandler.getInstance();
365
+ errorHandler.createAndHandleError(`Failed to create Tianditu layer: ${error}`, ErrorType.LAYER_ERROR, { options, error });
366
+ throw error;
367
+ }
139
368
  }
140
369
  /**
141
- * 地图注记
370
+ * 创建天地图注记图层(静态方法)
371
+ * @param options 注记图层选项
372
+ * @returns 创建的图层
373
+ */
374
+ static createAnnotationLayer(options) {
375
+ try {
376
+ if (!options.token) {
377
+ throw new Error('Token is required for annotation layer');
378
+ }
379
+ if (!options.type) {
380
+ throw new Error('Annotation type is required for annotation layer');
381
+ }
382
+ return new TileLayer({
383
+ source: new XYZ({
384
+ url: `//t{0-7}.tianditu.gov.cn/DataServer?T=${options.type}&tk=${options.token}&x={x}&y={y}&l={z}`,
385
+ projection: 'EPSG:4326'
386
+ }),
387
+ zIndex: options.zIndex ?? TIANDITU_CONFIG.DEFAULT_ZINDEX,
388
+ visible: options.visible ?? false
389
+ });
390
+ }
391
+ catch (error) {
392
+ const errorHandler = ErrorHandler.getInstance();
393
+ errorHandler.createAndHandleError(`Failed to create annotation layer: ${error}`, ErrorType.LAYER_ERROR, { options, error });
394
+ throw error;
395
+ }
396
+ }
397
+ /**
398
+ * 获取天地图注记图层(向后兼容的静态方法)
399
+ * @param options 注记图层选项
400
+ * @returns 创建的图层
401
+ * @deprecated 使用 createAnnotationLayer 替代
142
402
  */
143
403
  static getAnnotationLayer(options) {
144
- return new TileLayer({
145
- source: new XYZ({
146
- url: `//t{0-7}.tianditu.gov.cn/DataServer?T=${options.type}&tk=${options.token}&x={x}&y={y}&l={z}`,
147
- projection: 'EPSG:4326'
148
- }),
149
- zIndex: options.zIndex,
150
- visible: options.visible
151
- });
404
+ return MapBaseLayers.createAnnotationLayer(options);
152
405
  }
406
+ /**
407
+ * 创建WMTS瓦片网格
408
+ * @param length 层级数量
409
+ * @returns WMTS瓦片网格
410
+ */
153
411
  static getTileGrid(length) {
154
- const projection = getProjection('EPSG:4326');
155
- const projectionExtent = projection?.getExtent();
156
- const size = getWidth(projectionExtent) / 256;
157
- const resolutions = new Array(length);
158
- const matrixIds = new Array(length);
159
- for (let i = 0; i < length; i += 1) {
160
- const pow = Math.pow(2, i);
161
- resolutions[i] = size / pow;
162
- matrixIds[i] = i;
163
- }
164
- return new WMTSTileGrid({
165
- origin: getTopLeft(projectionExtent),
166
- resolutions,
167
- matrixIds
168
- });
412
+ try {
413
+ if (!length || length <= 0) {
414
+ throw new Error('Valid length is required for tile grid');
415
+ }
416
+ const projection = getProjection('EPSG:4326');
417
+ if (!projection) {
418
+ throw new Error('Failed to get EPSG:4326 projection');
419
+ }
420
+ const projectionExtent = projection.getExtent();
421
+ const size = getWidth(projectionExtent) / 256;
422
+ const resolutions = new Array(length);
423
+ const matrixIds = new Array(length);
424
+ for (let i = 0; i < length; i += 1) {
425
+ const pow = Math.pow(2, i);
426
+ resolutions[i] = size / pow;
427
+ matrixIds[i] = i;
428
+ }
429
+ return new WMTSTileGrid({
430
+ origin: getTopLeft(projectionExtent),
431
+ resolutions,
432
+ matrixIds
433
+ });
434
+ }
435
+ catch (error) {
436
+ const errorHandler = ErrorHandler.getInstance();
437
+ errorHandler.createAndHandleError(`Failed to create tile grid: ${error}`, ErrorType.MAP_ERROR, { length, error });
438
+ throw error;
439
+ }
440
+ }
441
+ /**
442
+ * 移除指定类型的图层
443
+ * @param type 图层类型
444
+ */
445
+ removeLayersByType(type) {
446
+ try {
447
+ if (!this.layers[type]) {
448
+ return;
449
+ }
450
+ this.layers[type].forEach((layer) => {
451
+ this.map.removeLayer(layer);
452
+ });
453
+ delete this.layers[type];
454
+ if (this.currentBaseLayerType === type) {
455
+ this.currentBaseLayerType = null;
456
+ }
457
+ }
458
+ catch (error) {
459
+ this.errorHandler.createAndHandleError(`Failed to remove layers of type '${type}': ${error}`, ErrorType.LAYER_ERROR, { type, error });
460
+ }
461
+ }
462
+ /**
463
+ * 清除所有图层
464
+ */
465
+ clearAllLayers() {
466
+ try {
467
+ for (const key in this.layers) {
468
+ this.layers[key]?.forEach((layer) => {
469
+ this.map.removeLayer(layer);
470
+ });
471
+ }
472
+ this.layers = {};
473
+ this.currentBaseLayerType = null;
474
+ }
475
+ catch (error) {
476
+ this.errorHandler.createAndHandleError(`Failed to clear all layers: ${error}`, ErrorType.LAYER_ERROR, { error });
477
+ }
478
+ }
479
+ /**
480
+ * 获取图层数量统计
481
+ * @returns 图层统计信息
482
+ */
483
+ getLayerStats() {
484
+ const layersByType = {};
485
+ let totalLayers = 0;
486
+ for (const key in this.layers) {
487
+ const count = this.layers[key]?.length || 0;
488
+ layersByType[key] = count;
489
+ totalLayers += count;
490
+ }
491
+ return {
492
+ totalTypes: Object.keys(this.layers).length,
493
+ totalLayers,
494
+ layersByType
495
+ };
496
+ }
497
+ /**
498
+ * 销毁实例,清理资源
499
+ */
500
+ destroy() {
501
+ try {
502
+ this.clearAllLayers();
503
+ }
504
+ catch (error) {
505
+ this.errorHandler.createAndHandleError(`Failed to destroy MapBaseLayers: ${error}`, ErrorType.MAP_ERROR, { error });
506
+ }
169
507
  }
170
508
  }