my-openlayer 1.0.14 → 2.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.
package/core/Line.js CHANGED
@@ -1,283 +1,288 @@
1
- import VectorSource from "ol/source/Vector";
2
- import GeoJSON from "ol/format/GeoJSON";
3
- import VectorLayer from "ol/layer/Vector";
4
- import { Stroke, Style } from "ol/style";
5
- import { Feature } from "ol";
6
- import MapTools from "./MapTools";
7
- import { ValidationUtils } from "../utils/ValidationUtils";
8
- import { EventManager } from "./EventManager";
9
- /**
10
- * 线要素管理类
11
- * 用于在地图上添加和管理线要素,包括普通线要素、河流图层等
12
- *
13
- * @example
14
- * ```typescript
15
- * const lineManager = new Line(map);
16
- * const layer = lineManager.addLine(geoJsonData, {
17
- * type: 'road',
18
- * strokeColor: '#ff0000',
19
- * strokeWidth: 3
20
- * });
21
- * ```
22
- */
23
- export default class Line {
24
- /**
25
- * 构造函数
26
- * @param map OpenLayers地图实例
27
- */
28
- constructor(map) {
29
- /** 河流图层列表 */
30
- this.riverLayerList = [];
31
- /** 河流图层显示状态 */
32
- this.riverLayerShow = false;
33
- /** 默认河流级别宽度映射 */
34
- this.defaultLevelWidthMap = {
35
- 1: 2,
36
- 2: 1,
37
- 3: 0.5,
38
- 4: 0.5,
39
- 5: 0.5
40
- };
41
- ValidationUtils.validateMapInstance(map);
42
- this.map = map;
43
- this.eventManager = new EventManager(map);
44
- }
45
- /**
46
- * 添加线要素
47
- * @param data GeoJSON格式的线数据
48
- * @param options 配置项
49
- * @returns 创建的矢量图层
50
- */
51
- addLine(data, options = {}) {
52
- ValidationUtils.validateGeoJSONData(data);
53
- const defaultOptions = {
54
- type: 'line',
55
- strokeColor: 'rgba(3, 122, 255, 1)',
56
- strokeWidth: 2,
57
- visible: true,
58
- zIndex: 15,
59
- layerName: options.layerName || 'lineLayer'
60
- };
61
- const mergedOptions = { ...defaultOptions, ...options };
62
- const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
63
- const layer = new VectorLayer({
64
- properties: {
65
- name: mergedOptions.layerName,
66
- layerName: mergedOptions.layerName
67
- },
68
- source: new VectorSource({ features }),
69
- style: (feature) => {
70
- if (feature instanceof Feature) {
71
- feature.set('type', mergedOptions.type);
72
- feature.set('layerName', mergedOptions.type);
73
- }
74
- return new Style({
75
- stroke: new Stroke({
76
- color: mergedOptions.strokeColor,
77
- width: mergedOptions.strokeWidth,
78
- lineDash: mergedOptions.lineDash,
79
- lineDashOffset: mergedOptions.lineDashOffset
80
- })
81
- });
82
- },
83
- zIndex: mergedOptions.zIndex
84
- });
85
- layer.setVisible(mergedOptions.visible);
86
- this.map.addLayer(layer);
87
- return layer;
88
- }
89
- /**
90
- * 添加分级河流图层,根据缩放级别显示不同级别的河流
91
- * @param fyRiverJson 河流 GeoJSON 数据
92
- * @param options 河流图层配置选项
93
- * @throws {Error} 当数据格式无效时抛出错误
94
- */
95
- addRiverLayersByZoom(fyRiverJson, options = {}) {
96
- ValidationUtils.validateGeoJSONData(fyRiverJson);
97
- const defaultOptions = {
98
- type: 'river',
99
- levelCount: 5,
100
- zoomOffset: 8,
101
- strokeColor: 'rgb(0,113,255)',
102
- strokeWidth: 3,
103
- visible: true,
104
- zIndex: 15,
105
- layerName: 'riverLayer',
106
- removeExisting: options.removeExisting ?? false,
107
- levelWidthMap: this.defaultLevelWidthMap
108
- };
109
- const mergedOptions = { ...defaultOptions, ...options };
110
- // 清除现有河流图层
111
- if (mergedOptions.removeExisting) {
112
- this.clearRiverLayers();
113
- }
114
- this.riverLayerShow = mergedOptions.visible;
115
- this.riverLayerList = [];
116
- // 创建分级河流图层
117
- for (let level = 1; level <= mergedOptions.levelCount; level++) {
118
- const vectorSource = new VectorSource({
119
- format: new GeoJSON(),
120
- loader: () => {
121
- const geojson = new GeoJSON();
122
- fyRiverJson.features.forEach((feature) => {
123
- if (feature.properties && feature.properties.level === level) {
124
- try {
125
- const olFeature = geojson.readFeature(feature);
126
- vectorSource.addFeature(olFeature);
127
- }
128
- catch (error) {
129
- console.warn(`Failed to load river feature at level ${level}:`, error);
130
- }
131
- }
132
- });
133
- }
134
- });
135
- const riverLayer = new VectorLayer({
136
- properties: {
137
- name: mergedOptions.layerName,
138
- layerName: mergedOptions.layerName,
139
- riverLevel: level
140
- },
141
- source: vectorSource,
142
- style: (feature) => {
143
- if (feature instanceof Feature) {
144
- feature.set('type', mergedOptions.layerName);
145
- feature.set('layerName', mergedOptions.layerName);
146
- }
147
- return new Style({
148
- stroke: new Stroke({
149
- color: mergedOptions.strokeColor,
150
- width: mergedOptions.strokeWidth
151
- })
152
- });
153
- },
154
- zIndex: mergedOptions.zIndex
155
- });
156
- riverLayer.setVisible(false);
157
- this.riverLayerList.push(riverLayer);
158
- this.map.addLayer(riverLayer);
159
- }
160
- // 设置缩放事件监听
161
- this.eventManager.on('moveend', () => {
162
- this.showRiverLayerByZoom();
163
- });
164
- // 初始显示
165
- this.showRiverLayerByZoom();
166
- }
167
- /**
168
- * 显示或隐藏河流图层
169
- * @param show 是否显示河流图层
170
- */
171
- showRiverLayer(show) {
172
- this.riverLayerShow = show;
173
- this.showRiverLayerByZoom();
174
- }
175
- /**
176
- * 根据缩放级别显示对应的河流图层
177
- * 缩放级别越高,显示的河流级别越详细
178
- */
179
- showRiverLayerByZoom() {
180
- const zoom = this.map.getView().getZoom();
181
- if (!zoom) {
182
- return;
183
- }
184
- this.riverLayerList.forEach((layer, index) => {
185
- // 计算显示阈值:级别索引 + 1(因为level从1开始)+ 缩放偏移量(默认8)
186
- const displayThreshold = index + 1 + 8;
187
- if (zoom > displayThreshold) {
188
- layer.setVisible(this.riverLayerShow);
189
- }
190
- else {
191
- layer.setVisible(false);
192
- }
193
- });
194
- }
195
- /**
196
- * 添加按级别显示不同宽度的河流图层
197
- * @param data 河流 GeoJSON 数据
198
- * @param options 河流图层配置选项
199
- * @returns 创建的河流图层
200
- */
201
- addRiverWidthByLevel(data, options = {}) {
202
- ValidationUtils.validateGeoJSONData(data);
203
- // 合并默认配置
204
- const mergedOptions = {
205
- type: 'river',
206
- layerName: 'river',
207
- strokeColor: 'rgba(3, 122, 255, 1)',
208
- strokeWidth: 2,
209
- visible: true,
210
- zIndex: 15,
211
- levelWidthMap: this.defaultLevelWidthMap,
212
- removeExisting: options.removeExisting ?? false,
213
- ...options
214
- };
215
- // 移除同名图层(如果存在)
216
- if (mergedOptions.removeExisting && mergedOptions.layerName) {
217
- MapTools.removeLayer(this.map, mergedOptions.layerName);
218
- }
219
- // 解析 GeoJSON 数据
220
- const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
221
- // 创建河流图层
222
- const riverLayer = new VectorLayer({
223
- properties: {
224
- name: mergedOptions.layerName,
225
- layerName: mergedOptions.layerName
226
- },
227
- source: new VectorSource({ features }),
228
- style: (feature) => {
229
- const level = feature.get('level');
230
- const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
231
- return new Style({
232
- stroke: new Stroke({
233
- color: mergedOptions.strokeColor,
234
- width: levelWidth
235
- })
236
- });
237
- },
238
- zIndex: mergedOptions.zIndex
239
- });
240
- riverLayer.setVisible(mergedOptions.visible);
241
- this.map.addLayer(riverLayer);
242
- return riverLayer;
243
- }
244
- /**
245
- * 移除线图层
246
- * @param layerName 图层名称
247
- */
248
- removeLineLayer(layerName) {
249
- ValidationUtils.validateLayerName(layerName);
250
- MapTools.removeLayer(this.map, layerName);
251
- }
252
- /**
253
- * 清除所有河流图层
254
- */
255
- clearRiverLayers() {
256
- this.riverLayerList.forEach(layer => {
257
- this.map.removeLayer(layer);
258
- });
259
- this.riverLayerList = [];
260
- this.riverLayerShow = false;
261
- }
262
- /**
263
- * 获取河流图层显示状态
264
- * @returns 河流图层是否显示
265
- */
266
- getRiverLayerVisibility() {
267
- return this.riverLayerShow;
268
- }
269
- /**
270
- * 获取河流图层列表
271
- * @returns 河流图层数组的副本
272
- */
273
- getRiverLayers() {
274
- return [...this.riverLayerList];
275
- }
276
- /**
277
- * 销毁线管理器,清理所有资源
278
- */
279
- destroy() {
280
- // 清除所有河流图层
281
- this.clearRiverLayers();
282
- }
283
- }
1
+ import VectorSource from "ol/source/Vector";
2
+ import GeoJSON from "ol/format/GeoJSON";
3
+ import VectorLayer from "ol/layer/Vector";
4
+ import { Stroke, Style } from "ol/style";
5
+ import { Feature } from "ol";
6
+ import MapTools from "./MapTools";
7
+ import { ValidationUtils } from "../utils/ValidationUtils";
8
+ import { EventManager } from "./EventManager";
9
+ /**
10
+ * 线要素管理类
11
+ * 用于在地图上添加和管理线要素,包括普通线要素、河流图层等
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const lineManager = new Line(map);
16
+ * const layer = lineManager.addLine(geoJsonData, {
17
+ * type: 'road',
18
+ * strokeColor: '#ff0000',
19
+ * strokeWidth: 3
20
+ * });
21
+ * ```
22
+ */
23
+ export default class Line {
24
+ /**
25
+ * 构造函数
26
+ * @param map OpenLayers地图实例
27
+ */
28
+ constructor(map) {
29
+ /** 河流图层列表 */
30
+ this.riverLayerList = [];
31
+ /** 河流图层显示状态 */
32
+ this.riverLayerShow = false;
33
+ /** 默认河流级别宽度映射 */
34
+ this.defaultLevelWidthMap = {
35
+ 1: 2,
36
+ 2: 1,
37
+ 3: 0.5,
38
+ 4: 0.5,
39
+ 5: 0.5
40
+ };
41
+ ValidationUtils.validateMapInstance(map);
42
+ this.map = map;
43
+ this.eventManager = new EventManager(map);
44
+ }
45
+ /**
46
+ * 添加线要素
47
+ * @param data GeoJSON格式的线数据
48
+ * @param options 配置项
49
+ * @returns 创建的矢量图层
50
+ */
51
+ addLine(data, options = {}) {
52
+ ValidationUtils.validateGeoJSONData(data);
53
+ const defaultOptions = {
54
+ type: 'line',
55
+ strokeColor: 'rgba(3, 122, 255, 1)',
56
+ strokeWidth: 2,
57
+ visible: true,
58
+ zIndex: 15,
59
+ layerName: options.layerName || 'lineLayer'
60
+ };
61
+ const mergedOptions = { ...defaultOptions, ...options };
62
+ const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
63
+ const layer = new VectorLayer({
64
+ properties: {
65
+ name: mergedOptions.layerName,
66
+ layerName: mergedOptions.layerName
67
+ },
68
+ source: new VectorSource({ features }),
69
+ style: (feature) => {
70
+ if (feature instanceof Feature) {
71
+ feature.set('type', mergedOptions.type);
72
+ feature.set('layerName', mergedOptions.type);
73
+ }
74
+ return new Style({
75
+ stroke: new Stroke({
76
+ color: mergedOptions.strokeColor,
77
+ width: mergedOptions.strokeWidth,
78
+ lineDash: mergedOptions.lineDash,
79
+ lineDashOffset: mergedOptions.lineDashOffset
80
+ })
81
+ });
82
+ },
83
+ zIndex: mergedOptions.zIndex
84
+ });
85
+ layer.setVisible(mergedOptions.visible);
86
+ this.map.addLayer(layer);
87
+ return layer;
88
+ }
89
+ /**
90
+ * 添加分级河流图层,根据缩放级别显示不同级别的河流
91
+ * @param fyRiverJson 河流 GeoJSON 数据
92
+ * @param options 河流图层配置选项
93
+ * @throws {Error} 当数据格式无效时抛出错误
94
+ */
95
+ addRiverLayersByZoom(fyRiverJson, options = {}) {
96
+ ValidationUtils.validateGeoJSONData(fyRiverJson);
97
+ const defaultOptions = {
98
+ type: 'river',
99
+ levelCount: 5,
100
+ zoomOffset: 8,
101
+ strokeColor: 'rgb(0,113,255)',
102
+ strokeWidth: 3,
103
+ visible: true,
104
+ zIndex: 15,
105
+ layerName: 'riverLayer',
106
+ removeExisting: options.removeExisting ?? false,
107
+ levelWidthMap: this.defaultLevelWidthMap
108
+ };
109
+ const mergedOptions = { ...defaultOptions, ...options };
110
+ // 清除现有河流图层
111
+ if (mergedOptions.removeExisting) {
112
+ this.clearRiverLayers();
113
+ }
114
+ this.riverLayerShow = mergedOptions.visible;
115
+ this.riverLayerList = [];
116
+ // 创建分级河流图层
117
+ for (let level = 1; level <= mergedOptions.levelCount; level++) {
118
+ const vectorSource = new VectorSource({
119
+ format: new GeoJSON(),
120
+ loader: () => {
121
+ const geojson = new GeoJSON();
122
+ fyRiverJson.features.forEach((feature) => {
123
+ if (feature.properties && feature.properties.level === level) {
124
+ try {
125
+ const olFeature = geojson.readFeature(feature);
126
+ if (Array.isArray(olFeature)) {
127
+ vectorSource.addFeatures(olFeature);
128
+ }
129
+ else {
130
+ vectorSource.addFeature(olFeature);
131
+ }
132
+ }
133
+ catch (error) {
134
+ console.warn(`Failed to load river feature at level ${level}:`, error);
135
+ }
136
+ }
137
+ });
138
+ }
139
+ });
140
+ const riverLayer = new VectorLayer({
141
+ properties: {
142
+ name: mergedOptions.layerName,
143
+ layerName: mergedOptions.layerName,
144
+ riverLevel: level
145
+ },
146
+ source: vectorSource,
147
+ style: (feature) => {
148
+ if (feature instanceof Feature) {
149
+ feature.set('type', mergedOptions.layerName);
150
+ feature.set('layerName', mergedOptions.layerName);
151
+ }
152
+ return new Style({
153
+ stroke: new Stroke({
154
+ color: mergedOptions.strokeColor,
155
+ width: mergedOptions.strokeWidth
156
+ })
157
+ });
158
+ },
159
+ zIndex: mergedOptions.zIndex
160
+ });
161
+ riverLayer.setVisible(false);
162
+ this.riverLayerList.push(riverLayer);
163
+ this.map.addLayer(riverLayer);
164
+ }
165
+ // 设置缩放事件监听
166
+ this.eventManager.on('moveend', () => {
167
+ this.showRiverLayerByZoom();
168
+ });
169
+ // 初始显示
170
+ this.showRiverLayerByZoom();
171
+ }
172
+ /**
173
+ * 显示或隐藏河流图层
174
+ * @param show 是否显示河流图层
175
+ */
176
+ showRiverLayer(show) {
177
+ this.riverLayerShow = show;
178
+ this.showRiverLayerByZoom();
179
+ }
180
+ /**
181
+ * 根据缩放级别显示对应的河流图层
182
+ * 缩放级别越高,显示的河流级别越详细
183
+ */
184
+ showRiverLayerByZoom() {
185
+ const zoom = this.map.getView().getZoom();
186
+ if (!zoom) {
187
+ return;
188
+ }
189
+ this.riverLayerList.forEach((layer, index) => {
190
+ // 计算显示阈值:级别索引 + 1(因为level从1开始)+ 缩放偏移量(默认8)
191
+ const displayThreshold = index + 1 + 8;
192
+ if (zoom > displayThreshold) {
193
+ layer.setVisible(this.riverLayerShow);
194
+ }
195
+ else {
196
+ layer.setVisible(false);
197
+ }
198
+ });
199
+ }
200
+ /**
201
+ * 添加按级别显示不同宽度的河流图层
202
+ * @param data 河流 GeoJSON 数据
203
+ * @param options 河流图层配置选项
204
+ * @returns 创建的河流图层
205
+ */
206
+ addRiverWidthByLevel(data, options = {}) {
207
+ ValidationUtils.validateGeoJSONData(data);
208
+ // 合并默认配置
209
+ const mergedOptions = {
210
+ type: 'river',
211
+ layerName: 'river',
212
+ strokeColor: 'rgba(3, 122, 255, 1)',
213
+ strokeWidth: 2,
214
+ visible: true,
215
+ zIndex: 15,
216
+ levelWidthMap: this.defaultLevelWidthMap,
217
+ removeExisting: options.removeExisting ?? false,
218
+ ...options
219
+ };
220
+ // 移除同名图层(如果存在)
221
+ if (mergedOptions.removeExisting && mergedOptions.layerName) {
222
+ MapTools.removeLayer(this.map, mergedOptions.layerName);
223
+ }
224
+ // 解析 GeoJSON 数据
225
+ const features = new GeoJSON().readFeatures(data, options.projectionOptOptions);
226
+ // 创建河流图层
227
+ const riverLayer = new VectorLayer({
228
+ properties: {
229
+ name: mergedOptions.layerName,
230
+ layerName: mergedOptions.layerName
231
+ },
232
+ source: new VectorSource({ features }),
233
+ style: (feature) => {
234
+ const level = feature.get('level');
235
+ const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
236
+ return new Style({
237
+ stroke: new Stroke({
238
+ color: mergedOptions.strokeColor,
239
+ width: levelWidth
240
+ })
241
+ });
242
+ },
243
+ zIndex: mergedOptions.zIndex
244
+ });
245
+ riverLayer.setVisible(mergedOptions.visible);
246
+ this.map.addLayer(riverLayer);
247
+ return riverLayer;
248
+ }
249
+ /**
250
+ * 移除线图层
251
+ * @param layerName 图层名称
252
+ */
253
+ removeLineLayer(layerName) {
254
+ ValidationUtils.validateLayerName(layerName);
255
+ MapTools.removeLayer(this.map, layerName);
256
+ }
257
+ /**
258
+ * 清除所有河流图层
259
+ */
260
+ clearRiverLayers() {
261
+ this.riverLayerList.forEach(layer => {
262
+ this.map.removeLayer(layer);
263
+ });
264
+ this.riverLayerList = [];
265
+ this.riverLayerShow = false;
266
+ }
267
+ /**
268
+ * 获取河流图层显示状态
269
+ * @returns 河流图层是否显示
270
+ */
271
+ getRiverLayerVisibility() {
272
+ return this.riverLayerShow;
273
+ }
274
+ /**
275
+ * 获取河流图层列表
276
+ * @returns 河流图层数组的副本
277
+ */
278
+ getRiverLayers() {
279
+ return [...this.riverLayerList];
280
+ }
281
+ /**
282
+ * 销毁线管理器,清理所有资源
283
+ */
284
+ destroy() {
285
+ // 清除所有河流图层
286
+ this.clearRiverLayers();
287
+ }
288
+ }