my-openlayer 0.1.18 → 1.0.1

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/dist/core/Line.js CHANGED
@@ -2,93 +2,187 @@ import VectorSource from "ol/source/Vector";
2
2
  import GeoJSON from "ol/format/GeoJSON";
3
3
  import VectorLayer from "ol/layer/Vector";
4
4
  import { Stroke, Style } from "ol/style";
5
+ import { Feature } from "ol";
5
6
  import MapTools from "./MapTools";
7
+ import { ValidationUtils } from "../utils/ValidationUtils";
8
+ /**
9
+ * 线要素管理类
10
+ * 用于在地图上添加和管理线要素,包括普通线要素、河流图层等
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const lineManager = new Line(map);
15
+ * const layer = lineManager.addLine(geoJsonData, {
16
+ * type: 'road',
17
+ * strokeColor: '#ff0000',
18
+ * strokeWidth: 3
19
+ * });
20
+ * ```
21
+ */
6
22
  export default class Line {
23
+ /**
24
+ * 构造函数
25
+ * @param map OpenLayers地图实例
26
+ */
7
27
  constructor(map) {
28
+ /** 河流图层列表 */
8
29
  this.riverLayerList = [];
30
+ /** 河流图层显示状态 */
9
31
  this.riverLayerShow = false;
32
+ /** 默认河流级别宽度映射 */
33
+ this.defaultLevelWidthMap = {
34
+ 1: 2,
35
+ 2: 1,
36
+ 3: 0.5,
37
+ 4: 0.5,
38
+ 5: 0.5
39
+ };
40
+ ValidationUtils.validateMapInstance(map);
10
41
  this.map = map;
11
42
  }
12
- addLine(data, options) {
43
+ /**
44
+ * 添加线要素
45
+ * @param data GeoJSON格式的线数据
46
+ * @param options 配置项
47
+ * @returns 创建的矢量图层
48
+ */
49
+ addLine(data, options = {}) {
50
+ ValidationUtils.validateGeoJSONData(data);
51
+ const defaultOptions = {
52
+ type: 'line',
53
+ strokeColor: 'rgba(3, 122, 255, 1)',
54
+ strokeWidth: 2,
55
+ visible: true,
56
+ zIndex: 15,
57
+ layerName: options.layerName || 'lineLayer'
58
+ };
59
+ const mergedOptions = { ...defaultOptions, ...options };
60
+ const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
13
61
  const layer = new VectorLayer({
14
- name: options.layerName,
15
- layerName: options.layerName,
16
- source: new VectorSource({
17
- features: (new GeoJSON()).readFeatures(data)
18
- }),
19
- style: function (feature) {
20
- feature.set('type', options.type);
21
- feature.set('layerName', options.type);
62
+ properties: {
63
+ name: mergedOptions.layerName,
64
+ layerName: mergedOptions.layerName
65
+ },
66
+ source: new VectorSource({ features }),
67
+ style: (feature) => {
68
+ if (feature instanceof Feature) {
69
+ feature.set('type', mergedOptions.type);
70
+ feature.set('layerName', mergedOptions.type);
71
+ }
22
72
  return new Style({
23
73
  stroke: new Stroke({
24
- color: options.strokeColor || 'rgba(3, 122, 255, 1)',
25
- width: options.strokeWidth || 2,
26
- lineDash: options?.lineDash,
27
- lineDashOffset: options?.lineDashOffset
28
- }),
74
+ color: mergedOptions.strokeColor,
75
+ width: mergedOptions.strokeWidth,
76
+ lineDash: mergedOptions.lineDash,
77
+ lineDashOffset: mergedOptions.lineDashOffset
78
+ })
29
79
  });
30
80
  },
31
- zIndex: options.zIndex ?? 15,
81
+ zIndex: mergedOptions.zIndex
32
82
  });
33
- layer.setVisible(options.visible === undefined ? true : options.visible);
34
- this[options.type + 'Layer'] = layer;
83
+ layer.setVisible(mergedOptions.visible);
35
84
  this.map.addLayer(layer);
36
85
  return layer;
37
86
  }
38
- // 添加水系并按照zoom显示不同级别
39
- addRiverLayersByZoom(fyRiverJson, options = { type: 'river' }) {
40
- this.riverLayerShow = !!options.visible;
87
+ /**
88
+ * 添加分级河流图层,根据缩放级别显示不同级别的河流
89
+ * @param fyRiverJson 河流 GeoJSON 数据
90
+ * @param options 河流图层配置选项
91
+ * @throws {Error} 当数据格式无效时抛出错误
92
+ */
93
+ addRiverLayersByZoom(fyRiverJson, options = {}) {
94
+ ValidationUtils.validateGeoJSONData(fyRiverJson);
95
+ const defaultOptions = {
96
+ type: 'river',
97
+ levelCount: 5,
98
+ zoomOffset: 8,
99
+ strokeColor: 'rgb(0,113,255)',
100
+ strokeWidth: 3,
101
+ visible: true,
102
+ zIndex: 15,
103
+ layerName: 'riverLayer',
104
+ removeExisting: options.removeExisting ?? false,
105
+ levelWidthMap: this.defaultLevelWidthMap
106
+ };
107
+ const mergedOptions = { ...defaultOptions, ...options };
108
+ // 清除现有河流图层
109
+ if (mergedOptions.removeExisting) {
110
+ this.clearRiverLayers();
111
+ }
112
+ this.riverLayerShow = mergedOptions.visible;
41
113
  this.riverLayerList = [];
42
- for (let i = 1; i <= 5; i++) {
114
+ // 创建分级河流图层
115
+ for (let level = 1; level <= mergedOptions.levelCount; level++) {
43
116
  const vectorSource = new VectorSource({
44
117
  format: new GeoJSON(),
45
- loader: function () {
118
+ loader: () => {
46
119
  const geojson = new GeoJSON();
47
120
  fyRiverJson.features.forEach((feature) => {
48
- if (feature.properties.level === i) {
49
- const olFeature = geojson.readFeature(feature);
50
- vectorSource.addFeature(olFeature);
121
+ if (feature.properties && feature.properties.level === level) {
122
+ try {
123
+ const olFeature = geojson.readFeature(feature);
124
+ vectorSource.addFeature(olFeature);
125
+ }
126
+ catch (error) {
127
+ console.warn(`Failed to load river feature at level ${level}:`, error);
128
+ }
51
129
  }
52
130
  });
53
- },
54
- // 其他配置
131
+ }
55
132
  });
56
133
  const riverLayer = new VectorLayer({
57
- name: 'river',
58
- layerName: 'river',
134
+ properties: {
135
+ name: mergedOptions.layerName,
136
+ layerName: mergedOptions.layerName,
137
+ riverLevel: level
138
+ },
59
139
  source: vectorSource,
60
- style: function (feature) {
61
- feature.set('type', options.layerName);
62
- feature.set('layerName', options.layerName);
140
+ style: (feature) => {
141
+ if (feature instanceof Feature) {
142
+ feature.set('type', mergedOptions.layerName);
143
+ feature.set('layerName', mergedOptions.layerName);
144
+ }
63
145
  return new Style({
64
146
  stroke: new Stroke({
65
- color: options.strokeColor || 'rgb(0,113,255)',
66
- width: options.strokeWidth || 3
147
+ color: mergedOptions.strokeColor,
148
+ width: mergedOptions.strokeWidth
67
149
  })
68
150
  });
69
151
  },
70
- zIndex: options.zIndex ?? 15
152
+ zIndex: mergedOptions.zIndex
71
153
  });
72
154
  riverLayer.setVisible(false);
73
155
  this.riverLayerList.push(riverLayer);
74
156
  this.map.addLayer(riverLayer);
75
157
  }
76
- this.showRiverLayerByZoom();
158
+ // 设置缩放事件监听
77
159
  MapTools.mapOnEvent(this.map, 'moveend', () => {
78
160
  this.showRiverLayerByZoom();
79
161
  });
162
+ // 初始显示
163
+ this.showRiverLayerByZoom();
80
164
  }
165
+ /**
166
+ * 显示或隐藏河流图层
167
+ * @param show 是否显示河流图层
168
+ */
81
169
  showRiverLayer(show) {
82
170
  this.riverLayerShow = show;
83
- this.riverNamePointLayer.setVisible(show);
84
171
  this.showRiverLayerByZoom();
85
172
  }
86
- //分级根据zoom显示河流
173
+ /**
174
+ * 根据缩放级别显示对应的河流图层
175
+ * 缩放级别越高,显示的河流级别越详细
176
+ */
87
177
  showRiverLayerByZoom() {
88
178
  const zoom = this.map.getView().getZoom();
179
+ if (!zoom) {
180
+ return;
181
+ }
89
182
  this.riverLayerList.forEach((layer, index) => {
90
- // 一共分5级,加1因为level从1开始
91
- if (zoom && zoom > index + 1 + 8) {
183
+ // 计算显示阈值:级别索引 + 1(因为level从1开始)+ 缩放偏移量(默认8)
184
+ const displayThreshold = index + 1 + 8;
185
+ if (zoom > displayThreshold) {
92
186
  layer.setVisible(this.riverLayerShow);
93
187
  }
94
188
  else {
@@ -96,27 +190,92 @@ export default class Line {
96
190
  }
97
191
  });
98
192
  }
99
- // 添加全部级别河流根据级别显示不同宽度
100
- addRiverWidthByLev(arr = {}) {
101
- const riverLayer = new VectorLayer({
102
- name: 'river',
193
+ /**
194
+ * 添加按级别显示不同宽度的河流图层
195
+ * @param data 河流 GeoJSON 数据
196
+ * @param options 河流图层配置选项
197
+ * @returns 创建的河流图层
198
+ */
199
+ addRiverWidthByLevel(data, options = {}) {
200
+ ValidationUtils.validateGeoJSONData(data);
201
+ // 合并默认配置
202
+ const mergedOptions = {
203
+ type: 'river',
103
204
  layerName: 'river',
104
- source: new VectorSource({
105
- features: (new GeoJSON()).readFeatures(arr)
106
- }),
107
- style: (feature) => this.setFeatureAttr(feature),
108
- zIndex: 15
205
+ strokeColor: 'rgba(3, 122, 255, 1)',
206
+ strokeWidth: 2,
207
+ visible: true,
208
+ zIndex: 15,
209
+ levelWidthMap: this.defaultLevelWidthMap,
210
+ removeExisting: options.removeExisting ?? false,
211
+ ...options
212
+ };
213
+ // 移除同名图层(如果存在)
214
+ if (mergedOptions.removeExisting && mergedOptions.layerName) {
215
+ MapTools.removeLayer(this.map, mergedOptions.layerName);
216
+ }
217
+ // 解析 GeoJSON 数据
218
+ const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
219
+ // 创建河流图层
220
+ const riverLayer = new VectorLayer({
221
+ properties: {
222
+ name: mergedOptions.layerName,
223
+ layerName: mergedOptions.layerName
224
+ },
225
+ source: new VectorSource({ features }),
226
+ style: (feature) => {
227
+ const level = feature.get('level');
228
+ const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
229
+ return new Style({
230
+ stroke: new Stroke({
231
+ color: mergedOptions.strokeColor,
232
+ width: levelWidth
233
+ })
234
+ });
235
+ },
236
+ zIndex: mergedOptions.zIndex
109
237
  });
238
+ riverLayer.setVisible(mergedOptions.visible);
110
239
  this.map.addLayer(riverLayer);
240
+ return riverLayer;
241
+ }
242
+ /**
243
+ * 移除线图层
244
+ * @param layerName 图层名称
245
+ */
246
+ removeLineLayer(layerName) {
247
+ ValidationUtils.validateLayerName(layerName);
248
+ MapTools.removeLayer(this.map, layerName);
111
249
  }
112
- setFeatureAttr(feature) {
113
- const level = feature.get('level');
114
- const levelWidth = { 1: 2, 2: 1, 3: 0.5, 4: 0.5, 5: 0.5 };
115
- return new Style({
116
- stroke: new Stroke({
117
- color: 'rgba(3, 122, 255, 1)',
118
- width: levelWidth[Number(level)]
119
- }),
250
+ /**
251
+ * 清除所有河流图层
252
+ */
253
+ clearRiverLayers() {
254
+ this.riverLayerList.forEach(layer => {
255
+ this.map.removeLayer(layer);
120
256
  });
257
+ this.riverLayerList = [];
258
+ this.riverLayerShow = false;
259
+ }
260
+ /**
261
+ * 获取河流图层显示状态
262
+ * @returns 河流图层是否显示
263
+ */
264
+ getRiverLayerVisibility() {
265
+ return this.riverLayerShow;
266
+ }
267
+ /**
268
+ * 获取河流图层列表
269
+ * @returns 河流图层数组的副本
270
+ */
271
+ getRiverLayers() {
272
+ return [...this.riverLayerList];
273
+ }
274
+ /**
275
+ * 销毁线管理器,清理所有资源
276
+ */
277
+ destroy() {
278
+ // 清除所有河流图层
279
+ this.clearRiverLayers();
121
280
  }
122
281
  }
@@ -1,5 +1,7 @@
1
1
  /**
2
- * 地图底图图层
2
+ * 地图底图图层管理类
3
+ * 提供天地图底图、注记图层、GeoServer图层等功能
4
+ * 支持图层切换、裁剪等高级功能
3
5
  */
4
6
  import Map from "ol/Map";
5
7
  import { Tile as TileLayer } from "ol/layer";
@@ -7,30 +9,196 @@ import { TileWMS } from "ol/source";
7
9
  import WMTSTileGrid from "ol/tilegrid/WMTS";
8
10
  import XYZ from "ol/source/XYZ";
9
11
  import { MapLayersOptions, TiandituType, AnnotationLayerOptions } from "../types";
12
+ /**
13
+ * GeoServer图层选项接口
14
+ */
15
+ interface GeoServerLayerOptions {
16
+ /** 图层层级 */
17
+ zIndex?: number;
18
+ /** 图层可见性 */
19
+ visible?: boolean;
20
+ /** WMS版本 */
21
+ version?: '1.1.1' | '1.3.0';
22
+ /** 服务器类型 */
23
+ serverType?: string;
24
+ /** 跨域设置 */
25
+ crossOrigin?: string;
26
+ /** WMS参数 */
27
+ params?: Record<string, any>;
28
+ }
29
+ /**
30
+ * 天地图图层选项接口
31
+ */
32
+ interface TiandituLayerOptions {
33
+ /** 图层类型 */
34
+ type: TiandituType;
35
+ /** 天地图token */
36
+ token: string;
37
+ /** 图层层级 */
38
+ zIndex: number;
39
+ /** 图层可见性 */
40
+ visible: boolean;
41
+ }
42
+ /**
43
+ * 地图底图图层管理类
44
+ */
10
45
  export default class MapBaseLayers {
11
- private map;
46
+ private readonly map;
12
47
  private readonly options;
13
- private readonly layers;
48
+ private layers;
49
+ private readonly errorHandler;
50
+ private currentBaseLayerType;
51
+ /**
52
+ * 构造函数
53
+ * @param map OpenLayers地图实例
54
+ * @param options 图层配置选项
55
+ */
14
56
  constructor(map: Map, options: MapLayersOptions);
57
+ /**
58
+ * 验证构造函数参数
59
+ * @param map 地图实例
60
+ * @param options 配置选项
61
+ * @private
62
+ */
63
+ private validateConstructorParams;
64
+ /**
65
+ * 合并默认配置选项
66
+ * @param options 用户配置选项
67
+ * @returns 合并后的配置选项
68
+ * @private
69
+ */
70
+ private mergeDefaultOptions;
71
+ /**
72
+ * 初始化图层
73
+ * @private
74
+ */
75
+ private initializeLayers;
76
+ /**
77
+ * 初始化天地图图层
78
+ * @private
79
+ */
80
+ private initTiandituLayers;
81
+ /**
82
+ * 添加注记图层
83
+ * @param token 天地图token
84
+ * @param baseZIndex 基础层级
85
+ * @private
86
+ */
87
+ private addAnnotationLayers;
88
+ /**
89
+ * 切换底图图层
90
+ * @param type 图层类型
91
+ */
15
92
  switchBaseLayer(type: TiandituType): void;
16
- initLayer(): void;
17
- addAnnotationLayer(options: Omit<AnnotationLayerOptions, 'token'>): void;
93
+ /**
94
+ * 获取当前底图类型
95
+ * @returns 当前底图类型
96
+ */
97
+ getCurrentBaseLayerType(): string | null;
98
+ /**
99
+ * 获取可用的图层类型列表
100
+ * @returns 图层类型数组
101
+ */
102
+ getAvailableLayerTypes(): string[];
103
+ /**
104
+ * 检查指定图层类型是否存在
105
+ * @param type 图层类型
106
+ * @returns 是否存在
107
+ */
108
+ hasLayerType(type: string): boolean;
109
+ /**
110
+ * 添加注记图层(实例方法)
111
+ * @param options 注记图层选项(不包含token)
112
+ * @returns 创建的图层
113
+ */
114
+ addAnnotationLayer(options: Omit<AnnotationLayerOptions, 'token'>): TileLayer<XYZ>;
115
+ /**
116
+ * 添加注记图层(静态方法)
117
+ * @param map 地图实例
118
+ * @param options 注记图层选项
119
+ * @returns 创建的图层
120
+ */
18
121
  static addAnnotationLayer(map: Map, options: AnnotationLayerOptions): TileLayer<XYZ>;
19
- addMapLayer(): void;
20
- createLayer(layer: any): any;
21
- addGeoServerLayer(url: string, layer: string, options: {
22
- zIndex?: number;
23
- visible?: boolean;
24
- }): TileLayer<TileWMS>;
25
- static getTiandiTuLayer(options: {
26
- type: TiandituType;
27
- token: string;
28
- zIndex: number;
29
- visible: boolean;
30
- }): TileLayer<XYZ>;
31
- /**
32
- * 地图注记
122
+ /**
123
+ * 将所有图层添加到地图
124
+ * @private
125
+ */
126
+ private addMapLayer;
127
+ /**
128
+ * 处理图层(应用裁剪等)
129
+ * @param layer 原始图层
130
+ * @returns 处理后的图层
131
+ * @private
132
+ */
133
+ private processLayer;
134
+ /**
135
+ * 添加GeoServer图层
136
+ * @param url GeoServer服务URL
137
+ * @param layerName 图层名称
138
+ * @param options 图层选项
139
+ * @returns 创建的WMS图层
140
+ */
141
+ addGeoServerLayer(url: string, layerName: string, options?: GeoServerLayerOptions): TileLayer<TileWMS>;
142
+ /**
143
+ * 创建天地图图层(实例方法)
144
+ * @param options 天地图图层选项
145
+ * @returns 创建的图层
146
+ * @private
147
+ */
148
+ private createTiandituLayer;
149
+ /**
150
+ * 创建注记图层(实例方法)
151
+ * @param options 注记图层选项
152
+ * @returns 创建的图层
153
+ * @private
154
+ */
155
+ private createAnnotationLayer;
156
+ /**
157
+ * 创建天地图底图图层(静态方法)
158
+ * @param options 天地图图层选项
159
+ * @returns 创建的图层
160
+ */
161
+ static getTiandiTuLayer(options: TiandituLayerOptions): TileLayer<XYZ>;
162
+ /**
163
+ * 创建天地图注记图层(静态方法)
164
+ * @param options 注记图层选项
165
+ * @returns 创建的图层
166
+ */
167
+ static createAnnotationLayer(options: AnnotationLayerOptions): TileLayer<XYZ>;
168
+ /**
169
+ * 获取天地图注记图层(向后兼容的静态方法)
170
+ * @param options 注记图层选项
171
+ * @returns 创建的图层
172
+ * @deprecated 使用 createAnnotationLayer 替代
33
173
  */
34
174
  static getAnnotationLayer(options: AnnotationLayerOptions): TileLayer<XYZ>;
175
+ /**
176
+ * 创建WMTS瓦片网格
177
+ * @param length 层级数量
178
+ * @returns WMTS瓦片网格
179
+ */
35
180
  static getTileGrid(length: number): WMTSTileGrid;
181
+ /**
182
+ * 移除指定类型的图层
183
+ * @param type 图层类型
184
+ */
185
+ removeLayersByType(type: string): void;
186
+ /**
187
+ * 清除所有图层
188
+ */
189
+ clearAllLayers(): void;
190
+ /**
191
+ * 获取图层数量统计
192
+ * @returns 图层统计信息
193
+ */
194
+ getLayerStats(): {
195
+ totalTypes: number;
196
+ totalLayers: number;
197
+ layersByType: Record<string, number>;
198
+ };
199
+ /**
200
+ * 销毁实例,清理资源
201
+ */
202
+ destroy(): void;
36
203
  }
204
+ export {};