my-openlayer 2.1.12 → 2.3.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.
package/core/Line.js CHANGED
@@ -5,11 +5,10 @@ import { Stroke, Style } from "ol/style";
5
5
  import { Feature } from "ol";
6
6
  import MapTools from "./MapTools";
7
7
  import { ValidationUtils } from "../utils/ValidationUtils";
8
- import { EventManager } from "./EventManager";
9
- import { ErrorHandler } from "../utils/ErrorHandler";
8
+ import { ConfigManager } from "./ConfigManager";
10
9
  /**
11
10
  * 线要素管理类
12
- * 用于在地图上添加和管理线要素,包括普通线要素、河流图层等
11
+ * 用于在地图上添加和管理线要素
13
12
  *
14
13
  * @example
15
14
  * ```typescript
@@ -22,454 +21,87 @@ import { ErrorHandler } from "../utils/ErrorHandler";
22
21
  * ```
23
22
  */
24
23
  export default class Line {
25
- /**
26
- * 构造函数
27
- * @param map OpenLayers地图实例
28
- */
29
- constructor(map) {
30
- /** 河流图层列表 */
31
- this.riverLayerList = [];
32
- /** 河流图层显示状态 */
33
- this.riverLayerShow = false;
34
- /** 默认河流级别宽度映射 */
35
- this.defaultLevelWidthMap = {
36
- 1: 2,
37
- 2: 1,
38
- 3: 0.5,
39
- 4: 0.5,
40
- 5: 0.5
41
- };
42
- ValidationUtils.validateMapInstance(map);
43
- this.map = map;
44
- this.eventManager = new EventManager(map);
45
- }
46
- addLine(data, options = {}) {
47
- ValidationUtils.validateGeoJSONData(data);
24
+ mergeDefaultOptions(options) {
25
+ const layerName = options?.layerName || ConfigManager.DEFAULT_LINE_OPTIONS.layerName;
48
26
  const defaultOptions = {
49
- type: 'line',
50
- strokeColor: 'rgba(3, 122, 255, 1)',
51
- strokeWidth: 2,
52
- visible: true,
53
- zIndex: 15,
54
- layerName: options.layerName || 'lineLayer'
27
+ ...ConfigManager.DEFAULT_LINE_OPTIONS,
28
+ layerName
55
29
  };
56
30
  const mergedOptions = { ...defaultOptions, ...options };
57
- const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
58
- const layer = new VectorLayer({
59
- properties: {
60
- name: mergedOptions.layerName,
61
- layerName: mergedOptions.layerName
62
- },
63
- source: new VectorSource({ features }),
64
- style: (feature) => {
65
- if (feature instanceof Feature) {
66
- feature.set('type', mergedOptions.type);
67
- feature.set('layerName', mergedOptions.layerName);
31
+ return { ...mergedOptions, layerName };
32
+ }
33
+ createStyleFunction(mergedOptions) {
34
+ return (feature) => {
35
+ if (feature instanceof Feature) {
36
+ feature.set('type', mergedOptions.type);
37
+ feature.set('layerName', mergedOptions.layerName);
38
+ }
39
+ if (mergedOptions.style) {
40
+ if (typeof mergedOptions.style === 'function') {
41
+ return mergedOptions.style(feature);
68
42
  }
69
- // 如果传入了自定义样式,直接使用
70
- if (mergedOptions.style) {
71
- if (typeof mergedOptions.style === 'function') {
72
- return mergedOptions.style(feature);
73
- }
74
- else {
75
- return mergedOptions.style;
76
- }
43
+ else {
44
+ return mergedOptions.style;
77
45
  }
78
- return new Style({
79
- stroke: new Stroke({
80
- color: mergedOptions.strokeColor,
81
- width: mergedOptions.strokeWidth,
82
- lineDash: mergedOptions.lineDash,
83
- lineDashOffset: mergedOptions.lineDashOffset
84
- })
85
- });
86
- },
87
- zIndex: mergedOptions.zIndex
88
- });
89
- layer.setVisible(mergedOptions.visible);
90
- this.map.addLayer(layer);
91
- return layer;
92
- }
93
- /**
94
- * 从URL添加线要素
95
- * @param url 数据URL
96
- * @param options 配置项
97
- * @returns 创建的矢量图层
98
- */
99
- addLineByUrl(url, options = {}) {
100
- const defaultOptions = {
101
- type: 'line',
102
- strokeColor: 'rgba(3, 122, 255, 1)',
103
- strokeWidth: 2,
104
- visible: true,
105
- zIndex: 15,
106
- layerName: options.layerName || 'lineLayer'
46
+ }
47
+ return new Style({
48
+ stroke: new Stroke({
49
+ color: mergedOptions.strokeColor,
50
+ width: mergedOptions.strokeWidth,
51
+ lineDash: mergedOptions.lineDash,
52
+ lineDashOffset: mergedOptions.lineDashOffset
53
+ })
54
+ });
107
55
  };
108
- const mergedOptions = { ...defaultOptions, ...options };
109
- const source = new VectorSource({
110
- url,
111
- format: new GeoJSON(options.projectionOptOptions)
112
- });
56
+ }
57
+ createLayer(source, mergedOptions) {
113
58
  const layer = new VectorLayer({
114
59
  properties: {
115
60
  name: mergedOptions.layerName,
116
61
  layerName: mergedOptions.layerName
117
62
  },
118
63
  source,
119
- style: (feature) => {
120
- if (feature instanceof Feature) {
121
- feature.set('type', mergedOptions.type);
122
- feature.set('layerName', mergedOptions.layerName);
123
- }
124
- // 如果传入了自定义样式,直接使用
125
- if (mergedOptions.style) {
126
- if (typeof mergedOptions.style === 'function') {
127
- return mergedOptions.style(feature);
128
- }
129
- else {
130
- return mergedOptions.style;
131
- }
132
- }
133
- return new Style({
134
- stroke: new Stroke({
135
- color: mergedOptions.strokeColor,
136
- width: mergedOptions.strokeWidth,
137
- lineDash: mergedOptions.lineDash,
138
- lineDashOffset: mergedOptions.lineDashOffset
139
- })
140
- });
141
- },
64
+ style: this.createStyleFunction(mergedOptions),
142
65
  zIndex: mergedOptions.zIndex
143
66
  });
144
67
  layer.setVisible(mergedOptions.visible);
145
68
  this.map.addLayer(layer);
146
69
  return layer;
147
70
  }
148
- addRiverLayersByZoom(fyRiverJson, options = {}) {
149
- ValidationUtils.validateGeoJSONData(fyRiverJson);
150
- const defaultOptions = {
151
- type: 'river',
152
- levelCount: 5,
153
- zoomOffset: 8,
154
- strokeColor: 'rgb(0,113,255)',
155
- strokeWidth: 3,
156
- visible: true,
157
- zIndex: 15,
158
- layerName: 'riverLayer',
159
- removeExisting: options.removeExisting ?? false,
160
- levelWidthMap: this.defaultLevelWidthMap
161
- };
162
- const mergedOptions = { ...defaultOptions, ...options };
163
- // 清除现有河流图层
164
- if (mergedOptions.removeExisting) {
165
- this.clearRiverLayers();
166
- }
167
- this.riverLayerShow = mergedOptions.visible;
168
- this.riverLayerList = [];
169
- // 创建分级河流图层
170
- for (let level = 1; level <= mergedOptions.levelCount; level++) {
171
- const vectorSource = new VectorSource({
172
- format: new GeoJSON(),
173
- loader: () => {
174
- const geojson = new GeoJSON();
175
- fyRiverJson.features.forEach((feature) => {
176
- if (feature.properties && feature.properties.level === level) {
177
- try {
178
- const olFeature = geojson.readFeature(feature);
179
- if (Array.isArray(olFeature)) {
180
- vectorSource.addFeatures(olFeature);
181
- }
182
- else {
183
- vectorSource.addFeature(olFeature);
184
- }
185
- }
186
- catch (error) {
187
- ErrorHandler.getInstance().warn(`Failed to load river feature at level ${level}:`, error);
188
- }
189
- }
190
- });
191
- }
192
- });
193
- const riverLayer = new VectorLayer({
194
- properties: {
195
- name: mergedOptions.layerName,
196
- layerName: mergedOptions.layerName,
197
- riverLevel: level
198
- },
199
- source: vectorSource,
200
- style: (feature) => {
201
- if (feature instanceof Feature) {
202
- feature.set('type', mergedOptions.layerName);
203
- feature.set('layerName', mergedOptions.layerName);
204
- }
205
- // 如果传入了自定义样式,直接使用
206
- if (mergedOptions.style) {
207
- if (typeof mergedOptions.style === 'function') {
208
- return mergedOptions.style(feature);
209
- }
210
- else {
211
- return mergedOptions.style;
212
- }
213
- }
214
- return new Style({
215
- stroke: new Stroke({
216
- color: mergedOptions.strokeColor,
217
- width: mergedOptions.strokeWidth
218
- })
219
- });
220
- },
221
- zIndex: mergedOptions.zIndex
222
- });
223
- riverLayer.setVisible(false);
224
- this.riverLayerList.push(riverLayer);
225
- this.map.addLayer(riverLayer);
226
- }
227
- // 设置缩放事件监听
228
- this.eventManager.on('moveend', () => {
229
- this.showRiverLayerByZoom();
230
- });
231
- // 初始显示
232
- this.showRiverLayerByZoom();
233
- }
234
71
  /**
235
- * 从URL添加分级河流图层,根据缩放级别显示不同级别的河流
236
- * @param url 河流数据URL
237
- * @param options 河流图层配置选项
238
- * @throws {Error} 当数据格式无效时抛出错误
239
- */
240
- addRiverLayersByZoomByUrl(url, options = {}) {
241
- const defaultOptions = {
242
- type: 'river',
243
- levelCount: 5,
244
- zoomOffset: 8,
245
- strokeColor: 'rgb(0,113,255)',
246
- strokeWidth: 3,
247
- visible: true,
248
- zIndex: 15,
249
- layerName: 'riverLayer',
250
- removeExisting: options.removeExisting ?? false,
251
- levelWidthMap: this.defaultLevelWidthMap
252
- };
253
- const mergedOptions = { ...defaultOptions, ...options };
254
- // 清除现有河流图层
255
- if (mergedOptions.removeExisting) {
256
- this.clearRiverLayers();
257
- }
258
- this.riverLayerShow = mergedOptions.visible;
259
- this.riverLayerList = [];
260
- // 创建分级河流图层
261
- for (let level = 1; level <= mergedOptions.levelCount; level++) {
262
- const vectorSource = new VectorSource({
263
- url,
264
- format: new GeoJSON(),
265
- loader: function (extent, resolution, projection, success, failure) {
266
- fetch(url)
267
- .then(response => response.json())
268
- .then(data => {
269
- const geojson = new GeoJSON();
270
- data.features.forEach((feature) => {
271
- if (feature.properties && feature.properties.level === level) {
272
- try {
273
- const olFeature = geojson.readFeature(feature);
274
- if (Array.isArray(olFeature)) {
275
- vectorSource.addFeatures(olFeature);
276
- }
277
- else {
278
- vectorSource.addFeature(olFeature);
279
- }
280
- }
281
- catch (error) {
282
- ErrorHandler.getInstance().warn(`Failed to load river feature at level ${level}:`, error);
283
- }
284
- }
285
- });
286
- success?.(vectorSource.getFeatures());
287
- })
288
- .catch(error => {
289
- ErrorHandler.getInstance().error('Error loading river data:', error);
290
- failure?.();
291
- });
292
- }
293
- });
294
- const riverLayer = new VectorLayer({
295
- properties: {
296
- name: mergedOptions.layerName,
297
- layerName: mergedOptions.layerName,
298
- riverLevel: level
299
- },
300
- source: vectorSource,
301
- style: (feature) => {
302
- if (feature instanceof Feature) {
303
- feature.set('type', mergedOptions.layerName);
304
- feature.set('layerName', mergedOptions.layerName);
305
- }
306
- // 如果传入了自定义样式,直接使用
307
- if (mergedOptions.style) {
308
- if (typeof mergedOptions.style === 'function') {
309
- return mergedOptions.style(feature);
310
- }
311
- else {
312
- return mergedOptions.style;
313
- }
314
- }
315
- return new Style({
316
- stroke: new Stroke({
317
- color: mergedOptions.strokeColor,
318
- width: mergedOptions.strokeWidth
319
- })
320
- });
321
- },
322
- zIndex: mergedOptions.zIndex
323
- });
324
- riverLayer.setVisible(false);
325
- this.riverLayerList.push(riverLayer);
326
- this.map.addLayer(riverLayer);
327
- }
328
- // 设置缩放事件监听
329
- this.eventManager.on('moveend', () => {
330
- this.showRiverLayerByZoom();
331
- });
332
- // 初始显示
333
- this.showRiverLayerByZoom();
334
- }
335
- /**
336
- * 显示或隐藏河流图层
337
- * @param show 是否显示河流图层
72
+ * 构造函数
73
+ * @param map OpenLayers地图实例
338
74
  */
339
- showRiverLayer(show) {
340
- this.riverLayerShow = show;
341
- this.showRiverLayerByZoom();
75
+ constructor(map) {
76
+ ValidationUtils.validateMapInstance(map);
77
+ this.map = map;
342
78
  }
343
79
  /**
344
- * 根据缩放级别显示对应的河流图层
345
- * 缩放级别越高,显示的河流级别越详细
80
+ * 添加线要素
81
+ * @param data GeoJSON格式的线数据
82
+ * @param options 配置项
83
+ * @returns 创建的矢量图层
346
84
  */
347
- showRiverLayerByZoom() {
348
- const zoom = this.map.getView().getZoom();
349
- if (!zoom) {
350
- return;
351
- }
352
- this.riverLayerList.forEach((layer, index) => {
353
- // 计算显示阈值:级别索引 + 1(因为level从1开始)+ 缩放偏移量(默认8)
354
- const displayThreshold = index + 1 + 8;
355
- if (zoom > displayThreshold) {
356
- layer.setVisible(this.riverLayerShow);
357
- }
358
- else {
359
- layer.setVisible(false);
360
- }
361
- });
362
- }
363
- addRiverWidthByLevel(data, options = {}) {
85
+ addLine(data, options) {
364
86
  ValidationUtils.validateGeoJSONData(data);
365
- // 合并默认配置
366
- const mergedOptions = {
367
- type: 'river',
368
- layerName: 'river',
369
- strokeColor: 'rgba(3, 122, 255, 1)',
370
- strokeWidth: 2,
371
- visible: true,
372
- zIndex: 15,
373
- levelWidthMap: this.defaultLevelWidthMap,
374
- removeExisting: options.removeExisting ?? false,
375
- ...options
376
- };
377
- // 移除同名图层(如果存在)
378
- if (mergedOptions.removeExisting && mergedOptions.layerName) {
379
- MapTools.removeLayer(this.map, mergedOptions.layerName);
380
- }
381
- // 解析 GeoJSON 数据
382
- const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
383
- // 创建河流图层
384
- const riverLayer = new VectorLayer({
385
- properties: {
386
- name: mergedOptions.layerName,
387
- layerName: mergedOptions.layerName
388
- },
389
- source: new VectorSource({ features }),
390
- style: (feature) => {
391
- // 如果传入了自定义样式,直接使用
392
- if (mergedOptions.style) {
393
- if (typeof mergedOptions.style === 'function') {
394
- return mergedOptions.style(feature);
395
- }
396
- else {
397
- return mergedOptions.style;
398
- }
399
- }
400
- const level = feature.get('level');
401
- const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
402
- return new Style({
403
- stroke: new Stroke({
404
- color: mergedOptions.strokeColor,
405
- width: levelWidth
406
- })
407
- });
408
- },
409
- zIndex: mergedOptions.zIndex
410
- });
411
- riverLayer.setVisible(mergedOptions.visible);
412
- this.map.addLayer(riverLayer);
413
- return riverLayer;
87
+ const mergedOptions = this.mergeDefaultOptions(options);
88
+ const features = new GeoJSON().readFeatures(data, options?.projectionOptOptions);
89
+ const source = new VectorSource({ features });
90
+ return this.createLayer(source, mergedOptions);
414
91
  }
415
92
  /**
416
- * 从URL添加按级别显示不同宽度的河流图层
417
- * @param url 河流数据URL
418
- * @param options 河流图层配置选项
419
- * @returns 创建的河流图层
93
+ * 从URL添加线要素
94
+ * @param url 数据URL
95
+ * @param options 配置项
96
+ * @returns 创建的矢量图层
420
97
  */
421
- addRiverWidthByLevelByUrl(url, options = {}) {
422
- // 合并默认配置
423
- const mergedOptions = {
424
- type: 'river',
425
- layerName: 'river',
426
- strokeColor: 'rgba(3, 122, 255, 1)',
427
- strokeWidth: 2,
428
- visible: true,
429
- zIndex: 15,
430
- levelWidthMap: this.defaultLevelWidthMap,
431
- removeExisting: options.removeExisting ?? false,
432
- ...options
433
- };
434
- // 移除同名图层(如果存在)
435
- if (mergedOptions.removeExisting && mergedOptions.layerName) {
436
- MapTools.removeLayer(this.map, mergedOptions.layerName);
437
- }
98
+ addLineByUrl(url, options = {}) {
99
+ const mergedOptions = this.mergeDefaultOptions(options);
438
100
  const source = new VectorSource({
439
101
  url,
440
102
  format: new GeoJSON(options.projectionOptOptions)
441
103
  });
442
- // 创建河流图层
443
- const riverLayer = new VectorLayer({
444
- properties: {
445
- name: mergedOptions.layerName,
446
- layerName: mergedOptions.layerName
447
- },
448
- source,
449
- style: (feature) => {
450
- // 如果传入了自定义样式,直接使用
451
- if (mergedOptions.style) {
452
- if (typeof mergedOptions.style === 'function') {
453
- return mergedOptions.style(feature);
454
- }
455
- else {
456
- return mergedOptions.style;
457
- }
458
- }
459
- const level = feature.get('level');
460
- const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
461
- return new Style({
462
- stroke: new Stroke({
463
- color: mergedOptions.strokeColor,
464
- width: levelWidth
465
- })
466
- });
467
- },
468
- zIndex: mergedOptions.zIndex
469
- });
470
- riverLayer.setVisible(mergedOptions.visible);
471
- this.map.addLayer(riverLayer);
472
- return riverLayer;
104
+ return this.createLayer(source, mergedOptions);
473
105
  }
474
106
  /**
475
107
  * 移除线图层
@@ -479,35 +111,4 @@ export default class Line {
479
111
  ValidationUtils.validateLayerName(layerName);
480
112
  MapTools.removeLayer(this.map, layerName);
481
113
  }
482
- /**
483
- * 清除所有河流图层
484
- */
485
- clearRiverLayers() {
486
- this.riverLayerList.forEach(layer => {
487
- this.map.removeLayer(layer);
488
- });
489
- this.riverLayerList = [];
490
- this.riverLayerShow = false;
491
- }
492
- /**
493
- * 获取河流图层显示状态
494
- * @returns 河流图层是否显示
495
- */
496
- getRiverLayerVisibility() {
497
- return this.riverLayerShow;
498
- }
499
- /**
500
- * 获取河流图层列表
501
- * @returns 河流图层数组的副本
502
- */
503
- getRiverLayers() {
504
- return [...this.riverLayerList];
505
- }
506
- /**
507
- * 销毁线管理器,清理所有资源
508
- */
509
- destroy() {
510
- // 清除所有河流图层
511
- this.clearRiverLayers();
512
- }
513
114
  }
@@ -80,6 +80,7 @@ export default class MapBaseLayers {
80
80
  * @private
81
81
  */
82
82
  private initTiandituLayers;
83
+ private getDefaultBaseLayerType;
83
84
  /**
84
85
  * 加载默认注记图层(cia_c)
85
86
  * @param token 天地图token